Skip to content

Commit 676f4ac

Browse files
authored
fix: allow scientific notation with trailing zeros matching exponent (#20002)
1 parent 327c672 commit 676f4ac

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,32 @@ module.exports = {
189189
* @returns {boolean} true if they do not match
190190
*/
191191
function baseTenLosesPrecision(node) {
192-
const normalizedRawNumber = convertNumberToScientificNotation(
193-
getRaw(node),
194-
);
192+
const rawNumber = getRaw(node);
193+
194+
/*
195+
* If trailing zeros equal the exponent, this is a valid representation
196+
* like "9.00e2" where 00 = 2 (meaning 9.00 * 10^2 = 900)
197+
* https://github.com/eslint/eslint/issues/19957
198+
*/
199+
if (rawNumber.includes(".") && rawNumber.includes("e")) {
200+
const parts = rawNumber.split("e");
201+
const coefficient = parts[0];
202+
const exponent = parseInt(parts[1], 10);
203+
204+
// Count trailing zeros after decimal
205+
const decimalParts = coefficient.split(".");
206+
if (decimalParts.length === 2) {
207+
const decimalPart = decimalParts[1];
208+
const trailingZeros = decimalPart.match(/0*$/u)[0].length;
209+
210+
if (trailingZeros === exponent) {
211+
return false;
212+
}
213+
}
214+
}
215+
216+
const normalizedRawNumber =
217+
convertNumberToScientificNotation(rawNumber);
195218
const requestedPrecision = normalizedRawNumber
196219
.split("e")[0]
197220
.replace(".", "").length;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ ruleTester.run("no-loss-of-precision", rule, {
4848
"var x = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000",
4949
"var x = -0",
5050
"var x = 123.0000000000000000000000",
51+
// https://github.com/eslint/eslint/issues/19957
52+
"var x = 9.00e2",
53+
"var x = 9.000e3",
54+
"var x = 9.0000000000e10",
5155
"var x = 019.5",
5256
"var x = 0195",
5357
"var x = 0e5",

0 commit comments

Comments
 (0)