]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-compare-neg-zero.js
9715c2f0f37226c70a21ac5ccb480e5bcbc6c080
2 * @fileoverview The rule should warn against code that tries to compare against -0.
3 * @author Aladdin-ADD <hh_2013@foxmail.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 /** @type {import('../shared/types').Rule} */
17 description
: "Disallow comparing against -0",
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
}