Skip to content

Commit

Permalink
Update: fix indentation of JSXExpressionContainer contents (fixes #8832
Browse files Browse the repository at this point in the history
…) (#8850)

Previously, the `indent` rule handled JSXExpressionContainer nodes by only setting the first token's offset, incorrectly assuming that all the other tokens in the expression would be dependent on the first token. (This had been a problem since JSX support was added to the rule.) As a result of an unrelated, correct fix in b5a70b4, the bug ended up also appearing for BinaryExpressions in JSXExpressionContainers.

This commit updates the JSXExpression logic to offset all of its inner tokens.
  • Loading branch information
not-an-aardvark authored Jul 3, 2017
1 parent 330dd58 commit 676af9e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,13 +1375,14 @@ module.exports = {

JSXExpressionContainer(node) {
const openingCurly = sourceCode.getFirstToken(node);
const firstExpressionToken = sourceCode.getFirstToken(node.expression);
const closingCurly = sourceCode.getLastToken(node);

if (firstExpressionToken) {
offsets.setDesiredOffset(firstExpressionToken, openingCurly, 1);
}

offsets.matchIndentOf(openingCurly, sourceCode.getLastToken(node));
offsets.setDesiredOffsets(
sourceCode.getTokensBetween(openingCurly, closingCurly, { includeComments: true }),
openingCurly,
1
);
offsets.matchIndentOf(openingCurly, closingCurly);
},

"Program:exit"() {
Expand Down
76 changes: 76 additions & 0 deletions tests/lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4649,6 +4649,44 @@ ruleTester.run("indent", rule, {
</span>
)
`
},
{
code: unIndent`
<div>
{
/* foo */
}
</div>
`
},

// https://github.com/eslint/eslint/issues/8832
{
code: unIndent`
<div>
{
(
1
)
}
</div>
`
},
{
code: unIndent`
function A() {
return (
<div>
{
b && (
<div>
</div>
)
}
</div>
);
}
`
}
],

Expand Down Expand Up @@ -8769,6 +8807,44 @@ ruleTester.run("indent", rule, {
)
`,
errors: expectedErrors([[5, 4, 8, "Punctuator"], [6, 4, 8, "Punctuator"], [7, 0, 4, "Punctuator"]])
},
{
code: unIndent`
<div>
{
(
1
)
}
</div>
`,
output: unIndent`
<div>
{
(
1
)
}
</div>
`,
errors: expectedErrors([[3, 8, 4, "Punctuator"], [4, 12, 8, "Numeric"], [5, 8, 4, "Punctuator"]])
},
{
code: unIndent`
<div>
{
/* foo */
}
</div>
`,
output: unIndent`
<div>
{
/* foo */
}
</div>
`,
errors: expectedErrors([3, 8, 6, "Block"])
}
]
});

0 comments on commit 676af9e

Please sign in to comment.