Skip to content

Commit 059dd68

Browse files
committed
[Fix] jsx-indent: Fix false positive when a jsx element is the last statement within a do expression
Fixes #2199.
1 parent 752de70 commit 059dd68

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

lib/rules/jsx-indent.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,56 @@ module.exports = {
195195
);
196196
}
197197

198+
/**
199+
* Check if the node is the last statement within a do expression
200+
* @param {ASTNode} node The node to check
201+
* @return {Boolean} true if its the case, false if not
202+
*/
203+
function isLastExpInDoExp(node) {
204+
/*
205+
Detect the structure below:
206+
DoExpression {
207+
body: (
208+
BlockStatement {
209+
body: [
210+
(...),
211+
ExpressionStatement {
212+
body: (
213+
JSXElement {
214+
openingElement: (node)
215+
}
216+
)
217+
}
218+
]
219+
}
220+
)
221+
}
222+
*/
223+
const isInExpStmt = (
224+
node.parent &&
225+
node.parent.parent &&
226+
node.parent.parent.type === 'ExpressionStatement'
227+
);
228+
if (!isInExpStmt) {
229+
return false;
230+
}
231+
232+
const expStmt = node.parent.parent;
233+
const isInBlockStmtWithinDoExp = (
234+
expStmt.parent &&
235+
expStmt.parent.type === 'BlockStatement' &&
236+
expStmt.parent.parent &&
237+
expStmt.parent.parent.type === 'DoExpression'
238+
);
239+
if (!isInBlockStmtWithinDoExp) {
240+
return false;
241+
}
242+
243+
const blockStmt = expStmt.parent;
244+
const blockStmtLastElm = blockStmt.body[blockStmt.body.length - 1];
245+
return blockStmtLastElm === expStmt;
246+
}
247+
198248
/**
199249
* Check indent for nodes list
200250
* @param {ASTNode} node The node to check
@@ -239,7 +289,8 @@ module.exports = {
239289
const indent = (
240290
prevToken.loc.start.line === node.loc.start.line ||
241291
isRightInLogicalExp(node) ||
242-
isAlternateInConditionalExp(node)
292+
isAlternateInConditionalExp(node) ||
293+
isLastExpInDoExp(node)
243294
) ? 0 : indentSize;
244295
checkNodesIndent(node, parentElementIndent + indent);
245296
}

0 commit comments

Comments
 (0)