@@ -188,7 +188,7 @@ namespace ts.codefix {
188
188
const useNamespace = tryUseExistingNamespaceImport ( existingImports , symbolName , symbolToken , checker ) ;
189
189
const addToExisting = tryAddToExistingImport ( existingImports ) ;
190
190
// Don't bother providing an action to add a new import if we can add to an existing one.
191
- const addImport = addToExisting ? [ addToExisting ] : getCodeActionsForAddImport ( exportInfos , existingImports , program , sourceFile , host , preferences ) ;
191
+ const addImport = addToExisting ? [ addToExisting ] : getFixesForAddImport ( exportInfos , existingImports , program , sourceFile , host , preferences ) ;
192
192
return [ ...( useNamespace ? [ useNamespace ] : emptyArray ) , ...addImport ] ;
193
193
}
194
194
@@ -261,7 +261,7 @@ namespace ts.codefix {
261
261
return flatten < FixAddNewImport > ( choicesForEachExportingModule . sort ( ( a , b ) => first ( a ) . moduleSpecifier . length - first ( b ) . moduleSpecifier . length ) ) ;
262
262
}
263
263
264
- function getCodeActionsForAddImport (
264
+ function getFixesForAddImport (
265
265
exportInfos : ReadonlyArray < SymbolExportInfo > ,
266
266
existingImports : ReadonlyArray < FixAddToExistingImportInfo > ,
267
267
program : Program ,
@@ -376,13 +376,9 @@ namespace ts.codefix {
376
376
// check the default export
377
377
const defaultExport = checker . tryGetMemberInModuleExports ( InternalSymbolName . Default , moduleSymbol ) ;
378
378
if ( defaultExport ) {
379
- const localSymbol = getLocalSymbolForExportDefault ( defaultExport ) ;
380
- if ( (
381
- localSymbol && localSymbol . escapedName === symbolName ||
382
- getEscapedNameForExportDefault ( defaultExport ) === symbolName ||
383
- moduleSymbolToValidIdentifier ( moduleSymbol , program . getCompilerOptions ( ) . target ! ) === symbolName
384
- ) && symbolHasMeaning ( localSymbol || defaultExport , currentTokenMeaning ) ) {
385
- addSymbol ( moduleSymbol , localSymbol || defaultExport , ImportKind . Default ) ;
379
+ const info = getDefaultExportInfo ( defaultExport , moduleSymbol , program ) ;
380
+ if ( info && info . name === symbolName && symbolHasMeaning ( info . symbolForMeaning , currentTokenMeaning ) ) {
381
+ addSymbol ( moduleSymbol , defaultExport , ImportKind . Default ) ;
386
382
}
387
383
}
388
384
@@ -391,22 +387,41 @@ namespace ts.codefix {
391
387
if ( exportSymbolWithIdenticalName && symbolHasMeaning ( exportSymbolWithIdenticalName , currentTokenMeaning ) ) {
392
388
addSymbol ( moduleSymbol , exportSymbolWithIdenticalName , ImportKind . Named ) ;
393
389
}
390
+ } ) ;
391
+ return originalSymbolToExportInfos ;
392
+ }
394
393
395
- function getEscapedNameForExportDefault ( symbol : Symbol ) : __String | undefined {
396
- return symbol . declarations && firstDefined ( symbol . declarations , declaration => {
397
- if ( isExportAssignment ( declaration ) ) {
398
- if ( isIdentifier ( declaration . expression ) ) {
399
- return declaration . expression . escapedText ;
400
- }
401
- }
402
- else if ( isExportSpecifier ( declaration ) ) {
403
- Debug . assert ( declaration . name . escapedText === InternalSymbolName . Default ) ;
404
- return declaration . propertyName && declaration . propertyName . escapedText ;
405
- }
406
- } ) ;
394
+ function getDefaultExportInfo ( defaultExport : Symbol , moduleSymbol : Symbol , program : Program ) : { readonly symbolForMeaning : Symbol , readonly name : string } | undefined {
395
+ const checker = program . getTypeChecker ( ) ;
396
+
397
+ const localSymbol = getLocalSymbolForExportDefault ( defaultExport ) ;
398
+ if ( localSymbol ) return { symbolForMeaning : localSymbol , name : localSymbol . name } ;
399
+
400
+ const name = getNameForExportDefault ( defaultExport ) ;
401
+ if ( name !== undefined ) return { symbolForMeaning : defaultExport , name } ;
402
+
403
+ if ( defaultExport . flags & SymbolFlags . Alias ) {
404
+ const aliased = checker . getAliasedSymbol ( defaultExport ) ;
405
+ return getDefaultExportInfo ( aliased , Debug . assertDefined ( aliased . parent ) , program ) ;
406
+ }
407
+ else {
408
+ const moduleName = moduleSymbolToValidIdentifier ( moduleSymbol , program . getCompilerOptions ( ) . target ! ) ;
409
+ return moduleName === undefined ? undefined : { symbolForMeaning : defaultExport , name : moduleName } ;
410
+ }
411
+ }
412
+
413
+ function getNameForExportDefault ( symbol : Symbol ) : string | undefined {
414
+ return symbol . declarations && firstDefined ( symbol . declarations , declaration => {
415
+ if ( isExportAssignment ( declaration ) ) {
416
+ if ( isIdentifier ( declaration . expression ) ) {
417
+ return declaration . expression . text ;
418
+ }
419
+ }
420
+ else if ( isExportSpecifier ( declaration ) ) {
421
+ Debug . assert ( declaration . name . text === InternalSymbolName . Default ) ;
422
+ return declaration . propertyName && declaration . propertyName . text ;
407
423
}
408
424
} ) ;
409
- return originalSymbolToExportInfos ;
410
425
}
411
426
412
427
function codeActionForFix ( context : textChanges . TextChangesContext , sourceFile : SourceFile , symbolName : string , fix : ImportFix , quotePreference : QuotePreference ) : CodeFixAction {
0 commit comments