Skip to content

Commit

Permalink
Fix jsx-wrap-multilines ternaries handling (fixes #916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Croissant committed Nov 14, 2016
1 parent fe5594b commit 874a3e3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,27 @@ module.exports = {
return {

VariableDeclarator: function(node) {
if (isEnabled('declaration')) {
check(node.init);
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')) {
check(node.right);
if (!isEnabled('assignment')) {
return;
}
if (node.right.type === 'ConditionalExpression') {
check(node.right.consequent);
check(node.right.alternate);
return;
}
check(node.right);
},

ReturnStatement: function(node) {
Expand Down
70 changes: 70 additions & 0 deletions tests/lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ var RETURN_NO_PAREN = '\
}\
});';

var DECLARATION_TERNARY_SINGLE_LINE = 'var hello = foo ? <p>Hello</p> : <p>Hi</p>;';

var DECLARATION_TERNARY_PAREN = '\
var hello = foo ? (<div>\n\
<p>Hello</p>\n\
</div>) : (<div>\n\
<p>Hi</p>\n\
</div>);';

var DECLARATION_TERNARY_NO_PAREN = '\
var hello = foo ? <div>\n\
<p>Hello</p>\n\
</div> : <div>\n\
<p>Hi</p>\n\
</div>;';

var ASSIGNMENT_TERNARY_SINGLE_LINE = 'var hello; hello = foo ? <p>Hello</p> : <p>Hi</p>;';

var ASSIGNMENT_TERNARY_PAREN = '\
var hello;\n\
hello = foo ? (<div>\n\
<p>Hello</p>\n\
</div>) : (<div>\n\
<p>Hi</p>\n\
</div>);';

var ASSIGNMENT_TERNARY_NO_PAREN = '\
var hello;\n\
hello = foo ? <div>\n\
<p>Hello</p>\n\
</div> : <div>\n\
<p>Hi</p>\n\
</div>;';

var DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;';

var DECLARATION_PAREN = '\
Expand Down Expand Up @@ -91,6 +125,26 @@ ruleTester.run('jsx-wrap-multilines', rule, {
code: RETURN_NO_PAREN,
options: [{return: false}],
parserOptions: parserOptions
}, {
code: DECLARATION_TERNARY_SINGLE_LINE,
parserOptions: parserOptions
}, {
code: DECLARATION_TERNARY_PAREN,
parserOptions: parserOptions
}, {
code: DECLARATION_TERNARY_NO_PAREN,
options: [{declaration: false}],
parserOptions: parserOptions
}, {
code: ASSIGNMENT_TERNARY_SINGLE_LINE,
parserOptions: parserOptions
}, {
code: ASSIGNMENT_TERNARY_PAREN,
parserOptions: parserOptions
}, {
code: ASSIGNMENT_TERNARY_NO_PAREN,
options: [{assignment: false}],
parserOptions: parserOptions
}, {
code: DECLARATION_SINGLE_LINE,
parserOptions: parserOptions
Expand Down Expand Up @@ -127,6 +181,22 @@ ruleTester.run('jsx-wrap-multilines', rule, {
parserOptions: parserOptions,
options: [{return: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: DECLARATION_TERNARY_NO_PAREN,
output: DECLARATION_TERNARY_PAREN,
parserOptions: parserOptions,
errors: [
{message: 'Missing parentheses around multilines JSX'},
{message: 'Missing parentheses around multilines JSX'}
]
}, {
code: ASSIGNMENT_TERNARY_NO_PAREN,
output: ASSIGNMENT_TERNARY_PAREN,
parserOptions: parserOptions,
errors: [
{message: 'Missing parentheses around multilines JSX'},
{message: 'Missing parentheses around multilines JSX'}
]
}, {
code: DECLARATION_NO_PAREN,
output: DECLARATION_PAREN,
Expand Down

0 comments on commit 874a3e3

Please sign in to comment.