From 6f6c40186f75ea8f3c6cee5afba80b30627519aa Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 6 Mar 2018 11:00:41 -0800 Subject: [PATCH 1/2] Add test for undefined action returned Test for #22343 --- src/harness/fourslash.ts | 4 ++++ ...ixInferFromUsageSetterWithInaccessibleType.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 401caac3234..68dd3169112 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2923,6 +2923,10 @@ Actual: ${stringify(fullActual)}`); if (!codeFixes.length) { this.raiseError(`verifyCodeFixAvailable failed - expected code fixes but none found.`); } + codeFixes.forEach(fix => fix.changes.forEach(change => { + assert.isObject(change, `Invalid change in code fix: ${JSON.stringify(fix)}`); + change.textChanges.forEach(textChange => assert.isObject(textChange, `Invalid textChange in codeFix: ${JSON.stringify(fix)}`)); + })); if (info) { assert.equal(info.length, codeFixes.length); ts.zipWith(codeFixes, info, (fix, info) => { diff --git a/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts b/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts new file mode 100644 index 00000000000..993a58e8e8e --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts @@ -0,0 +1,16 @@ +/// + +// @noImplicitAny: true + +// @Filename: /a.ts +////export class D {} +////export default new D(); + +// @Filename: /b.ts +////export class C { +//// [|set x(val) {}|] +//// method() { this.x = import("./a"); } +////} + +goTo.file("/b.ts"); +verify.codeFixAvailable(); \ No newline at end of file From 70944428a1a347274afe5052bd8b0d7adc19c72f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 6 Mar 2018 11:03:20 -0800 Subject: [PATCH 2/2] =?UTF-8?q?Create=20the=20action=20only=20if=20the=20t?= =?UTF-8?q?ext=20change=20creation=20is=20successful.=20=20=E2=80=A6=20Mak?= =?UTF-8?q?e=20change=20for=20the=20infer=20type=20from=20usage=20could=20?= =?UTF-8?q?return=20undefined=20even=20if=20type=20is=20present=20if=20the?= =?UTF-8?q?=20type=20cannot=20be=20named=20Fixes=20#22184?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/codefixes/inferFromUsage.ts | 3 ++- .../codeFixInferFromUsageSetterWithInaccessibleType.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index b5a9e15f5a4..18df488e979 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -186,7 +186,8 @@ namespace ts.codefix { } function makeFix(declaration: Declaration, start: number, type: Type | undefined, program: Program): Fix | undefined { - return type && { declaration, textChanges: [makeChange(declaration, start, type, program)] }; + const change = makeChange(declaration, start, type, program); + return change && { declaration, textChanges: [change] }; } function makeChange(declaration: Declaration, start: number, type: Type | undefined, program: Program): TextChange | undefined { diff --git a/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts b/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts index 993a58e8e8e..96c76a64837 100644 --- a/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts +++ b/tests/cases/fourslash/codeFixInferFromUsageSetterWithInaccessibleType.ts @@ -13,4 +13,4 @@ ////} goTo.file("/b.ts"); -verify.codeFixAvailable(); \ No newline at end of file +verify.not.codeFixAvailable(); \ No newline at end of file