@@ -45,7 +45,6 @@ namespace ts.codefix {
45
45
// Keys are import clause node IDs.
46
46
const addToExisting = createMap < { readonly importClauseOrBindingPattern : ImportClause | ObjectBindingPattern , defaultImport : string | undefined ; readonly namedImports : string [ ] , canUseTypeOnlyImport : boolean } > ( ) ;
47
47
const newImports = createMap < Mutable < ImportsCollection & { useRequire : boolean } > > ( ) ;
48
- let lastModuleSpecifier : string | undefined ;
49
48
return { addImportFromDiagnostic, addImportFromExportedSymbol, writeFixes } ;
50
49
51
50
function addImportFromDiagnostic ( diagnostic : DiagnosticWithLocation , context : CodeFixContextBase ) {
@@ -97,7 +96,6 @@ namespace ts.codefix {
97
96
let entry = newImports . get ( moduleSpecifier ) ;
98
97
if ( ! entry ) {
99
98
newImports . set ( moduleSpecifier , entry = { namedImports : [ ] , namespaceLikeImport : undefined , typeOnly, useRequire } ) ;
100
- lastModuleSpecifier = moduleSpecifier ;
101
99
}
102
100
else {
103
101
// An import clause can only be type-only if every import fix contributing to it can be type-only.
@@ -135,10 +133,15 @@ namespace ts.codefix {
135
133
addToExisting . forEach ( ( { importClauseOrBindingPattern, defaultImport, namedImports, canUseTypeOnlyImport } ) => {
136
134
doAddExistingFix ( changeTracker , sourceFile , importClauseOrBindingPattern , defaultImport , namedImports , canUseTypeOnlyImport ) ;
137
135
} ) ;
136
+
137
+ let newDeclarations : Statement | readonly Statement [ ] | undefined ;
138
138
newImports . forEach ( ( { useRequire, ...imports } , moduleSpecifier ) => {
139
- const addDeclarations = useRequire ? addNewRequires : addNewImports ;
140
- addDeclarations ( changeTracker , sourceFile , moduleSpecifier , quotePreference , imports , /*blankLineBetween*/ lastModuleSpecifier === moduleSpecifier ) ;
139
+ const getDeclarations = useRequire ? getNewRequires : getNewImports ;
140
+ newDeclarations = combine ( newDeclarations , getDeclarations ( moduleSpecifier , quotePreference , imports ) ) ;
141
141
} ) ;
142
+ if ( newDeclarations ) {
143
+ insertImports ( changeTracker , sourceFile , newDeclarations , /*blankLineBetween*/ true ) ;
144
+ }
142
145
}
143
146
}
144
147
@@ -631,11 +634,11 @@ namespace ts.codefix {
631
634
}
632
635
case ImportFixKind . AddNew : {
633
636
const { importKind, moduleSpecifier, typeOnly, useRequire } = fix ;
634
- const addDeclarations = useRequire ? addNewRequires : addNewImports ;
637
+ const getDeclarations = useRequire ? getNewRequires : getNewImports ;
635
638
const importsCollection = importKind === ImportKind . Default ? { defaultImport : symbolName , typeOnly } :
636
639
importKind === ImportKind . Named ? { namedImports : [ symbolName ] , typeOnly } :
637
640
{ namespaceLikeImport : { importKind, name : symbolName } , typeOnly } ;
638
- addDeclarations ( changes , sourceFile , moduleSpecifier , quotePreference , importsCollection , /*blankLineBetween*/ true ) ;
641
+ insertImports ( changes , sourceFile , getDeclarations ( moduleSpecifier , quotePreference , importsCollection ) , /*blankLineBetween*/ true ) ;
639
642
return [ importKind === ImportKind . Default ? Diagnostics . Import_default_0_from_module_1 : Diagnostics . Import_0_from_module_1 , symbolName , moduleSpecifier ] ;
640
643
}
641
644
default :
@@ -717,13 +720,13 @@ namespace ts.codefix {
717
720
readonly name : string ;
718
721
} ;
719
722
}
720
- function addNewImports ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , moduleSpecifier : string , quotePreference : QuotePreference , imports : ImportsCollection , blankLineBetween : boolean ) : void {
723
+ function getNewImports ( moduleSpecifier : string , quotePreference : QuotePreference , imports : ImportsCollection ) : Statement | readonly Statement [ ] {
721
724
const quotedModuleSpecifier = makeStringLiteral ( moduleSpecifier , quotePreference ) ;
725
+ let statements : Statement | readonly Statement [ ] | undefined ;
722
726
if ( imports . defaultImport !== undefined || imports . namedImports ?. length ) {
723
- insertImport ( changes , sourceFile ,
724
- makeImport (
725
- imports . defaultImport === undefined ? undefined : createIdentifier ( imports . defaultImport ) ,
726
- imports . namedImports ?. map ( n => createImportSpecifier ( /*propertyName*/ undefined , createIdentifier ( n ) ) ) , moduleSpecifier , quotePreference , imports . typeOnly ) , /*blankLineBetween*/ blankLineBetween ) ;
727
+ statements = combine ( statements , makeImport (
728
+ imports . defaultImport === undefined ? undefined : createIdentifier ( imports . defaultImport ) ,
729
+ imports . namedImports ?. map ( n => createImportSpecifier ( /*propertyName*/ undefined , createIdentifier ( n ) ) ) , moduleSpecifier , quotePreference , imports . typeOnly ) ) ;
727
730
}
728
731
const { namespaceLikeImport, typeOnly } = imports ;
729
732
if ( namespaceLikeImport ) {
@@ -741,26 +744,29 @@ namespace ts.codefix {
741
744
createNamespaceImport ( createIdentifier ( namespaceLikeImport . name ) ) ,
742
745
typeOnly ) ,
743
746
quotedModuleSpecifier ) ;
744
- insertImport ( changes , sourceFile , declaration , /*blankLineBetween*/ blankLineBetween ) ;
747
+ statements = combine ( statements , declaration ) ;
745
748
}
749
+ return Debug . checkDefined ( statements ) ;
746
750
}
747
751
748
- function addNewRequires ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , moduleSpecifier : string , quotePreference : QuotePreference , imports : ImportsCollection , blankLineBetween : boolean ) {
752
+ function getNewRequires ( moduleSpecifier : string , quotePreference : QuotePreference , imports : ImportsCollection ) : Statement | readonly Statement [ ] {
749
753
const quotedModuleSpecifier = makeStringLiteral ( moduleSpecifier , quotePreference ) ;
754
+ let statements : Statement | readonly Statement [ ] | undefined ;
750
755
// const { default: foo, bar, etc } = require('./mod');
751
756
if ( imports . defaultImport || imports . namedImports ?. length ) {
752
757
const bindingElements = imports . namedImports ?. map ( name => createBindingElement ( /*dotDotDotToken*/ undefined , /*propertyName*/ undefined , name ) ) || [ ] ;
753
758
if ( imports . defaultImport ) {
754
759
bindingElements . unshift ( createBindingElement ( /*dotDotDotToken*/ undefined , "default" , imports . defaultImport ) ) ;
755
760
}
756
761
const declaration = createConstEqualsRequireDeclaration ( createObjectBindingPattern ( bindingElements ) , quotedModuleSpecifier ) ;
757
- insertImport ( changes , sourceFile , declaration , blankLineBetween ) ;
762
+ statements = combine ( statements , declaration ) ;
758
763
}
759
764
// const foo = require('./mod');
760
765
if ( imports . namespaceLikeImport ) {
761
766
const declaration = createConstEqualsRequireDeclaration ( imports . namespaceLikeImport . name , quotedModuleSpecifier ) ;
762
- insertImport ( changes , sourceFile , declaration , blankLineBetween ) ;
767
+ statements = combine ( statements , declaration ) ;
763
768
}
769
+ return Debug . checkDefined ( statements ) ;
764
770
}
765
771
766
772
function createConstEqualsRequireDeclaration ( name : string | ObjectBindingPattern , quotedModuleSpecifier : StringLiteral ) : VariableStatement {
0 commit comments