Skip to content

Commit

Permalink
refactor(transformer): move ESTarget to its own file (#7189)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 7, 2024
1 parent 22898c8 commit 0a43c64
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
48 changes: 5 additions & 43 deletions crates/oxc_transformer/src/options/env.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::str::FromStr;

use cow_utils::CowUtils;
use oxc_diagnostics::Error;
use serde::Deserialize;

Expand All @@ -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<Self, Self::Err> {
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")]
Expand Down Expand Up @@ -134,9 +92,13 @@ impl EnvOptions {
}
}

/// Initialize from a [browserslist] query.
///
/// # Errors
///
/// * When the query failed to parse.
///
/// [browserslist]: <https://github.com/browserslist/browserslist>
pub fn from_browserslist_query(query: &str) -> Result<Self, Error> {
Self::try_from(BabelEnvOptions {
targets: EngineTargets::try_from_query(query)?,
Expand Down
42 changes: 42 additions & 0 deletions crates/oxc_transformer/src/options/es_target.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
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}\".")),
}
}
}
4 changes: 3 additions & 1 deletion crates/oxc_transformer/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod browserslist_query;
mod engine_targets;
mod env;
mod es_features;
mod es_target;

use std::path::PathBuf;

Expand All @@ -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;
Expand Down

0 comments on commit 0a43c64

Please sign in to comment.