From 4a524ed150f30e19f088ff92e9030342e19ee140 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:33:22 +0000 Subject: [PATCH] chore(transformer): support passing babel options path in example (#7790) Sometimes we want to use Babel test's options to test some code, If the example can get the TransformerOptions from Babel options, then we can easily do it --- crates/oxc_transformer/examples/transformer.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/oxc_transformer/examples/transformer.rs b/crates/oxc_transformer/examples/transformer.rs index ee37ca3b75dc6..19b56e6085f71 100644 --- a/crates/oxc_transformer/examples/transformer.rs +++ b/crates/oxc_transformer/examples/transformer.rs @@ -6,7 +6,7 @@ use oxc_codegen::CodeGenerator; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; use oxc_span::SourceType; -use oxc_transformer::{EnvOptions, HelperLoaderMode, TransformOptions, Transformer}; +use oxc_transformer::{BabelOptions, EnvOptions, HelperLoaderMode, TransformOptions, Transformer}; use pico_args::Arguments; // Instruction: @@ -15,6 +15,8 @@ use pico_args::Arguments; fn main() { let mut args = Arguments::from_env(); + let babel_options_path: Option = + args.opt_value_from_str("--babel-options").unwrap_or(None); let targets: Option = args.opt_value_from_str("--targets").unwrap_or(None); let target: Option = args.opt_value_from_str("--target").unwrap_or(None); let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string()); @@ -55,7 +57,11 @@ fn main() { let (symbols, scopes) = ret.semantic.into_symbol_table_and_scope_tree(); - let mut transform_options = if let Some(query) = &targets { + let mut transform_options = if let Some(babel_options_path) = babel_options_path { + let babel_options_path = Path::new(&babel_options_path); + let babel_options = BabelOptions::from_test_path(babel_options_path); + TransformOptions::try_from(&babel_options).unwrap() + } else if let Some(query) = &targets { TransformOptions { env: EnvOptions::from_browserslist_query(query).unwrap(), ..TransformOptions::default()