Skip to content

Commit

Permalink
feat(transformer): add TransformOptions::enable_all method (#5495)
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 authored Sep 6, 2024
1 parent 88b7ddb commit 32d4bbb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 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.

1 change: 1 addition & 0 deletions crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ oxc-browserslist = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }
oxc_codegen = { workspace = true }
pico-args = { workspace = true }

[features]
default = []
Expand Down
21 changes: 15 additions & 6 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{EnvOptions, Targets, TransformOptions, Transformer};
use pico_args::Arguments;

// Instruction:
// create a `test.tsx`,
// run `cargo run -p oxc_transformer --example transformer`
// or `just watch "run -p oxc_transformer --example transformer"`

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 source_text = std::fs::read_to_string(path).expect("{name} not found");
let allocator = Allocator::default();
Expand All @@ -34,17 +38,22 @@ 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)
.semantic
.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(
&allocator,
path,
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 32d4bbb

Please sign in to comment.