Skip to content

Commit

Permalink
[Fix] jsx-indent: Fix false positive when a jsx element is the last…
Browse files Browse the repository at this point in the history
… statement within a do expression

Fixes #2199.
  • Loading branch information
Kenneth-KT committed Mar 13, 2019
1 parent 752de70 commit 059dd68
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,56 @@ module.exports = {
);
}

/**
* Check if the node is the last statement within a do expression
* @param {ASTNode} node The node to check
* @return {Boolean} true if its the case, false if not
*/
function isLastExpInDoExp(node) {
/*
Detect the structure below:
DoExpression {
body: (
BlockStatement {
body: [
(...),
ExpressionStatement {
body: (
JSXElement {
openingElement: (node)
}
)
}
]
}
)
}
*/
const isInExpStmt = (
node.parent &&
node.parent.parent &&
node.parent.parent.type === 'ExpressionStatement'
);
if (!isInExpStmt) {
return false;
}

const expStmt = node.parent.parent;
const isInBlockStmtWithinDoExp = (
expStmt.parent &&
expStmt.parent.type === 'BlockStatement' &&
expStmt.parent.parent &&
expStmt.parent.parent.type === 'DoExpression'
);
if (!isInBlockStmtWithinDoExp) {
return false;
}

const blockStmt = expStmt.parent;
const blockStmtLastElm = blockStmt.body[blockStmt.body.length - 1];
return blockStmtLastElm === expStmt;
}

/**
* Check indent for nodes list
* @param {ASTNode} node The node to check
Expand Down Expand Up @@ -239,7 +289,8 @@ module.exports = {
const indent = (
prevToken.loc.start.line === node.loc.start.line ||
isRightInLogicalExp(node) ||
isAlternateInConditionalExp(node)
isAlternateInConditionalExp(node) ||
isLastExpInDoExp(node)
) ? 0 : indentSize;
checkNodesIndent(node, parentElementIndent + indent);
}
Expand Down

0 comments on commit 059dd68

Please sign in to comment.