diff --git a/crates/oxc_transformer/src/options/babel/env/mod.rs b/crates/oxc_transformer/src/options/babel/env/mod.rs index 823187875..bd2498716 100644 --- a/crates/oxc_transformer/src/options/babel/env/mod.rs +++ b/crates/oxc_transformer/src/options/babel/env/mod.rs @@ -4,10 +4,7 @@ use serde::Deserialize; pub use self::targets::BabelTargets; -use crate::options::{ - es_features::{features, ESFeature}, - EngineTargets, -}; +use crate::options::EngineTargets; fn default_as_true() -> bool { true @@ -59,9 +56,3 @@ pub struct BabelEnvOptions { #[deprecated = "Not Implemented"] pub shipped_proposals: bool, } - -impl BabelEnvOptions { - pub fn can_enable(&self, feature: ESFeature) -> bool { - self.targets.should_enable(&features()[&feature]) - } -} diff --git a/crates/oxc_transformer/src/options/engine_targets.rs b/crates/oxc_transformer/src/options/engine_targets.rs index c483014e6..2d789e2cb 100644 --- a/crates/oxc_transformer/src/options/engine_targets.rs +++ b/crates/oxc_transformer/src/options/engine_targets.rs @@ -10,7 +10,11 @@ use serde::Deserialize; use oxc_diagnostics::Error; -use super::{babel::BabelTargets, BrowserslistQuery}; +use super::{ + babel::BabelTargets, + es_features::{features, ESFeature}, + BrowserslistQuery, +}; #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize)] #[serde(rename_all = "lowercase")] @@ -94,6 +98,10 @@ impl EngineTargets { self.0.is_empty() } + pub fn has_feature(&self, feature: ESFeature) -> bool { + self.should_enable(&features()[&feature]) + } + pub fn should_enable(&self, engine_targets: &EngineTargets) -> bool { for (engine, version) in &engine_targets.0 { if let Some(v) = self.0.get(engine) { diff --git a/crates/oxc_transformer/src/options/env.rs b/crates/oxc_transformer/src/options/env.rs index c7c647134..1707ef362 100644 --- a/crates/oxc_transformer/src/options/env.rs +++ b/crates/oxc_transformer/src/options/env.rs @@ -148,45 +148,53 @@ impl From for EnvOptions { impl TryFrom for EnvOptions { type Error = String; + fn try_from(o: BabelEnvOptions) -> Result { + Self::try_from(o.targets) + } +} + +impl TryFrom for EnvOptions { + type Error = String; + #[allow(clippy::enum_glob_use)] /// If there are any errors in the `options.targets``, they will be returned as a list of errors. - fn try_from(o: BabelEnvOptions) -> Result { + fn try_from(o: EngineTargets) -> Result { use ESFeature::*; Ok(Self { regexp: RegExpOptions { - sticky_flag: o.can_enable(ES2015StickyRegex), - unicode_flag: o.can_enable(ES2015UnicodeRegex), - unicode_property_escapes: o.can_enable(ES2018UnicodePropertyRegex), - dot_all_flag: o.can_enable(ES2018DotallRegex), - named_capture_groups: o.can_enable(ES2018NamedCapturingGroupsRegex), - look_behind_assertions: o.can_enable(ES2018LookbehindRegex), - match_indices: o.can_enable(ES2022MatchIndicesRegex), - set_notation: o.can_enable(ES2024UnicodeSetsRegex), + sticky_flag: o.has_feature(ES2015StickyRegex), + unicode_flag: o.has_feature(ES2015UnicodeRegex), + unicode_property_escapes: o.has_feature(ES2018UnicodePropertyRegex), + dot_all_flag: o.has_feature(ES2018DotallRegex), + named_capture_groups: o.has_feature(ES2018NamedCapturingGroupsRegex), + look_behind_assertions: o.has_feature(ES2018LookbehindRegex), + match_indices: o.has_feature(ES2022MatchIndicesRegex), + set_notation: o.has_feature(ES2024UnicodeSetsRegex), }, es2015: ES2015Options { - arrow_function: o.can_enable(ES2015ArrowFunctions).then(Default::default), + arrow_function: o.has_feature(ES2015ArrowFunctions).then(Default::default), }, es2016: ES2016Options { - exponentiation_operator: o.can_enable(ES2016ExponentiationOperator), + exponentiation_operator: o.has_feature(ES2016ExponentiationOperator), }, - es2017: ES2017Options { async_to_generator: o.can_enable(ES2017AsyncToGenerator) }, + es2017: ES2017Options { async_to_generator: o.has_feature(ES2017AsyncToGenerator) }, es2018: ES2018Options { - object_rest_spread: o.can_enable(ES2018ObjectRestSpread).then(Default::default), - async_generator_functions: o.can_enable(ES2018AsyncGeneratorFunctions), + object_rest_spread: o.has_feature(ES2018ObjectRestSpread).then(Default::default), + async_generator_functions: o.has_feature(ES2018AsyncGeneratorFunctions), }, es2019: ES2019Options { - optional_catch_binding: o.can_enable(ES2018OptionalCatchBinding), + optional_catch_binding: o.has_feature(ES2018OptionalCatchBinding), }, es2020: ES2020Options { - nullish_coalescing_operator: o.can_enable(ES2020NullishCoalescingOperator), - big_int: o.can_enable(ES2020BigInt), + nullish_coalescing_operator: o.has_feature(ES2020NullishCoalescingOperator), + big_int: o.has_feature(ES2020BigInt), }, es2021: ES2021Options { - logical_assignment_operators: o.can_enable(ES2020LogicalAssignmentOperators), + logical_assignment_operators: o.has_feature(ES2020LogicalAssignmentOperators), }, es2022: ES2022Options { - class_static_block: o.can_enable(ES2022ClassStaticBlock), - class_properties: o.can_enable(ES2022ClassProperties).then(Default::default), + class_static_block: o.has_feature(ES2022ClassStaticBlock), + class_properties: o.has_feature(ES2022ClassProperties).then(Default::default), }, }) } diff --git a/crates/oxc_transformer/src/options/es_target.rs b/crates/oxc_transformer/src/options/es_target.rs index cf7d613cc..295372b10 100644 --- a/crates/oxc_transformer/src/options/es_target.rs +++ b/crates/oxc_transformer/src/options/es_target.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{fmt, str::FromStr}; use cow_utils::CowUtils; @@ -40,3 +40,23 @@ impl FromStr for ESTarget { } } } + +impl fmt::Display for ESTarget { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let s = match self { + Self::ES5 => "es5", + Self::ES2015 => "es2015", + Self::ES2016 => "es2016", + Self::ES2017 => "es2017", + Self::ES2018 => "es2018", + Self::ES2019 => "es2019", + Self::ES2020 => "es2020", + Self::ES2021 => "es2021", + Self::ES2022 => "es2022", + Self::ES2023 => "es2023", + Self::ES2024 => "es2024", + Self::ESNext => "esnext", + }; + write!(f, "{s}",) + } +}