Skip to content

Commit

Permalink
feat(transformer): support all regexp plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 3, 2024
1 parent aeda84f commit fa8ccfc
Show file tree
Hide file tree
Showing 32 changed files with 512 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ test = false
doctest = false

[dependencies]
oxc_ast = { workspace = true }
oxc_span = { workspace = true }
oxc_allocator = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_syntax = { workspace = true, features = ["to_js_string"] }
oxc_traverse = { workspace = true }
oxc_semantic = { workspace = true }
oxc_ast = { workspace = true }
oxc_span = { workspace = true }
oxc_allocator = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_syntax = { workspace = true, features = ["to_js_string"] }
oxc_traverse = { workspace = true }
oxc_semantic = { workspace = true }
oxc_regular_expression = { workspace = true }

dashmap = { workspace = true }
indexmap = { workspace = true }
Expand Down
9 changes: 8 additions & 1 deletion crates/oxc_transformer/src/env/data/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ use crate::env::{targets::version::Version, Versions};
fn features() -> &'static FxHashMap<String, Versions> {
static FEATURES: OnceLock<FxHashMap<String, Versions>> = OnceLock::new();
FEATURES.get_or_init(|| {
let map: FxHashMap<String, FxHashMap<String, String>> =
let mut map: FxHashMap<String, FxHashMap<String, String>> =
serde_json::from_str(include_str!("./@babel/compat_data/data/plugins.json"))
.expect("failed to parse json");

map.extend(
serde_json::from_str::<FxHashMap<String, FxHashMap<String, String>>>(include_str!(
"./esbuild/features.json"
))
.expect("failed to parse json"),
);

map.into_iter()
.map(|(feature, mut versions)| {
(feature, {
Expand Down
23 changes: 23 additions & 0 deletions crates/oxc_transformer/src/env/data/esbuild/features.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"esbuild-regexp-lookbehind-assertions": {
"chrome": "62",
"deno": "1.0",
"edge": "79",
"firefox": "78",
"hermes": "0.7",
"ios": "16.4",
"node": "8.10",
"opera": "49",
"safari": "16.4"
},
"esbuild-regexp-match-indices": {
"chrome": "90",
"deno": "1.8",
"edge": "90",
"firefox": "88",
"ios": "15.0",
"node": "16.0",
"opera": "76",
"safari": "15.0"
}
}
7 changes: 6 additions & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod es2019;
mod es2020;
mod es2021;
mod react;
mod regexp;
mod typescript;

mod helpers {
Expand All @@ -41,6 +42,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_span::{SourceType, SPAN};
use oxc_traverse::{traverse_mut, Traverse, TraverseCtx};
use regexp::RegExp;

pub use crate::{
compiler_assumptions::CompilerAssumptions,
Expand Down Expand Up @@ -74,6 +76,7 @@ pub struct Transformer<'a> {
x2_es2018: ES2018<'a>,
x2_es2016: ES2016<'a>,
x3_es2015: ES2015<'a>,
x4_regexp: RegExp<'a>,
}

impl<'a> Transformer<'a> {
Expand Down Expand Up @@ -102,7 +105,8 @@ impl<'a> Transformer<'a> {
x2_es2019: ES2019::new(options.es2019, Rc::clone(&ctx)),
x2_es2018: ES2018::new(options.es2018, Rc::clone(&ctx)),
x2_es2016: ES2016::new(options.es2016, Rc::clone(&ctx)),
x3_es2015: ES2015::new(options.es2015, ctx),
x3_es2015: ES2015::new(options.es2015, Rc::clone(&ctx)),
x4_regexp: RegExp::new(options.regexp, ctx),
}
}

Expand Down Expand Up @@ -177,6 +181,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
self.x2_es2018.enter_expression(expr, ctx);
self.x2_es2016.enter_expression(expr, ctx);
self.x3_es2015.enter_expression(expr, ctx);
self.x4_regexp.enter_expression(expr, ctx);
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down
34 changes: 34 additions & 0 deletions crates/oxc_transformer/src/options/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
es2021::ES2021Options,
options::babel::BabelOptions,
react::ReactOptions,
regexp::RegExpOptions,
typescript::TypeScriptOptions,
};

Expand All @@ -38,6 +39,8 @@ pub struct TransformOptions {
/// [preset-react](https://babeljs.io/docs/babel-preset-react)
pub react: ReactOptions,

pub regexp: RegExpOptions,

pub es2015: ES2015Options,

pub es2016: ES2016Options,
Expand All @@ -60,6 +63,7 @@ impl TransformOptions {
es2019: ES2019Options::from_targets_and_bugfixes(targets, bugfixes),
es2020: ES2020Options::from_targets_and_bugfixes(targets, bugfixes),
es2021: ES2021Options::from_targets_and_bugfixes(targets, bugfixes),
regexp: RegExpOptions::from_targets_and_bugfixes(targets, bugfixes),
..Default::default()
}
}
Expand Down Expand Up @@ -215,6 +219,36 @@ impl TransformOptions {
}
};

let regexp = transformer_options.regexp;
if !regexp.sticky_flag {
transformer_options
.regexp
.with_sticky_flag(options.has_plugin("transform-sticky-regex"));
}
if !regexp.unicode_flag {
transformer_options
.regexp
.with_unicode_flag(options.has_plugin("transform-unicode-regex"));
}
if !regexp.dot_all_flag {
transformer_options.regexp.with_dot_all(options.has_plugin("transform-dotall-regex"));
}
if !regexp.named_capture_groups {
transformer_options.regexp.with_named_capture_groups(
options.has_plugin("transform-named-capturing-groups-regex"),
);
}
if !regexp.unicode_property_escapes {
transformer_options.regexp.with_unicode_property_escapes(
options.has_plugin("transform-unicode-property-regex"),
);
}
if !regexp.set_notation {
transformer_options
.regexp
.with_regexp_set_notation(options.has_plugin("transform-unicode-sets-regex"));
}

transformer_options.assumptions = if options.assumptions.is_null() {
CompilerAssumptions::default()
} else {
Expand Down
Loading

0 comments on commit fa8ccfc

Please sign in to comment.