Skip to content

Commit 0b4a73b

Browse files
author
den$
authored
feat: add suggestions to no-compare-neg-zero (#21034)
* fixing 21025 * minor tweaks * simplify suggestion fixers * suggestion effects
1 parent 993539f commit 0b4a73b

2 files changed

Lines changed: 363 additions & 8 deletions

File tree

‎lib/rules/no-compare-neg-zero.js‎

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
*/
55
"use strict";
66

7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
const { getVariableByName } = require("./utils/ast-utils");
12+
713
//------------------------------------------------------------------------------
814
// Rule Definition
915
//------------------------------------------------------------------------------
@@ -20,15 +26,24 @@ module.exports = {
2026
},
2127

2228
fixable: null,
29+
hasSuggestions: true,
2330
schema: [],
2431

2532
messages: {
2633
unexpected:
2734
"Do not use the '{{operator}}' operator to compare against -0.",
35+
suggestRemoveMinus:
36+
"Replace '-0' with '0' (keeps the current comparison behavior).",
37+
suggestObjectIs:
38+
"Replace with 'Object.is()' (changes the comparison to distinguish -0 from +0).",
39+
suggestNotObjectIs:
40+
"Replace with '!Object.is()' (changes the comparison to distinguish -0 from +0).",
2841
},
2942
},
3043

3144
create(context) {
45+
const sourceCode = context.sourceCode;
46+
3247
//--------------------------------------------------------------------------
3348
// Helpers
3449
//--------------------------------------------------------------------------
@@ -46,6 +61,18 @@ module.exports = {
4661
node.argument.value === 0
4762
);
4863
}
64+
65+
/**
66+
* Gets the source text of an operand, keeping parentheses where required.
67+
* @param {ASTNode} operand A comparison operand node.
68+
* @returns {string} The source text of the operand.
69+
*/
70+
function getOperandText(operand) {
71+
const text = sourceCode.getText(operand);
72+
73+
return operand.type === "SequenceExpression" ? `(${text})` : text;
74+
}
75+
4976
const OPERATORS_TO_CHECK = new Set([
5077
">",
5178
">=",
@@ -59,15 +86,71 @@ module.exports = {
5986

6087
return {
6188
BinaryExpression(node) {
62-
if (OPERATORS_TO_CHECK.has(node.operator)) {
63-
if (isNegZero(node.left) || isNegZero(node.right)) {
64-
context.report({
65-
node,
66-
messageId: "unexpected",
67-
data: { operator: node.operator },
68-
});
69-
}
89+
if (!OPERATORS_TO_CHECK.has(node.operator)) {
90+
return;
7091
}
92+
93+
const leftIsNegZero = isNegZero(node.left);
94+
const rightIsNegZero = isNegZero(node.right);
95+
96+
if (!leftIsNegZero && !rightIsNegZero) {
97+
return;
98+
}
99+
100+
context.report({
101+
node,
102+
messageId: "unexpected",
103+
data: { operator: node.operator },
104+
suggest: [
105+
{
106+
messageId: "suggestRemoveMinus",
107+
*fix(fixer) {
108+
if (leftIsNegZero) {
109+
yield fixer.replaceText(node.left, "0");
110+
}
111+
if (rightIsNegZero) {
112+
yield fixer.replaceText(node.right, "0");
113+
}
114+
},
115+
},
116+
{
117+
messageId:
118+
node.operator === "==="
119+
? "suggestObjectIs"
120+
: "suggestNotObjectIs",
121+
fix(fixer) {
122+
if (
123+
(node.operator !== "===" &&
124+
node.operator !== "!==") ||
125+
sourceCode.getCommentsInside(node).length >
126+
0
127+
) {
128+
return null;
129+
}
130+
131+
const objectVariable = getVariableByName(
132+
sourceCode.getScope(node),
133+
"Object",
134+
);
135+
136+
if (
137+
!objectVariable ||
138+
objectVariable.identifiers.length > 0
139+
) {
140+
return null;
141+
}
142+
143+
const negation =
144+
node.operator === "===" ? "" : "!";
145+
146+
return fixer.replaceText(
147+
node,
148+
`${negation}Object.is(${getOperandText(node.left)}, ${getOperandText(node.right)})`,
149+
);
150+
},
151+
},
152+
],
153+
});
71154
},
72155
};
73156
},

0 commit comments

Comments
 (0)