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

Commit

Permalink
Fix: missing visitor-keys (#554)
Browse files Browse the repository at this point in the history
- typeParameters in TSInterfaceDeclaration
- decorators in Identifier
- parameters in TSFunctionType
  • Loading branch information
armano2 authored and mysticatea committed Nov 20, 2018
1 parent 9ed21cb commit ae7c4e6
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 3 deletions.
12 changes: 12 additions & 0 deletions analyze-scope.js
Expand Up @@ -69,20 +69,29 @@ class EnumScope extends Scope {
class PatternVisitor extends OriginalPatternVisitor {
Identifier(node) {
super.Identifier(node);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}

ArrayPattern(node) {
node.elements.forEach(this.visit, this);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
}

ObjectPattern(node) {
node.properties.forEach(this.visit, this);
if (node.decorators) {
this.rightHandNodes.push(...node.decorators);
}
if (node.typeAnnotation) {
this.rightHandNodes.push(node.typeAnnotation);
}
Expand Down Expand Up @@ -216,9 +225,12 @@ class Referencer extends OriginalReferencer {
* @returns {void}
*/
Identifier(node) {
this.visitDecorators(node.decorators);

if (!this.typeMode) {
super.Identifier(node);
}

this.visit(node.typeAnnotation);
}

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

}

interface R<T extends C> {
foo: C
}
308 changes: 308 additions & 0 deletions tests/lib/__snapshots__/scope-analysis.js.snap
Expand Up @@ -2927,6 +2927,289 @@ Object {
}
`;

exports[`TypeScript scope analysis tests/fixtures/scope-analysis/identifier-decorators.ts 1`] = `
Object {
"$id": 8,
"block": Object {
"range": Array [
0,
65,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 7,
"block": Object {
"range": Array [
0,
65,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 6,
"block": Object {
"range": Array [
7,
64,
],
"type": "ClassDeclaration",
},
"childScopes": Array [
Object {
"$id": 5,
"block": Object {
"range": Array [
35,
62,
],
"type": "FunctionExpression",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [
Object {
"$id": 4,
"from": Object {
"$ref": 5,
},
"identifier": Object {
"name": "Decorator",
"range": Array [
37,
46,
],
"type": "Identifier",
},
"kind": "r",
"resolved": null,
"writeExpr": undefined,
},
],
"throughReferences": Array [
Object {
"$ref": 4,
},
],
"type": "function",
"upperScope": Object {
"$ref": 6,
},
"variableMap": Object {
"arguments": Object {
"$ref": 2,
},
"config": Object {
"$ref": 3,
},
},
"variableScope": Object {
"$ref": 5,
},
"variables": Array [
Object {
"$id": 2,
"defs": Array [],
"eslintUsed": undefined,
"identifiers": Array [],
"name": "arguments",
"references": Array [],
"scope": Object {
"$ref": 5,
},
},
Object {
"$id": 3,
"defs": Array [
Object {
"name": Object {
"name": "config",
"range": Array [
47,
53,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
35,
62,
],
"type": "FunctionExpression",
},
"parent": null,
"type": "Parameter",
},
],
"eslintUsed": undefined,
"identifiers": Array [
Object {
"name": "config",
"range": Array [
47,
53,
],
"type": "Identifier",
},
],
"name": "config",
"references": Array [],
"scope": Object {
"$ref": 5,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [
Object {
"$ref": 4,
},
],
"type": "class",
"upperScope": Object {
"$ref": 7,
},
"variableMap": Object {
"Test": Object {
"$ref": 1,
},
},
"variableScope": Object {
"$ref": 7,
},
"variables": Array [
Object {
"$id": 1,
"defs": Array [
Object {
"name": Object {
"name": "Test",
"range": Array [
13,
17,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
7,
64,
],
"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": 6,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [
Object {
"$ref": 4,
},
],
"type": "module",
"upperScope": Object {
"$ref": 8,
},
"variableMap": Object {
"Test": Object {
"$ref": 0,
},
},
"variableScope": Object {
"$ref": 7,
},
"variables": Array [
Object {
"$id": 0,
"defs": Array [
Object {
"name": Object {
"name": "Test",
"range": Array [
13,
17,
],
"type": "Identifier",
},
"node": Object {
"range": Array [
7,
64,
],
"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": 7,
},
},
],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [
Object {
"$ref": 4,
},
],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 8,
},
"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 +3286,31 @@ Object {
}
`;

exports[`TypeScript scope analysis tests/fixtures/scope-analysis/interface-type.ts 1`] = `
Object {
"$id": 0,
"block": Object {
"range": Array [
0,
67,
],
"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
6 changes: 3 additions & 3 deletions visitor-keys.js
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 Expand Up @@ -46,9 +46,9 @@ module.exports = Evk.unionWith({
TSLiteralType: ["literal"],
TSIndexSignature: ["typeAnnotation", "index"],
TSInterfaceBody: ["body"],
TSInterfaceDeclaration: ["body", "id", "heritage"],
TSInterfaceDeclaration: ["id", "typeParameters", "heritage", "body"],
TSInterfaceHeritage: ["id", "typeParameters"],
TSFunctionType: ["typeAnnotation"],
TSFunctionType: ["parameters", "typeAnnotation"],
TSMethodSignature: ["typeAnnotation", "typeParameters", "key", "params"],
TSModuleBlock: ["body"],
TSModuleDeclaration: ["id", "body"],
Expand Down

0 comments on commit ae7c4e6

Please sign in to comment.