Skip to content

Commit 854f20e

Browse files
author
Andy
authored
Remove 'verify.fileAfterCodeFix', use 'verify.codeFix' (#28110)
1 parent 6409195 commit 854f20e

12 files changed

+88
-78
lines changed

src/harness/fourslash.ts

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,7 +2422,20 @@ Actual: ${stringify(fullActual)}`);
24222422
*/
24232423
public getAndApplyCodeActions(errorCode?: number, index?: number) {
24242424
const fileName = this.activeFile.fileName;
2425-
this.applyCodeActions(this.getCodeFixes(fileName, errorCode), index);
2425+
const fixes = this.getCodeFixes(fileName, errorCode);
2426+
if (index === undefined) {
2427+
if (!(fixes && fixes.length === 1)) {
2428+
this.raiseError(`Should find exactly one codefix, but ${fixes ? fixes.length : "none"} found. ${fixes ? fixes.map(a => `${Harness.IO.newLine()} "${a.description}"`) : ""}`);
2429+
}
2430+
index = 0;
2431+
}
2432+
else {
2433+
if (!(fixes && fixes.length >= index + 1)) {
2434+
this.raiseError(`Should find at least ${index + 1} codefix(es), but ${fixes ? fixes.length : "none"} found.`);
2435+
}
2436+
}
2437+
2438+
this.applyChanges(fixes[index].changes);
24262439
}
24272440

24282441
public applyCodeActionFromCompletion(markerName: string, options: FourSlashInterface.VerifyCompletionActionOptions) {
@@ -2433,12 +2446,12 @@ Actual: ${stringify(fullActual)}`);
24332446
if (codeActions.length !== 1) {
24342447
this.raiseError(`Expected one code action, got ${codeActions.length}`);
24352448
}
2449+
const codeAction = ts.first(codeActions);
24362450

