Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Jul 9, 2017
1 parent 9f95a3e commit e0f0101
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/indent.js
Expand Up @@ -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);
}
}
});
Expand Down
61 changes: 61 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e0f0101

Please sign in to comment.