From 2a8737cea38a08231446d7d5de25a84a0c1bfd19 Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 29 Nov 2018 00:28:30 +0100 Subject: [PATCH] Fix: scope for TSImportEqualsDeclaration --- analyze-scope.js | 16 ++++ .../fixtures/scope-analysis/import-equals.ts | 1 + .../lib/__snapshots__/scope-analysis.js.snap | 95 +++++++++++++++++++ visitor-keys.js | 2 + 4 files changed, 114 insertions(+) create mode 100644 tests/fixtures/scope-analysis/import-equals.ts diff --git a/analyze-scope.js b/analyze-scope.js index cfec9f5..95c378d 100644 --- a/analyze-scope.js +++ b/analyze-scope.js @@ -610,6 +610,22 @@ class Referencer extends OriginalReferencer { this.MethodDefinition(node); } + /** + * Process import equal declaration + * @param {TSImportEqualsDeclaration} node The TSImportEqualsDeclaration node to visit. + * @returns {void} + */ + TSImportEqualsDeclaration(node) { + const { name, moduleReference } = node; + if (name && name.type === "Identifier") { + this.currentScope().__define( + name, + new Definition("ImportBinding", name, node, null, null, null) + ); + } + this.visit(moduleReference); + } + /** * Process the global augmentation. * 1. Set the global scope as the current scope. diff --git a/tests/fixtures/scope-analysis/import-equals.ts b/tests/fixtures/scope-analysis/import-equals.ts new file mode 100644 index 0000000..be7ce51 --- /dev/null +++ b/tests/fixtures/scope-analysis/import-equals.ts @@ -0,0 +1 @@ +import foo = require('bar') diff --git a/tests/lib/__snapshots__/scope-analysis.js.snap b/tests/lib/__snapshots__/scope-analysis.js.snap index 2ae68e0..6316b14 100644 --- a/tests/lib/__snapshots__/scope-analysis.js.snap +++ b/tests/lib/__snapshots__/scope-analysis.js.snap @@ -4480,6 +4480,101 @@ Object { } `; +exports[`TypeScript scope analysis tests/fixtures/scope-analysis/import-equals.ts 1`] = ` +Object { + "$id": 2, + "block": Object { + "range": Array [ + 0, + 28, + ], + "type": "Program", + }, + "childScopes": Array [ + Object { + "$id": 1, + "block": Object { + "range": Array [ + 0, + 28, + ], + "type": "Program", + }, + "childScopes": Array [], + "functionExpressionScope": false, + "isStrict": true, + "references": Array [], + "throughReferences": Array [], + "type": "module", + "upperScope": Object { + "$ref": 2, + }, + "variableMap": Object { + "foo": Object { + "$ref": 0, + }, + }, + "variableScope": Object { + "$ref": 1, + }, + "variables": Array [ + Object { + "$id": 0, + "defs": Array [ + Object { + "name": Object { + "name": "foo", + "range": Array [ + 7, + 10, + ], + "type": "Identifier", + }, + "node": Object { + "range": Array [ + 0, + 27, + ], + "type": "TSImportEqualsDeclaration", + }, + "parent": null, + "type": "ImportBinding", + }, + ], + "eslintUsed": undefined, + "identifiers": Array [ + Object { + "name": "foo", + "range": Array [ + 7, + 10, + ], + "type": "Identifier", + }, + ], + "name": "foo", + "references": Array [], + "scope": Object { + "$ref": 1, + }, + }, + ], + }, + ], + "functionExpressionScope": false, + "isStrict": false, + "references": Array [], + "throughReferences": Array [], + "type": "global", + "upperScope": null, + "variableMap": Object {}, + "variableScope": Object { + "$ref": 2, + }, + "variables": Array [], +} +`; + exports[`TypeScript scope analysis tests/fixtures/scope-analysis/interface-type.ts 1`] = ` Object { "$id": 0, diff --git a/visitor-keys.js b/visitor-keys.js index 4beead8..40a5e49 100644 --- a/visitor-keys.js +++ b/visitor-keys.js @@ -44,12 +44,14 @@ module.exports = Evk.unionWith({ TSEnumMember: ["id", "initializer"], TSExportAssignment: ["expression"], TSExportKeyword: [], + TSExternalModuleReference: ["expression"], TSImportType: ["parameter", "qualifier", "typeParameters"], TSLiteralType: ["literal"], TSIndexSignature: ["typeAnnotation", "index"], TSInterfaceBody: ["body"], TSInterfaceDeclaration: ["id", "typeParameters", "heritage", "body"], TSInterfaceHeritage: ["id", "typeParameters"], + TSImportEqualsDeclaration: ["name", "moduleReference"], TSFunctionType: ["parameters", "typeAnnotation"], TSMethodSignature: ["typeAnnotation", "typeParameters", "key", "params"], TSModuleBlock: ["body"],