Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: fix multiline binary operator/parentheses indentation #8719

Merged
merged 1 commit into from Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -7036,6 +7105,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