Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: fix BinaryExpression indentation edge case (fixes #8914) (#8930)
This fixes an issue where the indentation of the right-hand side of a `BinaryExpression` did not depend on the indentation of the operator. This could result in a situation where the rule would verify the absolute indentation of a node within the BinaryExpression, rather than the relative indentation from the operator.
  • Loading branch information
not-an-aardvark committed Jul 14, 2017
1 parent 0e90453 commit 9abc6f7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
40 changes: 16 additions & 24 deletions lib/rules/indent.js
Expand Up @@ -895,27 +895,6 @@ module.exports = {
}
}

/**
* Adds indentation for the right-hand side of binary/logical expressions.
* @param {ASTNode} node A BinaryExpression or LogicalExpression node
* @returns {void}
*/
function addBinaryOrLogicalExpressionIndent(node) {
const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);

/*
* For backwards compatibility, don't check BinaryExpression indents, e.g.
* var foo = bar &&
* baz;
*/

const tokenAfterOperator = sourceCode.getTokenAfter(operator);

offsets.ignoreToken(operator);
offsets.ignoreToken(tokenAfterOperator);
offsets.setDesiredOffsets([operator.range[1], node.range[1]], tokenAfterOperator, 1);
}

/**
* Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`)
* @param {ASTNode} node A CallExpression or NewExpression node
Expand Down Expand Up @@ -1077,7 +1056,22 @@ module.exports = {
offsets.ignoreToken(sourceCode.getTokenAfter(operator));
},

BinaryExpression: addBinaryOrLogicalExpressionIndent,
"BinaryExpression, LogicalExpression"(node) {
const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);

/*
* For backwards compatibility, don't check BinaryExpression indents, e.g.
* var foo = bar &&
* baz;
*/

const tokenAfterOperator = sourceCode.getTokenAfter(operator);

offsets.ignoreToken(operator);
offsets.ignoreToken(tokenAfterOperator);
offsets.setDesiredOffset(tokenAfterOperator, operator, 0);
offsets.setDesiredOffsets([tokenAfterOperator.range[1], node.range[1]], tokenAfterOperator, 1);
},

BlockStatement: addBlockIndent,

Expand Down Expand Up @@ -1211,8 +1205,6 @@ module.exports = {
}
},

LogicalExpression: addBinaryOrLogicalExpressionIndent,

"MemberExpression, JSXMemberExpression"(node) {
const firstNonObjectToken = sourceCode.getFirstTokenBetween(node.object, node.property, astUtils.isNotClosingParenToken);
const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken);
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -2751,6 +2751,22 @@ ruleTester.run("indent", rule, {
)
`
},
{
code: unIndent`
foo
|| (
bar
)
`
},
{
code: unIndent`
foo
|| (
bar
)
`
},
{
code: unIndent`
var foo =
Expand Down Expand Up @@ -7424,6 +7440,21 @@ ruleTester.run("indent", rule, {
`,
errors: expectedErrors([3, 0, 4, "Punctuator"])
},
{
code: unIndent`
foo
|| (
bar
)
`,
output: unIndent`
foo
|| (
bar
)
`,
errors: expectedErrors([[3, 12, 16, "Identifier"], [4, 8, 12, "Punctuator"]])
},
{
code: unIndent`
1
Expand Down

0 comments on commit 9abc6f7

Please sign in to comment.