Skip to content

Commit ff58f1f

Browse files
committed
Handle different default export forms the same way in import code fixes
`export default C` and `export { C as default }` should be handled the same as `export default class C { }`. Fixes microsoft#19115
1 parent 0477f91 commit ff58f1f

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/services/codefixes/importFixes.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,11 @@ namespace ts.codefix {
770770
const defaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol);
771771
if (defaultExport) {
772772
const localSymbol = getLocalSymbolForExportDefault(defaultExport);
773-
if ((localSymbol && localSymbol.escapedName === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName)
774-
&& checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) {
773+
if ((
774+
localSymbol && localSymbol.escapedName === symbolName ||
775+
getEscapedNameForExportDefault(defaultExport) === symbolName ||
776+
moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName
777+
) && checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) {
775778
// check if this symbol is already used
776779
const symbolId = getUniqueSymbolId(localSymbol || defaultExport, checker);
777780
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Default }));
@@ -784,6 +787,23 @@ namespace ts.codefix {
784787
const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName, checker);
785788
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Named }));
786789
}
790+
791+
function getEscapedNameForExportDefault(symbol: Symbol): __String | undefined {
792+
const declarations = symbol.declarations;
793+
if (length(declarations) > 0) {
794+
const declaration = declarations[0];
795+
if (isExportAssignment(declaration)) {
796+
if (isIdentifier(declaration.expression)) {
797+
return declaration.expression.escapedText;
798+
}
799+
}
800+
else if (isExportSpecifier(declaration)) {
801+
if (declaration.propertyName) {
802+
return declaration.propertyName.escapedText;
803+
}
804+
}
805+
}
806+
}
787807
});
788808

789809
return symbolIdActionMap.getAllActions();

0 commit comments

Comments
 (0)