2 * @fileoverview The rule should warn against code that tries to compare against -0.
3 * @author Aladdin-ADD <hh_2013@foxmail.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "disallow comparing against -0",
17 category: "Possible Errors",
19 url: "https://eslint.org/docs/rules/no-compare-neg-zero"
26 unexpected: "Do not use the '{{operator}}' operator to compare against -0."
32 //--------------------------------------------------------------------------
34 //--------------------------------------------------------------------------
37 * Checks a given node is -0
38 * @param {ASTNode} node A node to check.
39 * @returns {boolean} `true` if the node is -0.
41 function isNegZero(node) {
42 return node.type === "UnaryExpression" && node.operator === "-" && node.argument.type === "Literal" && node.argument.value === 0;
44 const OPERATORS_TO_CHECK = new Set([">", ">=", "<", "<=", "==", "===", "!=", "!=="]);
47 BinaryExpression(node) {
48 if (OPERATORS_TO_CHECK.has(node.operator)) {
49 if (isNegZero(node.left) || isNegZero(node.right)) {
52 messageId: "unexpected",
53 data: { operator: node.operator }