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

add numeric-separator transformer #7280

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/oxc_transformer/src/es2021/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ use oxc_traverse::{Traverse, TraverseCtx};
use crate::TransformCtx;

mod logical_assignment_operators;
mod numeric_separator;
mod options;

pub use logical_assignment_operators::LogicalAssignmentOperators;
pub use numeric_separator::NumericSeparator;
pub use options::ES2021Options;

pub struct ES2021<'a, 'ctx> {
options: ES2021Options,

// Plugins
logical_assignment_operators: LogicalAssignmentOperators<'a, 'ctx>,
numeric_separator: NumericSeparator<'a, 'ctx>,
}

impl<'a, 'ctx> ES2021<'a, 'ctx> {
pub fn new(options: ES2021Options, ctx: &'ctx TransformCtx<'a>) -> Self {
Self { logical_assignment_operators: LogicalAssignmentOperators::new(ctx), options }
Self {
logical_assignment_operators: LogicalAssignmentOperators::new(ctx),
numeric_separator: NumericSeparator::new(ctx),
options,
}
}
}

Expand All @@ -27,5 +34,8 @@ impl<'a, 'ctx> Traverse<'a> for ES2021<'a, 'ctx> {
if self.options.logical_assignment_operators {
self.logical_assignment_operators.enter_expression(expr, ctx);
}
if self.options.numeric_separator {
self.numeric_separator.enter_expression(expr, ctx);
}
}
}
79 changes: 79 additions & 0 deletions crates/oxc_transformer/src/es2021/numeric_separator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! ES2021: Numeric Separator
//!
//! This plugin remove underscore(_) in number (100_0_0).
//!
//! > This plugin is included in `preset-env`, in ES2021
//!
//! ## Example
//!
//! Input:
//! ```js
//! Decimal Literals
//! let budget = 1_000_000_000_000;
//!
//! Binary Literals
//! let nibbles = 0b1010_0001_1000_0101;
//!
//! Hex Literal
//! let message = 0xa0_b0_c0;
//! ```
//!
//! Output:
//! ```js
//! Decimal Literals
//! let budget = 1000000000000;
//!
//! Binary Literals
//! let nibbles = 0b1010000110000101;
//!
//! Hex Literal
//! let message = 0xa0b0c0;
//! ```
//!
//! ## Implementation
//!
//! Implementation based on [@babel/plugin-transform-numeric-separator](https://babel.dev/docs/babel-plugin-transform-numeric-separator).
//!
//! ## References:
//! * Babel plugin implementation: <https://github.com/babel/babel/blob/v7.26.2/packages/babel-plugin-transform-numeric-separator/src/index.ts>
//! * Numeric Separator TC39 proposal: <https://github.com/tc39/proposal-numeric-separator?tab=readme-ov-file>

use oxc_ast::ast::*;
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::TransformCtx;

pub struct NumericSeparator<'a, 'ctx> {
_ctx: &'ctx TransformCtx<'a>,
}

impl<'a, 'ctx> NumericSeparator<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
Self { _ctx: ctx }
}
}

const SEPARATOR: &str = "_";

impl<'a, 'ctx> Traverse<'a> for NumericSeparator<'a, 'ctx> {
fn enter_numeric_literal(&mut self, node: &mut NumericLiteral<'a>, ctx: &mut TraverseCtx<'a>) {
let raw = node.raw;
if !raw.contains(SEPARATOR) {
return;
}
let new_raw = raw.replace(SEPARATOR, "");
let new_node = ctx.ast.numeric_literal(SPAN, node.value, new_raw, node.base);
*node = new_node;
}

fn enter_big_int_literal(&mut self, node: &mut BigIntLiteral<'a>, ctx: &mut TraverseCtx<'a>) {
let raw = &node.raw;
if !raw.contains(SEPARATOR) {
return;
}
let new_raw = raw.replace(SEPARATOR, "");
let new_node = ctx.ast.big_int_literal(SPAN, new_raw, node.base);
*node = new_node;
}
}
2 changes: 2 additions & 0 deletions crates/oxc_transformer/src/es2021/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ use serde::Deserialize;
pub struct ES2021Options {
#[serde(skip)]
pub logical_assignment_operators: bool,
#[serde(skip)]
pub numeric_separator: bool,
}
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/options/babel/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub struct BabelPlugins {
pub nullish_coalescing_operator: bool,
// ES2021
pub logical_assignment_operators: bool,
pub numeric_separator: bool,
// ES2022
pub class_static_block: bool,
pub class_properties: Option<ClassPropertiesOptions>,
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_transformer/src/options/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl EnvOptions {
// Turn this on would throw error for all bigints.
big_int: false,
},
es2021: ES2021Options { logical_assignment_operators: true },
es2021: ES2021Options { logical_assignment_operators: true, numeric_separator: true },
es2022: ES2022Options {
class_static_block: true,
class_properties: if include_unfinished_plugins {
Expand Down Expand Up @@ -184,6 +184,7 @@ impl From<EngineTargets> for EnvOptions {
},
es2021: ES2021Options {
logical_assignment_operators: o.has_feature(ES2020LogicalAssignmentOperators),
numeric_separator: o.has_feature(ES2021NumericSeparator),
},
es2022: ES2022Options {
class_static_block: o.has_feature(ES2022ClassStaticBlock),
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl TryFrom<&BabelOptions> for TransformOptions {
let es2021 = ES2021Options {
logical_assignment_operators: options.plugins.logical_assignment_operators
|| env.es2021.logical_assignment_operators,
numeric_separator: options.plugins.numeric_separator || env.es2021.numeric_separator,
};

let es2022 = ES2022Options {
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) const PLUGINS: &[&str] = &[
// // [Syntax] "babel-plugin-transform-syntax-top-level-await",
// ES2021
"babel-plugin-transform-logical-assignment-operators",
// "babel-plugin-transform-numeric-separator",
"babel-plugin-transform-numeric-separator",
// ES2020
// "babel-plugin-transform-export-namespace-from",
// "babel-plugin-transform-dynamic-import",
Expand Down
Loading