Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Add offsetTernaryExpressions option for indent rule #12556

Merged
merged 1 commit into from Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions docs/rules/indent.md
Expand Up @@ -84,6 +84,7 @@ This rule has an object option:
* `"ObjectExpression"` (default: 1) enforces indentation level for properties in objects. It can be set to the string `"first"`, indicating that all properties in the object should be aligned with the first property. This can also be set to `"off"` to disable checking for object properties.
* `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members.
* `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions.
* `"offsetTernaryExpressions": true` (`false` by default) requires indentation for values of ternary expressions.
* `"ignoredNodes"` accepts an array of [selectors](/docs/developer-guide/selectors.md). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern.
* `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line.

Expand Down Expand Up @@ -643,6 +644,76 @@ var a =
boop;
```

### offsetTernaryExpressions

Examples of **incorrect** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option:

```js
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/

condition
? () => {
return true
}
: () => {
false
}
```

Examples of **correct** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option:

```js
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/

condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
```

Examples of **incorrect** code for this rule with the `2, { "offsetTernaryExpressions": true }` option:

```js
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/

condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
```

Examples of **correct** code for this rule with the `2, { "offsetTernaryExpressions": true }` option:

```js
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/

condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
```

### ignoredNodes

The following configuration ignores the indentation of `ConditionalExpression` ("ternary expression") nodes:
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/indent.js
Expand Up @@ -590,6 +590,10 @@ module.exports = {
type: "boolean",
default: false
},
offsetTernaryExpressions: {
type: "boolean",
default: false
},
ignoredNodes: {
type: "array",
items: {
Expand Down Expand Up @@ -1142,7 +1146,8 @@ module.exports = {
offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
offsets.setDesiredOffset(colonToken, firstToken, 1);

offsets.setDesiredOffset(firstConsequentToken, firstToken, 1);
offsets.setDesiredOffset(firstConsequentToken, firstToken,
options.offsetTernaryExpressions ? 2 : 1);

/*
* The alternate and the consequent should usually have the same indentation.
Expand All @@ -1167,7 +1172,9 @@ module.exports = {
* If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up
* having no expected indentation.
*/
offsets.setDesiredOffset(firstAlternateToken, firstToken, 1);
offsets.setDesiredOffset(firstAlternateToken, firstToken,
firstAlternateToken.type === "Punctuator" &&
options.offsetTernaryExpressions ? 2 : 1);
}
}
},
Expand Down
162 changes: 162 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -2097,6 +2097,86 @@ ruleTester.run("indent", rule, {
`,
options: [2]
},
{
code: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
options: [2]
},
{
code: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
options: [2, { offsetTernaryExpressions: false }]
},
{
code: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
options: [2, { offsetTernaryExpressions: true }]
},
{
code: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
options: [4, { offsetTernaryExpressions: true }]
},
{
code: unIndent`
condition
\t? () => {
\t\t\treturn true
\t\t}
\t: condition2
\t\t? () => {
\t\t\t\treturn true
\t\t\t}
\t\t: () => {
\t\t\t\treturn false
\t\t\t}
`,
options: ["tab", { offsetTernaryExpressions: true }]
},
unIndent`
[
foo ?
Expand Down Expand Up @@ -7693,6 +7773,88 @@ ruleTester.run("indent", rule, {
`,
errors: expectedErrors([5, 4, 8, "Identifier"])
},
{
code: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
output: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
options: [2, { offsetTernaryExpressions: true }],
sheerun marked this conversation as resolved.
Show resolved Hide resolved
errors: expectedErrors([
[2, 2, 0, "Punctuator"],
[3, 6, 0, "Keyword"],
[4, 4, 0, "Punctuator"],
[5, 2, 0, "Punctuator"],
[6, 4, 0, "Punctuator"],
[7, 8, 0, "Keyword"],
[8, 6, 0, "Punctuator"],
[9, 4, 0, "Punctuator"],
[10, 8, 0, "Keyword"],
[11, 6, 0, "Punctuator"]
])
},
{
code: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
output: unIndent`
condition
? () => {
return true
}
: condition2
? () => {
return true
}
: () => {
return false
}
`,
options: [2, { offsetTernaryExpressions: false }],
errors: expectedErrors([
[2, 2, 0, "Punctuator"],
[3, 4, 0, "Keyword"],
[4, 2, 0, "Punctuator"],
[5, 2, 0, "Punctuator"],
[6, 4, 0, "Punctuator"],
[7, 6, 0, "Keyword"],
[8, 4, 0, "Punctuator"],
[9, 4, 0, "Punctuator"],
[10, 6, 0, "Keyword"],
[11, 4, 0, "Punctuator"]
])
},
{

/*
Expand Down