Skip to content

Commit

Permalink
Update validator of no-confusing-arrow for ESLint 6
Browse files Browse the repository at this point in the history
Fixes #90. Instead of adding ESLint version knowledge to the validators,
I went with the simpler approach of letting the validator consider the
ESLint 6 default value to always be the default. This means that ESLint
<=5 users now must explicitly set `{allowParens: false}`.
  • Loading branch information
lydell committed Jun 25, 2019
1 parent 595d6c1 commit db2081c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,29 +373,30 @@ For example, the rule could warn about this line:
var x = a => 1 ? 2 : 3;
```

By default, ESLint suggests switching to an explicit return:

```js
var x = a => { return 1 ? 2 : 3; };
```

That causes no problems with Prettier.

With `{allowParens: true}`, adding parentheses is also considered a valid way to
avoid the arrow confusion:
With `{allowParens: true}` (the default since ESLint 6.0.0), adding
parentheses is considered a valid way to avoid the arrow confusion:

```js
var x = a => (1 ? 2 : 3);
```

While Prettier keeps thoses parentheses, it removes them if the line is long
While Prettier keeps those parentheses, it removes them if the line is long
enough to introduce a line break:

```js
EnterpriseCalculator.prototype.calculateImportantNumbers = inputNumber =>
1 ? 2 : 3;
```

With `{allowParens: false}`, ESLint instead suggests switching to an explicit
return:

```js
var x = a => { return 1 ? 2 : 3; };
```

That causes no problems with Prettier.

If you like this rule, it can be used just fine with Prettier as long as the
`allowParens` option is off.

Expand All @@ -404,11 +405,18 @@ Example ESLint configuration:
```json
{
"rules": {
"no-confusing-arrow": "error"
"no-confusing-arrow": ["error", { "allowParens": false }]
}
}
```

(Note: The CLI helper tool considers `{allowParens: true}` to be the default,
which is the case since ESLint 6.0.0. The tool will produce a warning if you
use the default even if you use an older version of ESLint. It doesn’t hurt
to explicitly set `{allowParens: false}` even though it is technically
redundant. This way you are prepared for a future ESLint upgrade and the CLI
tool can be kept simple.)

### [no-mixed-operators]

**This rule requires special attention when writing code.**
Expand Down
2 changes: 1 addition & 1 deletion bin/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {

"no-confusing-arrow"(options) {
if (options.length === 0) {
return true;
return false;
}

const firstOption = options[0];
Expand Down
4 changes: 2 additions & 2 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ rule("lines-around-comment", {
});

rule("no-confusing-arrow", {
valid: [[], [{ allowParens: false }], [null]],
invalid: [[{ allowParens: true }]]
valid: [[{ allowParens: false }], [null]],
invalid: [[], [{ allowParens: true }]]
});

rule("vue/html-self-closing", {
Expand Down

0 comments on commit db2081c

Please sign in to comment.