Skip to content

Commit

Permalink
fix: JsNumberLiteralExpression::as_number ignore trivia (#1447)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
TheEdward162 and ematipico authored Jan 6, 2024
1 parent 01ada2e commit ec403e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,32 @@ invalid.js:59:1 lint/style/useNumericLiterals FIXABLE ━━━━━━━━
61 61parseInt('11', 2)//comment


```
```
invalid.js:60:1 lint/style/useNumericLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This call to parseInt() can be replaced by a binary literal.

58parseInt('11'/**/, 2);
59parseInt(`11`/**/, 2);
> 60parseInt('11', 2 /**/);
^^^^^^^^^^^^^^^^^^^^^^
61parseInt('11', 2)//comment
62 │ ;

i Using a literal avoids unnecessary computations.

i Unsafe fix: Use the computed binary literal instead.

58 58parseInt('11'/**/, 2);
59 59parseInt(`11`/**/, 2);
60- parseInt('11'2·/**/);
60+ 0b11;
61 61parseInt('11', 2)//comment
62 62 │ ;


```
```
Expand Down
12 changes: 11 additions & 1 deletion crates/biome_js_syntax/src/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,18 @@ impl JsObjectExpression {
}

impl JsNumberLiteralExpression {
/// ## Examples
///
/// ```
/// use biome_js_factory::make;
/// use biome_rowan::TriviaPieceKind;
///
/// let number = make::js_number_literal_expression(make::js_number_literal("1.23")
/// .with_trailing_trivia(vec![(TriviaPieceKind::Whitespace, " ")]));
/// assert_eq!(number.as_number().unwrap(), 1.23);
/// ```
pub fn as_number(&self) -> Option<f64> {
parse_js_number(self.value_token().unwrap().text())
parse_js_number(self.value_token().unwrap().text_trimmed())
}
}

Expand Down

0 comments on commit ec403e8

Please sign in to comment.