@@ -5,12 +5,21 @@ namespace ts.codefix {
5
5
const errorCodes = [
6
6
Diagnostics . _0_is_declared_but_its_value_is_never_read . code ,
7
7
Diagnostics . Property_0_is_declared_but_its_value_is_never_read . code ,
8
+ Diagnostics . No_import_of_0_is_used . code ,
8
9
] ;
9
10
registerCodeFix ( {
10
11
errorCodes,
11
12
getCodeActions ( context ) {
12
- const { sourceFile } = context ;
13
+ const { errorCode , sourceFile } = context ;
13
14
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
+
14
23
const result : CodeFixAction [ ] = [ ] ;
15
24
16
25
const deletion = textChanges . ChangeTracker . with ( context , t => tryDeleteDeclaration ( t , sourceFile , token ) ) ;
@@ -19,7 +28,7 @@ namespace ts.codefix {
19
28
result . push ( { description, changes : deletion , fixId : fixIdDelete } ) ;
20
29
}
21
30
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 ) ) ;
23
32
if ( prefix . length ) {
24
33
const description = formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Prefix_0_with_an_underscore ) , [ token . getText ( ) ] ) ;
25
34
result . push ( { description, changes : prefix , fixId : fixIdPrefix } ) ;
@@ -38,14 +47,24 @@ namespace ts.codefix {
38
47
}
39
48
break ;
40
49
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
+ }
42
56
break ;
43
57
default :
44
58
Debug . fail ( JSON . stringify ( context . fixId ) ) ;
45
59
}
46
60
} ) ,
47
61
} ) ;
48
62
63
+ function getImportDeclarationAtErrorLocation ( token : Node ) : ImportDeclaration {
64
+ Debug . assert ( token . kind === SyntaxKind . ImportKeyword ) ;
65
+ return cast ( token . parent , isImportDeclaration ) ;
66
+ }
67
+
49
68
function getToken ( sourceFile : SourceFile , pos : number ) : Node {
50
69
const token = getTokenAtPosition ( sourceFile , pos , /*includeJsDocComment*/ false ) ;
51
70
// this handles var ["computed"] = 12;
0 commit comments