diff --git a/source/rules/rxjsNoUnsafeScopeRule.ts b/source/rules/rxjsNoUnsafeScopeRule.ts index 7eacda4d..c114f1f1 100644 --- a/source/rules/rxjsNoUnsafeScopeRule.ts +++ b/source/rules/rxjsNoUnsafeScopeRule.ts @@ -8,7 +8,7 @@ import * as Lint from "tslint"; import * as ts from "typescript"; import * as tsutils from "tsutils"; import { ScopeWalker } from "../support/scope-walker"; -import { isThis, isWithinCallExpressionExpression } from "../support/util"; +import { isConstDeclaration, isThis, isWithinCallExpressionExpression } from "../support/util"; export class Rule extends Lint.Rules.TypedRule { @@ -128,10 +128,8 @@ class Walker extends ScopeWalker { } } - if (tsutils.isVariableDeclarationList(declaration.parent)) { - if (tsutils.getVariableDeclarationKind(declaration.parent) === tsutils.VariableDeclarationKind.Const) { - return false; - } + if (isConstDeclaration(declaration)) { + return false; } if (tsutils.isImportSpecifier(declaration)) { diff --git a/source/rules/rxjsNoUnsafeTakewhileRule.ts b/source/rules/rxjsNoUnsafeTakewhileRule.ts index 5fd47304..d9aeb3f4 100644 --- a/source/rules/rxjsNoUnsafeTakewhileRule.ts +++ b/source/rules/rxjsNoUnsafeTakewhileRule.ts @@ -8,7 +8,7 @@ import * as Lint from "tslint"; import * as ts from "typescript"; import * as tsutils from "tsutils"; import { ScopeWalker } from "../support/scope-walker"; -import { couldBeType, isThis } from "../support/util"; +import { isConstDeclaration, isThis } from "../support/util"; export class Rule extends Lint.Rules.TypedRule { @@ -80,10 +80,8 @@ class Walker extends ScopeWalker { } } - if (tsutils.isVariableDeclarationList(declaration.parent)) { - if (tsutils.getVariableDeclarationKind(declaration.parent) === tsutils.VariableDeclarationKind.Const) { - return false; - } + if (isConstDeclaration(declaration)) { + return false; } if (tsutils.isImportSpecifier(declaration)) { diff --git a/source/support/util.ts b/source/support/util.ts index b5d39605..7b6b5274 100644 --- a/source/support/util.ts +++ b/source/support/util.ts @@ -24,6 +24,24 @@ export function couldBeType(type: ts.Type, name: string | RegExp): boolean { return Boolean(baseTypes) && baseTypes.some((t) => couldBeType(t, name)); } +export function isConstDeclaration(declaration: ts.Declaration): boolean { + + if (tsutils.isVariableDeclaration(declaration)) { + if (tsutils.isVariableDeclarationList(declaration.parent)) { + return tsutils.getVariableDeclarationKind(declaration.parent) === tsutils.VariableDeclarationKind.Const; + } + } else if (tsutils.isBindingElement(declaration)) { + let parent: ts.Node = declaration.parent; + while (tsutils.isBindingPattern(parent) || tsutils.isVariableDeclaration(parent)) { + parent = parent.parent; + } + if (tsutils.isVariableDeclarationList(parent)) { + return tsutils.getVariableDeclarationKind(parent) === tsutils.VariableDeclarationKind.Const; + } + } + return false; +} + export function isReferenceType(type: ts.Type): type is ts.TypeReference { return tsutils.isTypeFlagSet(type, ts.TypeFlags.Object) && diff --git a/test/v6/fixtures/issues/54/fixture.ts.lint b/test/v6/fixtures/issues/54/fixture.ts.lint index be0c25ef..96b18126 100644 --- a/test/v6/fixtures/issues/54/fixture.ts.lint +++ b/test/v6/fixtures/issues/54/fixture.ts.lint @@ -1,18 +1,19 @@ import { of } from "rxjs"; import { map, tap } from "rxjs/operators"; -ok_mapAnArray () { - const q = '123' - return [ 1, 2, 3 ].map(val => { - return `${q}-${val}` - }) +function mapAnArray() { + const q = '123'; + return [1, 2, 3].map(val => `${q}-${val}`); } -notOk_mapAnArrayFromParam (input: { q: string }) { - const { q } = input - return [ 1, 2, 3 ].map(val => { - return `${q}-${val}` - }) +function mapAnArrayFromParam (input: { q: string }) { + const { q } = input; + return [1, 2, 3].map(val => `${q}-${val}`); +} + +function mapAnArrayFromRestParams (...inputs: string[]) { + const [q] = inputs; + return [1, 2, 3].map(val => `${q}-${val}`); } [no-unsafe-scope]: Unsafe scopes are forbidden