Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): numeric separator plugin. (#2795)
[es2021 numeric separator](https://babeljs.io/docs/babel-plugin-transform-numeric-separator)
- Loading branch information
Showing
8 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod logical_assignment_operators; | ||
mod numeric_separator; | ||
|
||
pub use logical_assignment_operators::LogicalAssignmentOperators; | ||
pub use numeric_separator::NumericSeparator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters