Skip to content

Commit

Permalink
Update: Add never option to multiline-ternary (fixes #6751)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Aug 14, 2016
1 parent 3fe3a4f commit 606be9b
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 32 deletions.
50 changes: 45 additions & 5 deletions docs/rules/multiline-ternary.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ var foo = bar > baz ?

## Rule Details

This rule enforces newlines between operands of a ternary expression.
This rule enforces or disallows newlines between operands of a ternary expression.
Note: The location of the operators is not enforced by this rule. Please see the [operator-linebreak](operator-linebreak.md) rule if you are interested in enforcing the location of the operators themselves.

Examples of **incorrect** code for this rule:
## Options

This rule has a string option:

* `"always"` (default) enforces newlines between the operands of a ternary expression.
* `"never"` disallows newlines between the operands of a ternary expression (enforcing that the entire ternary expression is on one line).

### always

This is the default option.

Examples of **incorrect** code for this rule with the `"always"` option:

```js
/*eslint multiline-ternary: "error"*/
/*eslint multiline-ternary: ["error", "always"]*/

foo > bar ? value1 : value2;

Expand All @@ -35,10 +46,10 @@ foo > bar ?
value : value2;
```

Examples of **correct** code for this rule:
Examples of **correct** code for this rule with the `"always"` option:

```js
/*eslint multiline-ternary: "error"*/
/*eslint multiline-ternary: ["error", "always"]*/

foo > bar ?
value1 :
Expand All @@ -51,6 +62,35 @@ foo > bar ?
value3;
```

### never

Examples of **incorrect** code for this rule with the `"never"` option:

```js
/*eslint multiline-ternary: ["error", "never"]*/

foo > bar ? value :
value2;

foo > bar ?
value : value2;

foo >
bar ?
value1 :
value2;
```

Examples of **correct** code for this rule with the `"never"` option:

```js
/*eslint multiline-ternary: ["error", "never"]*/

foo > bar ? value1 : value2;

foo > bar ? (baz > qux ? value1 : value2) : value3;
```

## When Not To Use It

You can safely disable this rule if you do not have any strict conventions about whether the operands of a ternary expression should be separated by newlines.
Expand Down
33 changes: 25 additions & 8 deletions lib/rules/multiline-ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ module.exports = {
category: "Stylistic Issues",
recommended: false
},
schema: []
schema: [
{
enum: ["always", "never"]
},
]
},

create: function(context) {
const multiline = context.options[0] !== "never";

//--------------------------------------------------------------------------
// Helpers
Expand All @@ -31,14 +36,16 @@ module.exports = {
* Tests whether node is preceded by supplied tokens
* @param {ASTNode} node - node to check
* @param {ASTNode} parentNode - parent of node to report
* @param {Boolean} expected - whether newline was expected or not
* @returns {void}
* @private
*/
function reportError(node, parentNode) {
function reportError(node, parentNode, expected) {
context.report({
node: node,
message: "Expected newline between {{typeOfError}} of ternary expression.",
message: "{{expected}} newline between {{typeOfError}} of ternary expression.",
data: {
expected: expected ? "Expected" : "Unexpected",
typeOfError: node === parentNode.test ? "test and consequent" : "consequent and alternate"
}
});
Expand All @@ -53,12 +60,22 @@ module.exports = {
const areTestAndConsequentOnSameLine = astUtils.isTokenOnSameLine(node.test, node.consequent);
const areConsequentAndAlternateOnSameLine = astUtils.isTokenOnSameLine(node.consequent, node.alternate);

if (areTestAndConsequentOnSameLine) {
reportError(node.test, node);
}
if (!multiline) {
if (!areTestAndConsequentOnSameLine) {
reportError(node.test, node, false);
}

if (!areConsequentAndAlternateOnSameLine) {
reportError(node.consequent, node, false);
}
} else {
if (areTestAndConsequentOnSameLine) {
reportError(node.test, node, true);
}

if (areConsequentAndAlternateOnSameLine) {
reportError(node.consequent, node);
if (areConsequentAndAlternateOnSameLine) {
reportError(node.consequent, node, true);
}
}
}
};
Expand Down

0 comments on commit 606be9b

Please sign in to comment.