Skip to content

Commit 09487b8

Browse files
committed
Added tests, pr feedback
1 parent e3b6df6 commit 09487b8

7 files changed

+114
-13
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,19 @@ namespace ts.codefix {
439439

440440
function getSingleQuoteStyleFromExistingImports() {
441441
const firstModuleSpecifier = forEach(sourceFile.statements, node => {
442-
switch (node.kind) {
443-
case SyntaxKind.ImportDeclaration:
444-
return (<ImportDeclaration>node).moduleSpecifier;
445-
case SyntaxKind.ImportEqualsDeclaration:
446-
const moduleReference = (<ImportEqualsDeclaration>node).moduleReference;
447-
return moduleReference.kind === SyntaxKind.ExternalModuleReference ? moduleReference.expression : undefined;
448-
case SyntaxKind.ExportDeclaration:
449-
return (<ExportDeclaration>node).moduleSpecifier;
442+
if (isImportDeclaration(node) || isExportDeclaration(node)) {
443+
if (node.moduleSpecifier && isStringLiteral(node.moduleSpecifier)) {
444+
return node.moduleSpecifier;
445+
}
446+
}
447+
else if (isImportEqualsDeclaration(node)) {
448+
if (isExternalModuleReference(node.moduleReference) && isStringLiteral(node.moduleReference.expression)) {
449+
return node.moduleReference.expression;
450+
}
450451
}
451452
});
452-
if (firstModuleSpecifier && isStringLiteral(firstModuleSpecifier)) {
453-
return sourceFile.text.charCodeAt(skipTrivia(sourceFile.text, firstModuleSpecifier.pos)) === CharacterCodes.singleQuote;
453+
if (firstModuleSpecifier) {
454+
return sourceFile.text.charCodeAt(firstModuleSpecifier.getStart()) === CharacterCodes.singleQuote;
454455
}
455456
}
456457

tests/cases/fourslash/importNameCodeFixNewImportFile6.ts renamed to tests/cases/fourslash/importNameCodeFixNewImportFileQuoteStyle0.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
////
55
//// f1/*0*/();|]
66

7-
// @Filename: module.ts
7+
// @Filename: module1.ts
88
//// export function f1() {}
9-
//// export var v1 = 5;
109

1110
// @Filename: module2.ts
1211
//// export var v2 = 6;
1312

1413
verify.importFixAtPosition([
1514
`import { v2 } from './module2';
16-
import { f1 } from './module';
15+
import { f1 } from './module1';
1716
1817
f1();`
1918
]);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|import { v2 } from "./module2";
4+
////
5+
//// f1/*0*/();|]
6+
7+
// @Filename: module1.ts
8+
//// export function f1() {}
9+
10+
// @Filename: module2.ts
11+
//// export var v2 = 6;
12+
13+
verify.importFixAtPosition([
14+
`import { v2 } from "./module2";
15+
import { f1 } from "./module1";
16+
17+
f1();`
18+
]);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|import m2 = require('./module2');
4+
////
5+
//// f1/*0*/();|]
6+
7+
// @Filename: module1.ts
8+
//// export function f1() {}
9+
10+
// @Filename: module2.ts
11+
//// export var v2 = 6;
12+
13+
verify.importFixAtPosition([
14+
`import m2 = require('./module2');
15+
import { f1 } from './module1';
16+
17+
f1();`
18+
]);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|export { v2 } from './module2';
4+
////
5+
//// f1/*0*/();|]
6+
7+
// @Filename: module1.ts
8+
//// export function f1() {}
9+
10+
// @Filename: module2.ts
11+
//// export var v2 = 6;
12+
13+
verify.importFixAtPosition([
14+
`import { f1 } from './module1';
15+
16+
export { v2 } from './module2';
17+
18+
f1();`
19+
]);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|import { v2 } from "./module2";
4+
//// import { v3 } from './module3';
5+
////
6+
//// f1/*0*/();|]
7+
8+
// @Filename: module1.ts
9+
//// export function f1() {}
10+
11+
// @Filename: module2.ts
12+
//// export var v2 = 6;
13+
14+
// @Filename: module3.ts
15+
//// export var v3 = 6;
16+
17+
verify.importFixAtPosition([
18+
`import { v2 } from "./module2";
19+
import { v3 } from './module3';
20+
import { f1 } from "./module1";
21+
22+
f1();`
23+
]);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|import { v2 } from './module2';
4+
//// import { v3 } from "./module3";
5+
////
6+
//// f1/*0*/();|]
7+
8+
// @Filename: module1.ts
9+
//// export function f1() {}
10+
11+
// @Filename: module2.ts
12+
//// export var v2 = 6;
13+
14+
// @Filename: module3.ts
15+
//// export var v3 = 6;
16+
17+
verify.importFixAtPosition([
18+
`import { v2 } from './module2';
19+
import { v3 } from "./module3";
20+
import { f1 } from './module1';
21+
22+
f1();`
23+
]);

0 commit comments

Comments
 (0)