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.

[JSX] - should prettier keep blank lines after opening and before closing tags?

See original GitHub issue

Prettier removes blank lines at the start and at the end of the blocks:

In

const func = () => {
  
  
  const foo = 1
  
  
  const bar = 2
  
  
  const baz = 3
  
  
}

if (true) {
    
  
  const foo = 1
  
  
  const bar = 2
  
  
  const baz = 3
  
  
}

Out

const func = () => {
  const foo = 1;

  const bar = 2;

  const baz = 3;
};

if (true) {
  const foo = 1;

  const bar = 2;

  const baz = 3;
}

But keeps extra one line in case of JSX tags:

In

const comp = (

  
<div>

  	<div>
  foo
  </div>
  
  
  
  	<div>
  bar
  </div>
  
  	<div>
  baz
  </div>
  
  </div>

)

Out

const comp = (
  <div>

    <div>
      foo
    </div>

    <div>
      bar
    </div>

    <div>
      baz
    </div>

  </div>
);


Example.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vjeuxcommented, Feb 5, 2017

Let’s summon @jlongster to make the final decision on this 😃

1reaction
rattrayalexcommented, Feb 5, 2017

It’s also worth mentioning that the general situation of

[several block-starts in a row] [several sibling blocks] [several block-ends in a row]

is less likely to occur in js code than in jsx. So enforcing the no-opening-newlines and no-closing-newlines rule makes more sense in js than in jsx.

eg; I would prefer this code:

for (i of outer) {
  for (j of inner) {
    for (k of innerer) {

      if (i === j) {
        console.log("i is j!")
      }

      if (j === k) {
        console.log("j is k!")
      }

      if (i === k) {
        console.log("i is k!")
      }
      
    }
  }
}

over this code:

for (i of outer) {
  for (j of inner) {
    for (k of innerer) {
      if (i === j) {
        console.log("i is j!")
      }

      if (j === k) {
        console.log("j is k!")
      }

      if (i === k) {
        console.log("i is k!")
      }    
    }
  }
}

but that happens so infrequently in JS (fortunately) that it’s not worth optimizing for. But it does happen pretty frequently in JSX.

Just my 2c (I’ve already committed the change to adopt this issue’s proposal)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rationale - Prettier
The approach that Prettier takes is to preserve empty lines the way they were in the original source code. There are two additional...
Read more >
Prettier not formatting empty line properly - Stack Overflow
Prettier collapses multiple blank lines into a single blank line. Empty lines at the start and end of blocks (and whole files) are...
Read more >
no-multiple-empty-lines - ESLint - Pluggable JavaScript Linter
This rule aims to reduce the scrolling required when reading through your code. It will warn when the maximum amount of empty lines...
Read more >
Prettier 2.0 “2020” · Prettier 中文网
Prettier had been adding newlines for every HTML template string, which could lead to unexpected whitespace in rendered HTML.
Read more >
VS Code - You don't need a formatting extension (Prettier and ...
You can use the built-in formatters for a number of languages. ... newlineBetweenRules - Separate rulesets by a blank line. css.format.
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