Skip to content

Commit

Permalink
Fix: indent "first" option false positive on nested arrays (fixes #7727
Browse files Browse the repository at this point in the history
…) (#7728)
  • Loading branch information
not-an-aardvark authored and kaicataldo committed Dec 9, 2016
1 parent 81f9e7d commit e95a230
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/rules/indent.js
Expand Up @@ -727,9 +727,19 @@ module.exports = {
if (parent.type === "VariableDeclarator" && parentVarNode.loc.start.line === parent.loc.start.line) {
nodeIndent = nodeIndent + (indentSize * options.VariableDeclarator[parentVarNode.parent.kind]);
} else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") {
nodeIndent += options[parent.type] * indentSize;
if (typeof options[parent.type] === "number") {
nodeIndent += options[parent.type] * indentSize;
} else {
const parentElements = node.parent.type === "ObjectExpression" ? node.parent.properties : node.parent.elements;

nodeIndent = parentElements[0].loc.start.column;
}
} else if (parent.type === "CallExpression" || parent.type === "NewExpression") {
nodeIndent += (typeof options.CallExpression.arguments === "number" ? options.CallExpression.arguments : 1) * indentSize;
if (typeof options.CallExpression.arguments === "number") {
nodeIndent += options.CallExpression.arguments * indentSize;
} else if (parent.arguments.indexOf(node) !== -1) {
nodeIndent = parent.arguments[0].loc.start.column;
}
} else if (parent.type === "LogicalExpression" || parent.type === "ArrowFunctionExpression") {
nodeIndent += indentSize;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -1829,6 +1829,40 @@ ruleTester.run("indent", rule, {
{
code: "{\n}",
options: [2, {ObjectExpression: 1}]
},
{
code:
"var foo = [\n" +
" [\n" +
" 1\n" +
" ]\n" +
"]",
options: [2, {ArrayExpression: "first"}]
},
{
code:
"var foo = [ 1,\n" +
" [\n" +
" 2\n" +
" ]\n" +
"];",
options: [2, {ArrayExpression: "first"}]
},
{
code:
"var foo = bar(1,\n" +
" [ 2,\n" +
" 3\n" +
" ]\n" +
");",
options: [4, {ArrayExpression: "first", CallExpression: {arguments: "first"}}]
},
{
code:
"var foo =\n" +
" [\n" +
" ]()",
options: [4, {CallExpression: {arguments: "first"}, ArrayExpression: "first"}]
}
],
invalid: [
Expand Down

0 comments on commit e95a230

Please sign in to comment.