-
-
Notifications
You must be signed in to change notification settings - Fork 474
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
ESTarget
(#7091)
closes #6982
- Loading branch information
Showing
13 changed files
with
251 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
#[derive(Default, Debug, Clone, Copy)] | ||
pub struct RegExpOptions { | ||
/// Enables plugin to transform the RegExp literal has `y` flag | ||
/// Enables plugin to transform the RegExp literal with a `y` flag | ||
/// ES2015 <https://babel.dev/docs/babel-plugin-transform-sticky-regex> | ||
pub sticky_flag: bool, | ||
|
||
/// Enables plugin to transform the RegExp literal has `u` flag | ||
/// Enables plugin to transform the RegExp literal with a `u` flag | ||
/// ES2015 <https://babel.dev/docs/babel-plugin-transform-unicode-regex> | ||
pub unicode_flag: bool, | ||
|
||
/// Enables plugin to transform the RegExp literal has `s` flag | ||
pub dot_all_flag: bool, | ||
/// Enables plugin to transform the RegExp literal that has `\p{}` and `\P{}` unicode property escapes | ||
/// ES2018 <https://babel.dev/docs/babel-plugin-transform-unicode-property-regex> | ||
pub unicode_property_escapes: bool, | ||
|
||
/// Enables plugin to transform the RegExp literal has `(?<=)` or `(?<!)` lookbehind assertions | ||
pub look_behind_assertions: bool, | ||
/// Enables plugin to transform the RegExp literal with a `s` flag | ||
/// ES2018 <https://babel.dev/docs/babel-plugin-transform-dotall-regex> | ||
pub dot_all_flag: bool, | ||
|
||
/// Enables plugin to transform the RegExp literal has `(?<name>x)` named capture groups | ||
/// Enables plugin to transform the RegExp literal that has `(?<name>x)` named capture groups | ||
/// ES2018 <https://babel.dev/docs/babel-plugin-transform-named-capturing-groups-regex> | ||
pub named_capture_groups: bool, | ||
|
||
/// Enables plugin to transform the RegExp literal has `\p{}` and `\P{}` unicode property escapes | ||
pub unicode_property_escapes: bool, | ||
/// Enables plugin to transform the RegExp literal that has `(?<=)` or `(?<!)` lookbehind assertions | ||
/// ES2018 <https://github.com/tc39/proposal-regexp-lookbehind> | ||
pub look_behind_assertions: bool, | ||
|
||
/// Enables plugin to transform `d` flag | ||
/// Enables plugin to transform the `d` flag | ||
/// ES2022 <https://github.com/tc39/proposal-regexp-match-indices> | ||
pub match_indices: bool, | ||
|
||
/// Enables plugin to transform the RegExp literal has `v` flag | ||
/// Enables plugin to transform the RegExp literal that has `v` flag | ||
/// ES2024 <https://babel.dev/docs/babel-plugin-transform-unicode-sets-regex> | ||
pub set_notation: bool, | ||
} |
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,60 @@ | ||
use std::{path::Path, str::FromStr}; | ||
|
||
use oxc_allocator::Allocator; | ||
use oxc_codegen::{CodeGenerator, CodegenOptions}; | ||
use oxc_parser::Parser; | ||
use oxc_semantic::SemanticBuilder; | ||
use oxc_span::SourceType; | ||
use oxc_transformer::{ESTarget, TransformOptions, Transformer}; | ||
|
||
use crate::run; | ||
|
||
pub(crate) fn test(source_text: &str, target: &str) -> String { | ||
let source_type = SourceType::default(); | ||
let allocator = Allocator::default(); | ||
let ret = Parser::new(&allocator, source_text, source_type).parse(); | ||
let mut program = ret.program; | ||
let (symbols, scopes) = | ||
SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree(); | ||
let options = TransformOptions::from(ESTarget::from_str(target).unwrap()); | ||
Transformer::new(&allocator, Path::new(""), options).build_with_symbols_and_scopes( | ||
symbols, | ||
scopes, | ||
&mut program, | ||
); | ||
CodeGenerator::new() | ||
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) | ||
.build(&program) | ||
.code | ||
} | ||
|
||
#[test] | ||
fn es2015() { | ||
use std::fmt::Write; | ||
|
||
let cases = [ | ||
("es5", "() => {}"), | ||
("es2015", "a ** b"), | ||
("es2016", "async function foo() {}"), | ||
("es2017", "({ ...x })"), | ||
("es2018", "try {} catch {}"), | ||
("es2019", "a ?? b"), | ||
("es2020", "a ||= b"), | ||
("es2021", "class foo { static {} }"), | ||
]; | ||
|
||
// Test no transformation for esnext. | ||
for (_, case) in cases { | ||
assert_eq!(run(case, SourceType::mjs()), test(case, "esnext")); | ||
} | ||
|
||
let snapshot = cases.iter().enumerate().fold(String::new(), |mut w, (i, (target, case))| { | ||
let result = test(case, target); | ||
write!(w, "########## {i} {target}\n{case}\n----------\n{result}\n").unwrap(); | ||
w | ||
}); | ||
|
||
insta::with_settings!({ prepend_module_to_snapshot => false, snapshot_suffix => "", omit_expression => true }, { | ||
insta::assert_snapshot!("es_target", snapshot); | ||
}); | ||
} |
54 changes: 54 additions & 0 deletions
54
crates/oxc_transformer/tests/es_target/snapshots/es_target.snap
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,54 @@ | ||
--- | ||
source: crates/oxc_transformer/tests/es_target/mod.rs | ||
snapshot_kind: text | ||
--- | ||
########## 0 es5 | ||
() => {} | ||
---------- | ||
(function() {}); | ||
|
||
########## 1 es2015 | ||
a ** b | ||
---------- | ||
Math.pow(a, b); | ||
|
||
########## 2 es2016 | ||
async function foo() {} | ||
---------- | ||
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; | ||
function foo() { | ||
return _foo.apply(this, arguments); | ||
} | ||
function _foo() { | ||
_foo = _asyncToGenerator(function* () {}); | ||
return _foo.apply(this, arguments); | ||
} | ||
|
||
########## 3 es2017 | ||
({ ...x }) | ||
---------- | ||
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; | ||
_objectSpread({}, x); | ||
|
||
########## 4 es2018 | ||
try {} catch {} | ||
---------- | ||
try {} catch (_unused) {} | ||
|
||
########## 5 es2019 | ||
a ?? b | ||
---------- | ||
var _a; | ||
(_a = a) !== null && _a !== void 0 ? _a : b; | ||
|
||
########## 6 es2020 | ||
a ||= b | ||
---------- | ||
a || (a = b); | ||
|
||
########## 7 es2021 | ||
class foo { static {} } | ||
---------- | ||
class foo { | ||
static #_ = (() => {})(); | ||
} |
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 |
---|---|---|
@@ -1 +1,16 @@ | ||
mod es_target; | ||
mod plugins; | ||
|
||
use oxc_allocator::Allocator; | ||
use oxc_codegen::{CodeGenerator, CodegenOptions}; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
pub fn run(source_text: &str, source_type: SourceType) -> String { | ||
let allocator = Allocator::default(); | ||
let ret = Parser::new(&allocator, source_text, source_type).parse(); | ||
CodeGenerator::new() | ||
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) | ||
.build(&ret.program) | ||
.code | ||
} |
Oops, something went wrong.