@@ -119,6 +119,12 @@ namespace ts.codefix {
119
119
}
120
120
121
121
function getCodeActionsForImport ( exportInfos : ReadonlyArray < SymbolExportInfo > , context : ImportCodeFixContext ) : CodeFixAction [ ] {
122
+ const result : CodeFixAction [ ] = [ ] ;
123
+ getCodeActionsForImport_separateExistingAndNew ( exportInfos , context , result , result ) ;
124
+ return result ;
125
+ }
126
+
127
+ function getCodeActionsForImport_separateExistingAndNew ( exportInfos : ReadonlyArray < SymbolExportInfo > , context : ImportCodeFixContext , useExisting : Push < CodeFixAction > , addNew : Push < CodeFixAction > ) : void {
122
128
const existingImports = flatMap ( exportInfos , info =>
123
129
getImportDeclarations ( info , context . checker , context . sourceFile , context . cachedImportDeclarations ) ) ;
124
130
// It is possible that multiple import statements with the same specifier exist in the file.
@@ -133,16 +139,18 @@ namespace ts.codefix {
133
139
// 1. change "member3" to "ns.member3"
134
140
// 2. add "member3" to the second import statement's import list
135
141
// and it is up to the user to decide which one fits best.
136
- const useExistingImportActions = ! context . symbolToken || ! isIdentifier ( context . symbolToken ) ? emptyArray : mapDefined ( existingImports , ( { declaration } ) => {
137
- const namespace = getNamespaceImportName ( declaration ) ;
138
- if ( namespace ) {
139
- const moduleSymbol = context . checker . getAliasedSymbol ( context . checker . getSymbolAtLocation ( namespace ) ) ;
140
- if ( moduleSymbol && moduleSymbol . exports . has ( escapeLeadingUnderscores ( context . symbolName ) ) ) {
141
- return getCodeActionForUseExistingNamespaceImport ( namespace . text , context , context . symbolToken as Identifier ) ;
142
+ if ( context . symbolToken && isIdentifier ( context . symbolToken ) ) {
143
+ for ( const { declaration } of existingImports ) {
144
+ const namespace = getNamespaceImportName ( declaration ) ;
145
+ if ( namespace ) {
146
+ const moduleSymbol = context . checker . getAliasedSymbol ( context . checker . getSymbolAtLocation ( namespace ) ) ;
147
+ if ( moduleSymbol && moduleSymbol . exports . has ( escapeLeadingUnderscores ( context . symbolName ) ) ) {
148
+ useExisting . push ( getCodeActionForUseExistingNamespaceImport ( namespace . text , context , context . symbolToken ) ) ;
149
+ }
142
150
}
143
151
}
144
- } ) ;
145
- return [ ... useExistingImportActions , ... getCodeActionsForAddImport ( exportInfos , context , existingImports ) ] ;
152
+ }
153
+ getCodeActionsForAddImport ( exportInfos , context , existingImports , useExisting , addNew ) ;
146
154
}
147
155
148
156
function getNamespaceImportName ( declaration : AnyImportSyntax ) : Identifier | undefined {
@@ -551,7 +559,9 @@ namespace ts.codefix {
551
559
exportInfos : ReadonlyArray < SymbolExportInfo > ,
552
560
ctx : ImportCodeFixContext ,
553
561
existingImports : ReadonlyArray < ExistingImportInfo > ,
554
- ) : CodeFixAction [ ] {
562
+ useExisting : Push < CodeFixAction > ,
563
+ addNew : Push < CodeFixAction > ,
564
+ ) : void {
555
565
const fromExistingImport = firstDefined ( existingImports , ( { declaration, importKind } ) => {
556
566
if ( declaration . kind === SyntaxKind . ImportDeclaration && declaration . importClause ) {
557
567
const changes = tryUpdateExistingImport ( ctx , isImportClause ( declaration . importClause ) && declaration . importClause || undefined , importKind ) ;
@@ -562,14 +572,17 @@ namespace ts.codefix {
562
572
}
563
573
} ) ;
564
574
if ( fromExistingImport ) {
565
- return [ fromExistingImport ] ;
575
+ useExisting . push ( fromExistingImport ) ;
576
+ return ;
566
577
}
567
578
568
579
const existingDeclaration = firstDefined ( existingImports , newImportInfoFromExistingSpecifier ) ;
569
580
const newImportInfos = existingDeclaration
570
581
? [ existingDeclaration ]
571
582
: getNewImportInfos ( ctx . program , ctx . sourceFile , exportInfos , ctx . compilerOptions , ctx . getCanonicalFileName , ctx . host , ctx . preferences ) ;
572
- return newImportInfos . map ( info => getCodeActionForNewImport ( ctx , info ) ) ;
583
+ for ( const info of newImportInfos ) {
584
+ addNew . push ( getCodeActionForNewImport ( ctx , info ) ) ;
585
+ }
573
586
}
574
587
575
588
function newImportInfoFromExistingSpecifier ( { declaration, importKind } : ExistingImportInfo ) : NewImportInfo | undefined {
@@ -760,7 +773,12 @@ namespace ts.codefix {
760
773
}
761
774
} ) ;
762
775
763
- return arrayFrom ( flatMapIterator ( originalSymbolToExportInfos . values ( ) , exportInfos => getCodeActionsForImport ( exportInfos , convertToImportCodeFixContext ( context , symbolToken , symbolName ) ) ) ) ;
776
+ const addToExistingDeclaration : CodeFixAction [ ] = [ ] ;
777
+ const addNewDeclaration : CodeFixAction [ ] = [ ] ;
778
+ originalSymbolToExportInfos . forEach ( exportInfos => {
779
+ getCodeActionsForImport_separateExistingAndNew ( exportInfos , convertToImportCodeFixContext ( context , symbolToken , symbolName ) , addToExistingDeclaration , addNewDeclaration ) ;
780
+ } ) ;
781
+ return [ ...addToExistingDeclaration , ...addNewDeclaration ] ;
764
782
}
765
783
766
784
function checkSymbolHasMeaning ( { declarations } : Symbol , meaning : SemanticMeaning ) : boolean {
0 commit comments