From b400c6112ce01e266229833853803204eb8b423a Mon Sep 17 00:00:00 2001 From: Jonas Kello Date: Thu, 21 Sep 2017 00:37:32 +0200 Subject: [PATCH 1/2] Add failing test again --- .../ignore-local/variable.ts.lint | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/rules/readonly-array/ignore-local/variable.ts.lint b/test/rules/readonly-array/ignore-local/variable.ts.lint index fa85b01..178e264 100644 --- a/test/rules/readonly-array/ignore-local/variable.ts.lint +++ b/test/rules/readonly-array/ignore-local/variable.ts.lint @@ -51,4 +51,44 @@ const foo = (bar: Array, zoo: ReadonlyArray, boo = [1, 2, 3]) => } +// -- Inside class + +class Foo { + + // -- Class-level variable declarations + + // Should not fail on ReadonlyArray in variable declaration (TypeReferenceNode) + const foo: ReadonlyArray = []; + + // Should fail on Array type in variable declaration (TypeReferenceNode) + const foo: Array = []; + ~~~~~~~~~~~~~ [failure] + + // Should fail on implicit Array type in variable declaration (ArrayLiteralExpression) + const foo = [1, 2, 3] + ~~~ [failure] + + // -- Class level functions: Local variable declarations + + // Should fail on Array type in variable declaration as function parameter (TypeReferenceNode) + foo(bar: Array, zoo: ReadonlyArray, boo = [1, 2, 3]) { + ~~~~~~~~~~~~~ [failure] + ~~~ [failure] + + // Should not fail on ReadonlyArray in variable declaration (TypeReferenceNode) + const foo: ReadonlyArray = []; + + // Should not fail on Array type in variable declaration when local (TypeReferenceNode) + const foo: Array = []; + + // Should not fail on Array shorthand in variable declaration when local (TypeReferenceNode) + const foo: string[] = []; + + // Should not fail on implicit Array type in variable declaration when local (ArrayLiteralExpression) + const foo = [1, 2, 3] + + } + +} + [failure]: Only ReadonlyArray allowed. From fc00af479d61538875436d3a6201454f298db55f Mon Sep 17 00:00:00 2001 From: Jonas Kello Date: Thu, 21 Sep 2017 17:46:29 +0200 Subject: [PATCH 2/2] Fix --- src/readonly-shared.ts | 7 ++++--- src/readonlyArrayRule.ts | 3 ++- test/rules/readonly-array/work/variable.ts.lint | 13 ++++++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/readonly-shared.ts b/src/readonly-shared.ts index 426d06e..13a7fca 100644 --- a/src/readonly-shared.ts +++ b/src/readonly-shared.ts @@ -45,9 +45,10 @@ export function walk(ctx: Lint.WalkContext, checkNode: CheckNodeFunctio function cb(node: ts.Node): void { // Skip checking in functions if ignore-local is set if (ctx.options.ignoreLocal && (node.kind === ts.SyntaxKind.FunctionDeclaration - || node.kind === ts.SyntaxKind.ArrowFunction || node.kind === ts.SyntaxKind.FunctionExpression)) { + || node.kind === ts.SyntaxKind.ArrowFunction || node.kind === ts.SyntaxKind.FunctionExpression + || node.kind === ts.SyntaxKind.MethodDeclaration)) { // We still need to check the parameters and return type - const functionNode: ts.FunctionDeclaration | ts.ArrowFunction = node as any; //tslint:disable-line + const functionNode: ts.FunctionDeclaration | ts.ArrowFunction | ts.MethodDeclaration = node as any; //tslint:disable-line const invalidNodes = checkIgnoreLocalFunctionNode(functionNode, ctx, checkNode); // invalidNodes.forEach((n) => reportInvalidNodes(n, ctx, failureString)); reportInvalidNodes(invalidNodes, ctx, failureString); @@ -65,7 +66,7 @@ export function reportInvalidNodes(invalidNodes: ReadonlyArray, ctx invalidNodes.forEach((invalidNode) => ctx.addFailureAtNode(invalidNode.node, failureString, invalidNode.replacement)); } -export function checkIgnoreLocalFunctionNode(functionNode: ts.FunctionDeclaration | ts.ArrowFunction, +export function checkIgnoreLocalFunctionNode(functionNode: ts.FunctionDeclaration | ts.ArrowFunction | ts.MethodDeclaration, ctx: Lint.WalkContext, checkNode: CheckNodeFunction): ReadonlyArray { let invalidNodes: Array = []; diff --git a/src/readonlyArrayRule.ts b/src/readonlyArrayRule.ts index 1eb7f87..d91c6f4 100644 --- a/src/readonlyArrayRule.ts +++ b/src/readonlyArrayRule.ts @@ -42,7 +42,8 @@ function checkArrayTypeOrReference(node: ts.Node, ctx: Lint.WalkContext): ReadonlyArray { - if (node.kind === ts.SyntaxKind.VariableDeclaration || node.kind === ts.SyntaxKind.Parameter) { + if (node.kind === ts.SyntaxKind.VariableDeclaration || node.kind === ts.SyntaxKind.Parameter + || node.kind === ts.SyntaxKind.PropertyDeclaration) { // The initializer is used to set and implicit type const varOrParamNode = node as ts.VariableDeclaration | ts.ParameterDeclaration; if (Shared.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) { diff --git a/test/rules/readonly-array/work/variable.ts.lint b/test/rules/readonly-array/work/variable.ts.lint index 53892ee..eaddcdf 100644 --- a/test/rules/readonly-array/work/variable.ts.lint +++ b/test/rules/readonly-array/work/variable.ts.lint @@ -1,6 +1,13 @@ -// Should fail on implicit Array type in variable declaration (ArrayLiteralExpression) -const foo = [1, 2, 3] - ~~~ [failure] +class Foo { + + foo(bar: Array, zoo: ReadonlyArray, boo = [1, 2, 3]) { + ~~~~~~~~~~~~~ [failure] + ~~~ [failure] + + const foo: Array = []; + } + +} [failure]: Only ReadonlyArray allowed.