2437-
if (codeActions[0].description !== options.description) {
2451+
if (codeAction.description !== options.description) {
24382452
this.raiseError(`Expected description to be:\n${options.description}\ngot:\n${codeActions[0].description}`);
24392453
}
2440-
2441-
this.applyCodeActions(codeActions);
2454+
this.applyChanges(codeAction.changes);
24422455

24432456
this.verifyNewContentAfterChange(options, ts.flatMap(codeActions, a => a.changes.map(c => c.fileName)));
24442457
}
@@ -2483,26 +2496,6 @@ Actual: ${stringify(fullActual)}`);
24832496
this.verifyNewContent({ newFileContent }, changes);
24842497
}
24852498

2486-
/**
2487-
* Applies fixes for the errors in fileName and compares the results to
2488-
* expectedContents after all fixes have been applied.
2489-
*
2490-
* Note: applying one codefix may generate another (eg: remove duplicate implements
2491-
* may generate an extends -> interface conversion fix).
2492-
* @param expectedContents The contents of the file after the fixes are applied.
2493-
* @param fileName The file to check. If not supplied, the current open file is used.
2494-
*/
2495-
public verifyFileAfterCodeFix(expectedContents: string, fileName?: string, index?: number) {
2496-
fileName = fileName ? fileName : this.activeFile.fileName;
2497-
2498-
this.applyCodeActions(this.getCodeFixes(fileName), index);
2499-
2500-
const actualContents: string = this.getFileContent(fileName);
2501-
if (this.removeWhitespace(actualContents) !== this.removeWhitespace(expectedContents)) {
2502-
this.raiseError(`Actual text doesn't match expected text. Actual:\n${actualContents}\n\nExpected:\n${expectedContents}`);
2503-
}
2504-
}
2505-
25062499
public verifyCodeFix(options: FourSlashInterface.VerifyCodeFixOptions) {
25072500
const fileName = this.activeFile.fileName;
25082501
const actions = this.getCodeFixes(fileName, options.errorCode, options.preferences);
@@ -2607,22 +2600,6 @@ Actual: ${stringify(fullActual)}`);
26072600
});
26082601
}
26092602

2610-
private applyCodeActions(actions: ReadonlyArray<ts.CodeAction>, index?: number): void {
2611-
if (index === undefined) {
2612-
if (!(actions && actions.length === 1)) {
2613-
this.raiseError(`Should find exactly one codefix, but ${actions ? actions.length : "none"} found. ${actions ? actions.map(a => `${Harness.IO.newLine()} "${a.description}"`) : ""}`);
2614-
}
2615-
index = 0;
2616-
}
2617-
else {
2618-
if (!(actions && actions.length >= index + 1)) {
2619-
this.raiseError(`Should find at least ${index + 1} codefix(es), but ${actions ? actions.length : "none"} found.`);
2620-
}
2621-
}
2622-
2623-
this.applyChanges(actions[index].changes);
2624-
}
2625-
26262603
private applyChanges(changes: ReadonlyArray<ts.FileTextChanges>): void {
26272604
for (const change of changes) {
26282605
this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false);
@@ -4364,10 +4341,6 @@ namespace FourSlashInterface {
43644341
this.state.verifyRangeAfterCodeFix(expectedText, includeWhiteSpace, errorCode, index);
43654342
}
43664343

4367-
public fileAfterCodeFix(expectedContents: string, fileName?: string, index?: number) {
4368-
this.state.verifyFileAfterCodeFix(expectedContents, fileName, index);
4369-
}
4370-
43714344
public codeFixAll(options: VerifyCodeFixAllOptions): void {
43724345
this.state.verifyCodeFixAll(options);
43734346
}

tests/cases/fourslash/codeFixInferFromUsageMemberJS.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
//// }
1515
////}
1616

17-
1817
// Note: Should be number[] | undefined, but inference currently privileges assignments
1918
// over usage (even when the only result is undefined) and infers only undefined.
20-
verify.fileAfterCodeFix(
19+
verify.codeFix({
20+
description: "Infer type of 'p' from usage",
21+
index: 2,
22+
newFileContent:
2123
`class C {
2224
constructor() {
2325
/** @type {undefined} */
@@ -26,5 +28,5 @@ verify.fileAfterCodeFix(
2628
method() {
2729
this.p.push(1)
2830
}
29-
}
30-
`, undefined, 2);
31+
}`
32+
});

tests/cases/fourslash/codeFixInferFromUsageMultipleParametersJS.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
//// }
1010
//// f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");
1111

12-
13-
verify.fileAfterCodeFix(
12+
verify.codeFix({
13+
description: "Infer parameter types from usage",
14+
index: 6,
15+
newFileContent:
1416
`/**
1517
* @param {number} a
1618
* @param {string} b
@@ -20,4 +22,5 @@ verify.fileAfterCodeFix(
2022
*/
2123
function f(a, b, c, d, e = 0, ...d ) {
2224
}
23-
f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");`, undefined, 6);
25+
f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");`,
26+
});

tests/cases/fourslash/codeFixInferFromUsageNumberIndexSignatureJS.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
//// return a[0] + 1;
99
////}
1010

11-
verify.fileAfterCodeFix(
11+
verify.codeFix({
12+
description: "Infer parameter types from usage",
13+
index: 2,
14+
newFileContent:
1215
`/**
1316
* @param {number[]} a
1417
*/
1518
function f(a) {
1619
return a[0] + 1;
17-
}`, undefined, 2);
20+
}`,
21+
});

tests/cases/fourslash/codeFixInferFromUsageOptionalParamJS.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
////f();
1111
////f(1);
1212

13-
verify.fileAfterCodeFix(
13+
verify.codeFix({
14+
description: "Infer parameter types from usage",
15+
index: 2,
16+
newFileContent:
1417
`/**
1518
* @param {number} [a]
1619
*/
17-
function f(a) {
20+
function f(a){
1821
a;
1922
}
2023
f();
21-
f(1);`, undefined, 2);
24+
f(1);`,
25+
});

tests/cases/fourslash/codeFixInferFromUsagePartialParameterListJS.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
////}
1414
////f(1, 2, 3)
1515

16-
verify.fileAfterCodeFix(
17-
`
18-
/**
16+
verify.codeFix({
17+
description: "Infer parameter types from usage",
18+
index: 2,
19+
newFileContent:
20+
`/**
1921
* @param {*} y
2022
*/
2123
/**
@@ -25,5 +27,5 @@ verify.fileAfterCodeFix(
2527
function f(x, y, z) {
2628
return x
2729
}
28-
f(1, 2, 3)
29-
`, undefined, 2);
30+
f(1, 2, 3)`,
31+
});

tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
//// return x.y.z
1717
////}
1818

19-
verify.fileAfterCodeFix(
19+
verify.codeFix({
20+
description: "Infer parameter types from usage",
21+
index: 2,
22+
newFileContent:
2023
`/**
2124
* @param {{ b: { c: any; }; }} a
2225
* @param {{ n: () => number; }} m
@@ -31,4 +34,5 @@ function foo(a, m, x) {
3134
x.y.z
3235
x.y.z.push(0);
3336
return x.y.z
34-
}`, undefined, 2);
37+
}`,
38+
});

tests/cases/fourslash/codeFixInferFromUsageRestParam2JS.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
////f(3, false, "s2");
1414
////f(4, "s1", "s2", false, "s4");
1515

16-
verify.fileAfterCodeFix(
16+
verify.codeFix({
17+
description: "Infer parameter types from usage",
18+
index: 2,
19+
newFileContent:
1720
`/** @param {number} a */
1821
/**
1922
* @param {(string | boolean)[]} rest
@@ -24,4 +27,5 @@ function f(a, ...rest){
2427
f(1);
2528
f(2, "s1");
2629
f(3, false, "s2");
27-
f(4, "s1", "s2", false, "s4");`, undefined, 2);
30+
f(4, "s1", "s2", false, "s4");`,
31+
});

tests/cases/fourslash/codeFixInferFromUsageRestParam3JS.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
// @noImplicitAny: true
66
// @Filename: important.js
77
/////** @param {number} a */
8-
////function f(a, [|...rest |]){
8+
////function f(a, [|...rest|]){
99
//// a;
1010
//// rest.push(22);
1111
////}
1212

13-
verify.fileAfterCodeFix(
13+
verify.codeFix({
14+
description: "Infer parameter types from usage",
15+
index: 2,
16+
newFileContent:
1417
`/** @param {number} a */
1518
/**
1619
* @param {number[]} rest
1720
*/
1821
function f(a, ...rest){
1922
a;
2023
rest.push(22);
21-
}`, undefined, 2);
24+
}`,
25+
});

tests/cases/fourslash/codeFixInferFromUsageRestParamJS.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
////f(3, "s1", "s2");
1414
////f(3, "s1", "s2", "s3", "s4");
1515

16-
verify.fileAfterCodeFix(
16+
verify.codeFix({
17+
description: "Infer parameter types from usage",
18+
index: 4,
19+
newFileContent:
1720
`/** @param {number} a */
1821
/**
1922
* @param {string[]} rest
@@ -24,4 +27,5 @@ function f(a: number, ...rest){
2427
f(1);
2528
f(2, "s1");
2629
f(3, "s1", "s2");
27-
f(3, "s1", "s2", "s3", "s4");`, undefined, 4);
30+
f(3, "s1", "s2", "s3", "s4");`,
31+
});

tests/cases/fourslash/codeFixInferFromUsageSetterJS.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
////}
1212
////(new C).x = 1;
1313

14-
verify.fileAfterCodeFix(
15-
`
16-
class C {
14+
verify.codeFix({
15+
description: "Infer type of \'x\' from usage",
16+
index: 2,
17+
newFileContent:
18+
`class C {
1719
/**
1820
* @param {number} v
1921
*/
2022
set x(v) {
2123
v;
2224
}
2325
}
24-
(new C).x = 1;`, undefined, 2);
26+
(new C).x = 1;`,
27+
});

tests/cases/fourslash/codeFixInferFromUsageSingleLineClassJS.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
////var c = new C()
1010
////c.m(1)
1111

12-
verify.fileAfterCodeFix(
13-
`
14-
class C {/**
12+
verify.codeFix({
13+
description: "Infer parameter types from usage",
14+
index: 2,
15+
newFileContent:
16+
`class C {/**
1517
* @param {number} x
1618
*/
1719
m(x) {return x;}}
1820
var c = new C()
19-
c.m(1)`, undefined, 2);
21+
c.m(1)`,
22+
});

0 commit comments

Comments
 (0)