From a579011e37c5012fe0cbbe4a0012b2e7214aa5b8 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Thu, 7 Nov 2024 07:39:33 +0000 Subject: [PATCH] feat(transformer): add features `ES2018NamedCapturingGroupsRegex` and `ES2018LookbehindRegex` (#7182) --- crates/oxc_transformer/src/options/env.rs | 6 +-- .../src/options/es_features.rs | 34 +++++++++++++++ tasks/compat_data/README.md | 11 +++++ tasks/compat_data/build.js | 2 - tasks/compat_data/data.json | 42 +++++++++++++++++++ tasks/compat_data/es-features.js | 13 ++++++ tasks/compat_data/src/main.rs | 16 +++++++ .../snapshots/oxc.snap.md | 8 +--- 8 files changed, 120 insertions(+), 12 deletions(-) diff --git a/crates/oxc_transformer/src/options/env.rs b/crates/oxc_transformer/src/options/env.rs index a6c4cdc0d93d4..4dda3f269aa16 100644 --- a/crates/oxc_transformer/src/options/env.rs +++ b/crates/oxc_transformer/src/options/env.rs @@ -190,10 +190,8 @@ impl TryFrom for EnvOptions { unicode_property_escapes: o.can_enable(ES2018UnicodePropertyRegex), dot_all_flag: o.can_enable(ES2018DotallRegex), named_capture_groups: o.can_enable(ES2018NamedCapturingGroupsRegex), - // FIXME - look_behind_assertions: false, // o.can_enable("esbuild-regexp-lookbehind-assertions"), - // FIXME - match_indices: false, // o.can_enable("esbuild-regexp-match-indices"), + look_behind_assertions: o.can_enable(ES2018LookbehindRegex), + match_indices: o.can_enable(ES2022MatchIndicesRegex), set_notation: o.can_enable(ES2024UnicodeSetsRegex), }, es2015: ES2015Options { diff --git a/crates/oxc_transformer/src/options/es_features.rs b/crates/oxc_transformer/src/options/es_features.rs index 4b8baa9332ae2..c3ace168d34e9 100644 --- a/crates/oxc_transformer/src/options/es_features.rs +++ b/crates/oxc_transformer/src/options/es_features.rs @@ -35,6 +35,7 @@ pub enum ESFeature { ES2018NamedCapturingGroupsRegex, ES2018UnicodePropertyRegex, ES2018DotallRegex, + ES2018LookbehindRegex, ES2018ObjectRestSpread, ES2018AsyncGeneratorFunctions, ES2018OptionalCatchBinding, @@ -47,6 +48,7 @@ pub enum ESFeature { ES2022ClassProperties, ES2022PrivatePropertyInObject, ES2022ClassStaticBlock, + ES2022MatchIndicesRegex, ES2024UnicodeSetsRegex, ES2025RegexpModifiers, ES2025DuplicateNamedCapturingGroupsRegex, @@ -539,6 +541,22 @@ pub fn features() -> &'static FxHashMap { (Edge, Version(79u32, 0u32, 0u32)), ])), ), + ( + ES2018LookbehindRegex, + EngineTargets::new(FxHashMap::from_iter([ + (Chrome, Version(62u32, 0u32, 0u32)), + (Safari, Version(16u32, 4u32, 0u32)), + (OperaMobile, Version(46u32, 0u32, 0u32)), + (Samsung, Version(8u32, 0u32, 0u32)), + (Node, Version(8u32, 10u32, 0u32)), + (Firefox, Version(78u32, 0u32, 0u32)), + (Deno, Version(1u32, 0u32, 0u32)), + (Electron, Version(3u32, 0u32, 0u32)), + (Opera, Version(49u32, 0u32, 0u32)), + (Ios, Version(16u32, 4u32, 0u32)), + (Edge, Version(79u32, 0u32, 0u32)), + ])), + ), ( ES2018ObjectRestSpread, EngineTargets::new(FxHashMap::from_iter([ @@ -733,6 +751,22 @@ pub fn features() -> &'static FxHashMap { (Edge, Version(94u32, 0u32, 0u32)), ])), ), + ( + ES2022MatchIndicesRegex, + EngineTargets::new(FxHashMap::from_iter([ + (Chrome, Version(90u32, 0u32, 0u32)), + (Safari, Version(15u32, 0u32, 0u32)), + (OperaMobile, Version(64u32, 0u32, 0u32)), + (Samsung, Version(15u32, 0u32, 0u32)), + (Node, Version(16u32, 0u32, 0u32)), + (Firefox, Version(91u32, 0u32, 0u32)), + (Deno, Version(1u32, 8u32, 0u32)), + (Electron, Version(13u32, 0u32, 0u32)), + (Opera, Version(76u32, 0u32, 0u32)), + (Ios, Version(15u32, 0u32, 0u32)), + (Edge, Version(90u32, 0u32, 0u32)), + ])), + ), ( ES2024UnicodeSetsRegex, EngineTargets::new(FxHashMap::from_iter([ diff --git a/tasks/compat_data/README.md b/tasks/compat_data/README.md index d7acd5aec278b..d7c9bd9f64aae 100644 --- a/tasks/compat_data/README.md +++ b/tasks/compat_data/README.md @@ -1 +1,12 @@ +# Compat Data + +Get engine compatibility Data from https://github.com/compat-table/compat-table/ + Code extracted from https://github.com/babel/babel/tree/main/packages/babel-compat-data + +## Adding a new feature + +- Find the feature from https://github.com/compat-table/compat-table/blob/gh-pages/data-es2016plus.js +- Add the feature in `./es-features.js` +- `pnpm install` +- `cargo run -p oxc_compat_data` diff --git a/tasks/compat_data/build.js b/tasks/compat_data/build.js index 5751a371105f4..0142ab262057f 100644 --- a/tasks/compat_data/build.js +++ b/tasks/compat_data/build.js @@ -59,7 +59,6 @@ const getLowestImplementedVersion = ( ok &&= !exclude(test.name); return ok; }); - const envTests = tests.map(({ res }) => { const versions = envsVersions[env]; let i = versions.length - 1; @@ -110,7 +109,6 @@ const generateData = (environments, items) => { item.targets = targets; } - console.log(items); return items; }; diff --git a/tasks/compat_data/data.json b/tasks/compat_data/data.json index 166833d0aecce..c481a1f585c7e 100644 --- a/tasks/compat_data/data.json +++ b/tasks/compat_data/data.json @@ -652,6 +652,27 @@ "electron": "3.0" } }, + { + "name": "LookbehindRegex", + "babel": null, + "features": [ + "RegExp Lookbehind Assertions" + ], + "es": "ES2018", + "targets": { + "chrome": "62", + "opera": "49", + "edge": "79", + "firefox": "78", + "safari": "16.4", + "node": "8.10", + "deno": "1", + "ios": "16.4", + "samsung": "8", + "opera_mobile": "46", + "electron": "3.0" + } + }, { "name": "ObjectRestSpread", "babel": "transform-object-rest-spread", @@ -910,6 +931,27 @@ "electron": "15.0" } }, + { + "name": "MatchIndicesRegex", + "babel": null, + "features": [ + "RegExp Match Indices (`hasIndices` / `d` flag) / constructor supports it" + ], + "es": "ES2022", + "targets": { + "chrome": "90", + "opera": "76", + "edge": "90", + "firefox": "91", + "safari": "15", + "node": "16", + "deno": "1.8", + "ios": "15", + "samsung": "15", + "opera_mobile": "64", + "electron": "13.0" + } + }, { "name": "UnicodeSetsRegex", "babel": "transform-unicode-sets-regex", diff --git a/tasks/compat_data/es-features.js b/tasks/compat_data/es-features.js index 25e62f3d4b99c..09e6b537d2885 100644 --- a/tasks/compat_data/es-features.js +++ b/tasks/compat_data/es-features.js @@ -201,6 +201,11 @@ const es2018 = [ babel: 'transform-dotall-regex', features: ['s (dotAll) flag for regular expressions'], }, + { + name: 'LookbehindRegex', + babel: null, + features: ['RegExp Lookbehind Assertions'], + }, { name: 'ObjectRestSpread', babel: 'transform-object-rest-spread', @@ -279,6 +284,14 @@ const es2022 = [ babel: 'transform-class-static-block', features: ['Class static initialization blocks'], }, + { + name: 'MatchIndicesRegex', + babel: null, + features: [ + 'RegExp Match Indices (`hasIndices` / `d` flag) / constructor supports it', + // ignore "shows up in flags" + ], + }, ].map(f('ES2022')); const es2024 = [ diff --git a/tasks/compat_data/src/main.rs b/tasks/compat_data/src/main.rs index c98329feb7bda..084199e7e3020 100644 --- a/tasks/compat_data/src/main.rs +++ b/tasks/compat_data/src/main.rs @@ -1,5 +1,21 @@ +#![allow(clippy::print_stdout)] +use std::process::Command; + use oxc_compat_data::generate; +use oxc_tasks_common::project_root; fn main() { + let cwd = project_root().join("tasks/compat_data"); + + if !cwd.join("compat-table").exists() { + println!("Cloning compat-table ..."); + Command::new("pnpm").current_dir(&cwd).args(["run", "init"]).output().unwrap(); + } + + let output = Command::new("pnpm").current_dir(cwd).args(["run", "build"]).output().unwrap(); + if !output.status.success() { + println!("{}", String::from_utf8(output.stderr).unwrap()); + } + generate(); } diff --git a/tasks/transform_conformance/snapshots/oxc.snap.md b/tasks/transform_conformance/snapshots/oxc.snap.md index 9bbaff25b1bd7..a0339d467778e 100644 --- a/tasks/transform_conformance/snapshots/oxc.snap.md +++ b/tasks/transform_conformance/snapshots/oxc.snap.md @@ -1,6 +1,6 @@ commit: d20b314c -Passed: 77/87 +Passed: 78/87 # All Passed: * babel-plugin-transform-class-static-block @@ -12,6 +12,7 @@ Passed: 77/87 * babel-plugin-transform-arrow-functions * babel-preset-typescript * babel-plugin-transform-react-jsx-source +* regexp # babel-plugin-transform-typescript (2/9) @@ -175,8 +176,3 @@ x Output mismatch x Output mismatch -# regexp (7/8) -* all-regex-plugins-enabled-by-targets/input.js -x Output mismatch - -