Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions crates/oxc_transformer/src/env/data/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hash::FxHashMap;
use crate::env::{Targets, Version};

/// Reference: <https://github.com/swc-project/swc/blob/ea14fc8e5996dcd736b8deb4cc99262d07dfff44/crates/swc_ecma_preset_env/src/transform_data.rs#L194-L218>
fn features() -> &'static FxHashMap<String, Targets> {
pub fn features() -> &'static FxHashMap<String, Targets> {
static FEATURES: OnceLock<FxHashMap<String, Targets>> = OnceLock::new();
FEATURES.get_or_init(|| {
let mut map: FxHashMap<String, FxHashMap<String, String>> =
Expand Down Expand Up @@ -39,7 +39,7 @@ fn features() -> &'static FxHashMap<String, Targets> {
}

/// Reference: <https://github.com/swc-project/swc/blob/ea14fc8e5996dcd736b8deb4cc99262d07dfff44/crates/swc_ecma_preset_env/src/transform_data.rs#L220-L237>
fn bugfix_features() -> &'static FxHashMap<String, Targets> {
pub fn bugfix_features() -> &'static FxHashMap<String, Targets> {
static BUGFIX_FEATURES: OnceLock<FxHashMap<String, Targets>> = OnceLock::new();
BUGFIX_FEATURES.get_or_init(|| {
let map = serde_json::from_str::<FxHashMap<String, Targets>>(include_str!(
Expand All @@ -49,12 +49,3 @@ fn bugfix_features() -> &'static FxHashMap<String, Targets> {
features().clone().into_iter().chain(map).collect()
})
}

pub fn can_enable_plugin(name: &str, targets: Option<&Targets>, bugfixes: bool) -> bool {
let versions = if bugfixes {
bugfix_features().get(name).unwrap_or_else(|| &features()[name])
} else {
&features()[name]
};
targets.is_some_and(|v| v.should_enable(versions))
}
4 changes: 1 addition & 3 deletions crates/oxc_transformer/src/env/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
mod babel;

pub use babel::can_enable_plugin;
pub mod babel;
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod data;
mod options;
mod targets;

pub use data::can_enable_plugin;
pub use data::babel::{bugfix_features, features};
pub use options::EnvOptions;
pub use targets::{Targets, Version};
24 changes: 17 additions & 7 deletions crates/oxc_transformer/src/env/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::Deserialize;
use serde_json::Value;

use crate::env::Targets;
use crate::env::{bugfix_features, features, Targets};

fn default_as_true() -> bool {
true
Expand All @@ -23,22 +22,22 @@ pub struct EnvOptions {
pub loose: bool,

#[deprecated = "Not Implemented"]
pub modules: Option<Value>,
pub modules: Option<serde_json::Value>,

#[deprecated = "Not Implemented"]
pub debug: bool,

#[deprecated = "Not Implemented"]
pub include: Option<Value>,
pub include: Option<serde_json::Value>,

#[deprecated = "Not Implemented"]
pub exclude: Option<Value>,
pub exclude: Option<serde_json::Value>,

#[deprecated = "Not Implemented"]
pub use_built_ins: Option<Value>,
pub use_built_ins: Option<serde_json::Value>,

#[deprecated = "Not Implemented"]
pub corejs: Option<Value>,
pub corejs: Option<serde_json::Value>,

#[deprecated = "Not Implemented"]
pub force_all_transforms: bool,
Expand All @@ -52,3 +51,14 @@ pub struct EnvOptions {
#[deprecated = "Not Implemented"]
pub shipped_proposals: bool,
}

impl EnvOptions {
pub fn can_enable_plugin(&self, plugin_name: &str) -> bool {
let versions = if self.bugfixes {
bugfix_features().get(plugin_name).unwrap_or_else(|| &features()[plugin_name])
} else {
&features()[plugin_name]
};
self.targets.should_enable(versions)
}
}
Loading