@@ -5,12 +5,21 @@ namespace ts.codefix {
55 const errorCodes = [
66 Diagnostics . _0_is_declared_but_its_value_is_never_read . code ,
77 Diagnostics . Property_0_is_declared_but_its_value_is_never_read . code ,
8+ Diagnostics . No_import_of_0_is_used . code ,
89 ] ;
910 registerCodeFix ( {
1011 errorCodes,
1112 getCodeActions ( context ) {
12- const { sourceFile } = context ;
13+ const { errorCode , sourceFile } = context ;
1314 const token = getToken ( sourceFile , context . span . start ) ;
15+
16+ if ( errorCode === Diagnostics . No_import_of_0_is_used . code ) {
17+ const decl = getImportDeclarationAtErrorLocation ( token ) ;
18+ const description = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Remove_declaration_for_Colon_0 ) , [ showModuleSpecifier ( decl ) ] ) ;
19+ const changes = textChanges . ChangeTracker . with ( context , t => t . deleteNode ( sourceFile , decl ) ) ;
20+ return [ { description, changes, fixId : fixIdDelete } ] ;
21+ }
22+
1423 const result : CodeFixAction [ ] = [ ] ;
1524
1625 const deletion = textChanges . ChangeTracker . with ( context , t => tryDeleteDeclaration ( t , sourceFile , token ) ) ;
@@ -19,7 +28,7 @@ namespace ts.codefix {
1928 result . push ( { description, changes : deletion , fixId : fixIdDelete } ) ;
2029 }
2130
22- const prefix = textChanges . ChangeTracker . with ( context , t => tryPrefixDeclaration ( t , context . errorCode , sourceFile , token ) ) ;
31+ const prefix = textChanges . ChangeTracker . with ( context , t => tryPrefixDeclaration ( t , errorCode , sourceFile , token ) ) ;
2332 if ( prefix . length ) {
2433 const description = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Prefix_0_with_an_underscore ) , [ token . getText ( ) ] ) ;
2534 result . push ( { description, changes : prefix , fixId : fixIdPrefix } ) ;
@@ -38,14 +47,24 @@ namespace ts.codefix {
3847 }
3948 break ;
4049 case fixIdDelete :
41- tryDeleteDeclaration ( changes , sourceFile , token ) ;
50+ if ( diag . code === Diagnostics . No_import_of_0_is_used . code ) {
51+ changes . deleteNode ( sourceFile , getImportDeclarationAtErrorLocation ( token ) ) ;
52+ }
53+ else {
54+ tryDeleteDeclaration ( changes , sourceFile , token ) ;
55+ }
4256 break ;
4357 default :
4458 Debug . fail ( JSON . stringify ( context . fixId ) ) ;
4559 }
4660 } ) ,
4761 } ) ;
4862
63+ function getImportDeclarationAtErrorLocation ( token : Node ) : ImportDeclaration {
64+ Debug . assert ( token . kind === SyntaxKind . ImportKeyword ) ;
65+ return cast ( token . parent , isImportDeclaration ) ;
66+ }
67+
4968 function getToken ( sourceFile : SourceFile , pos : number ) : Node {
5069 const token = getTokenAtPosition ( sourceFile , pos , /*includeJsDocComment*/ false ) ;
5170 // this handles var ["computed"] = 12;
0 commit comments