Skip to content

Commit

Permalink
Update: fix indentation of parenthesized MemberExpressions (fixes #8924
Browse files Browse the repository at this point in the history
…) (#8928)

The MemberExpression listener in `indent` contains some logic to ensure that if the object of a MemberExpression is wrapped in parentheses, the property is offset from the opening paren, not the object itself. Due to a bug, this logic also caused the property to be offset from the opening paren if the entire MemberExpression was wrapped in parentheses, raather than just the object. This commit updates the MemberExpression listener to specifically check for parentheses around the object, not the entire MemberExpression.
  • Loading branch information
not-an-aardvark committed Jul 15, 2017
1 parent 9abc6f7 commit 1ea3723
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/rules/indent.js
Expand Up @@ -1209,8 +1209,10 @@ module.exports = {
const firstNonObjectToken = sourceCode.getFirstTokenBetween(node.object, node.property, astUtils.isNotClosingParenToken);
const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken);

const tokenBeforeObject = sourceCode.getTokenBefore(node.object, token => astUtils.isNotOpeningParenToken(token) || parameterParens.has(token));
const firstObjectToken = tokenBeforeObject ? sourceCode.getTokenAfter(tokenBeforeObject) : sourceCode.ast.tokens[0];
const objectParenCount = sourceCode.getTokensBetween(node.object, node.property, { filter: astUtils.isClosingParenToken }).length;
const firstObjectToken = objectParenCount
? sourceCode.getTokenBefore(node.object, { skip: objectParenCount - 1 })
: sourceCode.getFirstToken(node.object);
const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken);
const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;

Expand Down
81 changes: 81 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -1822,6 +1822,72 @@ ruleTester.run("indent", rule, {
`,
options: [2, { MemberExpression: 2 }]
},
{
code: unIndent`
(
foo
.bar
)
`
},
{
code: unIndent`
(
(
foo
.bar
)
)
`
},
{
code: unIndent`
(
foo
)
.bar
`
},
{
code: unIndent`
(
(
foo
)
.bar
)
`
},
{
code: unIndent`
(
(
foo
)
[
(
bar
)
]
)
`
},
{
code: unIndent`
(
foo[bar]
)
.baz
`
},
{
code: unIndent`
(
(foo.bar)
)
.baz
`
},
{
code: unIndent`
MemberExpression
Expand Down Expand Up @@ -5216,6 +5282,21 @@ ruleTester.run("indent", rule, {
[3, 8, 10, "Punctuator"]
)
},
{
code: unIndent`
(
foo
.bar
)
`,
output: unIndent`
(
foo
.bar
)
`,
errors: expectedErrors([3, 8, 4, "Punctuator"])
},
{
code: unIndent`
var foo = function(){
Expand Down

0 comments on commit 1ea3723

Please sign in to comment.