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:
Andy 2017-11-07 07:41:21 -08:00 committed by GitHub
parent 9c8129eeac
commit 2fcf8b7068
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 4 deletions

View file

@ -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();
}
}

View 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`,
});