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")`
This commit is contained in:
parent
9c8129eeac
commit
2fcf8b7068
2 changed files with 46 additions and 4 deletions
|
@ -237,12 +237,12 @@ namespace ts.codefix {
|
|||
return parent as ImportDeclaration;
|
||||
case SyntaxKind.ExternalModuleReference:
|
||||
return (parent as ExternalModuleReference).parent;
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
return parent as ImportEqualsDeclaration;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
42
tests/cases/fourslash/completionsImport_require.ts
Normal file
42
tests/cases/fourslash/completionsImport_require.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @allowJs: true
|
||||
|
||||
// @Filename: /a.ts
|
||||
////export const foo = 0;
|
||||
|
||||
// @Filename: /b.js
|
||||
////const a = require("./a");
|
||||
////fo/*b*/
|
||||
|
||||
// @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, { includeExternalModuleExports: true });
|
||||
|
||||
verify.applyCodeActionFromCompletion("b", {
|
||||
name: "foo",
|
||||
source: "/a",
|
||||
description: `Import 'foo' from "./a".`,
|
||||
// TODO: GH#18445
|
||||
newFileContent: `import { foo } from "./a";\r
|
||||
\r
|
||||
const a = require("./a");
|
||||
fo`,
|
||||
});
|
||||
|
||||
goTo.marker("c");
|
||||
verify.completionListContains({ name: "foo", source: "/a" }, "const foo: 0", "", "const", /*spanIndex*/ undefined, /*hasAction*/ true, { includeExternalModuleExports: 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`,
|
||||
});
|
Loading…
Reference in a new issue