-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from 7 commits
eb039b8
04f3b9f
ed319dc
2bbd79c
21d9a20
657e77e
252c3ba
cea8305
a05c589
dc729fa
41a29c6
ccb9132
cfcb2bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 conditional expressions, 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` (not enabled by default) | ||
* `logical` (not enabled by default) | ||
* `attr` (not enabled by default) | ||
|
||
The following patterns are considered warnings: | ||
|
||
|
@@ -118,3 +121,62 @@ 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> not | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
``` | ||
|
||
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>; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,44 +52,6 @@ const RETURN_NO_PAREN = ` | |
}); | ||
`; | ||
|
||
const DECLARATION_TERNARY_SINGLE_LINE = 'var hello = foo ? <p>Hello</p> : <p>Hi</p>;'; | ||
|
||
const DECLARATION_TERNARY_PAREN = ` | ||
var hello = foo ? (<div> | ||
<p>Hello</p> | ||
</div>) : (<div> | ||
<p>Hi</p> | ||
</div>); | ||
`; | ||
|
||
const DECLARATION_TERNARY_NO_PAREN = ` | ||
var hello = foo ? <div> | ||
<p>Hello</p> | ||
</div> : <div> | ||
<p>Hi</p> | ||
</div>; | ||
`; | ||
|
||
const ASSIGNMENT_TERNARY_SINGLE_LINE = 'var hello; hello = foo ? <p>Hello</p> : <p>Hi</p>;'; | ||
|
||
const ASSIGNMENT_TERNARY_PAREN = ` | ||
var hello; | ||
hello = foo ? (<div> | ||
<p>Hello</p> | ||
</div>) : (<div> | ||
<p>Hi</p> | ||
</div>); | ||
`; | ||
|
||
const ASSIGNMENT_TERNARY_NO_PAREN = ` | ||
var hello; | ||
hello = foo ? <div> | ||
<p>Hello</p> | ||
</div> : <div> | ||
<p>Hi</p> | ||
</div>; | ||
`; | ||
|
||
const DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;'; | ||
|
||
const DECLARATION_PAREN = ` | ||
|
@@ -134,6 +96,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 | ||
// ------------------------------------------------------------------------------ | ||
|
@@ -149,20 +173,6 @@ ruleTester.run('jsx-wrap-multilines', rule, { | |
}, { | ||
code: RETURN_NO_PAREN, | ||
options: [{return: false}] | ||
}, { | ||
code: DECLARATION_TERNARY_SINGLE_LINE | ||
}, { | ||
code: DECLARATION_TERNARY_PAREN | ||
}, { | ||
code: DECLARATION_TERNARY_NO_PAREN, | ||
options: [{declaration: false}] | ||
}, { | ||
code: ASSIGNMENT_TERNARY_SINGLE_LINE | ||
}, { | ||
code: ASSIGNMENT_TERNARY_PAREN | ||
}, { | ||
code: ASSIGNMENT_TERNARY_NO_PAREN, | ||
options: [{assignment: false}] | ||
}, { | ||
code: DECLARATION_SINGLE_LINE | ||
}, { | ||
|
@@ -187,6 +197,27 @@ ruleTester.run('jsx-wrap-multilines', rule, { | |
}, { | ||
code: ARROW_NO_PAREN, | ||
options: [{arrow: false}] | ||
}, { | ||
code: CONDITION_SINGLE_LINE | ||
}, { | ||
code: CONDITION_NO_PAREN | ||
}, { | ||
code: CONDITION_PAREN, | ||
options: [{condition: true}] | ||
}, { | ||
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}] | ||
} | ||
], | ||
|
||
|
@@ -200,20 +231,6 @@ ruleTester.run('jsx-wrap-multilines', rule, { | |
output: RETURN_PAREN, | ||
options: [{return: true}], | ||
errors: [{message: 'Missing parentheses around multilines JSX'}] | ||
}, { | ||
code: DECLARATION_TERNARY_NO_PAREN, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do these tests need to be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same tests as tests for condition rule. I can return them if you think that they are useful. The more tests the better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For semver-minor PRs, it makes me happier when tests are only added :-) |
||
output: DECLARATION_TERNARY_PAREN, | ||
errors: [ | ||
{message: 'Missing parentheses around multilines JSX'}, | ||
{message: 'Missing parentheses around multilines JSX'} | ||
] | ||
}, { | ||
code: ASSIGNMENT_TERNARY_NO_PAREN, | ||
output: ASSIGNMENT_TERNARY_PAREN, | ||
errors: [ | ||
{message: 'Missing parentheses around multilines JSX'}, | ||
{message: 'Missing parentheses around multilines JSX'} | ||
] | ||
}, { | ||
code: DECLARATION_NO_PAREN, | ||
output: DECLARATION_PAREN, | ||
|
@@ -237,6 +254,21 @@ 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, | ||
options: [{condition: true}], | ||
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'}] | ||
} | ||
] | ||
}); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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