Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ternaryOperandBinaryExpressions option to no-extra-parens rule #17270

Merged
merged 11 commits into from Jun 30, 2023
13 changes: 13 additions & 0 deletions docs/src/rules/no-extra-parens.md
Expand Up @@ -31,6 +31,7 @@ This rule has a string option:
This rule has an object option for exceptions to the `"all"` option:

* `"conditionalAssign": false` allows extra parentheses around assignments in conditional test expressions
* `"conditionalTernary": false` allows extra parentheses around condition in ternary expressions
* `"returnAssign": false` allows extra parentheses around assignments in `return` statements
* `"nestedBinaryExpressions": false` allows extra parentheses in nested binary expressions
* `"ignoreJSX": "none|all|multi-line|single-line"` allows extra parentheses around no/all/multi-line/single-line JSX components. Defaults to `none`.
Expand Down Expand Up @@ -130,6 +131,18 @@ for (;(a = b););

:::

### conditionalTernary

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

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

(a && b) ? foo : bar
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

(a - b > a) ? foo : bar
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
```
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

### returnAssign

Examples of **correct** code for this rule with the `"all"` and `{ "returnAssign": false }` options:
Expand Down
28 changes: 28 additions & 0 deletions lib/rules/no-extra-parens.js
Expand Up @@ -46,6 +46,7 @@ module.exports = {
type: "object",
properties: {
conditionalAssign: { type: "boolean" },
conditionalTernary: { type: "boolean" },
nestedBinaryExpressions: { type: "boolean" },
returnAssign: { type: "boolean" },
ignoreJSX: { enum: ["none", "all", "single-line", "multi-line"] },
Expand Down Expand Up @@ -76,6 +77,7 @@ module.exports = {
const precedence = astUtils.getPrecedence;
const ALL_NODES = context.options[0] !== "functions";
const EXCEPT_COND_ASSIGN = ALL_NODES && context.options[1] && context.options[1].conditionalAssign === false;
const EXCEPT_COND_TERNARY = ALL_NODES && context.options[1] && context.options[1].conditionalTernary === false;
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;
Expand Down Expand Up @@ -232,6 +234,16 @@ module.exports = {
return EXCEPT_COND_ASSIGN && node.test.type === "AssignmentExpression";
}

/**
* Determines if a node test expression is allowed to have a parenthesised condition in ternary expression
* @param {ASTNode} node The node to be checked.
* @returns {boolean} True if the conditional in ternary expression can be parenthesised.
* @private
*/
function isCondTernaryException(node) {
return EXCEPT_COND_TERNARY && node.test && node.consequent && node.alternate;
}

/**
* Determines if a node is in a return statement
* @param {ASTNode} node The node to be checked.
Expand Down Expand Up @@ -860,6 +872,22 @@ module.exports = {
if (isReturnAssignException(node)) {
return;
}

const availableType = new Set(["BinaryExpression", "LogicalExpression"]);

if (isCondTernaryException(node)) {
if (!availableType.has(node.test.type) && isParenthesised(node.test)) {
report(node.test);
}
if (!availableType.has(node.consequent.type) && isParenthesised(node.consequent)) {
report(node.consequent);
}
if (!availableType.has(node.alternate.type) && isParenthesised(node.alternate)) {
report(node.alternate);
}
return;
}
kecrily marked this conversation as resolved.
Show resolved Hide resolved

if (
!isCondAssignException(node) &&
hasExcessParensWithPrecedence(node.test, precedence({ type: "LogicalExpression", operator: "||" }))
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Expand Up @@ -303,6 +303,12 @@ ruleTester.run("no-extra-parens", rule, {
{ code: "while (((foo = bar()))) {}", options: ["all", { conditionalAssign: false }] },
{ code: "var a = (((b = c))) ? foo : bar;", options: ["all", { conditionalAssign: false }] },

// ["all", { conditionalTernary: false }] enables extra parens around conditional ternary
{ code: "(a && b) ? foo : bar", options: ["all", { conditionalTernary: false }] },
{ code: "(a - b > a) ? foo : bar", options: ["all", { conditionalTernary: false }] },
{ code: "foo ? (bar || baz) : qux", options: ["all", { conditionalTernary: false }] },
{ code: "foo ? bar : (baz || qux)", options: ["all", { conditionalTernary: false }] },

// ["all", { nestedBinaryExpressions: false }] enables extra parens around conditional assignments
{ code: "a + (b * c)", options: ["all", { nestedBinaryExpressions: false }] },
{ code: "(a * b) + c", options: ["all", { nestedBinaryExpressions: false }] },
Expand Down Expand Up @@ -915,6 +921,10 @@ ruleTester.run("no-extra-parens", rule, {
invalid("a ? b : (c = d)", "a ? b : c = d", "AssignmentExpression"),
invalid("(c = d) ? (b) : c", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
invalid("(c = d) ? b : (c)", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
invalid("(a) ? foo : bar", "a ? foo : bar", "Identifier", null, { options: ["all", { conditionalTernary: false }] }),
invalid("(a()) ? foo : bar", "a() ? foo : bar", "CallExpression", null, { options: ["all", { conditionalTernary: false }] }),
invalid("(a.b) ? foo : bar", "a.b ? foo : bar", "MemberExpression", null, { options: ["all", { conditionalTernary: false }] }),
invalid("(a || b) ? foo : (bar)", "(a || b) ? foo : bar", "Identifier", null, { options: ["all", { conditionalTernary: false }] }),
invalid("f((a = b))", "f(a = b)", "AssignmentExpression"),
invalid("a, (b = c)", "a, b = c", "AssignmentExpression"),
invalid("a = (b * c)", "a = b * c", "BinaryExpression"),
Expand Down