Skip to content

Commit

Permalink
Update: Add CallExpression option for indent (fixes #5946) (#7189)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and ilyavolodin committed Oct 28, 2016
1 parent b200086 commit 5e7af30
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 3 deletions.
44 changes: 44 additions & 0 deletions docs/rules/indent.md
Expand Up @@ -80,6 +80,8 @@ This rule has an object option:
* `"FunctionExpression"` takes an object to define rules for function expressions.
* `parameters` (off by default) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the expression must be aligned with the first parameter.
* `body` (default: 1) enforces indentation level for the body of a function expression.
* `"CallExpression"` takes an object to define rules for function call expressions.
* `arguments` (off by default) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all arguments of the expression must be aligned with the first argument.

Level of indentation denotes the multiple of the indent specified. Example:

Expand Down Expand Up @@ -386,6 +388,48 @@ var foo = function(bar, baz,
}
```

### CallExpression

Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:

```js
/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/

foo(bar,
baz,
qux
);
```

Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:

```js
/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/

foo(bar,
baz,
qux
);
```

Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:

```js
/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/

foo(bar, baz,
baz, boop, beep);
```

Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:

```js
/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/

foo(bar, baz,
baz, boop, beep);
```

## Compatibility

* **JSHint**: `indent`
Expand Down
34 changes: 34 additions & 0 deletions lib/rules/indent.js
Expand Up @@ -113,6 +113,22 @@ module.exports = {
minimum: 0
}
}
},
CallExpression: {
type: "object",
properties: {
parameters: {
oneOf: [
{
type: "integer",
minimum: 0
},
{
enum: ["first"]
}
]
}
}
}
},
additionalProperties: false
Expand Down Expand Up @@ -142,6 +158,9 @@ module.exports = {
FunctionExpression: {
parameters: DEFAULT_PARAMETER_INDENT,
body: DEFAULT_FUNCTION_BODY_INDENT
},
CallExpression: {
arguments: DEFAULT_PARAMETER_INDENT
}
};

Expand Down Expand Up @@ -187,6 +206,10 @@ module.exports = {
if (typeof opts.FunctionExpression === "object") {
Object.assign(options.FunctionExpression, opts.FunctionExpression);
}

if (typeof opts.CallExpression === "object") {
Object.assign(options.CallExpression, opts.CallExpression);
}
}
}

Expand Down Expand Up @@ -1045,6 +1068,17 @@ module.exports = {
} else {
checkNodeIndent(node, firstLineIndent);
}
},

CallExpression(node) {
if (isSingleLineNode(node)) {
return;
}
if (options.CallExpression.arguments === "first" && node.arguments.length) {
checkNodesIndent(node.arguments.slice(1), node.arguments[0].loc.start.column);
} else if (options.CallExpression.arguments !== null) {
checkNodesIndent(node.arguments, getNodeIndent(node).goodChar + indentSize * options.CallExpression.arguments);
}
}

};
Expand Down
139 changes: 136 additions & 3 deletions tests/lib/rules/indent.js
Expand Up @@ -1515,7 +1515,7 @@ ruleTester.run("indent", rule, {
"{\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {body: 3}}] // FIXME: what is the default for `parameters`?
options: [2, {FunctionDeclaration: {body: 3}}]
},
{
code:
Expand All @@ -1524,7 +1524,7 @@ ruleTester.run("indent", rule, {
" bbb) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionDeclaration: {parameters: "first", body: 2}}] // FIXME: make sure this is correct
options: [2, {FunctionDeclaration: {parameters: "first", body: 2}}]
},
{
code:
Expand Down Expand Up @@ -1561,7 +1561,7 @@ ruleTester.run("indent", rule, {
" ddd, eee) {\n" +
" bar();\n" +
"}",
options: [2, {FunctionExpression: {parameters: "first", body: 3}}] // FIXME: make sure this is correct
options: [2, {FunctionExpression: {parameters: "first", body: 3}}]
},
{
code:
Expand Down Expand Up @@ -1641,6 +1641,63 @@ ruleTester.run("indent", rule, {
" (z === 3 || z === 4));\n" +
"}",
options: [2]
}, {
code:
"foo(\n" +
" bar,\n" +
" baz,\n" +
" qux\n" +
");",
options: [2, {CallExpression: {arguments: 1}}]
}, {
code:
"foo(\n" +
"\tbar,\n" +
"\tbaz,\n" +
"\tqux\n" +
");",
options: ["tab", {CallExpression: {arguments: 1}}]
}, {
code:
"foo(bar,\n" +
" baz,\n" +
" qux);",
options: [4, {CallExpression: {arguments: 2}}]
}, {
code:
"foo(\n" +
"bar,\n" +
"baz,\n" +
"qux\n" +
");",
options: [2, {CallExpression: {arguments: 0}}]
}, {
code:
"foo(bar,\n" +
" baz,\n" +
" qux\n" +
");",
options: [2, {CallExpression: {arguments: "first"}}]
}, {
code:
"foo(bar, baz,\n" +
" qux, barbaz,\n" +
" barqux, bazqux);",
options: [2, {CallExpression: {arguments: "first"}}]
}, {
code:
"foo(\n" +
" bar, baz,\n" +
" qux);",
options: [2, {CallExpression: {arguments: "first"}}]
}, {
code:
"foo(bar,\n" +
" 1 + 2,\n" +
" !baz,\n" +
" new Car('!')\n" +
");",
options: [2, {CallExpression: {arguments: 4}}]
}
],
invalid: [
Expand Down Expand Up @@ -3335,5 +3392,81 @@ ruleTester.run("indent", rule, {
options: [2],
errors: expectedErrors([[2, "2 spaces", "3", "ReturnStatement"]])
},
{
code:
"foo(\n" +
"bar,\n" +
" baz,\n" +
" qux);",
output:
"foo(\n" +
" bar,\n" +
" baz,\n" +
" qux);",
options: [2, {CallExpression: {arguments: 1}}],
errors: expectedErrors([[2, 2, 0, "Identifier"], [4, 2, 4, "Identifier"]])
},
{
code:
"foo(\n" +
"\tbar,\n" +
"\tbaz);",
output:
"foo(\n" +
" bar,\n" +
" baz);",
options: [2, {CallExpression: {arguments: 2}}],
errors: expectedErrors([[2, "4 spaces", "1 tab", "Identifier"], [3, "4 spaces", "1 tab", "Identifier"]])
},
{
code:
"foo(bar,\n" +
"\t\tbaz,\n" +
"\t\tqux);",
output:
"foo(bar,\n" +
"\tbaz,\n" +
"\tqux);",
options: ["tab", {CallExpression: {arguments: 1}}],
errors: expectedErrors("tab", [[2, 1, 2, "Identifier"], [3, 1, 2, "Identifier"]])
},
{
code:
"foo(bar, baz,\n" +
" qux);",
output:
"foo(bar, baz,\n" +
" qux);",
options: [2, {CallExpression: {arguments: "first"}}],
errors: expectedErrors([2, 4, 9, "Identifier"])
},
{
code:
"foo(\n" +
" bar,\n" +
" baz);",
output:
"foo(\n" +
" bar,\n" +
" baz);",
options: [2, {CallExpression: {arguments: "first"}}],
errors: expectedErrors([3, 10, 4, "Identifier"])
},
{
code:
"foo(bar,\n" +
" 1 + 2,\n" +
" !baz,\n" +
" new Car('!')\n" +
");",
output:
"foo(bar,\n" +
" 1 + 2,\n" +
" !baz,\n" +
" new Car('!')\n" +
");",
options: [2, {CallExpression: {arguments: 3}}],
errors: expectedErrors([[2, 6, 2, "BinaryExpression"], [3, 6, 14, "UnaryExpression"], [4, 6, 8, "NewExpression"]])
}
]
});

0 comments on commit 5e7af30

Please sign in to comment.