mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer): use Browserslist::Version (#7028)
This commit is contained in:
parent
76947e2aec
commit
6284f84f58
6 changed files with 13 additions and 139 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
|
@ -1047,7 +1047,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
|
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"windows-targets 0.48.5",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1405,9 +1405,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "oxc-browserslist"
|
name = "oxc-browserslist"
|
||||||
version = "1.0.3"
|
version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "50b5ad390a7cf69d5f574ea41eada88a7c0947272647703fe1ec6ce23bfe1714"
|
checksum = "d240f6572a29895f324ad834a42a0c4bad0739269a08d0181ad7d6db7537e0d1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"nom",
|
"nom",
|
||||||
|
|
@ -3505,7 +3505,7 @@ version = "0.1.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,7 @@ nonmax = "0.5.5"
|
||||||
num-bigint = "0.4.6"
|
num-bigint = "0.4.6"
|
||||||
num-traits = "0.2.19"
|
num-traits = "0.2.19"
|
||||||
once_cell = "1.20.2"
|
once_cell = "1.20.2"
|
||||||
oxc-browserslist = "1.0.3"
|
oxc-browserslist = "1.1.0"
|
||||||
oxc_resolver = "2.0.0"
|
oxc_resolver = "2.0.0"
|
||||||
petgraph = "0.6.5"
|
petgraph = "0.6.5"
|
||||||
phf = "0.11.2"
|
phf = "0.11.2"
|
||||||
|
|
|
||||||
2
crates/oxc_transformer/src/env/data/babel.rs
vendored
2
crates/oxc_transformer/src/env/data/babel.rs
vendored
|
|
@ -2,7 +2,7 @@ use std::sync::OnceLock;
|
||||||
|
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::env::targets::{version::Version, Targets};
|
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>
|
/// 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> {
|
fn features() -> &'static FxHashMap<String, Targets> {
|
||||||
|
|
|
||||||
2
crates/oxc_transformer/src/env/mod.rs
vendored
2
crates/oxc_transformer/src/env/mod.rs
vendored
|
|
@ -4,4 +4,4 @@ mod targets;
|
||||||
|
|
||||||
pub use data::can_enable_plugin;
|
pub use data::can_enable_plugin;
|
||||||
pub use options::EnvOptions;
|
pub use options::EnvOptions;
|
||||||
pub use targets::Targets;
|
pub use targets::{Targets, Version};
|
||||||
|
|
|
||||||
13
crates/oxc_transformer/src/env/targets/mod.rs
vendored
13
crates/oxc_transformer/src/env/targets/mod.rs
vendored
|
|
@ -5,17 +5,16 @@
|
||||||
//!
|
//!
|
||||||
//! This file is copied from <https://github.com/swc-project/swc/blob/ea14fc8e5996dcd736b8deb4cc99262d07dfff44/crates/preset_env_base/src/lib.rs>
|
//! This file is copied from <https://github.com/swc-project/swc/blob/ea14fc8e5996dcd736b8deb4cc99262d07dfff44/crates/preset_env_base/src/lib.rs>
|
||||||
|
|
||||||
use std::{ops::Deref, str::FromStr};
|
use std::ops::Deref;
|
||||||
|
|
||||||
use oxc_diagnostics::Error;
|
use oxc_diagnostics::Error;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
pub mod query;
|
pub mod query;
|
||||||
pub mod version;
|
|
||||||
|
|
||||||
|
pub use browserslist::Version;
|
||||||
pub use query::Query;
|
pub use query::Query;
|
||||||
pub use version::Version;
|
|
||||||
|
|
||||||
/// A map of browser names to data for feature support in browser.
|
/// A map of browser names to data for feature support in browser.
|
||||||
///
|
///
|
||||||
|
|
@ -150,13 +149,13 @@ impl TryFrom<BabelTargets> for Targets {
|
||||||
let BabelTargetsValue::String(v) = v else {
|
let BabelTargetsValue::String(v) = v else {
|
||||||
return Err(Error::msg(format!("{v:?} is not a string for {k}.")));
|
return Err(Error::msg(format!("{v:?} is not a string for {k}.")));
|
||||||
};
|
};
|
||||||
match Version::from_str(&v) {
|
match Version::parse(&v) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
new_map.insert(k, v);
|
new_map.insert(k, v);
|
||||||
}
|
}
|
||||||
Err(()) => {
|
Err(err) => {
|
||||||
return Err(oxc_diagnostics::Error::msg(format!(
|
return Err(oxc_diagnostics::Error::msg(format!(
|
||||||
"Failed to parse `{v}` for `{k}`"
|
"Failed to parse `{v}` for `{k}`\n{err:?}"
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -169,7 +168,7 @@ impl TryFrom<BabelTargets> for Targets {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::env::{targets::version::Version, Targets};
|
use crate::env::{targets::Version, Targets};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_enable_android_falls_back_to_chrome() {
|
fn should_enable_android_falls_back_to_chrome() {
|
||||||
|
|
|
||||||
125
crates/oxc_transformer/src/env/targets/version.rs
vendored
125
crates/oxc_transformer/src/env/targets/version.rs
vendored
|
|
@ -1,125 +0,0 @@
|
||||||
//! Module for browser versions
|
|
||||||
//!
|
|
||||||
//! This file is copied from <https://github.com/swc-project/swc/blob/ea14fc8e5996dcd736b8deb4cc99262d07dfff44/crates/preset_env_base/src/version.rs>
|
|
||||||
|
|
||||||
use std::{cmp, cmp::Ordering, fmt, str::FromStr};
|
|
||||||
|
|
||||||
use serde::{de, de::Visitor, Deserialize, Deserializer, Serialize};
|
|
||||||
|
|
||||||
/// A version of a browser.
|
|
||||||
///
|
|
||||||
/// This is similar to semver, but this assumes a production build. (No tag like
|
|
||||||
/// `alpha`)
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
|
|
||||||
pub struct Version {
|
|
||||||
/// `a` in `a.b.c`
|
|
||||||
pub major: u32,
|
|
||||||
/// `b` in `a.b.c`
|
|
||||||
pub minor: u32,
|
|
||||||
/// `c` in `a.b.c`
|
|
||||||
pub patch: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
|
||||||
impl FromStr for Version {
|
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
fn from_str(v: &str) -> Result<Self, Self::Err> {
|
|
||||||
let mut parts = v.split('.');
|
|
||||||
// safari tp
|
|
||||||
let major = parts.next().unwrap().parse::<u32>().map_err(|_| ())?;
|
|
||||||
let minor = parts.next().unwrap_or("0").parse::<u32>().unwrap();
|
|
||||||
let patch = parts.next().unwrap_or("0").parse::<u32>().unwrap();
|
|
||||||
Ok(Version { major, minor, patch })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl cmp::PartialOrd for Version {
|
|
||||||
fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl cmp::Ord for Version {
|
|
||||||
fn cmp(&self, other: &Version) -> Ordering {
|
|
||||||
match self.major.cmp(&other.major) {
|
|
||||||
Ordering::Equal => {}
|
|
||||||
r => return r,
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.minor.cmp(&other.minor) {
|
|
||||||
Ordering::Equal => {}
|
|
||||||
r => return r,
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.patch.cmp(&other.patch) {
|
|
||||||
Ordering::Equal => {}
|
|
||||||
r => return r,
|
|
||||||
}
|
|
||||||
|
|
||||||
Ordering::Equal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SerdeVisitor;
|
|
||||||
|
|
||||||
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
|
||||||
impl<'de> Visitor<'de> for SerdeVisitor {
|
|
||||||
type Value = Version;
|
|
||||||
|
|
||||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
formatter.write_str("a browser version")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: de::Error,
|
|
||||||
{
|
|
||||||
Ok(Version { major: v as _, minor: 0, patch: 0 })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: de::Error,
|
|
||||||
{
|
|
||||||
Ok(Version { major: v as _, minor: 0, patch: 0 })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: de::Error,
|
|
||||||
{
|
|
||||||
Ok(Version { major: v.floor() as _, minor: v.fract() as _, patch: 0 })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: de::Error,
|
|
||||||
{
|
|
||||||
v.parse().map_err(|()| de::Error::invalid_type(de::Unexpected::Str(v), &self))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: de::Error,
|
|
||||||
{
|
|
||||||
self.visit_str(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
|
||||||
where
|
|
||||||
E: de::Error,
|
|
||||||
{
|
|
||||||
self.visit_str(&v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for Version {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Version, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
deserializer.deserialize_any(SerdeVisitor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue