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.

Change HTML/JSX formatting to have one attribute/prop per line

See original GitHub issue

Similar to what was done for #3847 I think it best to break some of the discussion from #3101 into a new issue that people can 👍 or 👎.

I’m proposing that for HTML and JSX to have Prettier always have one attribute/prop per line (and thus not respect the developer’s original preference). Code would then be formatted as shown below. This is in contrast to the current behaviour where we fit as much as possible with the print-width.

Expected behavior:

<MyComponent lorem="1"/>

<MyComponent
  lorem="1"
  ipsum="2"
/>

<MyComponent
  lorem="1"
  ipsum="2"
  dolor="3"
/>

This suggestion for one attribute/prop per line was proposed several times in the discussion of #3101 but I think it is clearer if this is pulled into it’s own proposal. The original proposal in #3101 is that Prettier would add an option to preserve original formatting which, while I agree with the author with the style that they want, I don’t think a) an option, nor b) preserving original formatting follows the aims for Prettier (see also #2068). Instead I think the aims of #3101 are better served by this proposal to ignore the print-width and always place attributes/props on new lines (assuming that there is more than one).

This style appears to be the most common formatting for Angular, Vue and React from what I can tell. It style appears to be the style enforced by the rules:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:624
  • Comments:107 (18 by maintainers)

github_iconTop GitHub Comments

178reactions
ghostcommented, Nov 27, 2018

The point I am trying to make is that the vertical space matters. Wasting it (too much) has negative impact on code manageability.

On the other hand, many diff tools are line-based. So by putting each attribute on it’s own line, we reduce the number of conflicts a team has to solve.

From the example above consider

-     <ui-currency v-model="income.business.incomePrevYear" :minInclusive="0" required key="incomePrevYear">Last year income</ui-currency>
+     <ui-currency v-model="income.business.incomePrevYear" :minInclusive="1" required key="incomePrevYear">Last year income</ui-currency>

vs.

    <ui-currency
      v-model="income.business.incomePrevYear"
-     :minInclusive="0"
+     :minInclusive="1"
      required
      key="incomePrevYear"
    >Last year income</ui-currency>
102reactions
mixncommented, May 28, 2021

1520596752_FeelsBadMan

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prettier react/jsx-max-props-per-line format with VSCode
I've try to move the react/jsx-max-props-per-line to ESLint rules, but no result too. No change. Anyone can help me ? javascript · reactjs ......
Read more >
Prettier 2.6: new singleAttributePerLine option and new ...
This release includes a new singleAttributePerLine option. This is an option to print only one attribute per line in Vue SFC templates, HTML...
Read more >
Prettier react/jsx-max-props-per-line format with VSCode ...
Since Prettier 2.6.0 you can enforce single attribute per line in HTML or JSX if you add the following to your config: "singleAttributePerLine":...
Read more >
JSX In Depth - React
Specifying The React Element Type ... The first part of a JSX tag determines the type of the React element. Capitalized types indicate...
Read more >
How To Customize React Components with Props
Props are arguments that you provide to a JSX element in a React application. They look like standard HTML props, but they aren't...
Read more >

github_iconTop Related Medium Post

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