Skip to content

Commit

Permalink
[New] jsx-first-prop-new-line: add multiprop option
Browse files Browse the repository at this point in the history
  • Loading branch information
haydncomley authored and ljharb committed Feb 15, 2023
1 parent 3ab81d2 commit 45184ef
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,13 +7,15 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Added
* [`display-name`]: add `checkContextObjects` option ([#3529][] @JulesBlm)
* [`jsx-first-prop-new-line`]: add `multiprop` option ([#3533][] @haydncomley)

### Fixed
* [`no-array-index-key`]: consider flatMap ([#3530][] @k-yle)
* [`jsx-curly-brace-presence`]: handle single and only expression template literals ([#3538][] @taozhou-glean)
* [`no-unknown-property`]: allow `onLoad` on `source` (@ljharb)

[#3538]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3538
[#3533]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3533
[#3530]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3530
[#3529]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3529

Expand Down
1 change: 1 addition & 0 deletions docs/rules/jsx-first-prop-new-line.md
Expand Up @@ -15,6 +15,7 @@ This rule checks whether the first property of all JSX elements is correctly pla
- `always`: The first property should always be placed on a new line.
- `never` : The first property should never be placed on a new line, e.g. should always be on the same line as the Component opening tag.
- `multiline`: The first property should always be placed on a new line when the JSX tag takes up multiple lines.
- `multiprop`: The first property should never be placed on a new line unless there are multiple properties.
- `multiline-multiprop`: The first property should always be placed on a new line if the JSX tag takes up multiple lines and there are multiple properties. This is the `default` value.

Examples of **incorrect** code for this rule, when configured with `"always"`:
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/jsx-first-prop-new-line.js
Expand Up @@ -30,7 +30,7 @@ module.exports = {
messages,

schema: [{
enum: ['always', 'never', 'multiline', 'multiline-multiprop'],
enum: ['always', 'never', 'multiline', 'multiline-multiprop', 'multiprop'],
}],
},

Expand All @@ -46,6 +46,7 @@ module.exports = {
if (
(configuration === 'multiline' && isMultilineJSX(node))
|| (configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1)
|| (configuration === 'multiprop' && node.attributes.length > 1)
|| (configuration === 'always')
) {
node.attributes.some((decl) => {
Expand All @@ -59,7 +60,10 @@ module.exports = {
}
return true;
});
} else if (configuration === 'never' && node.attributes.length > 0) {
} else if (
(configuration === 'never' && node.attributes.length > 0)
|| (configuration === 'multiprop' && isMultilineJSX(node) && node.attributes.length <= 1)
) {
const firstNode = node.attributes[0];
if (node.loc.start.line < firstNode.loc.start.line) {
report(context, messages.propOnSameLine, 'propOnSameLine', {
Expand Down
51 changes: 51 additions & 0 deletions tests/lib/rules/jsx-first-prop-new-line.js
Expand Up @@ -139,6 +139,24 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
`,
options: ['always'],
},
{
code: `
<Foo />
`,
options: ['multiprop'],
},
{
code: `
<Foo bar />
`,
options: ['multiprop'],
},
{
code: `
<Foo {...this.props} />
`,
options: ['multiprop'],
},
]),

invalid: parsers.all([
Expand Down Expand Up @@ -209,5 +227,38 @@ bar={{
options: ['multiline-multiprop'],
errors: [{ messageId: 'propOnNewLine' }],
},
{
code: `
<Foo propOne="one" propTwo="two" />
`,
output: `
<Foo
propOne="one" propTwo="two" />
`,
options: ['multiprop'],
errors: [{ messageId: 'propOnNewLine' }],
},
{
code: `
<Foo
bar />
`,
output: `
<Foo bar />
`,
options: ['multiprop'],
errors: [{ messageId: 'propOnSameLine' }],
},
{
code: `
<Foo
{...this.props} />
`,
output: `
<Foo {...this.props} />
`,
options: ['multiprop'],
errors: [{ messageId: 'propOnSameLine' }],
},
]),
});

0 comments on commit 45184ef

Please sign in to comment.