From 1ea3723da52c6805ac5940a0ea8acfa64998a83a Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Fri, 14 Jul 2017 18:11:07 -0700 Subject: [PATCH] Update: fix indentation of parenthesized MemberExpressions (fixes #8924) (#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. --- lib/rules/indent.js | 6 ++- tests/lib/rules/indent.js | 81 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index ce21e6d54a5..2ebb6dcb8c0 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -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; diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index 6f816f03d76..26998267bcf 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -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 @@ -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(){