question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prettier formats a sensible one-liner into seven lines

See original GitHub issue

Prettier 1.15.3 Playground link

--parser babylon

Input:

eventStore.update(id, _.flow(updater, incrementVersion))

Output:

eventStore.update(
  id,
  _.flow(
    updater,
    incrementVersion
  )
);

Expected behavior: Unchanged:

eventStore.update(id, _.flow(updater, incrementVersion))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
duailibecommented, Jan 18, 2019

I think we maybe should revisit this heuristic.

2reactions
sam3dcommented, Jan 21, 2019

Could such a function call default to being one-line (as long as it’s within the character limit), but in the same way that an object can always be made to be on multiple lines if a newline is added after {, this could this be made to adapt the following behaviour:

  • If the function call has at least one argument that is also a function call
  • and a newline is present immediately after the first opening parentheses of the first function call
  • then split the function over multiple lines

OR

  • If the function call has at least one argument that is also a function call
  • and part of the function is split over multiple lines already
  • but no newline is present after the first opening parentheses
  • then collapse the partially multiline function down to one line
  • only if the resulting function call length does not exceed the character limit

This would also have the benefit of not affecting existing prettier code that has already been split over multiple lines by the existing heuristic.

It also brings it much more in-line with the way that prettier handles objects over multiple lines.

It also doesn’t rely on special function name detection, and as it is specific enough to only apply to function calls within function calls, it can handle many of these edge-cases without either being too constrictive or too open.


Just the one-liner as is

Input:

eventStore.update(id, _.flow(updater, incrementVersion))

Output:

eventStore.update(id, _.flow(updater, incrementVersion));

A newline after the first opening parentheses of the first function call

Input:

eventStore.update(
id, _.flow(updater, incrementVersion))

Output:

eventStore.update(
  id,
  _.flow(
    updater,
    incrementVersion
  )
);

Multiple lines, but no newline between first opening parentheses of first function call and first parameter

Input:

eventStore.update(id,
  _.flow(
    updater,
    incrementVersion
  )
);

Output:

eventStore.update(id, _.flow(updater, incrementVersion));

A one-liner with a parameter function call that exceeds the character limit

Input:

eventStore.update(id, _.flow(updater, incrementVersion), aReallyLongArgumentThatGoesPast80Characters)

Output:

eventStore.update(
  id,
  _.flow(
    updater,
    incrementVersion
  ),
  aReallyLongArgumentThatGoesPast80Characters
);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Options - Prettier
Prettier's printWidth option does not work the same way. It is not the hard upper allowed line length limit. It is a way...
Read more >
Prettier — The Formatting Big Brother of ESLint - In Plain English
Prettier is the formatting big brother to ESLint. Where ESLint focuses mainly on code quality with some code formatting, Prettier focuses ...
Read more >
Is there a config in prettier to keep line breaks? - Stack Overflow
Not true. This only stops formatting because lack of config file. – oxk4r. Dec 1, 2021 at 17:18.
Read more >
How to write a plugin for Prettier | by Franco Victorio | Medium
Prettier's philosophy is to keep them (although if there are two or more blank lines together, they are replaced by a single blank...
Read more >
Google JavaScript Style Guide
7 JSDoc: 7.1 General form: 7.2 Markdown: 7.3 JSDoc tags: 7.4 Line wrapping ... A JavaScript source file is described as being in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found