Skip to content

Commit 6654664

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

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

lib/rules/jsx-indent.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,52 @@ 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) return false;
229+
230+
const expStmt = node.parent.parent;
231+
const isInBlockStmtWithinDoExp = (
232+
expStmt.parent &&
233+
expStmt.parent.type === 'BlockStatement' &&
234+
expStmt.parent.parent &&
235+
expStmt.parent.parent.type === 'DoExpression'
236+
);
237+
if (!isInBlockStmtWithinDoExp) return false;
238+
239+
const blockStmt = expStmt.parent;
240+
const blockStmtLastElm = blockStmt.body[blockStmt.body.length - 1]
241+
return blockStmtLastElm === expStmt;
242+
}
243+
198244
/**
199245
* Check indent for nodes list
200246
* @param {ASTNode} node The node to check
@@ -239,7 +285,8 @@ module.exports = {
239285
const indent = (
240286
prevToken.loc.start.line === node.loc.start.line ||
241287
isRightInLogicalExp(node) ||
242-
isAlternateInConditionalExp(node)
288+
isAlternateInConditionalExp(node) ||
289+
isLastExpInDoExp(node)
243290
) ? 0 : indentSize;
244291
checkNodesIndent(node, parentElementIndent + indent);
245292
}

0 commit comments

Comments
 (0)