Skip to content

Commit

Permalink
Update: fix multiline binary operator/parentheses indentation (#8719)
Browse files Browse the repository at this point in the history
Fixes #8666, fixes #8717, fixes #8710

Previously, the logic for indenting multiline parenthesized expressions assumed that the indentation of every token in the expression other than the first was dependent on the first token. However, this assumption is not always correct. This led to bugs with multiline parenthesized expressions (#8710). Additionally, the BinaryExpression listener attempted to account for this assumption by always linking its tokens' indentation to the first token's indentation, even when it didn't make sense to do so. This led to other bugs (#8666, #8717). This commit updates the parenthesis logic to avoid making that assumption and check the indentation of all the tokens properly.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Jun 15, 2017
1 parent ab8b016 commit b5a70b4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/indent.js
Expand Up @@ -816,7 +816,6 @@ module.exports = {

offsets.ignoreToken(operator);
offsets.ignoreToken(tokensAfterOperator[0]);
offsets.setDesiredOffset(tokensAfterOperator[0], sourceCode.getFirstToken(node), 1);
offsets.setDesiredOffsets(tokensAfterOperator, tokensAfterOperator[0], 1);
}

Expand Down Expand Up @@ -882,6 +881,13 @@ module.exports = {

// We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments.
if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) {
const parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen));

parenthesizedTokens.forEach(token => {
if (!parenthesizedTokens.has(offsets.getFirstDependency(token))) {
offsets.setDesiredOffset(token, leftParen, 1);
}
});
offsets.setDesiredOffset(sourceCode.getTokenAfter(leftParen), leftParen, 1);
}

Expand Down
97 changes: 97 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -2563,6 +2563,40 @@ ruleTester.run("indent", rule, {
`,
options: [4]
},
{
code: unIndent`
[
] || [
]
`
},
{
code: unIndent`
(
[
] || [
]
)
`
},
{
code: unIndent`
1
+ (
1
)
`
},
{
code: unIndent`
(
foo && (
bar ||
baz
)
)
`
},
{
code: unIndent`
var foo =
Expand Down Expand Up @@ -2983,6 +3017,41 @@ ruleTester.run("indent", rule, {
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
function wrap() {
return (
foo ? bar :
baz ? qux :
foobar ? boop :
/*else*/ beep
)
}
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
function wrap() {
return foo
? bar
: baz
}
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
function wrap() {
return (
foo
? bar
: baz
)
}
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
foo(
Expand Down Expand Up @@ -7053,6 +7122,34 @@ ruleTester.run("indent", rule, {
options: [4],
errors: expectedErrors([2, 4, 8, "Identifier"])
},
{
code: unIndent`
[
] || [
]
`,
output: unIndent`
[
] || [
]
`,
errors: expectedErrors([3, 0, 4, "Punctuator"])
},
{
code: unIndent`
1
+ (
1
)
`,
output: unIndent`
1
+ (
1
)
`,
errors: expectedErrors([[3, 4, 8, "Numeric"], [4, 0, 4, "Punctuator"]])
},

// Template curlies
{
Expand Down

0 comments on commit b5a70b4

Please sign in to comment.