Skip to content

Commit

Permalink
refactor(transform): deserialize BabelPreests::env directly (#7051)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 1, 2024
1 parent a3b68b4 commit 7f1d1fe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 91 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub use crate::{
jsx::{JsxOptions, JsxRuntime, ReactRefreshOptions},
options::{
babel::{BabelEnvOptions, BabelOptions, Targets},
TransformOptions,
EnvOptions, TransformOptions,
},
plugins::*,
typescript::{RewriteExtensionsMode, TypeScriptOptions},
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_transformer/src/options/babel/presets.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use serde::Deserialize;

use super::{BabelEnvOptions, PluginPresetEntries};
use super::PluginPresetEntries;

use crate::{JsxOptions, TypeScriptOptions};
use crate::{EnvOptions, JsxOptions, TypeScriptOptions};

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(try_from = "PluginPresetEntries")]
pub struct BabelPresets {
pub errors: Vec<String>,
pub unsupported: Vec<String>,

pub env: Option<BabelEnvOptions>,
pub env: Option<EnvOptions>,

pub jsx: Option<JsxOptions>,

Expand All @@ -25,7 +25,7 @@ impl TryFrom<PluginPresetEntries> for BabelPresets {
for entry in entries.0 {
match entry.name() {
"env" => {
p.env = entry.value::<BabelEnvOptions>().map_err(|err| p.errors.push(err)).ok();
p.env = entry.value::<EnvOptions>().map_err(|err| p.errors.push(err)).ok();
}
"typescript" => {
p.typescript =
Expand Down
85 changes: 7 additions & 78 deletions crates/oxc_transformer/src/options/env.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use oxc_diagnostics::Error;
use serde::Deserialize;

use crate::{
es2015::ES2015Options, es2016::ES2016Options, es2017::ES2017Options, es2018::ES2018Options,
es2019::ES2019Options, es2020::ES2020Options, es2021::ES2021Options, es2022::ES2022Options,
regexp::RegExpOptions,
};

use super::babel::{BabelEnvOptions, BabelOptions};
use super::babel::BabelEnvOptions;

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(try_from = "BabelEnvOptions")]
pub struct EnvOptions {
pub regexp: RegExpOptions,

Expand Down Expand Up @@ -66,11 +67,11 @@ impl EnvOptions {
}
}

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

/// If there are any errors in the `options.targets``, they will be returned as a list of errors.
fn try_from(o: &BabelEnvOptions) -> Result<Self, Self::Error> {
fn try_from(o: BabelEnvOptions) -> Result<Self, Self::Error> {
Ok(Self {
regexp: RegExpOptions {
sticky_flag: o.can_enable_plugin("transform-sticky-regex"),
Expand Down Expand Up @@ -120,75 +121,3 @@ impl TryFrom<&BabelEnvOptions> for EnvOptions {
})
}
}

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

/// If the `options` contains any unknown fields, they will be returned as a list of errors.
fn try_from(options: &BabelOptions) -> Result<Self, Self::Error> {
let env = options
.presets
.env
.as_ref()
.and_then(|env_options| EnvOptions::try_from(env_options).ok())
.unwrap_or_default();

let regexp = RegExpOptions {
sticky_flag: env.regexp.sticky_flag || options.plugins.sticky_flag,
unicode_flag: env.regexp.unicode_flag || options.plugins.unicode_flag,
dot_all_flag: env.regexp.dot_all_flag || options.plugins.dot_all_flag,
look_behind_assertions: env.regexp.look_behind_assertions
|| options.plugins.look_behind_assertions,
named_capture_groups: env.regexp.named_capture_groups
|| options.plugins.named_capture_groups,
unicode_property_escapes: env.regexp.unicode_property_escapes
|| options.plugins.unicode_property_escapes,
match_indices: env.regexp.match_indices,
set_notation: env.regexp.set_notation || options.plugins.set_notation,
};

let es2015 = ES2015Options {
arrow_function: options.plugins.arrow_function.or(env.es2015.arrow_function),
};

let es2016 = ES2016Options {
exponentiation_operator: options.plugins.exponentiation_operator
|| env.es2016.exponentiation_operator,
};

let es2017 = ES2017Options {
async_to_generator: options.plugins.async_to_generator || env.es2017.async_to_generator,
};

let es2018 = ES2018Options {
object_rest_spread: options
.plugins
.object_rest_spread
.or(env.es2018.object_rest_spread),
async_generator_functions: options.plugins.async_generator_functions
|| env.es2018.async_generator_functions,
};

let es2019 = ES2019Options {
optional_catch_binding: options.plugins.optional_catch_binding
|| env.es2019.optional_catch_binding,
};

let es2020 = ES2020Options {
nullish_coalescing_operator: options.plugins.nullish_coalescing_operator
|| env.es2020.nullish_coalescing_operator,
};

let es2021 = ES2021Options {
logical_assignment_operators: options.plugins.logical_assignment_operators
|| env.es2021.logical_assignment_operators,
};

let es2022 = ES2022Options {
class_static_block: options.plugins.class_static_block || env.es2022.class_static_block,
class_properties: options.plugins.class_properties.or(env.es2022.class_properties),
};

Ok(Self { regexp, es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022 })
}
}
86 changes: 78 additions & 8 deletions crates/oxc_transformer/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ mod env;

use std::path::PathBuf;

use env::EnvOptions;
use oxc_diagnostics::Error;

pub use env::EnvOptions;

use crate::{
common::helper_loader::{HelperLoaderMode, HelperLoaderOptions},
compiler_assumptions::CompilerAssumptions,
es2015::ES2015Options,
es2016::ES2016Options,
es2017::ES2017Options,
es2018::ES2018Options,
es2019::ES2019Options,
es2020::ES2020Options,
es2021::ES2021Options,
es2022::ES2022Options,
jsx::JsxOptions,
regexp::RegExpOptions,
typescript::TypeScriptOptions,
ReactRefreshOptions,
};
Expand Down Expand Up @@ -100,12 +110,62 @@ impl TryFrom<&BabelOptions> for TransformOptions {
jsx_options
};

let env = match EnvOptions::try_from(options) {
Ok(env) => Some(env),
Err(errs) => {
errors.extend(errs);
None
}
let env = options.presets.env.clone().unwrap_or_default();

let regexp = RegExpOptions {
sticky_flag: env.regexp.sticky_flag || options.plugins.sticky_flag,
unicode_flag: env.regexp.unicode_flag || options.plugins.unicode_flag,
dot_all_flag: env.regexp.dot_all_flag || options.plugins.dot_all_flag,
look_behind_assertions: env.regexp.look_behind_assertions
|| options.plugins.look_behind_assertions,
named_capture_groups: env.regexp.named_capture_groups
|| options.plugins.named_capture_groups,
unicode_property_escapes: env.regexp.unicode_property_escapes
|| options.plugins.unicode_property_escapes,
match_indices: env.regexp.match_indices,
set_notation: env.regexp.set_notation || options.plugins.set_notation,
};

let es2015 = ES2015Options {
arrow_function: options.plugins.arrow_function.or(env.es2015.arrow_function),
};

let es2016 = ES2016Options {
exponentiation_operator: options.plugins.exponentiation_operator
|| env.es2016.exponentiation_operator,
};

let es2017 = ES2017Options {
async_to_generator: options.plugins.async_to_generator || env.es2017.async_to_generator,
};

let es2018 = ES2018Options {
object_rest_spread: options
.plugins
.object_rest_spread
.or(env.es2018.object_rest_spread),
async_generator_functions: options.plugins.async_generator_functions
|| env.es2018.async_generator_functions,
};

let es2019 = ES2019Options {
optional_catch_binding: options.plugins.optional_catch_binding
|| env.es2019.optional_catch_binding,
};

let es2020 = ES2020Options {
nullish_coalescing_operator: options.plugins.nullish_coalescing_operator
|| env.es2020.nullish_coalescing_operator,
};

let es2021 = ES2021Options {
logical_assignment_operators: options.plugins.logical_assignment_operators
|| env.es2021.logical_assignment_operators,
};

let es2022 = ES2022Options {
class_static_block: options.plugins.class_static_block || env.es2022.class_static_block,
class_properties: options.plugins.class_properties.or(env.es2022.class_properties),
};

if !errors.is_empty() {
Expand All @@ -126,7 +186,17 @@ impl TryFrom<&BabelOptions> for TransformOptions {
assumptions: options.assumptions,
typescript,
jsx,
env: env.unwrap_or_default(),
env: EnvOptions {
regexp,
es2015,
es2016,
es2017,
es2018,
es2019,
es2020,
es2021,
es2022,
},
helper_loader,
})
}
Expand Down

0 comments on commit 7f1d1fe

Please sign in to comment.