Skip to content

Fix assertion -- an import may come from a require() call #19667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 commits merged into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
42 changes: 42 additions & 0 deletions tests/cases/fourslash/completionsImport_require.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <reference path="fourslash.ts" />

// @allowJs: true

// @Filename: /a.ts
////export const foo = 0;

// @Filename: /b.js
////const a = require("./a");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add a test for import("./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`,
});