Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsx-wrap-multilines] Add new nodes for wrap #1384

Merged
merged 13 commits into from
Oct 30, 2017
70 changes: 69 additions & 1 deletion docs/rules/jsx-wrap-multilines.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ Wrapping multiline JSX in parentheses can improve readability and/or convenience

## Rule Details

This rule optionally takes a second parameter in the form of an object, containing places to apply the rule. By default, all the syntax listed below will be checked, but these can be explicitly disabled. Any syntax type missing in the object will follow the default behavior (become enabled).
This rule optionally takes a second parameter in the form of an object, containing places to apply the rule. By default, all the syntax listed below will be checked except the logical expressions and JSX attributes, but these can be explicitly disabled. Any syntax type missing in the object will follow the default behavior (become enabled).

There are the possible syntax available:

* `declaration`
* `assignment`
* `return`
* `arrow`
* `condition`
* `logical` (not enabled by default)
* `attr` (not enabled by default)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be "prop" - in jsx, elements have props, and "attributes" are something HTML elements have (which jsx isn't)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!
I've renamed to prop


The following patterns are considered warnings:

Expand Down Expand Up @@ -118,3 +121,68 @@ var hello = () => (
</div>
);
```

The following patterns are considered warnings when configured `{condition: true}`.

```jsx
<div>
{foo ? <div>
<p>Hello</p>
</div> : null}
</div>
```

The following patterns are not considered warnings when configured `{condition: true}`.

```jsx
<div>
{foo ? (<div>
<p>Hello</p>
</div>) : null}
</div>
```


The following patterns are considered warnings when configured `{logical: true}`.

```jsx
<div>
{foo &&
<div>
<p>Hello World</p>
</div>
}
</div>
```

The following patterns are not considered warnings when configured `{logical: true}`.

```jsx
<div>
{foo &&
(<div>
<p>Hello World</p>
</div>)
}
</div>
```

The following patterns are considered warnings when configured `{attr: true}`.

```jsx
<div attr={<div>
<p>Hello</p>
</div>}>
<p>Hello</p>
</div>;
```

The following patterns are not considered warnings when configured `{attr: true}`.

```jsx
<div attr={(<div>
<p>Hello</p>
</div>)}>
<p>Hello</p>
</div>;
```
43 changes: 32 additions & 11 deletions lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const DEFAULTS = {
declaration: true,
assignment: true,
return: true,
arrow: true
arrow: true,
condition: true,
logical: false,
attr: false
};

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -44,6 +47,15 @@ module.exports = {
},
arrow: {
type: 'boolean'
},
condition: {
type: 'boolean'
},
logical: {
type: 'boolean'
},
attr: {
type: 'boolean'
}
},
additionalProperties: false
Expand Down Expand Up @@ -100,23 +112,13 @@ module.exports = {
if (!isEnabled('declaration')) {
return;
}
if (node.init && node.init.type === 'ConditionalExpression') {
check(node.init.consequent);
check(node.init.alternate);
return;
}
check(node.init);
},

AssignmentExpression: function(node) {
if (!isEnabled('assignment')) {
return;
}
if (node.right.type === 'ConditionalExpression') {
check(node.right.consequent);
check(node.right.alternate);
return;
}
check(node.right);
},

Expand All @@ -132,6 +134,25 @@ module.exports = {
if (isEnabled('arrow') && arrowBody.type !== 'BlockStatement') {
check(arrowBody);
}
},

ConditionalExpression: function(node) {
if (isEnabled('condition')) {
check(node.consequent);
check(node.alternate);
}
},

LogicalExpression: function(node) {
if (isEnabled('logical')) {
check(node.right);
}
},

JSXAttribute: function (node) {
if (isEnabled('attr') && node.value && node.value.type === 'JSXExpressionContainer') {
check(node.value.expression);
}
}
};
}
Expand Down
101 changes: 99 additions & 2 deletions tests/lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,68 @@ const ARROW_NO_PAREN = `
</div>;
`;

const CONDITION_SINGLE_LINE = 'foo ? <p>Hello</p> : null;';

const CONDITION_PAREN = `
<div>
{foo ? (<div>
<p>Hello</p>
</div>) : null}
</div>
`;

const CONDITION_NO_PAREN = `
<div>
{foo ? <div>
<p>Hello</p>
</div> : null}
</div>
`;

const LOGICAL_SINGLE_LINE = 'foo && <p>Hello</p>;';

const LOGICAL_PAREN = `
<div>
{foo &&
(<div>
<p>Hello World</p>
</div>)
}
</div>
`;

const LOGICAL_NO_PAREN = `
<div>
{foo &&
<div>
<p>Hello World</p>
</div>
}
</div>
`;

const ATTR_SINGLE_LINE = '<div attr={<p>Hello</p>}></div>';

const ATTR_PAREN = `
<div attr={
(<div>
<p>Hello</p>
</div>)
}>
<p>Hello</p>
</div>
`;

const ATTR_NO_PAREN = `
<div attr={
<div>
<p>Hello</p>
</div>
}>
<p>Hello</p>
</div>
`;

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand All @@ -155,14 +217,14 @@ ruleTester.run('jsx-wrap-multilines', rule, {
code: DECLARATION_TERNARY_PAREN
}, {
code: DECLARATION_TERNARY_NO_PAREN,
options: [{declaration: false}]
options: [{condition: false}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this option need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case checked the condition in declaration. Now checking the condition is separated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean that "declaration: true" now doesn't check as much as it used to? That would make this a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a broken change for declarations. But now the logic of checking is better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we could avoid making it breaking?

Like if condition is absent, it uses the old behavior, but if it's present (true or false) then it uses the new behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can do it now. But it is not very good, so I suggest to do this only as temporary solution, and separate checking condition in major release

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely we'd want to separate that in the next major - but for now, we'd be able to add these new options as semver-minor.

}, {
code: ASSIGNMENT_TERNARY_SINGLE_LINE
}, {
code: ASSIGNMENT_TERNARY_PAREN
}, {
code: ASSIGNMENT_TERNARY_NO_PAREN,
options: [{assignment: false}]
options: [{condition: false}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this option need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case checked the condition in assignment. Now checking the condition is separated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, does that mean that "assignment: true" now doesn't check as much as it used to? That would make this a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a broken change for assignments too

}, {
code: DECLARATION_SINGLE_LINE
}, {
Expand All @@ -187,6 +249,27 @@ ruleTester.run('jsx-wrap-multilines', rule, {
}, {
code: ARROW_NO_PAREN,
options: [{arrow: false}]
}, {
code: CONDITION_SINGLE_LINE
}, {
code: CONDITION_NO_PAREN,
options: [{condition: false}]
}, {
code: CONDITION_PAREN
}, {
code: LOGICAL_SINGLE_LINE
}, {
code: LOGICAL_NO_PAREN
}, {
code: LOGICAL_PAREN,
options: [{logical: true}]
}, {
code: ATTR_SINGLE_LINE
}, {
code: ATTR_NO_PAREN
}, {
code: ATTR_PAREN,
options: [{attr: true}]
}
],

Expand Down Expand Up @@ -237,6 +320,20 @@ ruleTester.run('jsx-wrap-multilines', rule, {
output: ARROW_PAREN,
options: [{arrow: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: CONDITION_NO_PAREN,
output: CONDITION_PAREN,
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: LOGICAL_NO_PAREN,
output: LOGICAL_PAREN,
options: [{logical: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: ATTR_NO_PAREN,
output: ATTR_PAREN,
options: [{attr: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}
]
});