Skip to content

Commit 45184ef

Browse files
haydncomleyljharb
authored andcommitted
[New] jsx-first-prop-new-line: add multiprop option
1 parent 3ab81d2 commit 45184ef

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

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

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

1617
[#3538]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3538
18+
[#3533]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3533
1719
[#3530]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3530
1820
[#3529]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3529
1921

docs/rules/jsx-first-prop-new-line.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This rule checks whether the first property of all JSX elements is correctly pla
1515
- `always`: The first property should always be placed on a new line.
1616
- `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.
1717
- `multiline`: The first property should always be placed on a new line when the JSX tag takes up multiple lines.
18+
- `multiprop`: The first property should never be placed on a new line unless there are multiple properties.
1819
- `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.
1920

2021
Examples of **incorrect** code for this rule, when configured with `"always"`:

lib/rules/jsx-first-prop-new-line.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
messages,
3131

3232
schema: [{
33-
enum: ['always', 'never', 'multiline', 'multiline-multiprop'],
33+
enum: ['always', 'never', 'multiline', 'multiline-multiprop', 'multiprop'],
3434
}],
3535
},
3636

@@ -46,6 +46,7 @@ module.exports = {
4646
if (
4747
(configuration === 'multiline' && isMultilineJSX(node))
4848
|| (configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1)
49+
|| (configuration === 'multiprop' && node.attributes.length > 1)
4950
|| (configuration === 'always')
5051
) {
5152
node.attributes.some((decl) => {
@@ -59,7 +60,10 @@ module.exports = {
5960
}
6061
return true;
6162
});
62-
} else if (configuration === 'never' && node.attributes.length > 0) {
63+
} else if (
64+
(configuration === 'never' && node.attributes.length > 0)
65+
|| (configuration === 'multiprop' && isMultilineJSX(node) && node.attributes.length <= 1)
66+
) {
6367
const firstNode = node.attributes[0];
6468
if (node.loc.start.line < firstNode.loc.start.line) {
6569
report(context, messages.propOnSameLine, 'propOnSameLine', {

tests/lib/rules/jsx-first-prop-new-line.js

+51
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
139139
`,
140140
options: ['always'],
141141
},
142+
{
143+
code: `
144+
<Foo />
145+
`,
146+
options: ['multiprop'],
147+
},
148+
{
149+
code: `
150+
<Foo bar />
151+
`,
152+
options: ['multiprop'],
153+
},
154+
{
155+
code: `
156+
<Foo {...this.props} />
157+
`,
158+
options: ['multiprop'],
159+
},
142160
]),
143161

144162
invalid: parsers.all([
@@ -209,5 +227,38 @@ bar={{
209227
options: ['multiline-multiprop'],
210228
errors: [{ messageId: 'propOnNewLine' }],
211229
},
230+
{
231+
code: `
232+
<Foo propOne="one" propTwo="two" />
233+
`,
234+
output: `
235+
<Foo
236+
propOne="one" propTwo="two" />
237+
`,
238+
options: ['multiprop'],
239+
errors: [{ messageId: 'propOnNewLine' }],
240+
},
241+
{
242+
code: `
243+
<Foo
244+
bar />
245+
`,
246+
output: `
247+
<Foo bar />
248+
`,
249+
options: ['multiprop'],
250+
errors: [{ messageId: 'propOnSameLine' }],
251+
},
252+
{
253+
code: `
254+
<Foo
255+
{...this.props} />
256+
`,
257+
output: `
258+
<Foo {...this.props} />
259+
`,
260+
options: ['multiprop'],
261+
errors: [{ messageId: 'propOnSameLine' }],
262+
},
212263
]),
213264
});

0 commit comments

Comments
 (0)