Skip to content

Commit

Permalink
chore: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kecrily committed Jun 14, 2023
1 parent 1d043ed commit fbf3c0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,7 @@ module.exports = {
* @private
*/
function isCondTernaryException(node) {
const filterType = new Set(["Identifier", "CallExpression"]);

return EXCEPT_COND_TERNARY &&
node.test.type && node.consequent && node.alternate && (
!filterType.has(node.test.type) ||
!filterType.has(node.consequent.type) ||
!filterType.has(node.alternate.type));
return EXCEPT_COND_TERNARY && node.test && node.consequent && node.alternate;
}

/**
Expand Down Expand Up @@ -875,12 +869,25 @@ module.exports = {
CallExpression: checkCallNew,

ConditionalExpression(node) {
if (
isReturnAssignException(node) ||
isCondTernaryException(node)
) {
if (isReturnAssignException(node)) {
return;
}

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

if (isCondTernaryException(node)) {
if (!aviableType.has(node.test.type) && isParenthesised(node.test)) {
report(node.test);
}
if (!aviableType.has(node.consequent.type) && isParenthesised(node.consequent)) {
report(node.consequent);
}
if (!aviableType.has(node.alternate.type) && isParenthesised(node.alternate)) {
report(node.alternate);
}
return;
}

if (
!isCondAssignException(node) &&
hasExcessParensWithPrecedence(node.test, precedence({ type: "LogicalExpression", operator: "||" }))
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/rules/no-extra-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@ ruleTester.run("no-extra-parens", rule, {
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

0 comments on commit fbf3c0d

Please sign in to comment.