Skip to content

Commit c5420be

Browse files
committed
Prefer default imports over import = when auto-fixing with esModuleInterop being on - fixes microsoft#29038
1 parent 97b5671 commit c5420be

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7289,6 +7289,10 @@ namespace ts {
72897289
moduleKind === ModuleKind.System;
72907290
}
72917291

7292+
export function getEsModuleInterop(compilerOptions: CompilerOptions) {
7293+
return compilerOptions.esModuleInterop;
7294+
}
7295+
72927296
export function getEmitDeclarations(compilerOptions: CompilerOptions): boolean {
72937297
return !!(compilerOptions.declaration || compilerOptions.composite);
72947298
}

src/services/codefixes/importFixes.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,17 +420,22 @@ namespace ts.codefix {
420420
function getDefaultLikeExportInfo(
421421
moduleSymbol: Symbol, checker: TypeChecker, compilerOptions: CompilerOptions,
422422
): { readonly symbol: Symbol, readonly symbolForMeaning: Symbol, readonly name: string, readonly kind: ImportKind.Default | ImportKind.Equals } | undefined {
423-
const exported = getDefaultLikeExportWorker(moduleSymbol, checker);
423+
const exported = getDefaultLikeExportWorker(moduleSymbol, checker, compilerOptions);
424424
if (!exported) return undefined;
425425
const { symbol, kind } = exported;
426426
const info = getDefaultExportInfoWorker(symbol, moduleSymbol, checker, compilerOptions);
427427
return info && { symbol, kind, ...info };
428428
}
429429

430-
function getDefaultLikeExportWorker(moduleSymbol: Symbol, checker: TypeChecker): { readonly symbol: Symbol, readonly kind: ImportKind.Default | ImportKind.Equals } | undefined {
430+
function getDefaultLikeExportWorker(moduleSymbol: Symbol, checker: TypeChecker, compilerOptions: CompilerOptions): { readonly symbol: Symbol, readonly kind: ImportKind.Default | ImportKind.Equals } | undefined {
431431
const defaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol);
432432
if (defaultExport) return { symbol: defaultExport, kind: ImportKind.Default };
433433
const exportEquals = checker.resolveExternalModuleSymbol(moduleSymbol);
434+
435+
// When ESModule interop is on we want to always use default style instead
436+
if (getEsModuleInterop(compilerOptions)) {
437+
return { symbol: exportEquals, kind: ImportKind.Default };
438+
}
434439
return exportEquals === moduleSymbol ? undefined : { symbol: exportEquals, kind: ImportKind.Equals };
435440
}
436441

@@ -550,6 +555,7 @@ namespace ts.codefix {
550555
defaultImport === undefined ? undefined : createIdentifier(defaultImport),
551556
namedImports.map(n => createImportSpecifier(/*propertyName*/ undefined, createIdentifier(n))), moduleSpecifier, quotePreference));
552557
}
558+
553559
if (namespaceLikeImport) {
554560
insertImport(changes, sourceFile, namespaceLikeImport.importKind === ImportKind.Equals
555561
? createImportEqualsDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, createIdentifier(namespaceLikeImport.name), createExternalModuleReference(quotedModuleSpecifier))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
// #29038
3+
4+
// @allowJs: true
5+
// @checkJs: true
6+
// @esModuleInterop: true
7+
// @moduleResolution: node
8+
9+
// @Filename: /node_modules/moment.d.ts
10+
////declare function moment(): void;
11+
////export = moment;
12+
13+
// @Filename: /b.js
14+
////[|moment;|]
15+
16+
goTo.file("/b.js");
17+
verify.importFixAtPosition([
18+
`import moment from "moment";
19+
20+
moment;`,
21+
]);

0 commit comments

Comments
 (0)