Skip to content

Commit d4b99d6

Browse files
author
Paul van Brenk
committed
Simplifying handling import cases
1 parent 4097874 commit d4b99d6

File tree

9 files changed

+44
-20
lines changed

9 files changed

+44
-20
lines changed

src/services/codefixes/unusedIdentifierFixes.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,20 @@ namespace ts.codefix {
9292
}
9393

9494
// handle case where 'import a = A;'
95-
// remove entire line
9695
case SyntaxKind.ImportEqualsDeclaration:
97-
const importDecl = token.parent;
98-
return createCodeFix("", importDecl.pos, importDecl.end - importDecl.pos);
96+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
97+
98+
// handle case where 'import d from './file'
99+
case SyntaxKind.ImportClause:
100+
return createCodeFix("", token.parent.parent.pos, token.parent.parent.end - token.parent.parent.pos);
101+
102+
// handle case where 'import * as a from './file'
103+
case SyntaxKind.NamespaceImport:
104+
return createCodeFix("", token.parent.parent.parent.pos, token.parent.parent.parent.end - token.parent.parent.parent.pos);
99105

100106
case SyntaxKind.EnumDeclaration:
101107
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
102108
}
103-
104-
if (token.parent.parent.kind === SyntaxKind.ImportClause || token.parent.parent.kind === SyntaxKind.ImportDeclaration) {
105-
return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos);
106-
}
107109
break;
108110

109111
case SyntaxKind.PrivateKeyword:
@@ -112,7 +114,7 @@ namespace ts.codefix {
112114

113115
case SyntaxKind.AsteriskToken:
114116
case SyntaxKind.NamespaceImport:
115-
return createCodeFix("{}", token.parent.pos, token.parent.end - token.parent.pos);
117+
return createCodeFix("", token.parent.pos, token.parent.end - token.parent.pos);
116118
}
117119

118120
return undefined;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
// @Filename: file2.ts
5+
//// [| import f1, * as s from "./file1"; |]
6+
//// s.f2('hello');
7+
8+
// @Filename: file1.ts
9+
//// export var v1;
10+
//// export function f1(n: number){}
11+
//// export function f2(s: string){};
12+
//// export default f1;
13+
14+
verify.codeFixAtPosition('import * as s from "./file1";');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noUnusedLocals: true
4+
// @Filename: file2.ts
5+
//// [| import f1, * as s from "./file1"; |]
6+
//// f1(42);
7+
8+
// @Filename: file1.ts
9+
//// export function f1(n: number){}
10+
//// export function f2(s: string){};
11+
//// export default f1;
12+
13+
verify.codeFixAtPosition('import f1 "./file1";');

tests/cases/fourslash/unusedImports1FS.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
// @noUnusedLocals: true
44
// @Filename: file2.ts
5-
//// [| import {/*0*/Calculator/*1*/} from "./file1" |]
5+
//// [| import { Calculator } from "./file1" |]
66

77
// @Filename: file1.ts
88
//// export class Calculator {
99
////
1010
//// }
1111

12-
verify.codeFixAtPosition(`import {} from "./file1"`);
12+
verify.codeFixAtPosition('');

tests/cases/fourslash/unusedImports2FS.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
////
1717
//// }
1818

19-
verify.codeFixAtPosition(`import {Calculator} from "./file1"
20-
import {} from "./file1"`);
19+
verify.codeFixAtPosition(`import {Calculator} from "./file1"`);

tests/cases/fourslash/unusedImports3FS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// @noUnusedLocals: true
44
// @Filename: file2.ts
5-
////[| import {/*0*/Calculator,/*1*/ test, test2} from "./file1" |]
5+
////[| import {Calculator, test, test2} from "./file1" |]
66

77
//// test();
88
//// test2();

tests/cases/fourslash/unusedImports4FS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// @noUnusedLocals: true
44
// @Filename: file2.ts
5-
//// [| import {Calculator/*0*/, test/*1*/, test2} from "./file1" |]
5+
//// [| import {Calculator, test, test2} from "./file1" |]
66
////
77
//// var x = new Calculator();
88
//// x.handleChar();

tests/cases/fourslash/unusedImports6FS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
////
1818
//// }
1919

20-
verify.codeFixAtPosition(`import {} from "./file1"`);
20+
verify.codeFixAtPosition('');

tests/cases/fourslash/unusedImports7FS.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
//// export class Calculator {
99
//// handleChar() { }
1010
//// }
11-
////
1211
//// export function test() {
13-
////
1412
//// }
15-
////
1613
//// export default function test2() {
17-
////
1814
//// }
1915

20-
verify.codeFixAtPosition(`import {} from "./file1"`);
16+
verify.codeFixAtPosition('');

0 commit comments

Comments
 (0)