Skip to content

Respect paren + newline #2208

Description

@ryanflorence

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! 💙💙💙

Metadata

Metadata

Assignees

No one assigned

    Labels

    locked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.status:has prIssues with an accompanying pull request. These issues will probably be fixed soon!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions