Skip to content

Commit

Permalink
jsx-max-props-per-line: Add when option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kentor committed Jan 29, 2017
1 parent bd20a79 commit b485e28
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
24 changes: 23 additions & 1 deletion docs/rules/jsx-max-props-per-line.md
Expand Up @@ -35,7 +35,7 @@ The following patterns are not considered warnings:

```js
...
"jsx-max-props-per-line": [<enabled>, { "maximum": <number> }]
"jsx-max-props-per-line": [<enabled>, { "maximum": <number>, "when": <string> }]
...
```

Expand All @@ -60,6 +60,28 @@ The following patterns are not considered warnings:
/>;
```

### `when`

Possible values:
- `always` (default) - Always check for max props per line.
- `multiline` - Only check for max props per line when jsx tag spans multiple lines.

The following patterns are considered warnings:
```jsx
// [1, {when: always}]
<Hello firstName="John" lastName="Smith" />
```

The following patterns are not considered warnings:
```jsx
// [1, {when: multiline}]
<Hello firstName="John" lastName="Smith" />
<Hello
firstName="John"
lastName="Smith"
/>
```

## When not to use

If you are not using JSX then you can disable this rule.
9 changes: 9 additions & 0 deletions lib/rules/jsx-max-props-per-line.js
Expand Up @@ -23,6 +23,10 @@ module.exports = {
maximum: {
type: 'integer',
minimum: 1
},
when: {
type: 'string',
enum: ['always', 'multiline']
}
}
}]
Expand All @@ -33,6 +37,7 @@ module.exports = {
var sourceCode = context.getSourceCode();
var configuration = context.options[0] || {};
var maximum = configuration.maximum || 1;
var when = configuration.when || 'always';

function getPropName(propNode) {
if (propNode.type === 'JSXSpreadAttribute') {
Expand All @@ -47,6 +52,10 @@ module.exports = {
return;
}

if (when === 'multiline' && node.loc.start.line === node.loc.end.line) {
return;
}

var firstProp = node.attributes[0];
var linePartitionedProps = [[firstProp]];

Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/jsx-max-props-per-line.js
Expand Up @@ -25,12 +25,23 @@ var parserOptions = {
var ruleTester = new RuleTester();
ruleTester.run('jsx-max-props-per-line', rule, {
valid: [{
code: '<App />',
parserOptions: parserOptions
}, {
code: '<App foo />',
parserOptions: parserOptions
}, {
code: '<App foo bar />',
options: [{maximum: 2}],
parserOptions: parserOptions
}, {
code: '<App foo bar />',
options: [{when: 'multiline'}],
parserOptions: parserOptions
}, {
code: '<App foo bar baz />',
options: [{maximum: 2, when: 'multiline'}],
parserOptions: parserOptions
}, {
code: '<App {...this.props} bar />',
options: [{maximum: 2}],
Expand Down

0 comments on commit b485e28

Please sign in to comment.