Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support right precedence hack-style pipeline #11557

Merged
merged 14 commits into from Oct 2, 2021

Conversation

sosukesuzuki
Copy link
Member

@sosukesuzuki sosukesuzuki commented Sep 20, 2021

Description

Hack-style pipeline operators are right-associative. babel/parser@7.15.5 treats it as left-associative, babel/parser@7.15.6 treats it as right-associative.

ref: babel/babel#13668
ref: babel/babel#13668 (comment)

Clsoes #11540

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@sosukesuzuki sosukesuzuki force-pushed the support-right-preceding-hack branch 2 times, most recently from b0184cd to 9fa6e27 Compare September 22, 2021 14:30
Comment on lines +243 to +250
|> (await %)
|> % || throw new Error(\`foo \${bar1}\`)
|> bar2(%, ", ")
|> bar3(%)
|> % + "!"
|> new Bar.Foo(%)
|> (await bar.bar(%))
|> console.log(%);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the best way, but following official explainer, we don't add indent on here.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's clear for me to indent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker I prefer no indentation. Also other languages code formatter outputs following:

Elixir (by mix format)

Input:

100 |> foo(1) |> bar(2) |> baz(3) |> bar(2) |> baz(3) |> bar(2) |> baz(3)

output:

100
|> foo(1)
|> bar(2)
|> baz(3)
|> bar(2)
|> baz(3)
|> bar(2)
|> baz(3)

F# (by Fantomas)

input:

100 |> function1 |> function2 |> function1 |> function2 |> function1 |> function2 |> function1 |> function2 

output:

100
|> function1
|> function2
|> function1
|> function2
|> function1
|> function2
|> function1
|> function2

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't convince me. I'd like to hear opinions from other maintainers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I have no strong opinion for this. So for now, let's use indentation version, same as current Prettier.

@sosukesuzuki sosukesuzuki changed the base branch from main to dependabot/npm_and_yarn/babel/parser-7.15.7 September 22, 2021 16:19
@sosukesuzuki sosukesuzuki changed the base branch from dependabot/npm_and_yarn/babel/parser-7.15.7 to main September 22, 2021 16:21
@sosukesuzuki sosukesuzuki marked this pull request as ready for review September 22, 2021 16:55
@fisker
Copy link
Sponsor Member

fisker commented Sep 23, 2021

Let me understand the problem here

  1. We can't know the ast is a hack-style pipeline, even if we search for TopicReference, because TopicReference is not required in pipeline. So we have to store the plugins, right?
  2. This ast doesn't print right because we are trying to flat the BinaryExpression. What if we don't try to flat |>? (To be honest: I don't like prettier flat my operators, even simple logical operators) We already have ** , will the output ugly?

@sosukesuzuki
Copy link
Member Author

@fisker

We can't know the ast is a hack-style pipeline, even if we search for TopicReference, because TopicReference is not required in pipeline. So we have to store the plugins, right?

No! TopicReference is required in hack pipeline. So we can detect it in the way you said. I had no such idea.

This ast doesn't print right because we are trying to flat the BinaryExpression.

Why? Hack pipeline is right-associative operators, so it is no problem to flat it?

What if we don't try to flat |>? (To be honest: I don't like prettier flat my operators, even simple logical operators) We already have ** , will the output ugly?

When we don't flat it...

input:

const searchResults$ = fromEvent(document.querySelector('input'), 'input')
  |> map(%, event => event.target.value)
  |> filter(%, searchText => searchText.length > 2)
  |> debounce(%, 300)
  |> distinctUntilChanged(%)
  |> switchMap(%, searchText => queryApi(searchText) |> retry(%, 3))
  |> share(%);

output:

const searchResults$ =
  fromEvent(document.querySelector("input"), "input")
  |> (map(%, (event) => event.target.value)
    |> (filter(%, (searchText) => searchText.length > 2)
      |> (debounce(%, 300)
        |> (distinctUntilChanged(%)
          |> (switchMap(%, (searchText) => queryApi(searchText) |> retry(%, 3))
            |> share(%))))));

@fisker
Copy link
Sponsor Member

fisker commented Sep 24, 2021

This ast doesn't print right because we are trying to flat the BinaryExpression.
Why? Hack pipeline is right-associative operators, so it is no problem to flat it?

I don't know, my guess.

When we don't flat it...

That's ugly, but maybe not caused by flat? If we can know it's right-associative, why not just print differently, like not add parens and intentions? Never mind, your solution seems good.

dependabot bot and others added 14 commits September 25, 2021 02:09
Bumps [@babel/parser](https://github.com/babel/babel/tree/HEAD/packages/babel-parser) from 7.15.5 to 7.15.7.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.15.7/packages/babel-parser)

---
updated-dependencies:
- dependency-name: "@babel/parser"
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [@babel/parser](https://github.com/babel/babel/tree/HEAD/packages/babel-parser) from 7.15.5 to 7.15.6.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.15.6/packages/babel-parser)

---
updated-dependencies:
- dependency-name: "@babel/parser"
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
@@ -149,7 +149,8 @@ function createParse(parseMethod, ...optionsCombinations) {
throw createBabelParseError(error);
}

return postprocess(ast, { ...opts, originalText: text });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of scope: opts is big object, so maybe it is good that we don't copy it for other parsers too.

@sosukesuzuki
Copy link
Member Author

@fisker Can you review again?

@fisker
Copy link
Sponsor Member

fisker commented Sep 27, 2021

1 + 100 |> a(%) means a(1 + 100), not 1 + a(100)?

@sosukesuzuki
Copy link
Member Author

1 + 100 |> a(%) means a(1 + 100)

yes.

(Babel)

Input:

1 + 100 |> a(%)

Output:

"use strict";

var _ref;

_ref = 1 + 100, a(_ref);

Comment on lines 280 to 287
|> (yield %)
|> (await %)
|> y(%)
|> a.b(%)
|> a.b(%)
|> a.b(%)
|> a.b?.(%)
|> a.b?.(%)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker I've updated snapshots with indentation version (#11557 (comment)). What do you think this change? ( After seeing this change, I personally reconsidered and decided that it would be better to be consistent without indentation. )

@sosukesuzuki
Copy link
Member Author

I don't like that this PR is blocker to update babel/parser. So I'll merge this with the unindent version for now. Let's discuss in another issue. Since this syntax is not used by most users, there is no pain to change the behavior.

@sosukesuzuki sosukesuzuki merged commit fb7451a into prettier:main Oct 2, 2021
@sosukesuzuki sosukesuzuki deleted the support-right-preceding-hack branch October 2, 2021 02:34
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants