forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno_loss_of_precision.rs
More file actions
581 lines (528 loc) · 19.6 KB
/
Copy pathno_loss_of_precision.rs
File metadata and controls
581 lines (528 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use std::borrow::Cow;
use cow_utils::CowUtils;
use oxc_ast::{AstKind, ast::NumericLiteral};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{AstNode, context::LintContext, rule::Rule};
fn no_loss_of_precision_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("This number literal will lose precision at runtime.")
.with_help("Use a number literal that can be represented accurately by a 64-bit floating-point number.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoLossOfPrecision;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow precision loss of number literal.
///
/// ### Why is this bad?
///
/// It can lead to unexpected results in certain situations.
/// For example, when performing mathematical operations.
///
/// In JavaScript, Numbers are stored as double-precision floating-point numbers
/// according to the IEEE 754 standard. Because of this, numbers can only
/// retain accuracy up to a certain amount of digits. If the programmer
/// enters additional digits, those digits will be lost in the conversion
/// to the Number type and will result in unexpected/incorrect behavior.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```javascript
/// var x = 2e999;
/// ```
///
/// ```javascript
/// var x = 9007199254740993;
/// ```
///
/// ```javascript
/// var x = 5123000000000000000000000000001;
/// ```
///
/// ```javascript
/// var x = 1230000000000000000000000.0;
/// ```
///
/// ```javascript
/// var x = 0X200000_0000000_1;
/// ```
///
/// Examples of **correct** code for this rule:
/// ```javascript
/// var x = 12345;
/// ```
///
/// ```javascript
/// var x = 123.456;
/// ```
///
/// ```javascript
/// var x = 123.0000000000000000000000;
/// ```
///
/// ```javascript
/// var x = 123e34;
/// ```
///
/// ```javascript
/// var x = 0x1FFF_FFFF_FFF_FFF;
/// ```
NoLossOfPrecision,
eslint,
correctness
);
impl Rule for NoLossOfPrecision {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::NumericLiteral(node) if Self::lose_precision(node) => {
ctx.diagnostic(no_loss_of_precision_diagnostic(node.span));
}
_ => {}
}
}
}
pub struct RawNum<'a> {
int: &'a str,
frac: &'a str,
exp: isize,
}
#[derive(Debug)]
pub struct ScientificNotation<'a> {
int: &'a str,
frac: Cow<'a, str>,
exp: isize,
}
impl PartialEq for ScientificNotation<'_> {
fn eq(&self, other: &Self) -> bool {
if self.int == other.int && self.frac == other.frac {
if self.int == "0" && self.frac.is_empty() {
return true;
}
return self.exp == other.exp;
}
false
}
}
impl<'a> RawNum<'a> {
fn new(num: &str) -> Option<RawNum<'_>> {
// remove sign
let num = num.trim_start_matches(['+', '-']);
let (int, num_without_int) = {
// skip leading zeros
let num_without_zeros = num.trim_start_matches('0');
// read the integer part and store the end index
let int_end = num_without_zeros
.chars()
.position(|ch| !ch.is_ascii_digit())
.unwrap_or(num_without_zeros.len());
// if no integer part was found, default to 0
let int = if int_end == 0 { "0" } else { &num_without_zeros[..int_end] };
// make a slice of the rest of the string
let num_without_int = &num_without_zeros[int_end..];
(int, num_without_int)
};
// if next char is a dot, parse the fractional part
let (frac, num_without_frac) =
num_without_int.strip_prefix('.').map_or(("", num_without_int), |num_without_dot| {
let frac_end = num_without_dot
.chars()
.position(|ch| !ch.is_ascii_digit())
.unwrap_or(num_without_dot.len());
// slice the fractional part and the rest of the string
let frac = &num_without_dot[..frac_end];
let num_without_frac = &num_without_dot[frac_end..];
(frac, num_without_frac)
});
// if next char is an e, treat the rest as the exponent
let exp =
num_without_frac.strip_prefix(['e', 'E']).map_or("0", |num_without_e| num_without_e);
let Ok(exp) = exp.parse::<isize>() else {
return None;
};
Some(RawNum { int, frac, exp })
}
fn normalize(&mut self) -> ScientificNotation<'a> {
if self.int == "0" && !self.frac.is_empty() {
let frac_zeros = self.frac.chars().take_while(|&ch| ch == '0').count();
#[expect(clippy::cast_possible_wrap)]
let exp = self.exp - 1 - frac_zeros as isize;
self.frac = &self.frac[frac_zeros..];
match self.frac.len() {
0 => ScientificNotation { int: "0", frac: Cow::Borrowed(""), exp },
1 => ScientificNotation { int: &self.frac[..1], frac: Cow::Borrowed(""), exp },
_ => ScientificNotation {
int: &self.frac[..1],
frac: Cow::Borrowed(&self.frac[1..]),
exp,
},
}
} else {
#[expect(clippy::cast_possible_wrap)]
let exp = self.exp + self.int.len() as isize - 1;
if self.int.len() == 1 {
ScientificNotation { int: self.int, frac: Cow::Borrowed(self.frac), exp }
} else {
let frac = if self.frac.is_empty() {
let int_trimmed = self.int.trim_end_matches('0');
if int_trimmed.len() == 1 {
Cow::Borrowed("")
} else {
Cow::Borrowed(&int_trimmed[1..])
}
} else {
Cow::Owned(format!("{}{}", &self.int[1..], self.frac))
};
ScientificNotation { int: &self.int[..1], frac, exp }
}
}
}
}
impl NoLossOfPrecision {
fn not_base_ten_loses_precision(node: &'_ NumericLiteral) -> bool {
let raw = node.raw.as_ref().unwrap().as_str().cow_replace('_', "");
let raw = raw.cow_to_ascii_uppercase();
#[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
// AST always store number as f64, need a cast to format in bin/oct/hex
let value = node.value as u64;
let suffix = if raw.starts_with("0B") {
format!("{value:b}")
} else if raw.starts_with("0X") {
format!("{value:x}")
} else {
format!("{value:o}")
};
!raw.ends_with(&suffix.cow_to_ascii_uppercase().as_ref())
}
fn base_ten_loses_precision(node: &'_ NumericLiteral) -> bool {
let raw = node.raw.as_ref().unwrap().as_str().cow_replace('_', "");
let Some(raw) = Self::normalize(&raw) else {
return true;
};
let total_significant_digits = raw.int.len() + raw.frac.len();
if total_significant_digits > 100 {
return true;
}
let stored = to_precision(node.value, total_significant_digits);
let Some(stored) = Self::normalize(&stored) else {
return true;
};
raw != stored
}
fn normalize(num: &str) -> Option<ScientificNotation<'_>> {
Some(RawNum::new(num)?.normalize())
}
pub fn lose_precision(node: &'_ NumericLiteral) -> bool {
if node.base.is_base_10() {
Self::base_ten_loses_precision(node)
} else {
Self::not_base_ten_loses_precision(node)
}
}
}
/// `flt_str_to_exp` - used in `to_precision`
///
/// This function traverses a string representing a number,
/// returning the floored log10 of this number.
#[expect(clippy::cast_possible_truncation)]
#[expect(clippy::cast_possible_wrap)]
fn flt_str_to_exp(flt: &str) -> i32 {
let mut non_zero_encountered = false;
let mut dot_encountered = false;
for (i, c) in flt.char_indices() {
if c == '.' {
if non_zero_encountered {
return (i as i32) - 1;
}
dot_encountered = true;
} else if c != '0' {
if dot_encountered {
return 1 - (i as i32);
}
non_zero_encountered = true;
}
}
(flt.len() as i32) - 1
}
/// `round_to_precision` - used in `to_precision`
///
/// This procedure has two roles:
/// - If there are enough or more than enough digits in the
/// string to show the required precision, the number
/// represented by these digits is rounded using string
/// manipulation.
/// - Else, zeroes are appended to the string.
/// - Additionally, sometimes the exponent was wrongly computed and
/// while up-rounding we find that we need an extra digit. When this
/// happens, we return true so that the calling context can adjust
/// the exponent. The string is kept at an exact length of `precision`.
///
/// When this procedure returns, `digits` is exactly `precision` long.
fn round_to_precision(digits: &mut String, precision: usize) -> bool {
if digits.len() > precision {
let to_round = digits.split_off(precision);
let mut digit =
digits.pop().expect("already checked that length is bigger than precision") as u8;
if let Some(first) = to_round.chars().next()
&& first > '4'
{
digit += 1;
}
if digit as char == ':' {
// ':' is '9' + 1
// need to propagate the increment backward
let mut replacement = String::from("0");
let mut propagated = false;
for c in digits.chars().rev() {
let d = match (c, propagated) {
('0'..='8', false) => (c as u8 + 1) as char,
(_, false) => '0',
(_, true) => c,
};
replacement.push(d);
if d != '0' {
propagated = true;
}
}
digits.clear();
let replacement = if propagated {
replacement.as_str()
} else {
digits.push('1');
&replacement.as_str()[1..]
};
for c in replacement.chars().rev() {
digits.push(c);
}
!propagated
} else {
digits.push(digit as char);
false
}
} else {
digits.push_str(&"0".repeat(precision - digits.len()));
false
}
}
/// Mimics JavaScript's `Number.prototype.toPrecision()` method
///
/// The `toPrecision()` method returns a string representing the Number object to the specified precision.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-number.prototype.toprecision
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
#[expect(clippy::cast_possible_truncation)]
#[expect(clippy::cast_possible_wrap)]
#[expect(clippy::cast_sign_loss)]
pub fn to_precision(mut num: f64, precision: usize) -> String {
// Validate precision range (1-100)
debug_assert!((1..=100).contains(&precision), "Precision must be between 1 and 100");
// Handle non-finite numbers
if !num.is_finite() {
if num.is_nan() {
return "NaN".to_string();
} else if num.is_infinite() {
return if num.is_sign_positive() { "Infinity" } else { "-Infinity" }.to_string();
}
}
let precision_i32 = precision as i32;
// Handle sign
let mut prefix = String::new();
if num < 0.0 {
prefix.push('-');
num = -num;
}
let mut suffix: String;
let mut exponent: i32;
// Handle zero
if num == 0.0 {
suffix = "0".repeat(precision);
exponent = 0;
} else {
// Format with maximum precision to get all digits
suffix = format!("{num:.100}");
// Calculate exponent
exponent = flt_str_to_exp(&suffix);
// Extract relevant digits only
if exponent < 0 {
suffix = suffix.split_off((1 - exponent) as usize);
} else if let Some(n) = suffix.find('.') {
suffix.remove(n);
}
// Round to the specified precision
if round_to_precision(&mut suffix, precision) {
exponent += 1;
}
// Decide between scientific and fixed notation
let great_exp = exponent >= precision_i32;
if exponent < -6 || great_exp {
// Use scientific notation
if precision > 1 {
suffix.insert(1, '.');
}
suffix.push('e');
if great_exp {
suffix.push('+');
}
suffix.push_str(&exponent.to_string());
return prefix + &suffix;
}
}
// Use fixed-point notation
let e_inc = exponent + 1;
if e_inc == precision_i32 {
return prefix + &suffix;
}
if exponent >= 0 {
suffix.insert(e_inc as usize, '.');
} else {
prefix.push('0');
prefix.push('.');
prefix.push_str(&"0".repeat(-e_inc as usize));
}
prefix + &suffix
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
("var x = 12345", None),
("var x = 123.456", None),
("var x = -123.456", None),
("var x = -123456", None),
("var x = 123e34", None),
("var x = 123.0e34", None),
("var x = 123e-34", None),
("var x = -123e34", None),
("var x = -123e-34", None),
("var x = 12.3e34", None),
("var x = 12.3e-34", None),
("var x = -12.3e34", None),
("var x = -12.3e-34", None),
("var x = 12300000000000000000000000", None),
("var x = -12300000000000000000000000", None),
("var x = 0.00000000000000000000000123", None),
("var x = -0.00000000000000000000000123", None),
("var x = 9007199254740991", None),
("var x = 0", None),
("var x = 0.0", None),
(
"var x = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000",
None,
),
("var x = -0", None),
("var x = 123.0000000000000000000000", None),
("var x = 019.5", None),
("var x = 0195", None),
("var x = 0e5", None),
("var x = 12_34_56", None),
("var x = 12_3.4_56", None),
("var x = -12_3.4_56", None),
("var x = -12_34_56", None),
("var x = 12_3e3_4", None),
("var x = 123.0e3_4", None),
("var x = 12_3e-3_4", None),
("var x = 12_3.0e-3_4", None),
("var x = -1_23e-3_4", None),
("var x = -1_23.8e-3_4", None),
("var x = 1_230000000_00000000_00000_000", None),
("var x = -1_230000000_00000000_00000_000", None),
("var x = 0.0_00_000000000_000000000_00123", None),
("var x = -0.0_00_000000000_000000000_00123", None),
("var x = 0e5_3", None),
("var x = 0b11111111111111111111111111111111111111111111111111111", None),
("var x = 0b111_111_111_111_1111_11111_111_11111_1111111111_11111111_111_111", None),
("var x = 0B11111111111111111111111111111111111111111111111111111", None),
("var x = 0B111_111_111_111_1111_11111_111_11111_1111111111_11111111_111_111", None),
("var x = 0o377777777777777777", None),
("var x = 0o3_77_777_777_777_777_777", None),
("var x = 0O377777777777777777", None),
// ("var x = 0377777777777777777", None), /* '0'-prefixed octal literals and octal escape sequences are deprecated */
("var x = 0x1FFFFFFFFFFFFF", None),
("var x = 0X1FFFFFFFFFFFFF", None),
("var x = true", None),
("var x = 'abc'", None),
("var x = ''", None),
("var x = null", None),
("var x = undefined", None),
("var x = {}", None),
("var x = ['a', 'b']", None),
("var x = new Date()", None),
("var x = '9007199254740993'", None),
("var x = 0x1FFF_FFFF_FFF_FFF", None),
("var x = 0X1_FFF_FFFF_FFF_FFF", None),
("var a = Infinity", None),
("var a = 480.00", None),
("var a = -30.00", None),
("let a = Infinity", None),
("let a = 480.00", None),
("let a = -30.00", None),
("const a = Infinity", None),
("const a = 480.00", None),
("const a = -30.00", None),
("(1000000000000000128).toFixed(0)", None),
];
let fail = vec![
("var x = 9007199254740993", None),
("var x = 9007199254740.993e3", None),
("var x = 9.007199254740993e15", None),
("var x = -9007199254740993", None),
("var x = 900719.9254740994", None),
("var x = -900719.9254740994", None),
("var x = 900719925474099_3", None),
("var x = 90_0719925_4740.9_93e3", None),
("var x = 9.0_0719925_474099_3e15", None),
("var x = -9_00719_9254_740993", None),
("var x = 900_719.92_54740_994", None),
("var x = -900_719.92_5474_0994", None),
("let x = 9.0_0719925_474099_3e15", None),
("let x = -9_00719_9254_740993", None),
("let x = 900_719.92_54740_994", None),
("let x = -900_719.92_5474_0994", None),
("const x = 9.0_0719925_474099_3e15", None),
("const x = -9_00719_9254_740993", None),
("const x = 900_719.92_54740_994", None),
("const x = -900_719.92_5474_0994", None),
("var x = 5123000000000000000000000000001", None),
("var x = -5123000000000000000000000000001", None),
("var x = 1230000000000000000000000.0", None),
("var x = 1.0000000000000000000000123", None),
(
"var x = 17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430592",
None,
),
("var x = 2e999", None),
("var x = .1230000000000000000000000", None),
("var x = 0b100000000000000000000000000000000000000000000000000001", None),
("var x = 0B100000000000000000000000000000000000000000000000000001", None),
("var x = 0o400000000000000001", None),
("var x = 0O400000000000000001", None),
("var x = 0400000000000000001", None),
("var x = 0x20000000000001", None),
("var x = 0X20000000000001", None),
("var x = 5123_00000000000000000000000000_1", None),
("var x = -5_12300000000000000000000_0000001", None),
("var x = 123_00000000000000000000_00.0_0", None),
("var x = 1.0_00000000000000000_0000123", None),
(
"var x = 174_980057982_640953949800178169_409709228253554471456994_914061648512796239935950073857881054_1618443059_2",
None,
),
("var x = 2e9_99", None),
("var x = .1_23000000000000_00000_0000_0", None),
("var x = 0b1_0000000000000000000000000000000000000000000000000000_1", None),
("var x = 0B10000000000_0000000000000000000000000000_000000000000001", None),
("var x = 0o4_00000000000000_001", None),
("var x = 0O4_0000000000000000_1", None),
("var x = 0x2_0000000000001", None),
("var x = 0X200000_0000000_1", None),
("var x = 1e18_446_744_073_709_551_615", None),
];
Tester::new(NoLossOfPrecision::NAME, NoLossOfPrecision::PLUGIN, pass, fail).test_and_snapshot();
}