Skip to content

Commit 13824c6

Browse files
kentoryannickcr
authored andcommitted
Add multiline-multiprop option to jsx-first-prop-new-line (fixes #878)
1 parent 6620a0a commit 13824c6

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ Ensure correct position of the first property.
77
This rule checks whether the first property of all JSX elements is correctly placed. There are three possible configurations:
88
* `always`: The first property should always be placed on a new line.
99
* `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.
10-
* `multiline`: The first property should always be placed on a new line when the properties are spread over multiple lines.
10+
* `multiline`: The first property should always be placed on a new line when the JSX tag takes up multiple lines.
11+
* `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.
1112

1213
The following patterns are considered warnings when configured `"always"`:
1314

14-
```js
15+
```jsx
1516
<Hello personal={true} />
1617

1718
<Hello personal={true}
@@ -21,7 +22,7 @@ The following patterns are considered warnings when configured `"always"`:
2122

2223
The following patterns are not considered warnings when configured `"always"`:
2324

24-
```js
25+
```jsx
2526
<Hello
2627
personal />
2728

@@ -32,7 +33,7 @@ The following patterns are not considered warnings when configured `"always"`:
3233

3334
The following patterns are considered warnings when configured `"never"`:
3435

35-
```js
36+
```jsx
3637
<Hello
3738
personal />
3839

@@ -43,7 +44,7 @@ The following patterns are considered warnings when configured `"never"`:
4344

4445
The following patterns are not considered warnings when configured `"never"`:
4546

46-
```js
47+
```jsx
4748
<Hello personal={true} />
4849

4950
<Hello personal={true}
@@ -53,14 +54,19 @@ The following patterns are not considered warnings when configured `"never"`:
5354

5455
The following patterns are considered warnings when configured `"multiline"`:
5556

56-
```js
57+
```jsx
5758
<Hello personal
5859
prop />
5960
```
6061

62+
```jsx
63+
<Hello foo={{
64+
}} />
65+
```
66+
6167
The following patterns are not considered warnings when configured `"multiline"`:
6268

63-
```js
69+
```jsx
6470
<Hello personal={true} />
6571

6672
<Hello
@@ -69,6 +75,27 @@ The following patterns are not considered warnings when configured `"multiline"`
6975
/>
7076
```
7177

78+
The following patterns are considered warnings when configured `"multiline-multiprop"`:
79+
80+
```jsx
81+
<Hello foo={{
82+
}}
83+
bar />
84+
```
85+
86+
The following patterns are not considered warnings when configured `"multiline-multiprop"`:
87+
88+
```jsx
89+
<Hello foo={{
90+
}} />
91+
92+
<Hello
93+
foo={{
94+
}}
95+
bar
96+
/>
97+
```
98+
7299
## When not to use
73100

74101
If you are not using JSX then you can disable this rule.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
},
1818

1919
schema: [{
20-
enum: ['always', 'never', 'multiline']
20+
enum: ['always', 'never', 'multiline', 'multiline-multiprop']
2121
}]
2222
},
2323

@@ -30,7 +30,11 @@ module.exports = {
3030

3131
return {
3232
JSXOpeningElement: function (node) {
33-
if ((configuration === 'multiline' && isMultilineJSX(node)) || (configuration === 'always')) {
33+
if (
34+
(configuration === 'multiline' && isMultilineJSX(node)) ||
35+
(configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1) ||
36+
(configuration === 'always')
37+
) {
3438
node.attributes.forEach(function(decl) {
3539
if (decl.loc.start.line === node.loc.start.line) {
3640
context.report({

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,39 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
9090
options: ['multiline'],
9191
parser: parserOptions
9292
},
93+
{
94+
code: [
95+
'<Foo bar />'
96+
].join('\n'),
97+
options: ['multiline-multiprop'],
98+
parser: parserOptions
99+
},
100+
{
101+
code: [
102+
'<Foo bar baz />'
103+
].join('\n'),
104+
options: ['multiline-multiprop'],
105+
parser: parserOptions
106+
},
107+
{
108+
code: [
109+
'<Foo prop={{',
110+
'}} />'
111+
].join('\n'),
112+
options: ['multiline-multiprop'],
113+
parser: parserOptions
114+
},
115+
{
116+
code: [
117+
'<Foo ',
118+
' foo={{',
119+
' }}',
120+
' bar',
121+
'/>'
122+
].join('\n'),
123+
options: ['multiline-multiprop'],
124+
parser: parserOptions
125+
},
93126
{
94127
code: '<Foo />',
95128
options: ['always'],
@@ -144,6 +177,24 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
144177
options: ['never'],
145178
errors: [{message: 'Property should be placed on the same line as the component declaration'}],
146179
parser: parserOptions
180+
},
181+
{
182+
code: [
183+
'<Foo prop={{',
184+
'}} />'
185+
].join('\n'),
186+
options: ['multiline'],
187+
errors: [{message: 'Property should be placed on a new line'}],
188+
parser: parserOptions
189+
},
190+
{
191+
code: [
192+
'<Foo bar={{',
193+
'}} baz />'
194+
].join('\n'),
195+
options: ['multiline-multiprop'],
196+
errors: [{message: 'Property should be placed on a new line'}],
197+
parser: parserOptions
147198
}
148199
]
149200
});

0 commit comments

Comments
 (0)