From 5ec1201e3abd69d122f8e0576ad8cd7a7f886f33 Mon Sep 17 00:00:00 2001 From: BigAru Date: Wed, 23 Jan 2019 19:36:00 +0100 Subject: [PATCH] exclude nested functions and classes at containingThis check --- ...onvertArrowFunctionOrFunctionExpression.ts | 4 +++- ...xpression_Availability_Anon_nested_this.ts | 19 +++++++++++++++++++ ...nctionExpression_Availability_Anon_this.ts | 6 ++++-- ...pression_Availability_Arrow_nested_this.ts | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_nested_this.ts create mode 100644 tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Arrow_nested_this.ts diff --git a/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts b/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts index 78d2127ac01..9908dbcb8d5 100644 --- a/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts +++ b/src/services/refactors/convertArrowFunctionOrFunctionExpression.ts @@ -102,7 +102,9 @@ namespace ts.refactor.convertArrowFunctionOrFunctionExpression { return; } - forEachChild(child, checkThis); + if (!isClassLike(child) && !isFunctionDeclaration(child) && !isFunctionExpression(child)) { + forEachChild(child, checkThis); + } }); return containsThis; diff --git a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_nested_this.ts b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_nested_this.ts new file mode 100644 index 00000000000..cbf57122526 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_nested_this.ts @@ -0,0 +1,19 @@ +/// + +//// const zoo = /*x*/f/*w*/unction () { +//// class Animal { +//// weight = 42 +//// askWeight() { return this.weight } +//// } +//// const Insect = class { +//// weight = 42 +//// askWeight() { return this.weight } +//// } +//// function callTaxi() { this.no = "054 xxx xx xx" } +//// const callPizzaDelivery = function() { this.phone = "064 yyy yy yy"} +//// }; + +goTo.select("x", "w"); +verify.not.refactorAvailable("Convert arrow function or function expression", "Convert to named function"); +verify.not.refactorAvailable("Convert arrow function or function expression", "Convert to anonymous function"); +verify.refactorAvailable("Convert arrow function or function expression", "Convert to arrow function"); diff --git a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_this.ts b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_this.ts index 5ea85798bdd..a895fea432d 100644 --- a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_this.ts +++ b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Anon_this.ts @@ -1,7 +1,9 @@ /// -//// const bar = 42; -//// const foo = /*x*/f/*w*/unction() {return this.bar;}; +//// const foo = /*x*/f/*w*/unction() { +//// this.bar = "F-Express"; +//// return this.bar; +//// }; goTo.select("x", "w"); verify.not.refactorAvailable("Convert arrow function or function expression", "Convert to named function"); diff --git a/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Arrow_nested_this.ts b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Arrow_nested_this.ts new file mode 100644 index 00000000000..4b5985b9a85 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertArrowFunctionOrFunctionExpression_Availability_Arrow_nested_this.ts @@ -0,0 +1,19 @@ +/// + +//// const zoo = /*x*/(/*w*/) => { +//// class Animal { +//// weight = 42 +//// askWeight() { return this.weight } +//// } +//// const Insect = class { +//// weight = 42 +//// askWeight() { return this.weight } +//// } +//// function callTaxi() { this.no = "054 xxx xx xx" } +//// const callPizzaDelivery = function() { this.phone = "064 yyy yy yy"} +//// }; + +goTo.select("x", "w"); +verify.refactorAvailable("Convert arrow function or function expression", "Convert to named function"); +verify.refactorAvailable("Convert arrow function or function expression", "Convert to anonymous function"); +verify.not.refactorAvailable("Convert arrow function or function expression", "Convert to arrow function");