Skip to content

Commit

Permalink
Update: improve indent of flatTernaryExpressions (fixes #8481) (#8587)
Browse files Browse the repository at this point in the history
* Fix: improve indent of `flatTernaryExpressions` (fixes #8481)

* fix more
  • Loading branch information
mysticatea authored and not-an-aardvark committed May 17, 2017
1 parent 268d52e commit 3418479
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 61 deletions.
36 changes: 16 additions & 20 deletions docs/rules/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,47 +530,43 @@ Examples of **incorrect** code for this rule with the default `4, { "flatTernary
```js
/*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/

foo
? bar
: baz
? qux
: boop;
var a =
foo ? bar :
baz ? qux :
boop;
```

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

```js
/*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/

foo
? bar
: baz
? qux
: boop;
var a =
foo ? bar :
baz ? qux :
boop;
```

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

```js
/*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/

foo
? bar
: baz
? qux
: boop;
var a =
foo ? bar :
baz ? qux :
boop;
```

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

```js
/*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/

foo
? bar
: baz
? qux
: boop;
var a =
foo ? bar :
baz ? qux :
boop;
```


Expand Down
27 changes: 26 additions & 1 deletion lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,23 @@ module.exports = {
}
}

/**
* Check whether the given token is the first token of a statement.
* @param {Token} token The token to check.
* @param {ASTNode} leafNode The expression node that the token belongs directly.
* @returns {boolean} `true` if the token is the first token of a statement.
*/
function isFirstTokenOfStatement(token, leafNode) {
let node = leafNode;

while (node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration")) {
node = node.parent;
}
node = node.parent;

return !node || node.range[0] === token.range[0];
}

return {
ArrayExpression: addArrayOrObjectIndent,
ArrayPattern: addArrayOrObjectIndent,
Expand Down Expand Up @@ -967,7 +984,15 @@ module.exports = {
ConditionalExpression(node) {
const tokens = getTokensAndComments(node);

if (!(node.parent.type === "ConditionalExpression" && options.flatTernaryExpressions)) {
// `flatTernaryExpressions` option is for the following style:
// var a =
// foo > 0 ? bar :
// foo < 0 ? baz :
// /*else*/ qiz ;
if (!options.flatTernaryExpressions ||
!astUtils.isTokenOnSameLine(node.test, node.consequent) ||
isFirstTokenOfStatement(tokens[0], node)
) {
offsets.setDesiredOffsets(tokens, tokens[0], 1);
}
},
Expand Down
226 changes: 186 additions & 40 deletions tests/lib/rules/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2899,10 +2899,10 @@ ruleTester.run("indent", rule, {
foo
? bar
: baz
? qux
: foobar
? boop
: beep
? qux
: foobar
? boop
: beep
`,
options: [4, { flatTernaryExpressions: true }]
},
Expand All @@ -2911,10 +2911,94 @@ ruleTester.run("indent", rule, {
foo ?
bar :
baz ?
qux :
foobar ?
boop :
beep
qux :
foobar ?
boop :
beep
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
var a =
foo ? bar :
baz ? qux :
foobar ? boop :
/*else*/ beep
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
var a = foo
? bar
: baz
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
var a =
foo
? bar
: baz
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
a =
foo ? bar :
baz ? qux :
foobar ? boop :
/*else*/ beep
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
a = foo
? bar
: baz
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
a =
foo
? bar
: baz
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
foo(
foo ? bar :
baz ? qux :
foobar ? boop :
/*else*/ beep
)
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
foo(
foo
? bar
: baz
)
`,
options: [4, { flatTernaryExpressions: true }]
},
{
code: unIndent`
foo(foo
? bar
: baz
)
`,
options: [4, { flatTernaryExpressions: true }]
},
Expand Down Expand Up @@ -7170,56 +7254,118 @@ ruleTester.run("indent", rule, {
},
{
code: unIndent`
foo
? bar
: baz
? qux
: foobar
? boop
foo ? bar
: baz ? qux
: foobar ? boop
: beep
`,
output: unIndent`
foo
? bar
: baz
? qux
: foobar
? boop
foo ? bar
: baz ? qux
: foobar ? boop
: beep
`,
options: [4, { flatTernaryExpressions: true }],
errors: expectedErrors([
[4, 4, 8, "Punctuator"],
[5, 4, 8, "Punctuator"],
[6, 4, 12, "Punctuator"],
[7, 4, 12, "Punctuator"]
[3, 4, 8, "Punctuator"],
[4, 4, 12, "Punctuator"]
])
},
{
code: unIndent`
foo ?
bar :
baz ?
qux :
foobar ?
boop :
foo ? bar :
baz ? qux :
foobar ? boop :
beep
`,
output: unIndent`
foo ?
bar :
baz ?
qux :
foobar ?
boop :
foo ? bar :
baz ? qux :
foobar ? boop :
beep
`,
options: [4, { flatTernaryExpressions: true }],
errors: expectedErrors([
[4, 4, 8, "Identifier"],
[5, 4, 8, "Identifier"],
[6, 4, 12, "Identifier"],
[7, 4, 12, "Identifier"]
[3, 4, 8, "Identifier"],
[4, 4, 12, "Identifier"]
])
},
{
code: unIndent`
var a =
foo ? bar :
baz ? qux :
foobar ? boop :
/*else*/ beep
`,
output: unIndent`
var a =
foo ? bar :
baz ? qux :
foobar ? boop :
/*else*/ beep
`,
options: [4, { flatTernaryExpressions: true }],
errors: expectedErrors([
[3, 4, 6, "Identifier"],
[4, 4, 2, "Identifier"]
])
},
{
code: unIndent`
var a =
foo
? bar
: baz
`,
output: unIndent`
var a =
foo
? bar
: baz
`,
options: [4, { flatTernaryExpressions: true }],
errors: expectedErrors([
[3, 8, 4, "Punctuator"],
[4, 8, 4, "Punctuator"]
])
},
{
code: unIndent`
foo ? bar
: baz ? qux
: foobar ? boop
: beep
`,
output: unIndent`
foo ? bar
: baz ? qux
: foobar ? boop
: beep
`,
options: [4, { flatTernaryExpressions: false }],
errors: expectedErrors([
[3, 8, 4, "Punctuator"],
[4, 12, 4, "Punctuator"]
])
},
{
code: unIndent`
foo ? bar :
baz ? qux :
foobar ? boop :
beep
`,
output: unIndent`
foo ? bar :
baz ? qux :
foobar ? boop :
beep
`,
options: [4, { flatTernaryExpressions: false }],
errors: expectedErrors([
[3, 8, 4, "Identifier"],
[4, 12, 4, "Identifier"]
])
},
{
Expand Down

0 comments on commit 3418479

Please sign in to comment.