Skip to content

Commit 874a3e3

Browse files
author
Yannick Croissant
committed
Fix jsx-wrap-multilines ternaries handling (fixes #916)
1 parent fe5594b commit 874a3e3

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

lib/rules/jsx-wrap-multilines.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,27 @@ module.exports = {
9292
return {
9393

9494
VariableDeclarator: function(node) {
95-
if (isEnabled('declaration')) {
96-
check(node.init);
95+
if (!isEnabled('declaration')) {
96+
return;
9797
}
98+
if (node.init && node.init.type === 'ConditionalExpression') {
99+
check(node.init.consequent);
100+
check(node.init.alternate);
101+
return;
102+
}
103+
check(node.init);
98104
},
99105

100106
AssignmentExpression: function(node) {
101-
if (isEnabled('assignment')) {
102-
check(node.right);
107+
if (!isEnabled('assignment')) {
108+
return;
109+
}
110+
if (node.right.type === 'ConditionalExpression') {
111+
check(node.right.consequent);
112+
check(node.right.alternate);
113+
return;
103114
}
115+
check(node.right);
104116
},
105117

106118
ReturnStatement: function(node) {

tests/lib/rules/jsx-wrap-multilines.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ var RETURN_NO_PAREN = '\
4747
}\
4848
});';
4949

50+
var DECLARATION_TERNARY_SINGLE_LINE = 'var hello = foo ? <p>Hello</p> : <p>Hi</p>;';
51+
52+
var DECLARATION_TERNARY_PAREN = '\
53+
var hello = foo ? (<div>\n\
54+
<p>Hello</p>\n\
55+
</div>) : (<div>\n\
56+
<p>Hi</p>\n\
57+
</div>);';
58+
59+
var DECLARATION_TERNARY_NO_PAREN = '\
60+
var hello = foo ? <div>\n\
61+
<p>Hello</p>\n\
62+
</div> : <div>\n\
63+
<p>Hi</p>\n\
64+
</div>;';
65+
66+
var ASSIGNMENT_TERNARY_SINGLE_LINE = 'var hello; hello = foo ? <p>Hello</p> : <p>Hi</p>;';
67+
68+
var ASSIGNMENT_TERNARY_PAREN = '\
69+
var hello;\n\
70+
hello = foo ? (<div>\n\
71+
<p>Hello</p>\n\
72+
</div>) : (<div>\n\
73+
<p>Hi</p>\n\
74+
</div>);';
75+
76+
var ASSIGNMENT_TERNARY_NO_PAREN = '\
77+
var hello;\n\
78+
hello = foo ? <div>\n\
79+
<p>Hello</p>\n\
80+
</div> : <div>\n\
81+
<p>Hi</p>\n\
82+
</div>;';
83+
5084
var DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;';
5185

5286
var DECLARATION_PAREN = '\
@@ -91,6 +125,26 @@ ruleTester.run('jsx-wrap-multilines', rule, {
91125
code: RETURN_NO_PAREN,
92126
options: [{return: false}],
93127
parserOptions: parserOptions
128+
}, {
129+
code: DECLARATION_TERNARY_SINGLE_LINE,
130+
parserOptions: parserOptions
131+
}, {
132+
code: DECLARATION_TERNARY_PAREN,
133+
parserOptions: parserOptions
134+
}, {
135+
code: DECLARATION_TERNARY_NO_PAREN,
136+
options: [{declaration: false}],
137+
parserOptions: parserOptions
138+
}, {
139+
code: ASSIGNMENT_TERNARY_SINGLE_LINE,
140+
parserOptions: parserOptions
141+
}, {
142+
code: ASSIGNMENT_TERNARY_PAREN,
143+
parserOptions: parserOptions
144+
}, {
145+
code: ASSIGNMENT_TERNARY_NO_PAREN,
146+
options: [{assignment: false}],
147+
parserOptions: parserOptions
94148
}, {
95149
code: DECLARATION_SINGLE_LINE,
96150
parserOptions: parserOptions
@@ -127,6 +181,22 @@ ruleTester.run('jsx-wrap-multilines', rule, {
127181
parserOptions: parserOptions,
128182
options: [{return: true}],
129183
errors: [{message: 'Missing parentheses around multilines JSX'}]
184+
}, {
185+
code: DECLARATION_TERNARY_NO_PAREN,
186+
output: DECLARATION_TERNARY_PAREN,
187+
parserOptions: parserOptions,
188+
errors: [
189+
{message: 'Missing parentheses around multilines JSX'},
190+
{message: 'Missing parentheses around multilines JSX'}
191+
]
192+
}, {
193+
code: ASSIGNMENT_TERNARY_NO_PAREN,
194+
output: ASSIGNMENT_TERNARY_PAREN,
195+
parserOptions: parserOptions,
196+
errors: [
197+
{message: 'Missing parentheses around multilines JSX'},
198+
{message: 'Missing parentheses around multilines JSX'}
199+
]
130200
}, {
131201
code: DECLARATION_NO_PAREN,
132202
output: DECLARATION_PAREN,

0 commit comments

Comments
 (0)