Skip to content

Commit

Permalink
JavaScript: Fix pipeline bugs (#7979)
Browse files Browse the repository at this point in the history
* 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 <thorn.mailbox@gmail.com>
  • Loading branch information
sosukesuzuki and thorn0 committed Apr 8, 2020
1 parent f675922 commit 17d6e38
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
35 changes: 18 additions & 17 deletions 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:**

<!-- prettier-ignore -->
```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:**

<!-- prettier-ignore -->
```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
```
13 changes: 3 additions & 10 deletions src/language-js/needs-parens.js
Expand Up @@ -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":
Expand Down
13 changes: 7 additions & 6 deletions src/language-js/parser-babel.js
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -20,6 +20,9 @@ function f() {
)
}
((x) => x) + '';
'' + ((x) => x);
=====================================output=====================================
function f() {
const appEntities = getAppEntities(loadObject).filter(
Expand All @@ -43,6 +46,9 @@ function f() {
);
}
((x) => x) + "";
"" + ((x) => x);
================================================================================
`;

Expand Down
3 changes: 3 additions & 0 deletions tests/binary-expressions/arrow.js
Expand Up @@ -11,3 +11,6 @@ function f() {
}
)
}

((x) => x) + '';
'' + ((x) => x);

0 comments on commit 17d6e38

Please sign in to comment.