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.

Respect paren + newline

See original GitHub issue

Hi friends!

Been using prettier full time for a while now and it’s prettier great. I’d like to request one change that I think makes refactoring less error prone: respect ( + newline the same way if { + newline is treated.

// prettier maintains the block
if (foo) {
  doStuff()
}

// but changes this:
if (foo)
  doStuff()

// to this:
if (foo) doStuff()

I think doing the same for multi-line expressions (which happen all the time in JSX) would be equally beneficial. Since using prettier, I am getting a lot more syntax errors as I refactor my code because it puts the code on the same lines as the syntax. JSX is the most affected part in my code since it’s expression heavy, and also the most likely to be moved around as you build and refactor a UI.

Some examples

For example, consider this code:

const Slider = ({ isOpen, title, children }) => (
  <div>
    <h1>{title}</h1>
    {isOpen && (
      <div>
        {children}
      </div>
    )}
  </div>
);

Prettier changes it to:

const Slider = ({ isOpen, title, children }) =>
  <div>
    <h1>{title}</h1>
    {isOpen &&
      <div>
        {children}
      </div>}
  </div>;

If I am refactoring and move the <div/> somewhere else, maybe I’m inlining it into a different component, that semi-colon comes along with the div leading to syntax errors or in this case just a printed ; in the UI if you don’t catch it.

semicolon

Additionally, the closing } in the isOpen block gets put on the same line as the closing div, so refactoring that block is more likely to result in accidental syntax errors:

closing-curly

If parens were respected like if block curlies

However, if the parens were respected (just like if block’s curlies) we can just move stuff around w/o spending extra time adjusting syntax.

closing-curly2

semicolon2

Ternaries are also much easier to work with if the parens are respected:

ternary

As compared to not respecting them:

ternary-hard

I am not proposing prettier insert parens and new lines but rather, do what it does today, except when a developer puts a paren + newline in, respect it the same way an if { is respected.

Thanks! 💙💙💙

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:124
  • Comments:29 (14 by maintainers)

github_iconTop GitHub Comments

56reactions
suchipicommented, Jul 5, 2017

I’ve made a PR that:

  • Adds wrapping parens back to JSX Arrow functions (when the JSX is multiline):
// Before
const MyThing = () =>
  <div>
    <span>Hi</span>
  </div>;

// After
const MyThing = () => (
  <div>
    <span>Hi</span>
  </div>
);
  • Uses different formatting for ?: when using JSX
// Before
const MyThing = () =>
  <div>
    {isGood
      ? <div>
          <span>"yeah it's good"</span>
        </div>
      : isOkay ? <div>it's okay</div> : null}
  </div>;

// After:
const MyThing = () => (
  <div>
    {isGood ? (
      <div>
        <span>
          "yeah it's good"
        </span>
      </div>
    ) : isOkay ? (
      <div>it's okay</div>
    ) : (
      null
    )}
  </div>
);
  • Adds wrapping parens when JSX is used in a logical expression:
// Before:
<div> 
  {a || <span />} 
</div>;

// After:
<div>
  {a || (
    <span />
  )}
</div>;
29reactions
ryanflorencecommented, Jun 20, 2017

@vjeux some people can’t use a mouse. Also, respecting parens still allows you to do the same thing you are right now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

function-paren-newline - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
How to insert newlines on argparse help text? - Stack Overflow
Try using RawTextHelpFormatter to preserve all of your formatting: from argparse import RawTextHelpFormatter parser = ArgumentParser(description='test', ...
Read more >
Brace autoformat does not respect file's newline setting
In a C++ project with an .editorconfig file specifying LF newlines, with “Automatic brace completion” and “Automatically format braces when ...
Read more >
Options - Prettier
"preserve" - Respect the input use of quotes in object properties. ... instead of being alone on the next line (does not apply...
Read more >
The Black code style - Black 22.12.0 documentation
Black prefers parentheses over backslashes, and will remove backslashes if found. ... which will align more with black's “try to respect --line-length ...
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