Skip to content

Commit 963267a

Browse files
author
Yannick Croissant
committed
Fix jsx-indent in multi-line logical expressions (fixes #540)
1 parent 1e1c5c6 commit 963267a

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

lib/rules/jsx-indent.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,39 @@ module.exports = {
176176
return startLine !== endLine;
177177
}
178178

179+
/**
180+
* Check if the node is the right member of a logical expression
181+
* @param {ASTNode} node The node to check
182+
* @return {Boolean} true if its the case, false if not
183+
*/
184+
function isRightInLogicalExp(node) {
185+
return (
186+
node.parent &&
187+
node.parent.parent &&
188+
node.parent.parent.type === 'LogicalExpression' &&
189+
node.parent.parent.right === node.parent
190+
);
191+
}
192+
193+
/**
194+
* Check if the node is the parenthesized right member of a logical expression
195+
* @param {ASTNode} node The node to check
196+
* @return {Boolean} true if its the case, false if not
197+
*/
198+
function isParenthesizedInLogicalExp(node) {
199+
var token = sourceCode.getTokenBefore(node);
200+
return isRightInLogicalExp(node) && token.type === 'Punctuator' && token.value === '(';
201+
}
202+
179203
/**
180204
* Check indent for nodes list
181-
* @param {ASTNode[]} nodes list of node objects
205+
* @param {ASTNode} node The node to check
182206
* @param {Number} indent needed indent
183207
* @param {Boolean} excludeCommas skip comma on start of line
184208
*/
185209
function checkNodesIndent(node, indent, excludeCommas) {
186210
var nodeIndent = getNodeIndent(node, false, excludeCommas);
187-
if (nodeIndent !== indent && isNodeFirstInLine(node)) {
211+
if (nodeIndent !== indent && isNodeFirstInLine(node) && !isParenthesizedInLogicalExp(node)) {
188212
report(node, indent, nodeIndent);
189213
}
190214
}
@@ -199,7 +223,7 @@ module.exports = {
199223
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start).parent;
200224
}
201225
var parentElementIndent = getNodeIndent(prevToken);
202-
var indent = prevToken.loc.start.line === node.loc.start.line ? 0 : indentSize;
226+
var indent = prevToken.loc.start.line === node.loc.start.line || isRightInLogicalExp(node) ? 0 : indentSize;
203227
checkNodesIndent(node, parentElementIndent + indent);
204228
},
205229
JSXClosingElement: function(node) {

tests/lib/rules/jsx-indent.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,40 @@ ruleTester.run('jsx-indent', rule, {
133133
].join('\n'),
134134
parserOptions: parserOptions,
135135
options: [2]
136+
}, {
137+
code: [
138+
'{',
139+
' head.title &&',
140+
' <h1>',
141+
' {head.title}',
142+
' </h1>',
143+
'}'
144+
].join('\n'),
145+
parserOptions: parserOptions,
146+
options: [2]
147+
}, {
148+
code: [
149+
'{',
150+
' head.title && (',
151+
' <h1>',
152+
' {head.title}',
153+
' </h1>)',
154+
'}'
155+
].join('\n'),
156+
parserOptions: parserOptions,
157+
options: [2]
158+
}, {
159+
code: [
160+
'{',
161+
' head.title && (',
162+
' <h1>',
163+
' {head.title}',
164+
' </h1>',
165+
' )',
166+
'}'
167+
].join('\n'),
168+
parserOptions: parserOptions,
169+
options: [2]
136170
}],
137171

138172
invalid: [{

0 commit comments

Comments
 (0)