@@ -25,15 +25,15 @@ namespace ts.codefix {
2525 return [ deleteNode ( token . parent ) ] ;
2626
2727 default :
28- return [ deleteDefault ( ) ] ;
28+ return deleteDefault ( ) ;
2929 }
3030
31- function deleteDefault ( ) {
31+ function deleteDefault ( ) : CodeAction [ ] | undefined {
3232 if ( isDeclarationName ( token ) ) {
33- return deleteNode ( token . parent ) ;
33+ return [ deleteNode ( token . parent ) ] ;
3434 }
3535 else if ( isLiteralComputedPropertyDeclarationName ( token ) ) {
36- return deleteNode ( token . parent . parent ) ;
36+ return [ deleteNode ( token . parent . parent ) ] ;
3737 }
3838 else {
3939 return undefined ;
@@ -87,20 +87,16 @@ namespace ts.codefix {
8787 case SyntaxKind . ImportSpecifier :
8888 const namedImports = < NamedImports > parent . parent ;
8989 if ( namedImports . elements . length === 1 ) {
90- // Only 1 import and it is unused. So the entire declaration should be removed.
91- const importSpec = getAncestor ( identifier , SyntaxKind . ImportDeclaration ) ;
92- return [ deleteNode ( importSpec ) ] ;
90+ return deleteNamedImportBinding ( namedImports ) ;
9391 }
9492 else {
9593 // delete import specifier
9694 return [ deleteNodeInList ( parent ) ] ;
9795 }
9896
99- // handle case where "import d, * as ns from './file'"
100- // or "'import {a, b as ns} from './file'"
10197 case SyntaxKind . ImportClause : // this covers both 'import |d|' and 'import |d,| *'
10298 const importClause = < ImportClause > parent ;
103- if ( ! importClause . namedBindings ) { // |import d from './file'| or |import * as ns from './file'|
99+ if ( ! importClause . namedBindings ) { // |import d from './file'|
104100 const importDecl = getAncestor ( importClause , SyntaxKind . ImportDeclaration ) ;
105101 return [ deleteNode ( importDecl ) ] ;
106102 }
@@ -118,22 +114,30 @@ namespace ts.codefix {
118114 }
119115
120116 case SyntaxKind . NamespaceImport :
121- const namespaceImport = < NamespaceImport > parent ;
122- if ( namespaceImport . name === identifier && ! ( < ImportClause > namespaceImport . parent ) . name ) {
123- const importDecl = getAncestor ( namespaceImport , SyntaxKind . ImportDeclaration ) ;
124- return [ deleteNode ( importDecl ) ] ;
125- }
126- else {
127- const previousToken = getTokenAtPosition ( sourceFile , namespaceImport . pos - 1 , /*includeJsDocComment*/ false ) ;
128- if ( previousToken && previousToken . kind === SyntaxKind . CommaToken ) {
129- const startPosition = textChanges . getAdjustedStartPosition ( sourceFile , previousToken , { } , textChanges . Position . FullStart ) ;
130- return [ deleteRange ( { pos : startPosition , end : namespaceImport . end } ) ] ;
131- }
132- return [ deleteRange ( namespaceImport ) ] ;
133- }
117+ return deleteNamedImportBinding ( < NamespaceImport > parent ) ;
134118
135119 default :
136- return [ deleteDefault ( ) ] ;
120+ return deleteDefault ( ) ;
121+ }
122+ }
123+
124+ function deleteNamedImportBinding ( namedBindings : NamedImportBindings ) : CodeAction [ ] | undefined {
125+ if ( ( < ImportClause > namedBindings . parent ) . name ) {
126+ // Delete named imports while preserving the default import
127+ // import d|, * as ns| from './file'
128+ // import d|, { a }| from './file'
129+ const previousToken = getTokenAtPosition ( sourceFile , namedBindings . pos - 1 , /*includeJsDocComment*/ false ) ;
130+ if ( previousToken && previousToken . kind === SyntaxKind . CommaToken ) {
131+ return [ deleteRange ( { pos : previousToken . getStart ( ) , end : namedBindings . end } ) ] ;
132+ }
133+ return undefined ;
134+ }
135+ else {
136+ // Delete the entire import declaration
137+ // |import * as ns from './file'|
138+ // |import { a } from './file'|
139+ const importDecl = getAncestor ( namedBindings , SyntaxKind . ImportDeclaration ) ;
140+ return [ deleteNode ( importDecl ) ] ;
137141 }
138142 }
139143
0 commit comments