mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 13:48:55 +00:00
feat(transformer): add TransformOptions::enable_all method (#5495)
Make it **really** explicit about which transformer options are being turned on. I also need this in monitor-oxc.
This commit is contained in:
parent
88b7ddb7e0
commit
32d4bbb519
5 changed files with 48 additions and 19 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1960,6 +1960,7 @@ dependencies = [
|
||||||
"oxc_span",
|
"oxc_span",
|
||||||
"oxc_syntax",
|
"oxc_syntax",
|
||||||
"oxc_traverse",
|
"oxc_traverse",
|
||||||
|
"pico-args",
|
||||||
"ropey",
|
"ropey",
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ oxc-browserslist = { workspace = true }
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
oxc_parser = { workspace = true }
|
oxc_parser = { workspace = true }
|
||||||
oxc_codegen = { workspace = true }
|
oxc_codegen = { workspace = true }
|
||||||
|
pico-args = { workspace = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use oxc_parser::Parser;
|
||||||
use oxc_semantic::SemanticBuilder;
|
use oxc_semantic::SemanticBuilder;
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
|
use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
|
||||||
|
use pico_args::Arguments;
|
||||||
|
|
||||||
// Instruction:
|
// Instruction:
|
||||||
// create a `test.tsx`,
|
// create a `test.tsx`,
|
||||||
|
|
@ -14,7 +15,10 @@ use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
|
||||||
// or `just watch "run -p oxc_transformer --example transformer"`
|
// or `just watch "run -p oxc_transformer --example transformer"`
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let name = env::args().nth(1).unwrap_or_else(|| "test.tsx".to_string());
|
let mut args = Arguments::from_env();
|
||||||
|
let name = env::args().nth(1).unwrap_or_else(|| "test.js".to_string());
|
||||||
|
let targets: Option<String> = args.opt_value_from_str("--targets").unwrap_or(None);
|
||||||
|
|
||||||
let path = Path::new(&name);
|
let path = Path::new(&name);
|
||||||
let source_text = std::fs::read_to_string(path).expect("{name} not found");
|
let source_text = std::fs::read_to_string(path).expect("{name} not found");
|
||||||
let allocator = Allocator::default();
|
let allocator = Allocator::default();
|
||||||
|
|
@ -34,17 +38,22 @@ fn main() {
|
||||||
println!("{source_text}\n");
|
println!("{source_text}\n");
|
||||||
|
|
||||||
let mut program = ret.program;
|
let mut program = ret.program;
|
||||||
let transform_options = TransformOptions::from_preset_env(&EnvOptions {
|
|
||||||
targets: Targets::from_query("chrome 51"),
|
|
||||||
..EnvOptions::default()
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (symbols, scopes) = SemanticBuilder::new(&source_text, source_type)
|
let (symbols, scopes) = SemanticBuilder::new(&source_text, source_type)
|
||||||
.build(&program)
|
.build(&program)
|
||||||
.semantic
|
.semantic
|
||||||
.into_symbol_table_and_scope_tree();
|
.into_symbol_table_and_scope_tree();
|
||||||
|
|
||||||
|
let transform_options = if let Some(targets) = &targets {
|
||||||
|
TransformOptions::from_preset_env(&EnvOptions {
|
||||||
|
targets: Targets::from_query(targets),
|
||||||
|
..EnvOptions::default()
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
} else {
|
||||||
|
TransformOptions::enable_all()
|
||||||
|
};
|
||||||
|
|
||||||
let _ = Transformer::new(
|
let _ = Transformer::new(
|
||||||
&allocator,
|
&allocator,
|
||||||
path,
|
path,
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,35 @@ pub struct TransformOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TransformOptions {
|
impl TransformOptions {
|
||||||
|
/// Explicitly enable all plugins that are ready, mainly for testing purposes.
|
||||||
|
pub fn enable_all() -> Self {
|
||||||
|
Self {
|
||||||
|
cwd: PathBuf::new(),
|
||||||
|
assumptions: CompilerAssumptions::default(),
|
||||||
|
typescript: TypeScriptOptions::default(),
|
||||||
|
react: ReactOptions { development: true, ..ReactOptions::default() },
|
||||||
|
regexp: RegExpOptions {
|
||||||
|
sticky_flag: true,
|
||||||
|
unicode_flag: true,
|
||||||
|
dot_all_flag: true,
|
||||||
|
look_behind_assertions: true,
|
||||||
|
named_capture_groups: true,
|
||||||
|
unicode_property_escapes: true,
|
||||||
|
match_indices: true,
|
||||||
|
set_notation: true,
|
||||||
|
},
|
||||||
|
es2015: ES2015Options {
|
||||||
|
// Turned off because it is not ready.
|
||||||
|
arrow_function: None,
|
||||||
|
},
|
||||||
|
es2016: ES2016Options { exponentiation_operator: true },
|
||||||
|
es2018: ES2018Options { object_rest_spread: Some(ObjectRestSpreadOptions::default()) },
|
||||||
|
es2019: ES2019Options { optional_catch_binding: true },
|
||||||
|
es2020: ES2020Options { nullish_coalescing_operator: true },
|
||||||
|
es2021: ES2021Options { logical_assignment_operators: true },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
|
fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
es2015: ES2015Options::from_targets_and_bugfixes(targets, bugfixes),
|
es2015: ES2015Options::from_targets_and_bugfixes(targets, bugfixes),
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use oxc_parser::{Parser, ParserReturn};
|
||||||
use oxc_semantic::SemanticBuilder;
|
use oxc_semantic::SemanticBuilder;
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
use oxc_tasks_common::TestFiles;
|
use oxc_tasks_common::TestFiles;
|
||||||
use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
|
use oxc_transformer::{TransformOptions, Transformer};
|
||||||
|
|
||||||
fn bench_transformer(criterion: &mut Criterion) {
|
fn bench_transformer(criterion: &mut Criterion) {
|
||||||
let mut group = criterion.benchmark_group("transformer");
|
let mut group = criterion.benchmark_group("transformer");
|
||||||
|
|
@ -40,17 +40,6 @@ fn bench_transformer(criterion: &mut Criterion) {
|
||||||
// in measure.
|
// in measure.
|
||||||
let trivias_copy = trivias.clone();
|
let trivias_copy = trivias.clone();
|
||||||
|
|
||||||
let env_options = EnvOptions {
|
|
||||||
// >= ES2016
|
|
||||||
targets: Targets::from_query("chrome 51"),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let mut transform_options =
|
|
||||||
TransformOptions::from_preset_env(&env_options).unwrap();
|
|
||||||
|
|
||||||
// Enable React related plugins
|
|
||||||
transform_options.react.development = true;
|
|
||||||
|
|
||||||
runner.run(|| {
|
runner.run(|| {
|
||||||
let ret = Transformer::new(
|
let ret = Transformer::new(
|
||||||
&allocator,
|
&allocator,
|
||||||
|
|
@ -58,7 +47,7 @@ fn bench_transformer(criterion: &mut Criterion) {
|
||||||
source_type,
|
source_type,
|
||||||
source_text,
|
source_text,
|
||||||
trivias,
|
trivias,
|
||||||
transform_options,
|
TransformOptions::enable_all(),
|
||||||
)
|
)
|
||||||
.build_with_symbols_and_scopes(symbols, scopes, program);
|
.build_with_symbols_and_scopes(symbols, scopes, program);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue