diff --git a/crates/oxc_transformer/examples/transformer.rs b/crates/oxc_transformer/examples/transformer.rs index b851b6890561a..fc1185258b2a2 100644 --- a/crates/oxc_transformer/examples/transformer.rs +++ b/crates/oxc_transformer/examples/transformer.rs @@ -55,7 +55,7 @@ fn main() { let (symbols, scopes) = ret.semantic.into_symbol_table_and_scope_tree(); let transform_options = if let Some(targets) = &targets { - TransformOptions::from_preset_env(&EnvOptions { + TransformOptions::try_from(&EnvOptions { targets: Targets::from_query(targets), ..EnvOptions::default() }) diff --git a/crates/oxc_transformer/src/options/transformer.rs b/crates/oxc_transformer/src/options/transformer.rs index a695ff12ab4b3..28b8d78e83e1c 100644 --- a/crates/oxc_transformer/src/options/transformer.rs +++ b/crates/oxc_transformer/src/options/transformer.rs @@ -109,16 +109,18 @@ impl TransformOptions { }, } } +} + +impl TryFrom<&EnvOptions> for TransformOptions { + type Error = Vec; - /// # Errors - /// /// If there are any errors in the `options.targets``, they will be returned as a list of errors. - pub fn from_preset_env(env_options: &EnvOptions) -> Result> { - let targets = match env_options.targets.clone().get_targets() { + fn try_from(options: &EnvOptions) -> Result { + let targets = match options.targets.clone().get_targets() { Ok(targets) => Some(targets), Err(err) => return Err(vec![err]), }; - let bugfixes = env_options.bugfixes; + let bugfixes = options.bugfixes; let targets = targets.as_ref(); Ok(Self { regexp: RegExpOptions { @@ -200,11 +202,13 @@ impl TransformOptions { ..Default::default() }) } +} + +impl TryFrom<&BabelOptions> for TransformOptions { + type Error = Vec; - /// # Errors - /// /// If the `options` contains any unknown fields, they will be returned as a list of errors. - pub fn from_babel_options(options: &BabelOptions) -> Result> { + fn try_from(options: &BabelOptions) -> Result { let mut errors = Vec::::new(); let assumptions = if options.assumptions.is_null() { @@ -447,7 +451,7 @@ fn test_deny_unknown_fields() { "sourceType": "module" }); let babel_options = serde_json::from_value::(options).unwrap(); - let result = TransformOptions::from_babel_options(&babel_options); + let result = TransformOptions::try_from(&babel_options); assert!(result.is_err()); let err_message = result.err().unwrap().iter().map(ToString::to_string).collect::>().join("\n"); diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 0c6e1d6877f73..53d95be8275b0 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -242,7 +242,7 @@ impl Oxc { } if run_options.transform.unwrap_or_default() { - if let Ok(options) = TransformOptions::from_preset_env(&EnvOptions { + if let Ok(options) = TransformOptions::try_from(&EnvOptions { targets: Targets::from_query("chrome 51"), ..EnvOptions::default() }) { diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index 83dfeb1c5eda1..6d43abb508ea5 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -84,10 +84,6 @@ impl TestCaseKind { } } -fn transform_options(options: &BabelOptions) -> Result> { - TransformOptions::from_babel_options(options) -} - pub trait TestCase { fn new(cwd: &Path, path: &Path) -> Self; @@ -197,7 +193,7 @@ impl TestCase for ConformanceTestCase { fn new(cwd: &Path, path: &Path) -> Self { let mut options = BabelOptions::from_test_path(path.parent().unwrap()); options.cwd.replace(cwd.to_path_buf()); - let transform_options = transform_options(&options); + let transform_options = TransformOptions::try_from(&options); Self { path: path.to_path_buf(), options, transform_options, errors: vec![] } } @@ -416,7 +412,7 @@ impl TestCase for ExecTestCase { fn new(cwd: &Path, path: &Path) -> Self { let mut options = BabelOptions::from_test_path(path.parent().unwrap()); options.cwd.replace(cwd.to_path_buf()); - let transform_options = transform_options(&options); + let transform_options = TransformOptions::try_from(&options); Self { path: path.to_path_buf(), options, transform_options, errors: vec![] } }