@@ -79,8 +79,7 @@ function getFix(first, rest, sourceCode, context) {
7979
8080 return {
8181 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)
8483 isEmpty : ! hasSpecifiers ( node ) ,
8584 } ;
8685 } )
@@ -111,9 +110,15 @@ function getFix(first, rest, sourceCode, context) {
111110 closeBrace != null &&
112111 isPunctuator ( sourceCode . getTokenBefore ( closeBrace ) , ',' ) ;
113112 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+ ) ;
114119
115120 const [ specifiersText ] = specifiers . reduce (
116- ( [ result , needsComma ] , specifier ) => {
121+ ( [ result , needsComma , existingIdentifiers ] , specifier ) => {
117122 const isTypeSpecifier = specifier . importNode . importKind === 'type' ;
118123
119124 const preferInline = context . options [ 0 ] && context . options [ 0 ] [ 'prefer-inline' ] ;
@@ -122,15 +127,25 @@ function getFix(first, rest, sourceCode, context) {
122127 throw new Error ( 'Your version of TypeScript does not support inline type imports.' ) ;
123128 }
124129
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+
126140 return [
127- needsComma && ! specifier . isEmpty
128- ? `${ result } ,${ insertText } `
129- : `${ result } ${ insertText } ` ,
141+ needsComma && ! specifier . isEmpty && specifierText . length > 0
142+ ? `${ result } ,${ specifierText } `
143+ : `${ result } ${ specifierText } ` ,
130144 specifier . isEmpty ? needsComma : true ,
145+ updatedExistingIdentifiers ,
131146 ] ;
132147 } ,
133- [ '' , ! firstHasTrailingComma && ! firstIsEmpty ] ,
148+ [ '' , ! firstHasTrailingComma && ! firstIsEmpty , firstExistingIdentifiers ] ,
134149 ) ;
135150
136151 const fixes = [ ] ;
0 commit comments