From e0f0101fb949cf1aae5dc825cde9650743f0d5d1 Mon Sep 17 00:00:00 2001 From: Teddy Katz Date: Sat, 8 Jul 2017 18:35:55 -0700 Subject: [PATCH] Update: fix indentation of nested function parameters (fixes #8892) (#8900) Previously, the `indent` rule would only offset the first token of an element in a list (e.g. an array). However, this was incorrect because the other tokens of the element might not depend on the indentatio nof the first token. For example, in a function expression, the indentation of the parens does not depend on the indentation of the `function` token. This commit updates the `indent` rule to correctly offset all of the tokens in the element. --- lib/rules/indent.js | 2 +- tests/lib/rules/indent.js | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 99608ad519d..727ecfaaed2 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -738,7 +738,7 @@ module.exports = { const firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement); if (previousElement && sourceCode.getLastToken(previousElement).loc.start.line > startToken.loc.end.line) { - offsets.matchIndentOf(firstTokenOfPreviousElement, getFirstToken(element)); + offsets.setDesiredOffsets(getTokensAndComments(element), firstTokenOfPreviousElement, 0); } } }); diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index 0f6e05148cd..0001c76fd28 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -1470,6 +1470,27 @@ ruleTester.run("indent", rule, { `, options: [4, { VariableDeclarator: 0, SwitchCase: 1 }] }, + { + code: unIndent` + [[ + ], function( + foo + ) {} + ] + ` + }, + { + code: unIndent` + define([ + 'foo' + ], function( + bar + ) { + baz; + } + ) + ` + }, { code: unIndent` const func = function (opts) { @@ -5588,6 +5609,46 @@ ruleTester.run("indent", rule, { [5, 0, 2, "Punctuator"] ]) }, + { + code: unIndent` + [[ + ], function( + foo + ) {} + ] + `, + output: unIndent` + [[ + ], function( + foo + ) {} + ] + `, + errors: expectedErrors([[3, 4, 8, "Identifier"], [4, 0, 4, "Punctuator"]]) + }, + { + code: unIndent` + define([ + 'foo' + ], function( + bar + ) { + baz; + } + ) + `, + output: unIndent` + define([ + 'foo' + ], function( + bar + ) { + baz; + } + ) + `, + errors: expectedErrors([[4, 4, 8, "Identifier"], [5, 0, 4, "Punctuator"]]) + }, { code: unIndent` while (1 < 2)