Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix readonly-array ignore-local not working in class methods #47

Merged
merged 2 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/readonly-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export function walk(ctx: Lint.WalkContext<Options>, 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);
Expand All @@ -65,7 +66,7 @@ export function reportInvalidNodes(invalidNodes: ReadonlyArray<InvalidNode>, 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<Options>, checkNode: CheckNodeFunction): ReadonlyArray<InvalidNode> {

let invalidNodes: Array<InvalidNode> = [];
Expand Down
3 changes: 2 additions & 1 deletion src/readonlyArrayRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function checkArrayTypeOrReference(node: ts.Node, ctx: Lint.WalkContext<Shared.O

function checkVariableOrParameterImplicitType(node: ts.Node, ctx: Lint.WalkContext<Shared.Options>): ReadonlyArray<Shared.InvalidNode> {

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)) {
Expand Down
40 changes: 40 additions & 0 deletions test/rules/readonly-array/ignore-local/variable.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,44 @@ const foo = (bar: Array<string>, zoo: ReadonlyArray<string>, boo = [1, 2, 3]) =>

}

// -- Inside class

class Foo {

// -- Class-level variable declarations

// Should not fail on ReadonlyArray in variable declaration (TypeReferenceNode)
const foo: ReadonlyArray<string> = [];

// Should fail on Array type in variable declaration (TypeReferenceNode)
const foo: Array<string> = [];
~~~~~~~~~~~~~ [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<string>, zoo: ReadonlyArray<string>, boo = [1, 2, 3]) {
~~~~~~~~~~~~~ [failure]
~~~ [failure]

// Should not fail on ReadonlyArray in variable declaration (TypeReferenceNode)
const foo: ReadonlyArray<string> = [];

// Should not fail on Array type in variable declaration when local (TypeReferenceNode)
const foo: Array<string> = [];

// 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.
13 changes: 10 additions & 3 deletions test/rules/readonly-array/work/variable.ts.lint
Original file line number Diff line number Diff line change
@@ -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<string>, zoo: ReadonlyArray<string>, boo = [1, 2, 3]) {
~~~~~~~~~~~~~ [failure]
~~~ [failure]

const foo: Array<string> = [];
}

}

[failure]: Only ReadonlyArray allowed.