From 17d6e38d3409db5e0ff87a2a2c09329b7a0d1d9e Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 8 Apr 2020 23:49:46 +0900 Subject: [PATCH] JavaScript: Fix pipeline bugs (#7979) * Fix arrow regression * Fix resolvePluginConflict * Update changelog * Fix condition * Edit changelog: add link to Babel blog, change order (to be as in the blog) Co-authored-by: Georgii Dolzhykov --- changelog_unreleased/javascript/pr-6319.md | 35 ++++++++++--------- src/language-js/needs-parens.js | 13 ++----- src/language-js/parser-babel.js | 13 +++---- .../__snapshots__/jsfmt.spec.js.snap | 6 ++++ tests/binary-expressions/arrow.js | 3 ++ 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/changelog_unreleased/javascript/pr-6319.md b/changelog_unreleased/javascript/pr-6319.md index e2c6fc61a57d..ad225e38da4e 100644 --- a/changelog_unreleased/javascript/pr-6319.md +++ b/changelog_unreleased/javascript/pr-6319.md @@ -1,36 +1,37 @@ -#### Support the 'smart' and 'fsharp-style' pipeline operator proposal ([#6319](https://github.com/prettier/prettier/pull/6319) by [@sosukesuzuki](https://github.com/sosukesuzuki), [@thorn0](https://github.com/thorn0)) +#### Support the F# and Smart pipeline operator proposals ([#6319](https://github.com/prettier/prettier/pull/6319) by [@sosukesuzuki](https://github.com/sosukesuzuki), [@thorn0](https://github.com/thorn0), [#7979](https://github.com/prettier/prettier/pull/7979) by [@sosukesuzuki](https://github.com/sosukesuzuki)) -- [Link to the 'smart' pipeline proposal](https://github.com/js-choi/proposal-smart-pipelines) -- [Link to the 'fsharp-style' pipeline proposal](https://github.com/valtech-nyc/proposal-fsharp-pipelines) +- [F# Pipeline Operator proposal](https://github.com/valtech-nyc/proposal-fsharp-pipelines) +- [Smart Pipelines proposal](https://github.com/js-choi/proposal-smart-pipelines) +- [Babel Blog: What's Happening With the Pipeline (`|>`) Proposal?](https://babeljs.io/blog/2018/07/19/whats-happening-with-the-pipeline-proposal) -**Smart Pipeline:** +**F#-style Pipeline:** ```js // Input -5 |> # * 2 +promises |> await; -// Output (Prettier Stable) -SyntaxError: Unexpected character '#' (1:6) -> 1 | 5 |> # * 2 - | ^ +// Output (Prettier stable) +SyntaxError: Unexpected token (1:18) +> 1 | promises |> await; + | ^ // Output (Prettier master) -5 |> # * 2 +promises |> await; ``` -**F#-style Pipeline:** +**Smart Pipeline:** ```js // Input -promises |> await; +5 |> # * 2 -// Output (Prettier stable) -SyntaxError: Unexpected token (1:18) -> 1 | promises |> await; - | ^ +// Output (Prettier Stable) +SyntaxError: Unexpected character '#' (1:6) +> 1 | 5 |> # * 2 + | ^ // Output (Prettier master) -promises |> await; +5 |> # * 2 ``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index e73659c409b3..dbeeeed2322c 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -583,16 +583,9 @@ function needsParens(path, options) { return !!(node.extra && node.extra.parenthesized); case "BinaryExpression": - if (node.extra && node.extra.parenthesized) { - return ( - parent.operator === "|>" || - (parent.operator !== "|>" && - node.body.type === "BinaryExpression" && - node.body.operator === "|>") - ); - } - return false; - + return ( + parent.operator !== "|>" || (node.extra && node.extra.parenthesized) + ); case "NewExpression": case "CallExpression": case "OptionalCallExpression": diff --git a/src/language-js/parser-babel.js b/src/language-js/parser-babel.js index fbbadce56374..9121ef6f4ead 100644 --- a/src/language-js/parser-babel.js +++ b/src/language-js/parser-babel.js @@ -48,12 +48,13 @@ function resolvePluginsConflict( pluginCombinations, conflictPlugins ) { - const combinations = [...pluginCombinations]; - if (condition) { - for (const combination of pluginCombinations) { - for (const plugin of conflictPlugins) { - combinations.push([...combination, plugin]); - } + if (!condition) { + return pluginCombinations; + } + const combinations = []; + for (const combination of pluginCombinations) { + for (const plugin of conflictPlugins) { + combinations.push([...combination, plugin]); } } return combinations; diff --git a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap index 1bf874e44ea2..f4d28e9200fb 100644 --- a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap @@ -20,6 +20,9 @@ function f() { ) } +((x) => x) + ''; +'' + ((x) => x); + =====================================output===================================== function f() { const appEntities = getAppEntities(loadObject).filter( @@ -43,6 +46,9 @@ function f() { ); } +((x) => x) + ""; +"" + ((x) => x); + ================================================================================ `; diff --git a/tests/binary-expressions/arrow.js b/tests/binary-expressions/arrow.js index 754368f6f819..a4d82f35522c 100644 --- a/tests/binary-expressions/arrow.js +++ b/tests/binary-expressions/arrow.js @@ -11,3 +11,6 @@ function f() { } ) } + +((x) => x) + ''; +'' + ((x) => x);