Skip to content

Commit 2bbd79c

Browse files
committed
[jsx-wrap-multilines] Add tests
1 parent ed319dc commit 2bbd79c

File tree

2 files changed

+102
-75
lines changed

2 files changed

+102
-75
lines changed

lib/rules/jsx-wrap-multilines.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const DEFAULTS = {
1414
declaration: true,
1515
assignment: true,
1616
return: true,
17-
arrow: true
17+
arrow: true,
18+
condition: false,
19+
logical: false,
20+
attr: false
1821
};
1922

2023
// ------------------------------------------------------------------------------
@@ -44,6 +47,15 @@ module.exports = {
4447
},
4548
arrow: {
4649
type: 'boolean'
50+
},
51+
condition: {
52+
type: 'boolean'
53+
},
54+
logical: {
55+
type: 'boolean'
56+
},
57+
attr: {
58+
type: 'boolean'
4759
}
4860
},
4961
additionalProperties: false
@@ -76,7 +88,7 @@ module.exports = {
7688
node: node,
7789
message: 'Missing parentheses around multilines JSX',
7890
fix: function(fixer) {
79-
return fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`);
91+
return fixer.replaceText(node, `(${sourceCode.getText(node)})`);
8092
}
8193
});
8294
}
@@ -100,11 +112,6 @@ module.exports = {
100112
if (!isEnabled('declaration')) {
101113
return;
102114
}
103-
if (node.init && node.init.type === 'ConditionalExpression') {
104-
check(node.init.consequent);
105-
check(node.init.alternate);
106-
return;
107-
}
108115
check(node.init);
109116
},
110117

@@ -135,16 +142,20 @@ module.exports = {
135142
},
136143

137144
ConditionalExpression: function(node) {
138-
check(node.consequent);
139-
check(node.alternate);
145+
if (isEnabled('condition')) {
146+
check(node.consequent);
147+
check(node.alternate);
148+
}
140149
},
141150

142151
LogicalExpression: function(node) {
143-
check(node.right);
152+
if (isEnabled('logical')) {
153+
check(node.right);
154+
}
144155
},
145156

146157
JSXAttribute: function (node) {
147-
if (node.value && node.value.type === 'JSXExpressionContainer') {
158+
if (isEnabled('attr') && node.value && node.value.type === 'JSXExpressionContainer') {
148159
check(node.value.expression);
149160
}
150161
}

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

Lines changed: 80 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -52,84 +52,92 @@ const RETURN_NO_PAREN = `
5252
});
5353
`;
5454

55-
const DECLARATION_TERNARY_SINGLE_LINE = 'var hello = foo ? <p>Hello</p> : <p>Hi</p>;';
55+
const DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;';
5656

57-
const DECLARATION_TERNARY_PAREN = `
58-
var hello = foo ? (<div>
57+
const DECLARATION_PAREN = `
58+
var hello = (<div>
5959
<p>Hello</p>
60-
</div>) : (<div>
61-
<p>Hi</p>
6260
</div>);
6361
`;
6462

65-
const DECLARATION_TERNARY_NO_PAREN = `
66-
var hello = foo ? <div>
63+
const DECLARATION_NO_PAREN = `
64+
var hello = <div>
6765
<p>Hello</p>
68-
</div> : <div>
69-
<p>Hi</p>
7066
</div>;
7167
`;
7268

73-
const ASSIGNMENT_TERNARY_SINGLE_LINE = 'var hello; hello = foo ? <p>Hello</p> : <p>Hi</p>;';
69+
const ASSIGNMENT_SINGLE_LINE = 'var hello; hello = <p>Hello</p>;';
7470

75-
const ASSIGNMENT_TERNARY_PAREN = `
71+
const ASSIGNMENT_PAREN = `
7672
var hello;
77-
hello = foo ? (<div>
73+
hello = (<div>
7874
<p>Hello</p>
79-
</div>) : (<div>
80-
<p>Hi</p>
8175
</div>);
8276
`;
8377

84-
const ASSIGNMENT_TERNARY_NO_PAREN = `
78+
const ASSIGNMENT_NO_PAREN = `
8579
var hello;
86-
hello = foo ? <div>
80+
hello = <div>
8781
<p>Hello</p>
88-
</div> : <div>
89-
<p>Hi</p>
9082
</div>;
9183
`;
9284

93-
const DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;';
85+
const ARROW_SINGLE_LINE = 'var hello = () => <p>Hello</p>;';
9486

95-
const DECLARATION_PAREN = `
96-
var hello = (<div>
87+
const ARROW_PAREN = `
88+
var hello = () => (<div>
9789
<p>Hello</p>
9890
</div>);
9991
`;
10092

101-
const DECLARATION_NO_PAREN = `
102-
var hello = <div>
93+
const ARROW_NO_PAREN = `
94+
var hello = () => <div>
10395
<p>Hello</p>
10496
</div>;
10597
`;
10698

107-
const ASSIGNMENT_SINGLE_LINE = 'var hello; hello = <p>Hello</p>;';
99+
const CONDITION_SINGLE_LINE = 'foo ? <p>Hello</p> : null;';
108100

109-
const ASSIGNMENT_PAREN = `
110-
var hello;
111-
hello = (<div>
101+
const CONDITION_PAREN = `
102+
foo ? (<div>
103+
<p>Hello</p>
104+
</div>) : null;
105+
`;
106+
107+
const CONDITION_NO_PAREN = `
108+
foo ? <div>
109+
<p>Hello</p>
110+
</div> : null;
111+
`;
112+
113+
const LOGICAL_SINGLE_LINE = 'foo && <p>Hello</p>;';
114+
115+
const LOGICAL_PAREN = `
116+
foo && (<div>
112117
<p>Hello</p>
113118
</div>);
114119
`;
115120

116-
const ASSIGNMENT_NO_PAREN = `
117-
var hello;
118-
hello = <div>
121+
const LOGICAL_NO_PAREN = `
122+
foo && <div>
119123
<p>Hello</p>
120124
</div>;
121125
`;
122126

123-
const ARROW_SINGLE_LINE = 'var hello = () => <p>Hello</p>;';
127+
const ATTR_SINGLE_LINE = '<div attr={<p>Hello</p>}></div>';
124128

125-
const ARROW_PAREN = `
126-
var hello = () => (<div>
129+
const ATTR_PAREN = `
130+
<div attr={(<div>
131+
<p>Hello</p>
132+
</div>)}>
127133
<p>Hello</p>
128-
</div>);
134+
</div>;
129135
`;
130136

131-
const ARROW_NO_PAREN = `
132-
var hello = () => <div>
137+
const ATTR_NO_PAREN = `
138+
<div attr={<div>
139+
<p>Hello</p>
140+
</div>}>
133141
<p>Hello</p>
134142
</div>;
135143
`;
@@ -149,20 +157,6 @@ ruleTester.run('jsx-wrap-multilines', rule, {
149157
}, {
150158
code: RETURN_NO_PAREN,
151159
options: [{return: false}]
152-
}, {
153-
code: DECLARATION_TERNARY_SINGLE_LINE
154-
}, {
155-
code: DECLARATION_TERNARY_PAREN
156-
}, {
157-
code: DECLARATION_TERNARY_NO_PAREN,
158-
options: [{declaration: false}]
159-
}, {
160-
code: ASSIGNMENT_TERNARY_SINGLE_LINE
161-
}, {
162-
code: ASSIGNMENT_TERNARY_PAREN
163-
}, {
164-
code: ASSIGNMENT_TERNARY_NO_PAREN,
165-
options: [{assignment: false}]
166160
}, {
167161
code: DECLARATION_SINGLE_LINE
168162
}, {
@@ -187,6 +181,27 @@ ruleTester.run('jsx-wrap-multilines', rule, {
187181
}, {
188182
code: ARROW_NO_PAREN,
189183
options: [{arrow: false}]
184+
}, {
185+
code: CONDITION_SINGLE_LINE
186+
}, {
187+
code: CONDITION_NO_PAREN
188+
}, {
189+
code: CONDITION_PAREN,
190+
options: [{condition: true}]
191+
}, {
192+
code: LOGICAL_SINGLE_LINE
193+
}, {
194+
code: LOGICAL_NO_PAREN
195+
}, {
196+
code: LOGICAL_PAREN,
197+
options: [{logical: true}]
198+
}, {
199+
code: ATTR_SINGLE_LINE
200+
}, {
201+
code: ATTR_NO_PAREN
202+
}, {
203+
code: ATTR_PAREN,
204+
options: [{attr: true}]
190205
}
191206
],
192207

@@ -200,20 +215,6 @@ ruleTester.run('jsx-wrap-multilines', rule, {
200215
output: RETURN_PAREN,
201216
options: [{return: true}],
202217
errors: [{message: 'Missing parentheses around multilines JSX'}]
203-
}, {
204-
code: DECLARATION_TERNARY_NO_PAREN,
205-
output: DECLARATION_TERNARY_PAREN,
206-
errors: [
207-
{message: 'Missing parentheses around multilines JSX'},
208-
{message: 'Missing parentheses around multilines JSX'}
209-
]
210-
}, {
211-
code: ASSIGNMENT_TERNARY_NO_PAREN,
212-
output: ASSIGNMENT_TERNARY_PAREN,
213-
errors: [
214-
{message: 'Missing parentheses around multilines JSX'},
215-
{message: 'Missing parentheses around multilines JSX'}
216-
]
217218
}, {
218219
code: DECLARATION_NO_PAREN,
219220
output: DECLARATION_PAREN,
@@ -237,6 +238,21 @@ ruleTester.run('jsx-wrap-multilines', rule, {
237238
output: ARROW_PAREN,
238239
options: [{arrow: true}],
239240
errors: [{message: 'Missing parentheses around multilines JSX'}]
241+
}, {
242+
code: CONDITION_NO_PAREN,
243+
output: CONDITION_PAREN,
244+
options: [{condition: true}],
245+
errors: [{message: 'Missing parentheses around multilines JSX'}]
246+
}, {
247+
code: LOGICAL_NO_PAREN,
248+
output: LOGICAL_PAREN,
249+
options: [{logical: true}],
250+
errors: [{message: 'Missing parentheses around multilines JSX'}]
251+
}, {
252+
code: ATTR_NO_PAREN,
253+
output: ATTR_PAREN,
254+
options: [{attr: true}],
255+
errors: [{message: 'Missing parentheses around multilines JSX'}]
240256
}
241257
]
242258
});

0 commit comments

Comments
 (0)