Skip to content

Commit b485e28

Browse files
committed
jsx-max-props-per-line: Add when option
If `when` is set to `multiline`, then this rule skips checking entirely if the jsx opening tag is a single line. Defaults `when` to `always` which is the current behavior.
1 parent bd20a79 commit b485e28

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

docs/rules/jsx-max-props-per-line.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The following patterns are not considered warnings:
3535

3636
```js
3737
...
38-
"jsx-max-props-per-line": [<enabled>, { "maximum": <number> }]
38+
"jsx-max-props-per-line": [<enabled>, { "maximum": <number>, "when": <string> }]
3939
...
4040
```
4141

@@ -60,6 +60,28 @@ The following patterns are not considered warnings:
6060
/>;
6161
```
6262

63+
### `when`
64+
65+
Possible values:
66+
- `always` (default) - Always check for max props per line.
67+
- `multiline` - Only check for max props per line when jsx tag spans multiple lines.
68+
69+
The following patterns are considered warnings:
70+
```jsx
71+
// [1, {when: always}]
72+
<Hello firstName="John" lastName="Smith" />
73+
```
74+
75+
The following patterns are not considered warnings:
76+
```jsx
77+
// [1, {when: multiline}]
78+
<Hello firstName="John" lastName="Smith" />
79+
<Hello
80+
firstName="John"
81+
lastName="Smith"
82+
/>
83+
```
84+
6385
## When not to use
6486

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

lib/rules/jsx-max-props-per-line.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ module.exports = {
2323
maximum: {
2424
type: 'integer',
2525
minimum: 1
26+
},
27+
when: {
28+
type: 'string',
29+
enum: ['always', 'multiline']
2630
}
2731
}
2832
}]
@@ -33,6 +37,7 @@ module.exports = {
3337
var sourceCode = context.getSourceCode();
3438
var configuration = context.options[0] || {};
3539
var maximum = configuration.maximum || 1;
40+
var when = configuration.when || 'always';
3641

3742
function getPropName(propNode) {
3843
if (propNode.type === 'JSXSpreadAttribute') {
@@ -47,6 +52,10 @@ module.exports = {
4752
return;
4853
}
4954

55+
if (when === 'multiline' && node.loc.start.line === node.loc.end.line) {
56+
return;
57+
}
58+
5059
var firstProp = node.attributes[0];
5160
var linePartitionedProps = [[firstProp]];
5261

tests/lib/rules/jsx-max-props-per-line.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ var parserOptions = {
2525
var ruleTester = new RuleTester();
2626
ruleTester.run('jsx-max-props-per-line', rule, {
2727
valid: [{
28+
code: '<App />',
29+
parserOptions: parserOptions
30+
}, {
2831
code: '<App foo />',
2932
parserOptions: parserOptions
3033
}, {
3134
code: '<App foo bar />',
3235
options: [{maximum: 2}],
3336
parserOptions: parserOptions
37+
}, {
38+
code: '<App foo bar />',
39+
options: [{when: 'multiline'}],
40+
parserOptions: parserOptions
41+
}, {
42+
code: '<App foo bar baz />',
43+
options: [{maximum: 2, when: 'multiline'}],
44+
parserOptions: parserOptions
3445
}, {
3546
code: '<App {...this.props} bar />',
3647
options: [{maximum: 2}],

0 commit comments

Comments
 (0)