Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transformer): numeric separator plugin. #2795

Merged
merged 24 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
07271c2
feat(transformer): numeric separator plugin.
rzvxa Mar 23, 2024
eb54d89
refactor(ast): use CompactStr instead of Atom for BigIntLiteral's value.
rzvxa Mar 23, 2024
6111184
feat(trans): add support for big int numeric seperators.
rzvxa Mar 23, 2024
3262446
fix: remove the code left behind by mistake.
rzvxa Mar 23, 2024
6c39138
refactor(codegen): change how numeric literals are generated.
rzvxa Mar 24, 2024
9c04b72
refactor(trans): use ast allocator to modify numeric raw values.
rzvxa Mar 24, 2024
d3615a6
feat(transformer): add transform literal for numeric literals.
rzvxa Mar 24, 2024
b59d0cf
fix(trans): clippy issues.
rzvxa Mar 24, 2024
72acedf
Merge branch 'trans-lit' of https://github.com/rzvxa/oxc into num-sep
rzvxa Mar 24, 2024
08609eb
fix(trans): issue in pattern matching octal and binary lits.
rzvxa Mar 24, 2024
cc3de9b
Merge branch 'trans-lit' of https://github.com/rzvxa/oxc into num-sep
rzvxa Mar 24, 2024
89dbb0b
fix(codegen): lowering characters casing in numeric literals.
rzvxa Mar 24, 2024
ba7c4d0
fix(codegen): revert lowering numberic literals in the generator.
rzvxa Mar 24, 2024
d007d8e
refactor(ast): revert `BigIntLiteral` `raw` field.
rzvxa Mar 25, 2024
2eb7568
test(conformance/babel): update the snapshot to allow failing test.
rzvxa Mar 25, 2024
43502fa
test(conformance/babel): update snapshot.
rzvxa Mar 25, 2024
6821886
test(conformance/babel): update snapshot.
rzvxa Mar 25, 2024
cf145da
Merge branch 'main' into num-sep
rzvxa Mar 25, 2024
eb12222
Merge branch 'main' into trans-lit
rzvxa Mar 25, 2024
058e806
Merge branch 'trans-lit' of https://github.com/rzvxa/oxc into num-sep
rzvxa Mar 25, 2024
934135c
Merge branch 'num-sep' of https://github.com/rzvxa/oxc into num-sep
rzvxa Mar 25, 2024
86eb172
Merge branch 'main' into num-sep
rzvxa Mar 25, 2024
a6e459f
fix(conformance): revert babel snapshot.
rzvxa Mar 26, 2024
dcbde1d
Merge branch 'main' of https://github.com/oxc-project/oxc into num-sep
rzvxa Mar 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions crates/oxc_codegen/src/gen.rs
Expand Up @@ -1167,11 +1167,7 @@ fn print_non_negative_float<const MINIFY: bool>(value: f64, _p: &Codegen<{ MINIF
impl<'a, const MINIFY: bool> Gen<MINIFY> for BigIntLiteral<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
p.add_source_mapping(self.span.start);
if self.raw.contains('_') {
p.print_str(self.raw.replace('_', "").as_bytes());
} else {
p.print_str(self.raw.as_bytes());
}
p.print_str(self.raw.as_bytes());
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2015/literals.rs
Dunqing marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -33,7 +33,7 @@ impl<'a> Literals<'a> {

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.
// go generate them from their value.
lit.raw = "";
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_transformer/src/es2021/mod.rs
@@ -1,3 +1,5 @@
mod logical_assignment_operators;
mod numeric_separator;

pub use logical_assignment_operators::LogicalAssignmentOperators;
pub use numeric_separator::NumericSeparator;
36 changes: 36 additions & 0 deletions crates/oxc_transformer/src/es2021/numeric_separator.rs
@@ -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()));
}
}
}
19 changes: 10 additions & 9 deletions crates/oxc_transformer/src/lib.rs
Expand Up @@ -49,7 +49,7 @@ use crate::{
es2016::ExponentiationOperator,
es2019::{JsonStrings, OptionalCatchBinding},
es2020::NullishCoalescingOperator,
es2021::LogicalAssignmentOperators,
es2021::{LogicalAssignmentOperators, NumericSeparator},
es2022::ClassStaticBlock,
es3::PropertyLiteral,
react_jsx::ReactJsx,
Expand Down Expand Up @@ -78,6 +78,7 @@ pub struct Transformer<'a> {
es2022_class_static_block: Option<ClassStaticBlock<'a>>,
// es2021
es2021_logical_assignment_operators: Option<LogicalAssignmentOperators<'a>>,
es2021_numeric_separator: Option<NumericSeparator<'a>>,
// es2020
es2020_nullish_coalescing_operators: Option<NullishCoalescingOperator<'a>>,
// es2019
Expand Down Expand Up @@ -120,6 +121,7 @@ impl<'a> Transformer<'a> {
es2022_class_static_block: es2022::ClassStaticBlock::new(Rc::clone(&ast), &options),
// es2021
es2021_logical_assignment_operators: LogicalAssignmentOperators::new(Rc::clone(&ast), ctx.clone(), &options),
es2021_numeric_separator: NumericSeparator::new(ctx.clone(), &options),
// es2020
es2020_nullish_coalescing_operators: NullishCoalescingOperator::new(Rc::clone(&ast), ctx.clone(), &options),
// es2019
Expand Down Expand Up @@ -247,21 +249,20 @@ impl<'a> VisitMut<'a> for Transformer<'a> {
}

fn visit_directive(&mut self, directive: &mut Directive<'a>) {
self.es2019_json_strings
.as_mut()
.map(|t: &mut JsonStrings| t.transform_directive(directive));
// TODO: we didn't walk this through, but maybe we should?
// walk_directive_mut(self, directive);
self.es2019_json_strings.as_mut().map(|t| t.transform_directive(directive));
}

fn visit_number_literal(&mut self, lit: &mut NumericLiteral<'a>) {
self.es2021_numeric_separator.as_mut().map(|t| t.transform_number_literal(lit));
self.es2015_literals.as_mut().map(|t| t.transform_number_literal(lit));
}

fn visit_bigint_literal(&mut self, lit: &mut BigIntLiteral<'a>) {
self.es2021_numeric_separator.as_mut().map(|t| t.transform_bigint_literal(lit));
}

fn visit_string_literal(&mut self, lit: &mut StringLiteral<'a>) {
self.es2019_json_strings
.as_mut()
.map(|t: &mut JsonStrings| t.transform_string_literal(lit));
self.es2019_json_strings.as_mut().map(|t| t.transform_string_literal(lit));
self.es2015_literals.as_mut().map(|t| t.transform_string_literal(lit));
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/options.rs
Expand Up @@ -18,6 +18,7 @@ pub struct TransformOptions {
pub class_static_block: bool,
// es2021
pub logical_assignment_operators: bool,
pub numeric_separator: bool,
// es2020
pub nullish_coalescing_operator: Option<NullishCoalescingOperatorOptions>,
// es2019
Expand Down
6 changes: 2 additions & 4 deletions tasks/transform_conformance/babel.snap.md
@@ -1,6 +1,7 @@
Passed: 319/1415
Passed: 320/1415

# All Passed:
* babel-plugin-transform-numeric-separator
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-json-strings
* babel-plugin-transform-shorthand-properties
Expand Down Expand Up @@ -519,9 +520,6 @@ Passed: 319/1415
# babel-plugin-transform-logical-assignment-operators (5/6)
* logical-assignment/null-coalescing/input.js

# babel-plugin-transform-numeric-separator (1/2)
* numeric-separator/used-with-transform-literals/input.js

# babel-plugin-transform-export-namespace-from (0/4)
* export-namespace/namespace-default/input.mjs
* export-namespace/namespace-es6/input.mjs
Expand Down
1 change: 1 addition & 0 deletions tasks/transform_conformance/src/test_case.rs
Expand Up @@ -109,6 +109,7 @@ pub trait TestCase {
logical_assignment_operators: options
.get_plugin("transform-logical-assignment-operators")
.is_some(),
numeric_separator: options.get_plugin("transform-numeric-separator").is_some(),
nullish_coalescing_operator: options
.get_plugin("transform-nullish-coalescing-operator")
.map(get_options::<NullishCoalescingOperatorOptions>),
Expand Down