Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Fix: visiting decorators in Identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Nov 17, 2018
1 parent f6bb022 commit d5b5d51
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 1 deletion.
2 changes: 2 additions & 0 deletions analyze-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ class Referencer extends OriginalReferencer {
if (!this.typeMode) {
super.Identifier(node);
}

this.visit(node.typeAnnotation);
this.visitDecorators(node.decorators);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/scope-analysis/identifier-decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class Test {
constructor(@Decorator config) {
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/scope-analysis/interface-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface C<T = any> {

}

interface R<T extends C> {
foo: C
}
274 changes: 274 additions & 0 deletions tests/lib/__snapshots__/scope-analysis.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2927,6 +2927,255 @@ Object {
}
`;

exports[`TypeScript scope analysis tests/fixtures/scope-analysis/identifier-decorators.ts 1`] = `
Object {
"$id": 7,
"block": Object {
"range": Array [
0,
69,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 6,
"block": Object {
"range": Array [
0,
69,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 5,
"block": Object {
"range": Array [
7,
67,
],
"type": "ClassDeclaration",
},
"childScopes": Array [
Object {
"$id": 4,
"block": Object {
"range": Array [
36,
64,
],
"type": "FunctionExpression",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "function",
"upperScope": Object {
"$ref": 5,
},
"variableMap": Object {
"arguments": Object {
"$ref": 2,
},
"config": Object {
"$ref": 3,
},
},
"variableScope": Object {
"$ref": 4,
},
"variables": Array [
Object {
"$id": 2,
"defs": Array [],
"eslintUsed": undefined,
"identifiers": Array [],
"name": "arguments",
"references": Array [],
"scope": Object {
"$ref": 4,
},
},
Object {
"$id": 3,
"defs": Array [
Object {
"name": Object {
"name": "config",
"range": Array [
48,
54,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
36,
64,
],
"type": "FunctionExpression",
},
"parent": null,
"type": "Parameter",
},
],
"eslintUsed": undefined,
"identifiers": Array [
Object {
"name": "config",
"range": Array [
48,
54,
],
"type": "Identifier",
},
],
"name": "config",
"references": Array [],
"scope": Object {
"$ref": 4,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "class",
"upperScope": Object {
"$ref": 6,
},
"variableMap": Object {
"Test": Object {
"$ref": 1,
},
},
"variableScope": Object {
"$ref": 6,
},
"variables": Array [
Object {
"$id": 1,
"defs": Array [
Object {
"name": Object {
"name": "Test",
"range": Array [
13,
17,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
7,
67,
],
"type": "ClassDeclaration",
},
"parent": undefined,
"type": "ClassName",
},
],
"eslintUsed": undefined,
"identifiers": Array [
Object {
"name": "Test",
"range": Array [
13,
17,
],
"type": "Identifier",
},
],
"name": "Test",
"references": Array [],
"scope": Object {
"$ref": 5,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "module",
"upperScope": Object {
"$ref": 7,
},
"variableMap": Object {
"Test": Object {
"$ref": 0,
},
},
"variableScope": Object {
"$ref": 6,
},
"variables": Array [
Object {
"$id": 0,
"defs": Array [
Object {
"name": Object {
"name": "Test",
"range": Array [
13,
17,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
7,
67,
],
"type": "ClassDeclaration",
},
"parent": null,
"type": "ClassName",
},
],
"eslintUsed": undefined,
"identifiers": Array [
Object {
"name": "Test",
"range": Array [
13,
17,
],
"type": "Identifier",
},
],
"name": "Test",
"references": Array [],
"scope": Object {
"$ref": 6,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 7,
},
"variables": Array [],
}
`;

exports[`TypeScript scope analysis tests/fixtures/scope-analysis/ignore-type-only-stuff.ts 1`] = `
Object {
"$id": 1,
Expand Down Expand Up @@ -3003,6 +3252,31 @@ Object {
}
`;

exports[`TypeScript scope analysis tests/fixtures/scope-analysis/interface-type.ts 1`] = `
Object {
"$id": 0,
"block": Object {
"range": Array [
0,
74,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 0,
},
"variables": Array [],
}
`;

exports[`TypeScript scope analysis tests/fixtures/scope-analysis/method-overload.ts 1`] = `
Object {
"$id": 10,
Expand Down
2 changes: 1 addition & 1 deletion visitor-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = Evk.unionWith({
ClassExpression: ["decorators", "id", "typeParameters", "superClass", "body"],
FunctionDeclaration: ["id", "typeParameters", "params", "returnType", "body"],
FunctionExpression: ["id", "typeParameters", "params", "returnType", "body"],
Identifier: ["typeAnnotation"],
Identifier: ["decorators", "typeAnnotation"],
MethodDefinition: ["decorators", "key", "value"],
ObjectPattern: ["properties", "typeAnnotation"],

Expand Down

0 comments on commit d5b5d51

Please sign in to comment.