From 03ee86ea80c37980c16585d2809bc05586041879 Mon Sep 17 00:00:00 2001 From: Eugene Datsky Date: Fri, 13 Jul 2018 00:58:57 +1000 Subject: [PATCH] Blacklist this in functional composition heuristics (#4836) --- src/language-js/printer-estree.js | 18 +++++++++++++- .../__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++ .../functional_compose.js | 12 ++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index bed172c6a..2009401e6 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3440,11 +3440,27 @@ const functionCompositionFunctionNames = new Set([ "connect" // Redux ]); +function isThisExpression(node) { + switch (node.type) { + case "OptionalMemberExpression": + case "MemberExpression": { + return isThisExpression(node.object); + } + case "ThisExpression": + case "Super": { + return true; + } + } +} + function isFunctionCompositionFunction(node) { switch (node.type) { case "OptionalMemberExpression": case "MemberExpression": { - return isFunctionCompositionFunction(node.property); + return ( + !isThisExpression(node.object) && + isFunctionCompositionFunction(node.property) + ); } case "Identifier": { return functionCompositionFunctionNames.has(node.name); diff --git a/tests/functional_composition/__snapshots__/jsfmt.spec.js.snap b/tests/functional_composition/__snapshots__/jsfmt.spec.js.snap index a7329a75a..a4a84a264 100644 --- a/tests/functional_composition/__snapshots__/jsfmt.spec.js.snap +++ b/tests/functional_composition/__snapshots__/jsfmt.spec.js.snap @@ -27,6 +27,18 @@ somelib.composeFlipped( // no regression (#4602) const hasValue = hasOwnProperty(a, b); + +// filter out ThisExpression +this.compose(sortBy(x => x), flatten); +this.a.b.c.compose(sortBy(x => x), flatten); +someObj.someMethod(this.field.compose(a, b)); + +// filter out Super +class A extends B { + compose() { + super.compose(sortBy(x => x), flatten); + } +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compose( sortBy(x => x), @@ -55,6 +67,18 @@ somelib.composeFlipped( // no regression (#4602) const hasValue = hasOwnProperty(a, b); +// filter out ThisExpression +this.compose(sortBy(x => x), flatten); +this.a.b.c.compose(sortBy(x => x), flatten); +someObj.someMethod(this.field.compose(a, b)); + +// filter out Super +class A extends B { + compose() { + super.compose(sortBy(x => x), flatten); + } +} + `; exports[`lodash_flow.js - flow-verify 1`] = ` diff --git a/tests/functional_composition/functional_compose.js b/tests/functional_composition/functional_compose.js index aa62337e0..0e3e28a95 100644 --- a/tests/functional_composition/functional_compose.js +++ b/tests/functional_composition/functional_compose.js @@ -24,3 +24,15 @@ somelib.composeFlipped( // no regression (#4602) const hasValue = hasOwnProperty(a, b); + +// filter out ThisExpression +this.compose(sortBy(x => x), flatten); +this.a.b.c.compose(sortBy(x => x), flatten); +someObj.someMethod(this.field.compose(a, b)); + +// filter out Super +class A extends B { + compose() { + super.compose(sortBy(x => x), flatten); + } +}