Skip to content

Commit

Permalink
New: Add offsetTernaryExpressions option for indent rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Nov 11, 2019
1 parent 45aa6a3 commit 8621041
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
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
32 changes: 32 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -2097,6 +2097,38 @@ 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: true }]
},
unIndent`
[
foo ?
Expand Down

0 comments on commit 8621041

Please sign in to comment.