Skip to content

Remove 'verify.fileAfterCodeFix', use 'verify.codeFix' #28110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 17 additions & 44 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,20 @@ Actual: ${stringify(fullActual)}`);
*/
public getAndApplyCodeActions(errorCode?: number, index?: number) {
const fileName = this.activeFile.fileName;
this.applyCodeActions(this.getCodeFixes(fileName, errorCode), index);
const fixes = this.getCodeFixes(fileName, errorCode);
if (index === undefined) {
if (!(fixes && fixes.length === 1)) {
this.raiseError(`Should find exactly one codefix, but ${fixes ? fixes.length : "none"} found. ${fixes ? fixes.map(a => `${Harness.IO.newLine()} "${a.description}"`) : ""}`);
}
index = 0;
}
else {
if (!(fixes && fixes.length >= index + 1)) {
this.raiseError(`Should find at least ${index + 1} codefix(es), but ${fixes ? fixes.length : "none"} found.`);
}
}

this.applyChanges(fixes[index].changes);
}

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

if (codeActions[0].description !== options.description) {
if (codeAction.description !== options.description) {
this.raiseError(`Expected description to be:\n${options.description}\ngot:\n${codeActions[0].description}`);
}

this.applyCodeActions(codeActions);
this.applyChanges(codeAction.changes);

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

/**
* Applies fixes for the errors in fileName and compares the results to
* expectedContents after all fixes have been applied.
*
* Note: applying one codefix may generate another (eg: remove duplicate implements
* may generate an extends -> interface conversion fix).
* @param expectedContents The contents of the file after the fixes are applied.
* @param fileName The file to check. If not supplied, the current open file is used.
*/
public verifyFileAfterCodeFix(expectedContents: string, fileName?: string, index?: number) {
fileName = fileName ? fileName : this.activeFile.fileName;

this.applyCodeActions(this.getCodeFixes(fileName), index);

const actualContents: string = this.getFileContent(fileName);
if (this.removeWhitespace(actualContents) !== this.removeWhitespace(expectedContents)) {
this.raiseError(`Actual text doesn't match expected text. Actual:\n${actualContents}\n\nExpected:\n${expectedContents}`);
}
}

public verifyCodeFix(options: FourSlashInterface.VerifyCodeFixOptions) {
const fileName = this.activeFile.fileName;
const actions = this.getCodeFixes(fileName, options.errorCode, options.preferences);
Expand Down Expand Up @@ -2607,22 +2600,6 @@ Actual: ${stringify(fullActual)}`);
});
}

private applyCodeActions(actions: ReadonlyArray<ts.CodeAction>, index?: number): void {
if (index === undefined) {
if (!(actions && actions.length === 1)) {
this.raiseError(`Should find exactly one codefix, but ${actions ? actions.length : "none"} found. ${actions ? actions.map(a => `${Harness.IO.newLine()} "${a.description}"`) : ""}`);
}
index = 0;
}
else {
if (!(actions && actions.length >= index + 1)) {
this.raiseError(`Should find at least ${index + 1} codefix(es), but ${actions ? actions.length : "none"} found.`);
}
}

this.applyChanges(actions[index].changes);
}

private applyChanges(changes: ReadonlyArray<ts.FileTextChanges>): void {
for (const change of changes) {
this.applyEdits(change.fileName, change.textChanges, /*isFormattingEdit*/ false);
Expand Down Expand Up @@ -4364,10 +4341,6 @@ namespace FourSlashInterface {
this.state.verifyRangeAfterCodeFix(expectedText, includeWhiteSpace, errorCode, index);
}

public fileAfterCodeFix(expectedContents: string, fileName?: string, index?: number) {
this.state.verifyFileAfterCodeFix(expectedContents, fileName, index);
}

public codeFixAll(options: VerifyCodeFixAllOptions): void {
this.state.verifyCodeFixAll(options);
}
Expand Down
10 changes: 6 additions & 4 deletions tests/cases/fourslash/codeFixInferFromUsageMemberJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
//// }
////}


// Note: Should be number[] | undefined, but inference currently privileges assignments
// over usage (even when the only result is undefined) and infers only undefined.
verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer type of 'p' from usage",
index: 2,
newFileContent:
`class C {
constructor() {
/** @type {undefined} */
Expand All @@ -26,5 +28,5 @@ verify.fileAfterCodeFix(
method() {
this.p.push(1)
}
}
`, undefined, 2);
}`
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
//// }
//// f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");


verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 6,
newFileContent:
`/**
* @param {number} a
* @param {string} b
Expand All @@ -20,4 +22,5 @@ verify.fileAfterCodeFix(
*/
function f(a, b, c, d, e = 0, ...d ) {
}
f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");`, undefined, 6);
f(1, "string", { a: 1 }, {shouldNotBeHere: 2}, {shouldNotBeHere: 2}, 3, "string");`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
//// return a[0] + 1;
////}

verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/**
* @param {number[]} a
*/
function f(a) {
return a[0] + 1;
}`, undefined, 2);
}`,
});
10 changes: 7 additions & 3 deletions tests/cases/fourslash/codeFixInferFromUsageOptionalParamJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
////f();
////f(1);

verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/**
* @param {number} [a]
*/
function f(a) {
function f(a){
a;
}
f();
f(1);`, undefined, 2);
f(1);`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
////}
////f(1, 2, 3)

verify.fileAfterCodeFix(
`
/**
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/**
* @param {*} y
*/
/**
Expand All @@ -25,5 +27,5 @@ verify.fileAfterCodeFix(
function f(x, y, z) {
return x
}
f(1, 2, 3)
`, undefined, 2);
f(1, 2, 3)`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
//// return x.y.z
////}

verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't know the index, how can I figure out the index? With fileAfterCodeFix, leaving off the last two arguments prints out a list of provided code fixes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verify.codeFix does the same thing -- using the same (duplicated) code.

newFileContent:
`/**
* @param {{ b: { c: any; }; }} a
* @param {{ n: () => number; }} m
Expand All @@ -31,4 +34,5 @@ function foo(a, m, x) {
x.y.z
x.y.z.push(0);
return x.y.z
}`, undefined, 2);
}`,
});
8 changes: 6 additions & 2 deletions tests/cases/fourslash/codeFixInferFromUsageRestParam2JS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
////f(3, false, "s2");
////f(4, "s1", "s2", false, "s4");

verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/** @param {number} a */
/**
* @param {(string | boolean)[]} rest
Expand All @@ -24,4 +27,5 @@ function f(a, ...rest){
f(1);
f(2, "s1");
f(3, false, "s2");
f(4, "s1", "s2", false, "s4");`, undefined, 2);
f(4, "s1", "s2", false, "s4");`,
});
10 changes: 7 additions & 3 deletions tests/cases/fourslash/codeFixInferFromUsageRestParam3JS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
// @noImplicitAny: true
// @Filename: important.js
/////** @param {number} a */
////function f(a, [|...rest |]){
////function f(a, [|...rest|]){
//// a;
//// rest.push(22);
////}

verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/** @param {number} a */
/**
* @param {number[]} rest
*/
function f(a, ...rest){
a;
rest.push(22);
}`, undefined, 2);
}`,
});
8 changes: 6 additions & 2 deletions tests/cases/fourslash/codeFixInferFromUsageRestParamJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
////f(3, "s1", "s2");
////f(3, "s1", "s2", "s3", "s4");

verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 4,
newFileContent:
`/** @param {number} a */
/**
* @param {string[]} rest
Expand All @@ -24,4 +27,5 @@ function f(a: number, ...rest){
f(1);
f(2, "s1");
f(3, "s1", "s2");
f(3, "s1", "s2", "s3", "s4");`, undefined, 4);
f(3, "s1", "s2", "s3", "s4");`,
});
11 changes: 7 additions & 4 deletions tests/cases/fourslash/codeFixInferFromUsageSetterJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
////}
////(new C).x = 1;

verify.fileAfterCodeFix(
`
class C {
verify.codeFix({
description: "Infer type of \'x\' from usage",
index: 2,
newFileContent:
`class C {
/**
* @param {number} v
*/
set x(v) {
v;
}
}
(new C).x = 1;`, undefined, 2);
(new C).x = 1;`,
});
11 changes: 7 additions & 4 deletions tests/cases/fourslash/codeFixInferFromUsageSingleLineClassJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
////var c = new C()
////c.m(1)

verify.fileAfterCodeFix(
`
class C {/**
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`class C {/**
* @param {number} x
*/
m(x) {return x;}}
var c = new C()
c.m(1)`, undefined, 2);
c.m(1)`,
});