Skip to content

Commit

Permalink
feat(transformer): add TransformOptions::enable_all method
Browse files Browse the repository at this point in the history
Make it **really explicit** about which transformer options are being turned on. I also need this in monitor-oxc.
  • Loading branch information
Boshen committed Sep 5, 2024
1 parent a96866d commit 6d8b7d3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
9 changes: 2 additions & 7 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_codegen::CodeGenerator;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
use oxc_transformer::{TransformOptions, Transformer};

// Instruction:
// create a `test.tsx`,
Expand Down Expand Up @@ -34,11 +34,6 @@ fn main() {
println!("{source_text}\n");

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)
.build(&program)
Expand All @@ -51,7 +46,7 @@ fn main() {
source_type,
&source_text,
ret.trivias.clone(),
transform_options,
TransformOptions::enable_all(),
)
.build_with_symbols_and_scopes(symbols, scopes, &mut program);

Expand Down
29 changes: 29 additions & 0 deletions crates/oxc_transformer/src/options/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ pub struct 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 {
Self {
es2015: ES2015Options::from_targets_and_bugfixes(targets, bugfixes),
Expand Down
15 changes: 2 additions & 13 deletions tasks/benchmark/benches/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_parser::{Parser, ParserReturn};
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_tasks_common::TestFiles;
use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
use oxc_transformer::{TransformOptions, Transformer};

fn bench_transformer(criterion: &mut Criterion) {
let mut group = criterion.benchmark_group("transformer");
Expand Down Expand Up @@ -40,25 +40,14 @@ fn bench_transformer(criterion: &mut Criterion) {
// in measure.
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(|| {
let ret = Transformer::new(
&allocator,
Path::new(&file.file_name),
source_type,
source_text,
trivias,
transform_options,
TransformOptions::enable_all(),
)
.build_with_symbols_and_scopes(symbols, scopes, program);

Expand Down

0 comments on commit 6d8b7d3

Please sign in to comment.