Skip to content

Commit ec865b7

Browse files
Andyaluanhaddad
Andy
authored andcommitted
Fix assertion -- an import may come from a require() call (#19667)
* Fix assertion -- an import may come from a require() call * Add test for `import("./a")`
1 parent 9ea971d commit ec865b7

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/services/codefixes/importFixes.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ namespace ts.codefix {
238238
return parent as ImportDeclaration;
239239
case SyntaxKind.ExternalModuleReference:
240240
return (parent as ExternalModuleReference).parent;
241-
case SyntaxKind.ImportEqualsDeclaration:
242-
return parent as ImportEqualsDeclaration;
243-
default:
244-
Debug.assert(parent.kind === SyntaxKind.ExportDeclaration);
241+
case SyntaxKind.ExportDeclaration:
242+
case SyntaxKind.CallExpression: // For "require()" calls
245243
// Ignore these, can't add imports to them.
246244
return undefined;
245+
default:
246+
Debug.fail();
247247
}
248248
}
249249

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
5+
// @Filename: /a.ts
6+
////export const foo = 0;
7+
8+
// @Filename: /b.js
9+
////const a = require("./a");
10+
////fo/*b*/
11+
12+
// @Filename: /c.js
13+
////const a = import("./a");
14+
////fo/*c*/
15+
16+
goTo.marker("b");
17+
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
18+
19+
verify.applyCodeActionFromCompletion("b", {
20+
name: "foo",
21+
source: "/a",
22+
description: `Import 'foo' from "./a".`,
23+
// TODO: GH#18445
24+
newFileContent: `import { foo } from "./a";\r
25+
\r
26+
const a = require("./a");
27+
fo`,
28+
});
29+
30+
goTo.marker("c");
31+
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: true });
32+
33+
verify.applyCodeActionFromCompletion("c", {
34+
name: "foo",
35+
source: "/a",
36+
description: `Import 'foo' from "./a".`,
37+
// TODO: GH#18445
38+
newFileContent: `import { foo } from "./a";\r
39+
\r
40+
const a = import("./a");
41+
fo`,
42+
});

0 commit comments

Comments
 (0)