Skip to content

Commit 686630e

Browse files
authored
fix: no-loss-of-precision false positive with 0.e5 (#21337)
1 parent 3782dd4 commit 686630e

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ function convertNumberToScientificNotation(stringNumber, parseAsFloat) {
178178
* @returns {boolean} true if they do not match
179179
*/
180180
function baseTenLosesPrecision(node) {
181-
const rawNumber = getRaw(node).toLowerCase().replace(/\.$/u, "");
181+
const rawNumber = getRaw(node)
182+
.toLowerCase()
183+
.replace(/\.(?=e|$)/u, "");
182184
const normalizedRawNumber = convertNumberToScientificNotation(
183185
rawNumber,
184186
false,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ ruleTester.run("no-loss-of-precision", rule, {
6565
"var x = 5e-324",
6666
"var x = .42",
6767
"var x = 42.",
68+
"var x = 0.e5",
69+
"var x = 0.E5",
70+
"var x = 0.e-5",
71+
"var x = 1.e5",
72+
"var x = 42.e0",
73+
"var x = 1230000000000000000000000.e0",
6874

6975
{ code: "var x = 12_34_56", languageOptions: { ecmaVersion: 2021 } },
7076
{ code: "var x = 12_3.4_56", languageOptions: { ecmaVersion: 2021 } },
@@ -96,6 +102,11 @@ ruleTester.run("no-loss-of-precision", rule, {
96102
languageOptions: { ecmaVersion: 2021 },
97103
},
98104
{ code: "var x = 0e5_3", languageOptions: { ecmaVersion: 2021 } },
105+
{ code: "var x = 12_3.e3_4", languageOptions: { ecmaVersion: 2021 } },
106+
{
107+
code: "var x = 1_230000000_00000000_00000_000.e0",
108+
languageOptions: { ecmaVersion: 2021 },
109+
},
99110

100111
{
101112
code: "var x = 0b11111111111111111111111111111111111111111111111111111",
@@ -159,6 +170,10 @@ ruleTester.run("no-loss-of-precision", rule, {
159170
code: "var x = 9007199254740993.",
160171
errors: [{ messageId: "noLossOfPrecision" }],
161172
},
173+
{
174+
code: "var x = 9007199254740993.e0",
175+
errors: [{ messageId: "noLossOfPrecision" }],
176+
},
162177
{
163178
code: "var x = 9007199254740.993e3",
164179
errors: [{ messageId: "noLossOfPrecision" }],
@@ -233,6 +248,10 @@ ruleTester.run("no-loss-of-precision", rule, {
233248
code: "var x = 1230000000000000000000000.0",
234249
errors: [{ messageId: "noLossOfPrecision" }],
235250
},
251+
{
252+
code: "var x = 1230000000000000000000000.0e0",
253+
errors: [{ messageId: "noLossOfPrecision" }],
254+
},
236255
{
237256
code: "var x = 1.0000000000000000000000123",
238257
errors: [{ messageId: "noLossOfPrecision" }],

0 commit comments

Comments
 (0)