Skip to content

Fix import statement completions for export= in JS files #45128

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
merged 1 commit into from
Jul 29, 2021
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
24 changes: 15 additions & 9 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,21 @@ namespace ts.codefix {
: undefined;
}

export function getImportKind(importingFile: SourceFile, exportKind: ExportKind, compilerOptions: CompilerOptions): ImportKind {
/**
* @param forceImportKeyword Indicates that the user has already typed `import`, so the result must start with `import`.
* (In other words, do not allow `const x = require("...")` for JS files.)
Comment on lines +568 to +569
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param forceImportKeyword Indicates that the user has already typed `import`, so the result must start with `import`.
* (In other words, do not allow `const x = require("...")` for JS files.)
* @param forceImportKeyword Indicates that the user has already typed `import`, so the result must start with `import`.
* For example, we should not provide `const x = require("...")` for JS files if it is clear that a user is writing an `import`.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would apply this except it has a bunch of leading spaces?

*/
export function getImportKind(importingFile: SourceFile, exportKind: ExportKind, compilerOptions: CompilerOptions, forceImportKeyword?: boolean): ImportKind {
switch (exportKind) {
case ExportKind.Named: return ImportKind.Named;
case ExportKind.Default: return ImportKind.Default;
case ExportKind.ExportEquals: return getExportEqualsImportKind(importingFile, compilerOptions);
case ExportKind.UMD: return getUmdImportKind(importingFile, compilerOptions);
case ExportKind.ExportEquals: return getExportEqualsImportKind(importingFile, compilerOptions, !!forceImportKeyword);
case ExportKind.UMD: return getUmdImportKind(importingFile, compilerOptions, !!forceImportKeyword);
default: return Debug.assertNever(exportKind);
}
}

function getUmdImportKind(importingFile: SourceFile, compilerOptions: CompilerOptions): ImportKind {
function getUmdImportKind(importingFile: SourceFile, compilerOptions: CompilerOptions, forceImportKeyword: boolean): ImportKind {
// Import a synthetic `default` if enabled.
if (getAllowSyntheticDefaultImports(compilerOptions)) {
return ImportKind.Default;
Expand All @@ -587,7 +591,7 @@ namespace ts.codefix {
case ModuleKind.CommonJS:
case ModuleKind.UMD:
if (isInJSFile(importingFile)) {
return isExternalModule(importingFile) ? ImportKind.Namespace : ImportKind.CommonJS;
return isExternalModule(importingFile) || forceImportKeyword ? ImportKind.Namespace : ImportKind.CommonJS;
}
return ImportKind.CommonJS;
case ModuleKind.System:
Expand Down Expand Up @@ -675,18 +679,20 @@ namespace ts.codefix {
return originalSymbolToExportInfos;
}

function getExportEqualsImportKind(importingFile: SourceFile, compilerOptions: CompilerOptions): ImportKind {
function getExportEqualsImportKind(importingFile: SourceFile, compilerOptions: CompilerOptions, forceImportKeyword: boolean): ImportKind {
const allowSyntheticDefaults = getAllowSyntheticDefaultImports(compilerOptions);
const isJS = isInJSFile(importingFile);
// 1. 'import =' will not work in es2015+ TS files, so the decision is between a default
// and a namespace import, based on allowSyntheticDefaultImports/esModuleInterop.
if (!isJS && getEmitModuleKind(compilerOptions) >= ModuleKind.ES2015) {
return allowSyntheticDefaults ? ImportKind.Default : ImportKind.Namespace;
}
// 2. 'import =' will not work in JavaScript, so the decision is between a default
// and const/require.
// 2. 'import =' will not work in JavaScript, so the decision is between a default import,
// a namespace import, and const/require.
if (isJS) {
return isExternalModule(importingFile) ? ImportKind.Default : ImportKind.CommonJS;
return isExternalModule(importingFile) || forceImportKeyword
? allowSyntheticDefaults ? ImportKind.Default : ImportKind.Namespace
: ImportKind.CommonJS;
}
// 3. At this point the most correct choice is probably 'import =', but people
// really hate that, so look to see if the importing file has any precedent
Expand Down
4 changes: 2 additions & 2 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,12 @@ namespace ts.Completions {
origin.exportName === InternalSymbolName.ExportEquals ? ExportKind.ExportEquals :
ExportKind.Named;
const tabStop = preferences.includeCompletionsWithSnippetText ? "$1" : "";
const importKind = codefix.getImportKind(sourceFile, exportKind, options);
const importKind = codefix.getImportKind(sourceFile, exportKind, options, /*forceImportKeyword*/ true);
const suffix = useSemicolons ? ";" : "";
switch (importKind) {
case ImportKind.CommonJS: return { replacementSpan, insertText: `import ${name}${tabStop} = require(${quotedModuleSpecifier})${suffix}` };
case ImportKind.Default: return { replacementSpan, insertText: `import ${name}${tabStop} from ${quotedModuleSpecifier}${suffix}` };
case ImportKind.Namespace: return { replacementSpan, insertText: `import * as ${name}${tabStop} from ${quotedModuleSpecifier}${suffix}` };
case ImportKind.Namespace: return { replacementSpan, insertText: `import * as ${name} from ${quotedModuleSpecifier}${suffix}` };
Copy link
Member Author

Choose a reason for hiding this comment

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

Drive by fix: tabstop is unnecessary in this snippet because you can’t add a comma and import more things after import * as whatever

case ImportKind.Named: return { replacementSpan, insertText: `import { ${name}${tabStop} } from ${quotedModuleSpecifier}${suffix}` };
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/cases/fourslash/importStatementCompletions_js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path="fourslash.ts" />

// These options resemble the defaults for inferred projects with JS

// @allowJs: true
// @target: es2020
// @checkJs: true
// @module: commonjs
// @noEmit: true
// @allowSyntheticDefaultImports: true

// @Filename: /node_modules/react/index.d.ts
//// declare namespace React {}
//// export = React;

// @Filename: /test.js
//// [|import R/**/|]

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [{
name: "React",
source: "react",
insertText: `import React$1 from "react";`,
isSnippet: true,
replacementSpan: test.ranges()[0],
sourceDisplay: "react",
}],
preferences: {
includeCompletionsForImportStatements: true,
includeInsertTextCompletions: true,
includeCompletionsWithSnippetText: true,
}
});
37 changes: 37 additions & 0 deletions tests/cases/fourslash/importStatementCompletions_js2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference path="fourslash.ts" />

// These options resemble the defaults for inferred projects with JS,
// except notably lacking --allowSyntheticDefaultImports. Probably nobody
// ever wants a configuration like this, but we maintain that this is
// correct and consistent behavior for these settings.

// @allowJs: true
// @target: es2020
// @checkJs: true
// @module: commonjs
// @noEmit: true

// @Filename: /node_modules/react/index.d.ts
//// declare namespace React {}
//// export = React;

// @Filename: /test.js
//// [|import R/**/|]

verify.completions({
marker: "",
isNewIdentifierLocation: true,
exact: [{
name: "React",
source: "react",
insertText: `import * as React from "react";`,
isSnippet: true,
replacementSpan: test.ranges()[0],
sourceDisplay: "react",
}],
preferences: {
includeCompletionsForImportStatements: true,
includeInsertTextCompletions: true,
includeCompletionsWithSnippetText: true,
}
});