diff --git a/crates/oxc_transformer/src/options/env.rs b/crates/oxc_transformer/src/options/env.rs index ffec2ce7a1620..c7c647134852e 100644 --- a/crates/oxc_transformer/src/options/env.rs +++ b/crates/oxc_transformer/src/options/env.rs @@ -1,6 +1,3 @@ -use std::str::FromStr; - -use cow_utils::CowUtils; use oxc_diagnostics::Error; use serde::Deserialize; @@ -17,46 +14,7 @@ use crate::{ EngineTargets, }; -use super::{babel::BabelEnvOptions, ESFeature}; - -#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] -pub enum ESTarget { - ES5, - ES2015, - ES2016, - ES2017, - ES2018, - ES2019, - ES2020, - ES2021, - ES2022, - ES2023, - ES2024, - #[default] - ESNext, -} - -impl FromStr for ESTarget { - type Err = String; - - fn from_str(s: &str) -> Result { - match s.cow_to_lowercase().as_ref() { - "es5" => Ok(Self::ES5), - "es2015" => Ok(Self::ES2015), - "es2016" => Ok(Self::ES2016), - "es2017" => Ok(Self::ES2017), - "es2018" => Ok(Self::ES2018), - "es2019" => Ok(Self::ES2019), - "es2020" => Ok(Self::ES2020), - "es2021" => Ok(Self::ES2021), - "es2022" => Ok(Self::ES2022), - "es2023" => Ok(Self::ES2023), - "es2024" => Ok(Self::ES2024), - "esnext" => Ok(Self::ESNext), - _ => Err(format!("Invalid target \"{s}\".")), - } - } -} +use super::{babel::BabelEnvOptions, ESFeature, ESTarget}; #[derive(Debug, Default, Clone, Deserialize)] #[serde(try_from = "BabelEnvOptions")] @@ -134,9 +92,13 @@ impl EnvOptions { } } + /// Initialize from a [browserslist] query. + /// /// # Errors /// /// * When the query failed to parse. + /// + /// [browserslist]: pub fn from_browserslist_query(query: &str) -> Result { Self::try_from(BabelEnvOptions { targets: EngineTargets::try_from_query(query)?, diff --git a/crates/oxc_transformer/src/options/es_target.rs b/crates/oxc_transformer/src/options/es_target.rs new file mode 100644 index 0000000000000..cf7d613cc0ef3 --- /dev/null +++ b/crates/oxc_transformer/src/options/es_target.rs @@ -0,0 +1,42 @@ +use std::str::FromStr; + +use cow_utils::CowUtils; + +#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] +pub enum ESTarget { + ES5, + ES2015, + ES2016, + ES2017, + ES2018, + ES2019, + ES2020, + ES2021, + ES2022, + ES2023, + ES2024, + #[default] + ESNext, +} + +impl FromStr for ESTarget { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.cow_to_lowercase().as_ref() { + "es5" => Ok(Self::ES5), + "es2015" => Ok(Self::ES2015), + "es2016" => Ok(Self::ES2016), + "es2017" => Ok(Self::ES2017), + "es2018" => Ok(Self::ES2018), + "es2019" => Ok(Self::ES2019), + "es2020" => Ok(Self::ES2020), + "es2021" => Ok(Self::ES2021), + "es2022" => Ok(Self::ES2022), + "es2023" => Ok(Self::ES2023), + "es2024" => Ok(Self::ES2024), + "esnext" => Ok(Self::ESNext), + _ => Err(format!("Invalid target \"{s}\".")), + } + } +} diff --git a/crates/oxc_transformer/src/options/mod.rs b/crates/oxc_transformer/src/options/mod.rs index 2574df6ae9fa2..83f6f550cbdb4 100644 --- a/crates/oxc_transformer/src/options/mod.rs +++ b/crates/oxc_transformer/src/options/mod.rs @@ -4,6 +4,7 @@ mod browserslist_query; mod engine_targets; mod env; mod es_features; +mod es_target; use std::path::PathBuf; @@ -29,8 +30,9 @@ use crate::{ pub use self::{ browserslist_query::BrowserslistQuery, engine_targets::{Engine, EngineTargets}, - env::{ESTarget, EnvOptions}, + env::EnvOptions, es_features::ESFeature, + es_target::ESTarget, }; use self::babel::BabelOptions;