Skip to content

Commit a282fc0

Browse files
committed
not using the generic verify method for import fixes.
1 parent 26cd24a commit a282fc0

36 files changed

+77
-57
lines changed

src/harness/fourslash.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,31 @@ namespace FourSlash {
20182018
return this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start, diagnostic.length, [diagnostic.code]);
20192019
}
20202020

2021-
public verifyCodeFixAtPosition(expectedTextArray: string[], errorCode?: number) {
2021+
public verifyCodeFixAtPosition(expectedText: string, errorCode?: number) {
2022+
const ranges = this.getRanges();
2023+
if (ranges.length == 0) {
2024+
this.raiseError("At least one range should be specified in the testfile.");
2025+
}
2026+
2027+
const actual = this.getCodeFixes(errorCode);
2028+
2029+
if (!actual || actual.length == 0) {
2030+
this.raiseError("No codefixes returned.");
2031+
}
2032+
2033+
if (actual.length > 1) {
2034+
this.raiseError("More than 1 codefix returned.");
2035+
}
2036+
2037+
this.applyEdits(actual[0].changes[0].fileName, actual[0].changes[0].textChanges, /*isFormattingEdit*/ false);
2038+
const actualText = this.rangeText(ranges[0]);
2039+
2040+
if (this.removeWhitespace(actualText) !== this.removeWhitespace(expectedText)) {
2041+
this.raiseError(`Actual text doesn't match expected text. Actual: '${actualText}' Expected: '${expectedText}'`);
2042+
}
2043+
}
2044+
2045+
public verifyImportFixAtPosition(expectedTextArray: string[], errorCode?: number) {
20222046
const ranges = this.getRanges();
20232047
if (ranges.length == 0) {
20242048
this.raiseError("At least one range should be specified in the testfile.");
@@ -2042,7 +2066,7 @@ namespace FourSlash {
20422066
const sortedActualArray = actualTextArray.sort();
20432067
if (!ts.arrayIsEqualTo(sortedExpectedArray, sortedActualArray)) {
20442068
this.raiseError(
2045-
`Actual text array doesn't match expected text array. \nActual: \n'${sortedActualArray.join('\n\n')}'\n---\nExpected: \n'${sortedExpectedArray.join('\n\n')}'`);
2069+
`Actual text array doesn't match expected text array. \nActual: \n"${sortedActualArray.join("\n\n")}"\n---\nExpected: \n'${sortedExpectedArray.join("\n\n")}'`);
20462070
}
20472071
}
20482072

@@ -3299,8 +3323,12 @@ namespace FourSlashInterface {
32993323
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
33003324
}
33013325

3302-
public codeFixAtPosition(expectedTextArray: string[], errorCode?: number): void {
3303-
this.state.verifyCodeFixAtPosition(expectedTextArray, errorCode);
3326+
public codeFixAtPosition(expectedText: string, errorCode?: number): void {
3327+
this.state.verifyCodeFixAtPosition(expectedText, errorCode);
3328+
}
3329+
3330+
public importFixAtPosition(expectedTextArray: string[], errorCode?: number): void {
3331+
this.state.verifyImportFixAtPosition(expectedTextArray, errorCode);
33043332
}
33053333

33063334
public navigationBar(json: any) {

src/services/codefixes/importFixes.ts

+10-19
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace ts.codefix {
2121

2222
const allPotentialModules = checker.getAmbientModules();
2323
for (const otherSourceFile of allSourceFiles) {
24-
if (otherSourceFile !== sourceFile && otherSourceFile.symbol) {
24+
if (otherSourceFile !== sourceFile && isExternalOrCommonJsModule(otherSourceFile) && otherSourceFile.symbol) {
2525
allPotentialModules.push(otherSourceFile.symbol);
2626
}
2727
}
@@ -203,24 +203,15 @@ namespace ts.codefix {
203203
// original text: import { foo, bar } from "module"
204204
// change to: import { foo, bar, name } from "module"
205205
const insertPoint = importList.elements[importList.elements.length - 1].getEnd();
206-
// If the import list has one import per line, preserve that. Otherwise, insert on same line as last element
207-
let oneImportPerLine: boolean;
208-
if (importList.elements.length === 1) {
209-
/**
210-
* If there is only one symbol being imported, still check to see if it's set up for multi-line imports like this:
211-
* import {
212-
* foo
213-
* } from "./module";
214-
*/
215-
const startLine = getLineOfLocalPosition(sourceFile, importList.getStart());
216-
const endLine = getLineOfLocalPosition(sourceFile, importList.getEnd());
217-
oneImportPerLine = endLine - startLine >= 2;
218-
}
219-
else {
220-
const startLine = getLineOfLocalPosition(sourceFile, importList.elements[0].getStart());
221-
const endLine = getLineOfLocalPosition(sourceFile, insertPoint);
222-
oneImportPerLine = endLine - startLine >= importList.elements.length - 1;
223-
}
206+
/**
207+
* If the import list has one import per line, preserve that. Otherwise, insert on same line as last element
208+
* import {
209+
* foo
210+
* } from "./module";
211+
*/
212+
const startLine = getLineOfLocalPosition(sourceFile, importList.getStart());
213+
const endLine = getLineOfLocalPosition(sourceFile, importList.getEnd());
214+
const oneImportPerLine = endLine - startLine > importList.elements.length;
224215

225216
return {
226217
newText: `,${oneImportPerLine ? context.newLineCharacter : " "}${newImportText}`,

tests/cases/fourslash/fourslash.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ declare namespace FourSlashInterface {
209209
noMatchingBracePositionInCurrentFile(bracePosition: number): void;
210210
DocCommentTemplate(expectedText: string, expectedOffset: number, empty?: boolean): void;
211211
noDocCommentTemplate(): void;
212-
codeFixAtPosition(expectedTextArray: string[], errorCode?: number): void;
212+
codeFixAtPosition(expectedText: string, errorCode?: number): void;
213+
importFixAtPosition(expectedTextArray: string[], errorCode?: number): void;
213214

214215
navigationBar(json: any): void;
215216
navigationTree(json: any): void;

tests/cases/fourslash/importNameCodeFixExistingImport0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//// export function f1() {}
88
//// export var v1 = 5;
99

10-
verify.codeFixAtPosition([`{ v1, f1 }`]);
10+
verify.importFixAtPosition([`{ v1, f1 }`]);

tests/cases/fourslash/importNameCodeFixExistingImport1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
//// export var v1 = 5;
99
//// export default var d1 = 6;
1010

11-
verify.codeFixAtPosition([`{ v1, f1 }`]);
11+
verify.importFixAtPosition([`{ v1, f1 }`]);

tests/cases/fourslash/importNameCodeFixExistingImport10.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//// export var v2 = 5;
1313
//// export var v3 = 5;
1414

15-
verify.codeFixAtPosition([
15+
verify.importFixAtPosition([
1616
`{
1717
v1,
1818
v2,

tests/cases/fourslash/importNameCodeFixExistingImport11.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//// export var v2 = 5;
1313
//// export var v3 = 5;
1414

15-
verify.codeFixAtPosition([
15+
verify.importFixAtPosition([
1616
`{
1717
v1, v2,
1818
v3, f1

tests/cases/fourslash/importNameCodeFixExistingImport12.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
//// export var v2 = 5;
1010
//// export var v3 = 5;
1111

12-
verify.codeFixAtPosition([`{ f1 }`]);
12+
verify.importFixAtPosition([`{ f1 }`]);

tests/cases/fourslash/importNameCodeFixExistingImport2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//// export function f1() {}
88
//// export var v1 = 5;
99

10-
verify.codeFixAtPosition([
10+
verify.importFixAtPosition([
1111
`import * as ns from "./module";
1212
import { f1 } from "./module";
1313
f1();`,

tests/cases/fourslash/importNameCodeFixExistingImport3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//// export var v1 = 5;
99
//// export default var d1 = 6;
1010

11-
verify.codeFixAtPosition([
11+
verify.importFixAtPosition([
1212
`import d, * as ns from "./module";
1313
ns.f1();`,
1414
`import d, * as ns from "./module";

tests/cases/fourslash/importNameCodeFixExistingImport4.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//// export var v1 = 5;
99
//// export default var d1 = 6;
1010

11-
verify.codeFixAtPosition([
11+
verify.importFixAtPosition([
1212
`import d, { f1 } from "./module";
1313
f1();`
1414
]);

tests/cases/fourslash/importNameCodeFixExistingImport5.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
//// export function f1() {}
88
//// export var v1 = 5;
99

10-
verify.codeFixAtPosition([`import "./module";
10+
verify.importFixAtPosition([`import "./module";
1111
import { f1 } from "./module";
1212
f1();`]);

tests/cases/fourslash/importNameCodeFixExistingImport6.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
//// export var v1 = 5;
1111
//// export function f1();
1212

13-
verify.codeFixAtPosition([`{ v1, f1 }`]);
13+
verify.importFixAtPosition([`{ v1, f1 }`]);

tests/cases/fourslash/importNameCodeFixExistingImport7.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//// export var v1 = 5;
88
//// export function f1();
99

10-
verify.codeFixAtPosition([`{ v1, f1 }`]);
10+
verify.importFixAtPosition([`{ v1, f1 }`]);

tests/cases/fourslash/importNameCodeFixExistingImport8.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
//// export var v2 = 5;
1010
//// export var v3 = 5;
1111

12-
verify.codeFixAtPosition([`{v1, v2, v3, f1,}`]);
12+
verify.importFixAtPosition([`{v1, v2, v3, f1,}`]);

tests/cases/fourslash/importNameCodeFixExistingImport9.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//// export function f1() {}
1010
//// export var v1 = 5;
1111

12-
verify.codeFixAtPosition([
12+
verify.importFixAtPosition([
1313
`{
1414
v1,
1515
f1

tests/cases/fourslash/importNameCodeFixNewImportAmbient0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//// export var v1;
99
//// }
1010

11-
verify.codeFixAtPosition([
11+
verify.importFixAtPosition([
1212
`import { f1 } from "ambient-module";
1313
1414
f1();`

tests/cases/fourslash/importNameCodeFixNewImportAmbient1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//// export var v3;
2222
//// }
2323

24-
verify.codeFixAtPosition([
24+
verify.importFixAtPosition([
2525
`import * as ns from "yet-another-ambient-module";
2626
import { v1 } from "ambient-module";
2727
var x = v1 + 5;`

tests/cases/fourslash/importNameCodeFixNewImportAmbient2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//// export var v1;
1212
//// }
1313

14-
verify.codeFixAtPosition([
14+
verify.importFixAtPosition([
1515
`/*
1616
* I'm a license or something
1717
*/

tests/cases/fourslash/importNameCodeFixNewImportAmbient3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//// export var v3;
2323
//// }
2424

25-
verify.codeFixAtPosition([
25+
verify.importFixAtPosition([
2626
`import * as ns from "yet-another-ambient-module";
2727
import { v1 } from "ambient-module";
2828
var x = v1 + 5;`

tests/cases/fourslash/importNameCodeFixNewImportBaseUrl0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// @Filename: a/b.ts
1313
//// export function f1() { };
1414

15-
verify.codeFixAtPosition([
15+
verify.importFixAtPosition([
1616
`import { f1 } from "b";
1717
1818
f1();`

tests/cases/fourslash/importNameCodeFixNewImportDefault0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// @Filename: module.ts
66
//// export default function f1() { };
77

8-
verify.codeFixAtPosition([
8+
verify.importFixAtPosition([
99
`import f1 from "./module";
1010
1111
f1();`

tests/cases/fourslash/importNameCodeFixNewImportFile0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//// export function f1() {}
77
//// export var v1 = 5;
88

9-
verify.codeFixAtPosition([
9+
verify.importFixAtPosition([
1010
`import { f1 } from "./module";
1111
1212
f1();`

tests/cases/fourslash/importNameCodeFixNewImportFile1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// @Filename: tripleSlashReference.ts
1111
//// var x = 5;/*dummy*/
1212

13-
verify.codeFixAtPosition([
13+
verify.importFixAtPosition([
1414
`/// <reference path="./tripleSlashReference.ts" />
1515
import { f1 } from "./module";
1616

tests/cases/fourslash/importNameCodeFixNewImportFile2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//// export var v1 = 5;
77
//// export function f1();
88

9-
verify.codeFixAtPosition([
9+
verify.importFixAtPosition([
1010
`import { f1 } from "../../other_dir/module";
1111
1212
f1();`

tests/cases/fourslash/importNameCodeFixNewImportNodeModules0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// @Filename: ../node_modules/fake-module/package.json
1313
//// {}
1414

15-
verify.codeFixAtPosition([
15+
verify.importFixAtPosition([
1616
`import { f1 } from "fake-module";
1717
1818
f1();`

tests/cases/fourslash/importNameCodeFixNewImportNodeModules1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//// export var v1 = 5;
1010
//// export function f1();
1111

12-
verify.codeFixAtPosition([
12+
verify.importFixAtPosition([
1313
`import { f1 } from "fake-module/nested";
1414
1515
f1();`

tests/cases/fourslash/importNameCodeFixNewImportNodeModules2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// @Filename: ../node_modules/fake-module/package.json
1919
//// { "main":"./notindex.js", "typings":"./notindex.d.ts" }
2020

21-
verify.codeFixAtPosition([
21+
verify.importFixAtPosition([
2222
`import { f1 } from "fake-module";
2323
2424
f1();`

tests/cases/fourslash/importNameCodeFixNewImportNodeModules3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//// export var v1 = 5;
88
//// export function f1();
99

10-
verify.codeFixAtPosition([
10+
verify.importFixAtPosition([
1111
`import { f1 } from "random";
1212
1313
f1();`

tests/cases/fourslash/importNameCodeFixNewImportPaths0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//// }
1616
//// }
1717

18-
verify.codeFixAtPosition([
18+
verify.importFixAtPosition([
1919
`import { foo } from "a";
2020
2121
foo();`

tests/cases/fourslash/importNameCodeFixNewImportPaths1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//// }
1616
//// }
1717

18-
verify.codeFixAtPosition([
18+
verify.importFixAtPosition([
1919
`import { foo } from "b/f2";
2020
2121
foo();`

tests/cases/fourslash/importNameCodeFixNewImportRootDirs0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//// }
1717
//// }
1818

19-
verify.codeFixAtPosition([
19+
verify.importFixAtPosition([
2020
`import { foo } from "./f2";
2121
2222
foo();`

tests/cases/fourslash/importNameCodeFixNewImportTypeRoots0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//// }
1616
//// }
1717

18-
verify.codeFixAtPosition([
18+
verify.importFixAtPosition([
1919
`import { foo } from "random";
2020
2121
foo();`

tests/cases/fourslash/server/codefix.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//// }
88
////}
99

10-
verify.codeFixAtPosition(['super();']);
10+
verify.codeFixAtPosition('super();');

tests/cases/fourslash/superFix1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
//// }
88
////}
99

10-
verify.codeFixAtPosition(['super();']);
10+
verify.codeFixAtPosition('super();');

tests/cases/fourslash/superFix2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
//// }
1111
////}
1212

13-
verify.codeFixAtPosition(["super(); this.a = 12;"]);
13+
verify.codeFixAtPosition("super(); this.a = 12;");

0 commit comments

Comments
 (0)