mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(transformer): add impl TryFrom<EngineTargets> for EnvOptions (#7191)
This commit is contained in:
parent
0a43c640e6
commit
de560837eb
4 changed files with 59 additions and 32 deletions
|
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -148,45 +148,53 @@ impl From<ESTarget> for EnvOptions {
|
|||
impl TryFrom<BabelEnvOptions> for EnvOptions {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(o: BabelEnvOptions) -> Result<Self, Self::Error> {
|
||||
Self::try_from(o.targets)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<EngineTargets> 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<Self, Self::Error> {
|
||||
fn try_from(o: EngineTargets) -> Result<Self, Self::Error> {
|
||||
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),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}",)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue