From 05bf00af0cca3e43ca566c99c436a277bebfe23a Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 22 Nov 2018 02:35:32 +0100 Subject: [PATCH] Fix: missing implements in classes --- analyze-scope.js | 13 +- .../scope-analysis/class-implements.ts | 3 + .../lib/__snapshots__/scope-analysis.js.snap | 140 ++++++++++++++++++ visitor-keys.js | 4 +- 4 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/scope-analysis/class-implements.ts diff --git a/analyze-scope.js b/analyze-scope.js index 0fb87e9..4c4ec12 100644 --- a/analyze-scope.js +++ b/analyze-scope.js @@ -210,11 +210,19 @@ class Referencer extends OriginalReferencer { /** * Override. * Visit decorators. - * @param {ClassDeclaration|ClassExpression} node The class node to visit. + * @param {ClassDeclaration|ClassExpression|TSAbstractClassDeclaration} node The class node to visit. * @returns {void} */ visitClass(node) { this.visitDecorators(node.decorators); + + const upperTypeMode = this.typeMode; + this.typeMode = true; + if (node.implements) { + this.visit(node.implements); + } + this.typeMode = upperTypeMode; + super.visitClass(node); } @@ -315,6 +323,7 @@ class Referencer extends OriginalReferencer { this.visit(returnType); this.typeMode = upperTypeMode; } + TSEmptyBodyDeclareFunction(node) { this.TSEmptyBodyFunctionDeclaration(node); } @@ -550,9 +559,11 @@ class Referencer extends OriginalReferencer { TSAbstractClassDeclaration(node) { this.ClassDeclaration(node); } + TSAbstractClassProperty(node) { this.ClassProperty(node); } + TSAbstractMethodDefinition(node) { this.MethodDefinition(node); } diff --git a/tests/fixtures/scope-analysis/class-implements.ts b/tests/fixtures/scope-analysis/class-implements.ts new file mode 100644 index 0000000..8da64a0 --- /dev/null +++ b/tests/fixtures/scope-analysis/class-implements.ts @@ -0,0 +1,3 @@ +class Foo implements Component, {}>, Component2 { + +} diff --git a/tests/lib/__snapshots__/scope-analysis.js.snap b/tests/lib/__snapshots__/scope-analysis.js.snap index 8cf1838..2be8d59 100644 --- a/tests/lib/__snapshots__/scope-analysis.js.snap +++ b/tests/lib/__snapshots__/scope-analysis.js.snap @@ -273,6 +273,146 @@ Object { } `; +exports[`TypeScript scope analysis tests/fixtures/scope-analysis/class-implements.ts 1`] = ` +Object { + "$id": 3, + "block": Object { + "range": Array [ + 0, + 83, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 2, + "block": Object { + "range": Array [ + 0, + 82, + ], + "type": "ClassDeclaration", + }, + "childScopes": Array [], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "class", + "upperScope": Object { + "$ref": 3, + }, + "variableMap": Object { + "Foo": Object { + "$ref": 1, + }, + }, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [ + Object { + "$id": 1, + "defs": Array [ + Object { + "name": Object { + "name": "Foo", + "range": Array [ + 6, + 9, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 0, + 82, + ], + "type": "ClassDeclaration", + }, + "parent": undefined, + "type": "ClassName", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "Foo", + "range": Array [ + 6, + 9, + ], + "type": "Identifier", + }, + ], + "name": "Foo", + "references": Array [], + "scope": Object { + "$ref": 2, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": false, + "references": Array [], + "throughReferences": Array [], + "type": "global", + "upperScope": null, + "variableMap": Object { + "Foo": Object { + "$ref": 0, + }, + }, + "variableScope": Object { + "$ref": 3, + }, + "variables": Array [ + Object { + "$id": 0, + "defs": Array [ + Object { + "name": Object { + "name": "Foo", + "range": Array [ + 6, + 9, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 0, + 82, + ], + "type": "ClassDeclaration", + }, + "parent": null, + "type": "ClassName", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "Foo", + "range": Array [ + 6, + 9, + ], + "type": "Identifier", + }, + ], + "name": "Foo", + "references": Array [], + "scope": Object { + "$ref": 3, + }, + }, + ], +} +`; + exports[`TypeScript scope analysis tests/fixtures/scope-analysis/class-properties.ts 1`] = ` Object { "$id": 8, diff --git a/visitor-keys.js b/visitor-keys.js index 1de50cd..4c06260 100644 --- a/visitor-keys.js +++ b/visitor-keys.js @@ -12,8 +12,8 @@ module.exports = Evk.unionWith({ // Additional Properties. ArrayPattern: ["elements", "typeAnnotation"], ArrowFunctionExpression: ["typeParameters", "params", "returnType", "body"], - ClassDeclaration: ["decorators", "id", "typeParameters", "superClass", "body"], - ClassExpression: ["decorators", "id", "typeParameters", "superClass", "body"], + ClassDeclaration: ["decorators", "id", "typeParameters", "superClass", "implements", "body"], + ClassExpression: ["decorators", "id", "typeParameters", "superClass", "implements", "body"], FunctionDeclaration: ["id", "typeParameters", "params", "returnType", "body"], FunctionExpression: ["id", "typeParameters", "params", "returnType", "body"], Identifier: ["decorators", "typeAnnotation"],