Skip to content

Commit

Permalink
Update: add confusingArrow to no-extra-parens (fixes eslint#6196)
Browse files Browse the repository at this point in the history
Allows extra parentheses in arrow function syntax in places where it could be confused with a comparison operator.
  • Loading branch information
evilebottnawi committed Apr 10, 2017
1 parent 0c2a386 commit 198ba8d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/rules/no-extra-parens.md
Expand Up @@ -165,6 +165,17 @@ const Component = (<div />)
const Component = (<div><p /></div>)
```

### confusingArrow

Examples of **correct** code for this rule with the `"all"` and `{ "confusingArrow": false }` options:

```js
/* eslint no-extra-parens: ["error", "all", { "confusingArrow": false }] */

const b = a => 1 ? 2 : 3;
const d = c => (1 ? 2 : 3);
```

### functions

Examples of **incorrect** code for this rule with the `"functions"` option:
Expand Down
8 changes: 7 additions & 1 deletion lib/rules/no-extra-parens.js
Expand Up @@ -44,7 +44,8 @@ module.exports = {
conditionalAssign: { type: "boolean" },
nestedBinaryExpressions: { type: "boolean" },
returnAssign: { type: "boolean" },
ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] }
ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
confusingArrow: { type: "boolean" }
},
additionalProperties: false
}
Expand All @@ -67,6 +68,7 @@ module.exports = {
const NESTED_BINARY = ALL_NODES && context.options[1] && context.options[1].nestedBinaryExpressions === false;
const EXCEPT_RETURN_ASSIGN = ALL_NODES && context.options[1] && context.options[1].returnAssign === false;
const IGNORE_JSX = ALL_NODES && context.options[1] && context.options[1].ignoreJSX;
const EXCEPT_CONFUSING_ARROW = ALL_NODES && context.options[1] && context.options[1].confusingArrow === false;
const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });

Expand Down Expand Up @@ -431,6 +433,10 @@ module.exports = {
return;
}

if (node.body.type === "ConditionalExpression" && EXCEPT_CONFUSING_ARROW) {
return;
}

if (node.body.type !== "BlockStatement") {
const firstBodyToken = sourceCode.getFirstToken(node.body, astUtils.isNotOpeningParenToken);
const tokenBeforeFirst = sourceCode.getTokenBefore(firstBodyToken);
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -369,6 +369,10 @@ ruleTester.run("no-extra-parens", rule, {
"/>)"
].join("\n"), options: ["all", { ignoreJSX: "multi-line" }] },

// ["all", { confusingArrow: false }]
{ code: "var a = b => 1 ? 2 : 3", options: ["all", { confusingArrow: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "var a = (b) => (1 ? 2 : 3)", options: ["all", { confusingArrow: false }], parserOptions: { ecmaVersion: 6 } },

{
code: "let a = [ ...b ]",
parserOptions: { ecmaVersion: 2015 }
Expand Down

0 comments on commit 198ba8d

Please sign in to comment.