diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 82268975dfb..fb143806cf2 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -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); @@ -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); } }, diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index 335e8882f00..86e38a8ea05 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -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