forked from oxc-project/oxc
/
numeric_separator.rs
36 lines (30 loc) · 1.01 KB
/
numeric_separator.rs
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
use oxc_ast::ast::*;
use oxc_span::Atom;
use crate::{
context::TransformerCtx,
options::{TransformOptions, TransformTarget},
};
/// ES2021: Numeric Separator
///
/// References:
/// * <https://babeljs.io/docs/babel-plugin-transform-numeric-separator>
pub struct NumericSeparator<'a> {
ctx: TransformerCtx<'a>,
}
impl<'a> NumericSeparator<'a> {
#![allow(clippy::unused_self)]
pub fn new(ctx: TransformerCtx<'a>, options: &TransformOptions) -> Option<Self> {
(options.target < TransformTarget::ES2021 || options.numeric_separator)
.then_some(Self { ctx })
}
pub fn transform_number_literal(&mut self, lit: &mut NumericLiteral<'a>) {
if !lit.raw.is_empty() {
lit.raw = self.ctx.ast.new_str(lit.raw.replace('_', "").as_str());
}
}
pub fn transform_bigint_literal(&mut self, lit: &mut BigIntLiteral<'a>) {
if !lit.raw.is_empty() {
lit.raw = Atom::from(self.ctx.ast.new_str(lit.raw.replace('_', "").as_str()));
}
}
}