Skip to content

Commit 08de88e

Browse files
feat: handle underflow in no-loss-of-precision (#21218)
* fix: handle underflow in no-loss-of-precision * chore: retrigger CI * refactor: address review feedback
1 parent 9ef407a commit 08de88e

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎lib/rules/no-loss-of-precision.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ function baseTenLosesPrecision(node) {
183183
rawNumber,
184184
false,
185185
);
186+
187+
if (node.value === 0) {
188+
return !/^0+$/u.test(normalizedRawNumber.coefficient);
189+
}
190+
186191
const requestedPrecision = normalizedRawNumber.coefficient.length;
187192

188193
if (requestedPrecision > 100) {
@@ -236,7 +241,7 @@ module.exports = {
236241
create(context) {
237242
return {
238243
Literal(node) {
239-
if (node.value && isNumber(node) && losesPrecision(node)) {
244+
if (isNumber(node) && losesPrecision(node)) {
240245
context.report({
241246
messageId: "noLossOfPrecision",
242247
node,

‎tests/lib/rules/no-loss-of-precision.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ruleTester.run("no-loss-of-precision", rule, {
6161
"var x = 00195",
6262
"var x = 0008",
6363
"var x = 0e5",
64+
"var x = 5e-324",
6465
"var x = .42",
6566
"var x = 42.",
6667

@@ -340,6 +341,18 @@ ruleTester.run("no-loss-of-precision", rule, {
340341
languageOptions: { ecmaVersion: 2021 },
341342
errors: [{ messageId: "noLossOfPrecision" }],
342343
},
344+
{
345+
code: "var x = 1e-350",
346+
errors: [{ messageId: "noLossOfPrecision" }],
347+
},
348+
{
349+
code: "var x = 1e-324",
350+
errors: [{ messageId: "noLossOfPrecision" }],
351+
},
352+
{
353+
code: "var x = -1e-350",
354+
errors: [{ messageId: "noLossOfPrecision" }],
355+
},
343356
],
344357
});
345358

0 commit comments

Comments
 (0)