From 854f20e90f2631278a6af9afd1058d3eb800bbc8 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 24 Oct 2018 15:34:15 -0700 Subject: [PATCH] Remove 'verify.fileAfterCodeFix', use 'verify.codeFix' (#28110) --- src/harness/fourslash.ts | 61 ++++++------------- .../codeFixInferFromUsageMemberJS.ts | 10 +-- ...deFixInferFromUsageMultipleParametersJS.ts | 9 ++- ...FixInferFromUsageNumberIndexSignatureJS.ts | 8 ++- .../codeFixInferFromUsageOptionalParamJS.ts | 10 ++- ...FixInferFromUsagePartialParameterListJS.ts | 12 ++-- .../codeFixInferFromUsagePropertyAccessJS.ts | 8 ++- .../codeFixInferFromUsageRestParam2JS.ts | 8 ++- .../codeFixInferFromUsageRestParam3JS.ts | 10 ++- .../codeFixInferFromUsageRestParamJS.ts | 8 ++- .../codeFixInferFromUsageSetterJS.ts | 11 ++-- .../codeFixInferFromUsageSingleLineClassJS.ts | 11 ++-- 12 files changed, 88 insertions(+), 78 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index d69df1d6cbd..406985f1efa 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -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, 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): 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); } diff --git a/tests/cases/fourslash/codeFixInferFromUsageMemberJS.ts b/tests/cases/fourslash/codeFixInferFromUsageMemberJS.ts index cb86347cb9f..3fd76defd21 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageMemberJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageMemberJS.ts @@ -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); +}` +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageMultipleParametersJS.ts b/tests/cases/fourslash/codeFixInferFromUsageMultipleParametersJS.ts index 8f544df4323..d57f3292375 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageMultipleParametersJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageMultipleParametersJS.ts @@ -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");`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageNumberIndexSignatureJS.ts b/tests/cases/fourslash/codeFixInferFromUsageNumberIndexSignatureJS.ts index b6b8fe4c25f..d494d1dbb7b 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageNumberIndexSignatureJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageNumberIndexSignatureJS.ts @@ -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); +}`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageOptionalParamJS.ts b/tests/cases/fourslash/codeFixInferFromUsageOptionalParamJS.ts index cc00c1ddb5d..7003950b7ff 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageOptionalParamJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageOptionalParamJS.ts @@ -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);`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsagePartialParameterListJS.ts b/tests/cases/fourslash/codeFixInferFromUsagePartialParameterListJS.ts index 67795b6a178..308041dd09c 100644 --- a/tests/cases/fourslash/codeFixInferFromUsagePartialParameterListJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsagePartialParameterListJS.ts @@ -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)`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts b/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts index a52c66312fc..322d51a79ab 100644 --- a/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts @@ -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); +}`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageRestParam2JS.ts b/tests/cases/fourslash/codeFixInferFromUsageRestParam2JS.ts index 0504816c597..85ee0de9cef 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageRestParam2JS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageRestParam2JS.ts @@ -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");`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageRestParam3JS.ts b/tests/cases/fourslash/codeFixInferFromUsageRestParam3JS.ts index bda7090ba06..689d12c783a 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageRestParam3JS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageRestParam3JS.ts @@ -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); +}`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageRestParamJS.ts b/tests/cases/fourslash/codeFixInferFromUsageRestParamJS.ts index 36582bfa7db..b46bd7dd843 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageRestParamJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageRestParamJS.ts @@ -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");`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageSetterJS.ts b/tests/cases/fourslash/codeFixInferFromUsageSetterJS.ts index 71621f48239..c9c0c719de4 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageSetterJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageSetterJS.ts @@ -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;`, +}); diff --git a/tests/cases/fourslash/codeFixInferFromUsageSingleLineClassJS.ts b/tests/cases/fourslash/codeFixInferFromUsageSingleLineClassJS.ts index f78a70aa14a..0ea9a4d5719 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageSingleLineClassJS.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageSingleLineClassJS.ts @@ -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)`, +});