Skip to content

Commit

Permalink
fix(scope): Recognise destructured constants.
Browse files Browse the repository at this point in the history
Closes #54.
  • Loading branch information
cartant committed Aug 1, 2018
1 parent a72b5e5 commit 53af1d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
8 changes: 3 additions & 5 deletions source/rules/rxjsNoUnsafeScopeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 3 additions & 5 deletions source/rules/rxjsNoUnsafeTakewhileRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)) {
Expand Down
18 changes: 18 additions & 0 deletions source/support/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
21 changes: 11 additions & 10 deletions test/v6/fixtures/issues/54/fixture.ts.lint
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53af1d4

Please sign in to comment.