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
19 changes: 19 additions & 0 deletions docs/src/rules/no-extra-parens.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This rule has an object option for exceptions to the `"all"` option:
* `"conditionalAssign": false` allows extra parentheses around assignments in conditional test expressions
* `"returnAssign": false` allows extra parentheses around assignments in `return` statements
* `"nestedBinaryExpressions": false` allows extra parentheses in nested binary expressions
* `"ternaryOperandBinaryExpressions": false` allows extra parentheses around binary expressions that are operands of ternary `?:`
* `"ignoreJSX": "none|all|multi-line|single-line"` allows extra parentheses around no/all/multi-line/single-line JSX components. Defaults to `none`.
* `"enforceForArrowConditionals": false` allows extra parentheses around ternary expressions which are the body of an arrow function
* `"enforceForSequenceExpressions": false` allows extra parentheses around sequence expressions
Expand Down Expand Up @@ -130,6 +131,24 @@ for (;(a = b););

:::

### ternaryOperandBinaryExpressions

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

```js
/* eslint no-extra-parens: ["error", "all", { "ternaryOperandBinaryExpressions": 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

foo ? (bar || baz) : qux
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

foo ? bar : (baz || qux)
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

(a, b) ? (c, d) : (e, f)
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
type: "object",
properties: {
conditionalAssign: { type: "boolean" },
ternaryOperandBinaryExpressions: { 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].ternaryOperandBinaryExpressions === 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 availableTypes = new Set(["BinaryExpression", "LogicalExpression", "SequenceExpression"]);

if (isCondTernaryException(node)) {
if (!availableTypes.has(node.test.type) && isParenthesised(node.test)) {
report(node.test);
}
if (!availableTypes.has(node.consequent.type) && isParenthesised(node.consequent)) {
report(node.consequent);
}
if (!availableTypes.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
11 changes: 11 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ 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", { ternaryOperandBinaryExpressions: false }] enables extra parens around conditional ternary
{ code: "(a && b) ? foo : bar", options: ["all", { ternaryOperandBinaryExpressions: false }] },
{ code: "(a - b > a) ? foo : bar", options: ["all", { ternaryOperandBinaryExpressions: false }] },
{ code: "foo ? (bar || baz) : qux", options: ["all", { ternaryOperandBinaryExpressions: false }] },
{ code: "foo ? bar : (baz || qux)", options: ["all", { ternaryOperandBinaryExpressions: false }] },
{ code: "(a, b) ? (c, d) : (e, f)", options: ["all", { ternaryOperandBinaryExpressions: 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 +922,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", { ternaryOperandBinaryExpressions: false }] }),
invalid("(a()) ? foo : bar", "a() ? foo : bar", "CallExpression", null, { options: ["all", { ternaryOperandBinaryExpressions: false }] }),
invalid("(a.b) ? foo : bar", "a.b ? foo : bar", "MemberExpression", null, { options: ["all", { ternaryOperandBinaryExpressions: false }] }),
invalid("(a || b) ? foo : (bar)", "(a || b) ? foo : bar", "Identifier", null, { options: ["all", { ternaryOperandBinaryExpressions: 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