Skip to content

Commit

Permalink
refactor(transformer): clean up TransformerOptions (#7005)
Browse files Browse the repository at this point in the history
This is a work towards #6982

Next PR will try and make sense of env options vs babel options vs targets and bugfixes, I'm super confused right now.
  • Loading branch information
Boshen committed Oct 30, 2024
1 parent caaf00e commit 5b11cdf
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 390 deletions.
12 changes: 1 addition & 11 deletions crates/oxc_transformer/src/env/options.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use serde::Deserialize;
use serde_json::Value;

use oxc_diagnostics::Error;

use super::targets::{query::Targets, Versions};
use super::targets::query::Targets;

fn default_as_true() -> bool {
true
Expand Down Expand Up @@ -54,11 +52,3 @@ pub struct EnvOptions {
#[deprecated = "Not Implemented"]
pub shipped_proposals: bool,
}

impl EnvOptions {
/// # Errors
///
pub fn get_targets(&self) -> Result<Versions, Error> {
self.targets.clone().get_targets()
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2015/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use serde::Deserialize;

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

use super::ArrowFunctionsOptions;

#[derive(Debug, Default, Clone, Deserialize)]
Expand All @@ -10,21 +8,3 @@ pub struct ES2015Options {
#[serde(skip)]
pub arrow_function: Option<ArrowFunctionsOptions>,
}

impl ES2015Options {
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),
}
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2016/options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
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 {
#[serde(skip)]
pub exponentiation_operator: bool,
}

impl ES2016Options {
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,
),
}
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2017/options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
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 ES2017Options {
#[serde(skip)]
pub async_to_generator: bool,
}

impl ES2017Options {
pub fn with_async_to_generator(&mut self, enable: bool) -> &mut Self {
self.async_to_generator = enable;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
async_to_generator: can_enable_plugin(
"transform-async-to-generator",
targets,
bugfixes,
),
}
}
}
24 changes: 0 additions & 24 deletions crates/oxc_transformer/src/es2018/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use serde::Deserialize;

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

use super::ObjectRestSpreadOptions;

#[derive(Debug, Default, Clone, Deserialize)]
Expand All @@ -10,25 +8,3 @@ pub struct ES2018Options {
#[serde(skip)]
pub object_rest_spread: Option<ObjectRestSpreadOptions>,
}

impl ES2018Options {
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),
}
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2019/options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
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 {
#[serde(skip)]
pub optional_catch_binding: bool,
}

impl ES2019Options {
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,
),
}
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2020/options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
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 {
#[serde(skip)]
pub nullish_coalescing_operator: bool,
}

impl ES2020Options {
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,
),
}
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2021/options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
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 {
#[serde(skip)]
pub logical_assignment_operators: bool,
}

impl ES2021Options {
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,
),
}
}
}
20 changes: 0 additions & 20 deletions crates/oxc_transformer/src/es2022/options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
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 ES2022Options {
#[serde(skip)]
pub class_static_block: bool,
}

impl ES2022Options {
pub fn with_class_static_block(&mut self, enable: bool) -> &mut Self {
self.class_static_block = enable;
self
}

#[must_use]
pub fn from_targets_and_bugfixes(targets: Option<&Versions>, bugfixes: bool) -> Self {
Self {
class_static_block: can_enable_plugin(
"transform-class-static-block",
targets,
bugfixes,
),
}
}
}
20 changes: 19 additions & 1 deletion crates/oxc_transformer/src/options/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,48 @@ use serde::Deserialize;
use serde_json::Value;

/// Babel options
///
/// <https://babel.dev/docs/options#plugin-and-preset-options>
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BabelOptions {
// Primary options
pub cwd: Option<PathBuf>,
pub source_type: Option<String>,

// Config Loading options

// Plugin and Preset options
#[serde(default)]
pub plugins: Vec<Value>, // Can be a string or an array

#[serde(default)]
pub presets: Vec<Value>, // Can be a string or an array

// Misc options
pub source_type: Option<String>,

#[serde(default)]
pub assumptions: Value,

// Test options
pub throws: Option<String>,

#[serde(rename = "BABEL_8_BREAKING")]
pub babel_8_breaking: Option<bool>,

/// Babel test helper for running tests on specific operating systems
pub os: Option<Vec<TestOs>>,

// Parser options for babel-parser
#[serde(default)]
pub allow_return_outside_function: bool,

#[serde(default)]
pub allow_await_outside_function: bool,

#[serde(default)]
pub allow_undeclared_exports: bool,

#[serde(default = "default_as_true")]
pub external_helpers: bool,
}
Expand Down
Loading

0 comments on commit 5b11cdf

Please sign in to comment.