Skip to content

Commit

Permalink
refactor(transformer)!: API to `TryFrom<&EnvOptions> for TransformOpt…
Browse files Browse the repository at this point in the history
…ions` and `TryFrom<&BabelOptions> TransformOptions` (#7020)

…ions` and `TryFrom<&BabelOptions> TransformOptions`
  • Loading branch information
Boshen committed Oct 30, 2024
1 parent 17a938e commit b8daab3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
22 changes: 13 additions & 9 deletions crates/oxc_transformer/src/options/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,18 @@ impl TransformOptions {
},
}
}
}

impl TryFrom<&EnvOptions> for TransformOptions {
type Error = Vec<Error>;

/// # 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<Self, Vec<Error>> {
let targets = match env_options.targets.clone().get_targets() {
fn try_from(options: &EnvOptions) -> Result<Self, Self::Error> {
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 {
Expand Down Expand Up @@ -200,11 +202,13 @@ impl TransformOptions {
..Default::default()
})
}
}

impl TryFrom<&BabelOptions> for TransformOptions {
type Error = Vec<Error>;

/// # 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<Self, Vec<Error>> {
fn try_from(options: &BabelOptions) -> Result<Self, Self::Error> {
let mut errors = Vec::<Error>::new();

let assumptions = if options.assumptions.is_null() {
Expand Down Expand Up @@ -447,7 +451,7 @@ fn test_deny_unknown_fields() {
"sourceType": "module"
});
let babel_options = serde_json::from_value::<BabelOptions>(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::<Vec<_>>().join("\n");
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}) {
Expand Down
8 changes: 2 additions & 6 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ impl TestCaseKind {
}
}

fn transform_options(options: &BabelOptions) -> Result<TransformOptions, Vec<Error>> {
TransformOptions::from_babel_options(options)
}

pub trait TestCase {
fn new(cwd: &Path, path: &Path) -> Self;

Expand Down Expand Up @@ -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![] }
}

Expand Down Expand Up @@ -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![] }
}

Expand Down

0 comments on commit b8daab3

Please sign in to comment.