Skip to content

Commit

Permalink
Update: fix MemberExpression indentation with "off" option (fixes #8721
Browse files Browse the repository at this point in the history
…) (#8724)

This fixes a bug where token dependencies were configured incorrectly when the "off" MemberExpression option was set, resulting in incorrect behavior for chained expressions.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Jun 15, 2017
1 parent eb5d12c commit ab8b016
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/rules/indent.js
Expand Up @@ -1114,28 +1114,29 @@ module.exports = {
const tokenBeforeObject = sourceCode.getTokenBefore(node.object, token => astUtils.isNotOpeningParenToken(token) || parameterParens.has(token));
const firstObjectToken = tokenBeforeObject ? sourceCode.getTokenAfter(tokenBeforeObject) : sourceCode.ast.tokens[0];
const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken);
const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;

if (node.computed) {

// For computed MemberExpressions, match the closing bracket with the opening bracket.
offsets.matchIndentOf(firstNonObjectToken, sourceCode.getLastToken(node));
offsets.setDesiredOffsets(getTokensAndComments(node.property), firstNonObjectToken, 1);
}

if (typeof options.MemberExpression === "number") {
const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;
/*
* If the object ends on the same line that the property starts, match against the last token
* of the object, to ensure that the MemberExpression is not indented.
*
* Otherwise, match against the first token of the object, e.g.
* foo
* .bar
* .baz // <-- offset by 1 from `foo`
*/
const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line
? lastObjectToken
: firstObjectToken;

/*
* If the object ends on the same line that the property starts, match against the last token
* of the object, to ensure that the MemberExpression is not indented.
*
* Otherwise, match against the first token of the object, e.g.
* foo
* .bar
* .baz // <-- offset by 1 from `foo`
*/
const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line
? lastObjectToken
: firstObjectToken;
if (typeof options.MemberExpression === "number") {

// Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object.
offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression);
Expand All @@ -1150,6 +1151,9 @@ module.exports = {
// If the MemberExpression option is off, ignore the dot and the first token of the property.
offsets.ignoreToken(firstNonObjectToken);
offsets.ignoreToken(secondNonObjectToken);

// To ignore the property indentation, ensure that the property tokens depend on the ignored tokens.
offsets.matchIndentOf(offsetBase, firstNonObjectToken);
offsets.matchIndentOf(firstNonObjectToken, secondNonObjectToken);
}
},
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -3376,6 +3376,23 @@ ruleTester.run("indent", rule, {
`,
options: [4, { MemberExpression: "off" }]
},
{
code: unIndent`
foo = bar(
).baz(
)
`,
options: [4, { MemberExpression: "off" }]
},
{
code: unIndent`
foo[
bar ? baz :
qux
]
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
foo
Expand Down

0 comments on commit ab8b016

Please sign in to comment.