From e49f094b7ed7b650f0d9b29e707704a9ec79315a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 1 Nov 2017 13:30:08 -0700 Subject: [PATCH 1/2] Fix assertion -- an import may come from a require() call --- src/services/codefixes/importFixes.ts | 6 +++-- .../fourslash/completionsImport_require.ts | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/completionsImport_require.ts diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 3f0d27e5b070f..1844f98cb37c1 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -237,10 +237,12 @@ namespace ts.codefix { return parent as ImportDeclaration; case SyntaxKind.ExternalModuleReference: return (parent as ExternalModuleReference).parent; - default: - Debug.assert(parent.kind === SyntaxKind.ExportDeclaration); + case SyntaxKind.ExportDeclaration: + case SyntaxKind.CallExpression: // For "require()" calls // Ignore these, can't add imports to them. return undefined; + default: + Debug.fail(); } } diff --git a/tests/cases/fourslash/completionsImport_require.ts b/tests/cases/fourslash/completionsImport_require.ts new file mode 100644 index 0000000000000..056b4ebf71f4d --- /dev/null +++ b/tests/cases/fourslash/completionsImport_require.ts @@ -0,0 +1,24 @@ +/// + +// @allowJs: true + +// @Filename: /a.ts +////export const foo = 0; + +// @Filename: /b.js +////const a = require("./a"); +////fo/**/ + +goTo.marker(""); +verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("", { + name: "foo", + source: "/a", + description: `Import 'foo' from "./a".`, + // TODO: GH#18445 + newFileContent: `import { foo } from "./a";\r +\r +const a = require("./a"); +fo`, +}); From e23ddc63376e33c33e2a7d665ec82bb6ed00bdfe Mon Sep 17 00:00:00 2001 From: andy-ms Date: Mon, 6 Nov 2017 18:51:29 -0800 Subject: [PATCH 2/2] Add test for `import("./a")` --- .../fourslash/completionsImport_require.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/cases/fourslash/completionsImport_require.ts b/tests/cases/fourslash/completionsImport_require.ts index 056b4ebf71f4d..c9c9f3362c082 100644 --- a/tests/cases/fourslash/completionsImport_require.ts +++ b/tests/cases/fourslash/completionsImport_require.ts @@ -7,12 +7,16 @@ // @Filename: /b.js ////const a = require("./a"); -////fo/**/ +////fo/*b*/ -goTo.marker(""); +// @Filename: /c.js +////const a = import("./a"); +////fo/*c*/ + +goTo.marker("b"); verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true); -verify.applyCodeActionFromCompletion("", { +verify.applyCodeActionFromCompletion("b", { name: "foo", source: "/a", description: `Import 'foo' from "./a".`, @@ -22,3 +26,17 @@ verify.applyCodeActionFromCompletion("", { const a = require("./a"); fo`, }); + +goTo.marker("c"); +verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true); + +verify.applyCodeActionFromCompletion("c", { + name: "foo", + source: "/a", + description: `Import 'foo' from "./a".`, + // TODO: GH#18445 + newFileContent: `import { foo } from "./a";\r +\r +const a = import("./a"); +fo`, +});