Skip to content

Functional composition experiments#8300

Draft
thorn0 wants to merge 1 commit intoprettier:mainfrom
thorn0:functional-composition-experiments
Draft

Functional composition experiments#8300
thorn0 wants to merge 1 commit intoprettier:mainfrom
thorn0:functional-composition-experiments

Conversation

@thorn0
Copy link
Copy Markdown
Member

@thorn0 thorn0 commented May 13, 2020

Playing with different ideas to (partially) address #6921.

Try the playground for this PR
Or test it locally: npm install thorn0/prettier#functional-composition-experiments

  • The proposed solution tries to distinguish between calls to functions like addEventListener (Node.js callback style) and to functional-composition functions like createSelector from Reselect.
    • Calls to "functions with callbacks" are formatted the same way they're formatted now: the last argument (function expression) is "hugged", i.e. isn't placed on a new line.
    • For "functional-composition calls", every argument is placed on its own line.
    • The detection is based on the presupposition that
      1. functional-composition calls aren't used as expression statements, and that
      2. the callee of such a call is a simple identifier.
    • The second condition was added only because of Lodash functions like _.map to keep their last arguments hugged.
  • If the last argument is a concise arrow function it won't be hugged (arrow functions "glue" long lines at badly split places #4303).
  • Examples from Revisit function literal heuristic introduced in Prettier 1.19 #6921 added to the tests.
  • 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/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

@thorn0 thorn0 marked this pull request as draft May 13, 2020 13:44
@thorn0 thorn0 force-pushed the functional-composition-experiments branch 2 times, most recently from 5a12f60 to e14a67b Compare May 13, 2020 14:19
@fisker
Copy link
Copy Markdown
Member

fisker commented May 29, 2020

Run this on our code base, result https://github.com/fisker/prettier/pull/429/files

@fisker
Copy link
Copy Markdown
Member

fisker commented Jul 14, 2020

Accept this?

@alexander-akait
Copy link
Copy Markdown
Member

`${new Date()
    .toISOString()
    .replace(/T.+/, "")}-${version}.md`

Ugly

@fisker
Copy link
Copy Markdown
Member

fisker commented Jul 14, 2020

`${new Date()
    .toISOString()
    .replace(/T.+/, "")}-${version}.md`

Ugly

This one seems not related. It's changed by #7889

#7889 Playground

@brody2consult
Copy link
Copy Markdown

Definitely looks like a positive change to me. Wouldn't this looks like a breaking change to some users?

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Jul 14, 2020

${new Date()
This one seems not related. It's changed by #7889

Yes. I wonder what to do with it. Should we special-case Date to exempt it from the "always break constructor-call headed chains" rule or is it better to disable that rule inside template literals?

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Jul 14, 2020

As for this PR, what do you all think about the reformatting of the mapDoc calls? E.g. here:
https://github.com/fisker/prettier/pull/429/files#diff-b943e07d378ae28b03b5ebd46e7e86ac
Does that look okay? I'm not sure.

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Jul 14, 2020

@brodybits

Wouldn't this looks like a breaking change to some users?

Where exactly do you think this change might look unexpected?
Do you have any specific libraries/usage in mind?

@brody2consult
Copy link
Copy Markdown

Where exactly do you think this change might look unexpected?

If a massive set of changes would suddenly happen to a code base after a minor update of Prettier, like we would see in Prettier itself (fisker#429), I would be a bit surprised. Or am I too pedantic?

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Jul 15, 2020

@brodybits I think people are okay with massive changes when these massive changes are perceived as in improvement.

@fisker
Copy link
Copy Markdown
Member

fisker commented Jul 19, 2020

what do you all think about the reformatting of the mapDoc calls?

I think it's good.

@karptonite
Copy link
Copy Markdown

@brodybits I think people are okay with massive changes when these massive changes are perceived as in improvement.

Recall also that, to some extent, this sort of rolls back another "massive" change that appeared in 1.19.

@rassie
Copy link
Copy Markdown

rassie commented Nov 13, 2020

Any progress and/or resolution on this one? I've rebased this branch on master and tried running my codebase through it. It's somewhat better than the original, but there are still those weird single-line pipes, which used to be multi-line.

@thorn0
Copy link
Copy Markdown
Member Author

thorn0 commented Nov 13, 2020

@rassie This PR wasn't meant to change the situation for RxJS pipes as I don't know a solution to it.

Base automatically changed from master to main January 23, 2021 17:13
@thorn0 thorn0 force-pushed the functional-composition-experiments branch 2 times, most recently from 0ab08dc to e090f0a Compare March 5, 2021 11:16
if (
hasComment(
lastArg,
CommentCheckFlags.Leading | CommentCheckFlags.Trailing
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@fisker This doesn't work as expected. Something is wrong inside hasComment.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was wrong. Actually, it makes sense. CommentCheckFlags.Leading | CommentCheckFlags.Trailing doesn't mean "leading or trailing". It means "leading and trailing".

Copy link
Copy Markdown
Member

@fisker fisker Mar 5, 2021

Choose a reason for hiding this comment

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

Maybe we can add conflicting flags check? eg Leading/Trailing/Dangling and Block/Line

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, as an assertion removed at build time. Good idea.

@thorn0 thorn0 force-pushed the functional-composition-experiments branch from e090f0a to 11952f1 Compare March 5, 2021 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revisit function literal heuristic introduced in Prettier 1.19 arrow functions "glue" long lines at badly split places

6 participants