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

This commit is contained in:
Andy
2018-10-24 15:34:15 -07:00
committed by GitHub
parent 6409195054
commit 854f20e90f
12 changed files with 88 additions and 78 deletions

View File

@@ -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) {
@@ -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)));
}
@@ -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);
@@ -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);
@@ -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);
}

View File

@@ -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} */
@@ -26,5 +28,5 @@ verify.fileAfterCodeFix(
method() {
this.p.push(1)
}
}
`, undefined, 2);
}`
});

View File

@@ -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
@@ -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");`,
});

View File

@@ -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);
}`,
});

View File

@@ -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);`,
});

View File

@@ -13,9 +13,11 @@
////}
////f(1, 2, 3)
verify.fileAfterCodeFix(
`
/**
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/**
* @param {*} y
*/
/**
@@ -25,5 +27,5 @@ verify.fileAfterCodeFix(
function f(x, y, z) {
return x
}
f(1, 2, 3)
`, undefined, 2);
f(1, 2, 3)`,
});

View File

@@ -16,7 +16,10 @@
//// return x.y.z
////}
verify.fileAfterCodeFix(
verify.codeFix({
description: "Infer parameter types from usage",
index: 2,
newFileContent:
`/**
* @param {{ b: { c: any; }; }} a
* @param {{ n: () => number; }} m
@@ -31,4 +34,5 @@ function foo(a, m, x) {
x.y.z
x.y.z.push(0);
return x.y.z
}`, undefined, 2);
}`,
});

View File

@@ -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
@@ -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");`,
});

View File

@@ -5,12 +5,15 @@
// @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
@@ -18,4 +21,5 @@ verify.fileAfterCodeFix(
function f(a, ...rest){
a;
rest.push(22);
}`, undefined, 2);
}`,
});

View File

@@ -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
@@ -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");`,
});

View File

@@ -11,9 +11,11 @@
////}
////(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
*/
@@ -21,4 +23,5 @@ class C {
v;
}
}
(new C).x = 1;`, undefined, 2);
(new C).x = 1;`,
});

View File

@@ -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)`,
});