Skip to content

Commit b1f9106

Browse files
authored
feat: detect Symbol() and BigInt() in no-constant-binary-expression (#20981)
1 parent 3dbacdb commit b1f9106

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

lib/rules/no-constant-binary-expression.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ function hasConstantNullishness(scope, node, nonNullish) {
8989
return (
9090
(functionName === "Boolean" ||
9191
functionName === "String" ||
92-
functionName === "Number") &&
92+
functionName === "Number" ||
93+
functionName === "Symbol" ||
94+
functionName === "BigInt") &&
9395
isReferenceToGlobalVariable(scope, node.callee)
9496
);
9597
}
@@ -374,7 +376,10 @@ function hasConstantStrictBooleanComparison(scope, node) {
374376
const functionName = node.callee.name;
375377

376378
if (
377-
(functionName === "String" || functionName === "Number") &&
379+
(functionName === "String" ||
380+
functionName === "Number" ||
381+
functionName === "BigInt" ||
382+
functionName === "Symbol") &&
378383
isReferenceToGlobalVariable(scope, node.callee)
379384
) {
380385
return true;

tests/lib/rules/no-constant-binary-expression.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ ruleTester.run("no-constant-binary-expression", rule, {
5555
"function Boolean(n) { return n; }; Boolean(x) ?? foo",
5656
"function String(n) { return n; }; String(x) ?? foo",
5757
"function Number(n) { return n; }; Number(x) ?? foo",
58+
"function Symbol(n) { return n; }; Symbol(x) ?? foo",
59+
"function BigInt(n) { return n; }; BigInt(x) ?? foo",
5860
"function Boolean(n) { return Math.random(); }; Boolean(x) === 1",
5961
"function Boolean(n) { return Math.random(); }; Boolean(1) == true",
6062

@@ -894,6 +896,24 @@ ruleTester.run("no-constant-binary-expression", rule, {
894896
},
895897
],
896898
},
899+
{
900+
code: "Symbol(x) ?? foo",
901+
errors: [
902+
{
903+
messageId: "constantShortCircuit",
904+
data: { property: "nullishness", operator: "??" },
905+
},
906+
],
907+
},
908+
{
909+
code: "BigInt(x) ?? foo",
910+
errors: [
911+
{
912+
messageId: "constantShortCircuit",
913+
data: { property: "nullishness", operator: "??" },
914+
},
915+
],
916+
},
897917

898918
// Binary expression with comparison to null
899919
{
@@ -941,6 +961,42 @@ ruleTester.run("no-constant-binary-expression", rule, {
941961
},
942962
],
943963
},
964+
{
965+
code: "Symbol(x) != null",
966+
errors: [
967+
{
968+
messageId: "constantBinaryOperand",
969+
data: { otherSide: "right", operator: "!=" },
970+
},
971+
],
972+
},
973+
{
974+
code: "Symbol(x) != undefined",
975+
errors: [
976+
{
977+
messageId: "constantBinaryOperand",
978+
data: { otherSide: "right", operator: "!=" },
979+
},
980+
],
981+
},
982+
{
983+
code: "BigInt(x) != null",
984+
errors: [
985+
{
986+
messageId: "constantBinaryOperand",
987+
data: { otherSide: "right", operator: "!=" },
988+
},
989+
],
990+
},
991+
{
992+
code: "BigInt(x) != undefined",
993+
errors: [
994+
{
995+
messageId: "constantBinaryOperand",
996+
data: { otherSide: "right", operator: "!=" },
997+
},
998+
],
999+
},
9441000

9451001
// Binary expression with loose comparison to boolean
9461002
{
@@ -1476,6 +1532,24 @@ ruleTester.run("no-constant-binary-expression", rule, {
14761532
},
14771533
],
14781534
},
1535+
{
1536+
code: "true === Symbol(x)",
1537+
errors: [
1538+
{
1539+
messageId: "constantBinaryOperand",
1540+
data: { otherSide: "left", operator: "===" },
1541+
},
1542+
],
1543+
},
1544+
{
1545+
code: "true === BigInt(x)",
1546+
errors: [
1547+
{
1548+
messageId: "constantBinaryOperand",
1549+
data: { otherSide: "left", operator: "===" },
1550+
},
1551+
],
1552+
},
14791553
{
14801554
code: "Boolean(0) == !({})",
14811555
errors: [
@@ -1721,6 +1795,24 @@ ruleTester.run("no-constant-binary-expression", rule, {
17211795
},
17221796
],
17231797
},
1798+
{
1799+
code: "Symbol(x) === null",
1800+
errors: [
1801+
{
1802+
messageId: "constantBinaryOperand",
1803+
data: { otherSide: "right", operator: "===" },
1804+
},
1805+
],
1806+
},
1807+
{
1808+
code: "BigInt(x) === null",
1809+
errors: [
1810+
{
1811+
messageId: "constantBinaryOperand",
1812+
data: { otherSide: "right", operator: "===" },
1813+
},
1814+
],
1815+
},
17241816

17251817
// Binary expression with strict comparison to undefined
17261818
{
@@ -1957,6 +2049,24 @@ ruleTester.run("no-constant-binary-expression", rule, {
19572049
},
19582050
],
19592051
},
2052+
{
2053+
code: "Symbol(x) === undefined",
2054+
errors: [
2055+
{
2056+
messageId: "constantBinaryOperand",
2057+
data: { otherSide: "right", operator: "===" },
2058+
},
2059+
],
2060+
},
2061+
{
2062+
code: "BigInt(x) === undefined",
2063+
errors: [
2064+
{
2065+
messageId: "constantBinaryOperand",
2066+
data: { otherSide: "right", operator: "===" },
2067+
},
2068+
],
2069+
},
19602070

19612071
/*
19622072
* If both sides are newly constructed objects, we can tell they will

0 commit comments

Comments
 (0)