Skip to content

Commit

Permalink
feat(transformer): support TransformOptions::from_preset_env API (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Aug 29, 2024
1 parent b103737 commit 0eb7602
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 72 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/env/data/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ fn bugfix_features() -> &'static FxHashMap<String, Versions> {
})
}

pub fn can_enable_plugin(name: &str, targets: &Versions, bugfixes: bool) -> bool {
pub fn can_enable_plugin(name: &str, targets: Option<&Versions>, bugfixes: bool) -> bool {
let versions = if bugfixes {
bugfix_features().get(name).unwrap_or_else(|| &features()[name])
} else {
&features()[name]
};
targets.should_enable(versions)
targets.is_some_and(|v| v.should_enable(versions))
}
16 changes: 14 additions & 2 deletions crates/oxc_transformer/src/es2015/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Deserialize;

use crate::env::{can_enable_plugin, Versions};

use super::ArrowFunctionsOptions;

#[derive(Debug, Default, Clone, Deserialize)]
Expand All @@ -10,9 +12,19 @@ pub struct ES2015Options {
}

impl ES2015Options {
#[must_use]
pub fn with_arrow_function(mut self, arrow_function: Option<ArrowFunctionsOptions>) -> Self {
pub fn with_arrow_function(
&mut self,
arrow_function: Option<ArrowFunctionsOptions>,
) -> &mut Self {
self.arrow_function = arrow_function;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
arrow_function: can_enable_plugin("transform-arrow-functions", targets, bugfixes)
.then(Default::default),
}
}
}
16 changes: 14 additions & 2 deletions crates/oxc_transformer/src/es2016/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Deserialize;

use crate::env::{can_enable_plugin, Versions};

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ES2016Options {
Expand All @@ -8,9 +10,19 @@ pub struct ES2016Options {
}

impl ES2016Options {
#[must_use]
pub fn with_exponentiation_operator(mut self, enable: bool) -> Self {
pub fn with_exponentiation_operator(&mut self, enable: bool) -> &mut Self {
self.exponentiation_operator = enable;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
exponentiation_operator: can_enable_plugin(
"transform-exponentiation-operator",
targets,
bugfixes,
),
}
}
}
23 changes: 20 additions & 3 deletions crates/oxc_transformer/src/es2018/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::object_rest_spread::ObjectRestSpreadOptions;
use crate::env::{can_enable_plugin, Versions};

use serde::Deserialize;

use super::ObjectRestSpreadOptions;

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ES2018Options {
Expand All @@ -9,9 +12,23 @@ pub struct ES2018Options {
}

impl ES2018Options {
#[must_use]
pub fn with_object_rest_spread(mut self, option: Option<ObjectRestSpreadOptions>) -> Self {
pub fn with_object_rest_spread(
&mut self,
option: Option<ObjectRestSpreadOptions>,
) -> &mut Self {
self.object_rest_spread = option;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
object_rest_spread: can_enable_plugin(
"transform-object-rest-spread",
targets,
bugfixes,
)
.then(Default::default),
}
}
}
16 changes: 14 additions & 2 deletions crates/oxc_transformer/src/es2019/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Deserialize;

use crate::env::{can_enable_plugin, Versions};

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ES2019Options {
Expand All @@ -8,9 +10,19 @@ pub struct ES2019Options {
}

impl ES2019Options {
#[must_use]
pub fn with_optional_catch_binding(mut self, enable: bool) -> Self {
pub fn with_optional_catch_binding(&mut self, enable: bool) -> &mut Self {
self.optional_catch_binding = enable;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
optional_catch_binding: can_enable_plugin(
"transform-optional-catch-binding",
targets,
bugfixes,
),
}
}
}
16 changes: 14 additions & 2 deletions crates/oxc_transformer/src/es2020/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Deserialize;

use crate::env::{can_enable_plugin, Versions};

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ES2020Options {
Expand All @@ -8,9 +10,19 @@ pub struct ES2020Options {
}

impl ES2020Options {
#[must_use]
pub fn with_nullish_coalescing_operator(mut self, enable: bool) -> Self {
pub fn with_nullish_coalescing_operator(&mut self, enable: bool) -> &mut Self {
self.nullish_coalescing_operator = enable;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
nullish_coalescing_operator: can_enable_plugin(
"transform-nullish-coalescing-operator",
targets,
bugfixes,
),
}
}
}
16 changes: 14 additions & 2 deletions crates/oxc_transformer/src/es2021/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Deserialize;

use crate::env::{can_enable_plugin, Versions};

#[derive(Debug, Default, Clone, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct ES2021Options {
Expand All @@ -8,9 +10,19 @@ pub struct ES2021Options {
}

impl ES2021Options {
#[must_use]
pub fn with_logical_assignment_operators(mut self, enable: bool) -> Self {
pub fn with_logical_assignment_operators(&mut self, enable: bool) -> &mut Self {
self.logical_assignment_operators = enable;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
logical_assignment_operators: can_enable_plugin(
"transform-logical-assignment-operators",
targets,
bugfixes,
),
}
}
}
Loading

0 comments on commit 0eb7602

Please sign in to comment.