-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): add transform literal for numeric literals. (#2797)
[es2015 transform literals](https://babeljs.io/docs/babel-plugin-transform-literals) --------- Co-authored-by: Dunqing <[email protected]>
- Loading branch information
Showing
5 changed files
with
57 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::rc::Rc; | ||
|
||
use oxc_ast::{ast::*, AstBuilder}; | ||
|
||
use crate::{ | ||
context::TransformerCtx, | ||
options::{TransformOptions, TransformTarget}, | ||
}; | ||
|
||
/// ES2015: Shorthand Properties | ||
/// | ||
/// References: | ||
/// * <https://babeljs.io/docs/babel-plugin-transform-literals> | ||
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-literals/src/index.ts> | ||
pub struct Literals<'a> { | ||
_ast: Rc<AstBuilder<'a>>, | ||
} | ||
|
||
impl<'a> Literals<'a> { | ||
#![allow(clippy::unused_self)] | ||
|
||
pub fn new(ctx: TransformerCtx<'a>, options: &TransformOptions) -> Option<Self> { | ||
(options.target < TransformTarget::ES2015 || options.literals) | ||
.then_some(Self { _ast: ctx.ast }) | ||
} | ||
|
||
pub fn transform_number_literal(&mut self, lit: &mut NumericLiteral<'a>) { | ||
// early return if number's raw value is empty or shorter than 2 characters, | ||
// both `0bxxx` and `0oxxx` need at least 3 characters be defined. | ||
if lit.raw.len() <= 2 { | ||
return; | ||
} | ||
|
||
if let [b'0', b'b' | b'B' | b'o' | b'O'] = lit.raw[0..2].as_bytes() { | ||
// Set binary and octal raw values to empty, It would force the codegen, | ||
// to generate them from their value. | ||
lit.raw = ""; | ||
} | ||
} | ||
|
||
pub fn transform_string_literal(&mut self, _: &mut StringLiteral<'a>) { | ||
// TODO: As of today oxc_lexer takes care of this, We have to rework it | ||
// so it can be controlled via the transformer. | ||
} | ||
} |
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