@@ -79,8 +79,7 @@ function getFix(first, rest, sourceCode, context) {
79
79
80
80
return {
81
81
importNode : node ,
82
- text : sourceCode . text . slice ( openBrace . range [ 1 ] , closeBrace . range [ 0 ] ) ,
83
- hasTrailingComma : isPunctuator ( sourceCode . getTokenBefore ( closeBrace ) , ',' ) ,
82
+ identifiers : sourceCode . text . slice ( openBrace . range [ 1 ] , closeBrace . range [ 0 ] ) . split ( ',' ) , // Split the text into separate identifiers (retaining any whitespace before or after)
84
83
isEmpty : ! hasSpecifiers ( node ) ,
85
84
} ;
86
85
} )
@@ -111,9 +110,15 @@ function getFix(first, rest, sourceCode, context) {
111
110
closeBrace != null &&
112
111
isPunctuator ( sourceCode . getTokenBefore ( closeBrace ) , ',' ) ;
113
112
const firstIsEmpty = ! hasSpecifiers ( first ) ;
113
+ const firstExistingIdentifiers = firstIsEmpty
114
+ ? new Set ( )
115
+ : new Set ( sourceCode . text . slice ( openBrace . range [ 1 ] , closeBrace . range [ 0 ] )
116
+ . split ( ',' )
117
+ . map ( ( x ) => x . trim ( ) ) ,
118
+ ) ;
114
119
115
120
const [ specifiersText ] = specifiers . reduce (
116
- ( [ result , needsComma ] , specifier ) => {
121
+ ( [ result , needsComma , existingIdentifiers ] , specifier ) => {
117
122
const isTypeSpecifier = specifier . importNode . importKind === 'type' ;
118
123
119
124
const preferInline = context . options [ 0 ] && context . options [ 0 ] [ 'prefer-inline' ] ;
@@ -122,15 +127,25 @@ function getFix(first, rest, sourceCode, context) {
122
127
throw new Error ( 'Your version of TypeScript does not support inline type imports.' ) ;
123
128
}
124
129
125
- const insertText = `${ preferInline && isTypeSpecifier ? 'type ' : '' } ${ specifier . text } ` ;
130
+ // Add *only* the new identifiers that don't already exist, and track any new identifiers so we don't add them again in the next loop
131
+ const [ specifierText , updatedExistingIdentifiers ] = specifier . identifiers . reduce ( ( [ text , set ] , cur ) => {
132
+ const trimmed = cur . trim ( ) ; // Trim whitespace before/after to compare to our set of existing identifiers
133
+ const curWithType = trimmed . length > 0 && preferInline && isTypeSpecifier ? `type ${ cur } ` : cur ;
134
+ if ( existingIdentifiers . has ( trimmed ) ) {
135
+ return [ text , set ] ;
136
+ }
137
+ return [ text . length > 0 ? `${ text } ,${ curWithType } ` : curWithType , set . add ( trimmed ) ] ;
138
+ } , [ '' , existingIdentifiers ] ) ;
139
+
126
140
return [
127
- needsComma && ! specifier . isEmpty
128
- ? `${ result } ,${ insertText } `
129
- : `${ result } ${ insertText } ` ,
141
+ needsComma && ! specifier . isEmpty && specifierText . length > 0
142
+ ? `${ result } ,${ specifierText } `
143
+ : `${ result } ${ specifierText } ` ,
130
144
specifier . isEmpty ? needsComma : true ,
145
+ updatedExistingIdentifiers ,
131
146
] ;
132
147
} ,
133
- [ '' , ! firstHasTrailingComma && ! firstIsEmpty ] ,
148
+ [ '' , ! firstHasTrailingComma && ! firstIsEmpty , firstExistingIdentifiers ] ,
134
149
) ;
135
150
136
151
const fixes = [ ] ;
0 commit comments