Skip to content

Commit d1a0d04

Browse files
committed
feat(compat): extract compatibility data to oxc_compat crate (#13932)
Extracted compatibility data to a new `oxc_compat` crate so that we can use this information from the minifier as well.
1 parent 51a2c30 commit d1a0d04

File tree

17 files changed

+126
-57
lines changed

17 files changed

+126
-57
lines changed

Cargo.lock

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ oxc_ast_macros = { version = "0.90.0", path = "crates/oxc_ast_macros" }
110110
oxc_ast_visit = { version = "0.90.0", path = "crates/oxc_ast_visit" }
111111
oxc_cfg = { version = "0.90.0", path = "crates/oxc_cfg" }
112112
oxc_codegen = { version = "0.90.0", path = "crates/oxc_codegen" }
113+
oxc_compat = { version = "0.90.0", path = "crates/oxc_compat" }
113114
oxc_data_structures = { version = "0.90.0", path = "crates/oxc_data_structures" }
114115
oxc_diagnostics = { version = "0.90.0", path = "crates/oxc_diagnostics" }
115116
oxc_ecmascript = { version = "0.90.0", path = "crates/oxc_ecmascript" }

crates/oxc_compat/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "oxc_compat"
3+
version = "0.90.0"
4+
authors.workspace = true
5+
categories.workspace = true
6+
edition.workspace = true
7+
homepage.workspace = true
8+
include = ["/src"]
9+
keywords.workspace = true
10+
license.workspace = true
11+
publish = true
12+
repository.workspace = true
13+
rust-version.workspace = true
14+
description.workspace = true
15+
16+
[lints]
17+
workspace = true
18+
19+
[lib]
20+
test = true
21+
doctest = false
22+
23+
[dependencies]
24+
cow-utils = { workspace = true }
25+
oxc-browserslist = { workspace = true }
26+
oxc_syntax = { workspace = true }
27+
rustc-hash = { workspace = true }
28+
serde = { workspace = true, features = ["derive"] }

crates/oxc_compat/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Oxc Compat

crates/oxc_transformer/src/options/babel/env/targets.rs renamed to crates/oxc_compat/src/babel_targets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use browserslist::Version;
44
use rustc_hash::FxHashMap;
55
use serde::Deserialize;
66

7-
use crate::options::{BrowserslistQuery, Engine, EngineTargets};
7+
use crate::{BrowserslistQuery, Engine, EngineTargets};
88

99
/// <https://babel.dev/docs/babel-preset-env#targets>
1010
#[derive(Debug, Deserialize)]

crates/oxc_transformer/src/options/browserslist_query.rs renamed to crates/oxc_compat/src/browserslist_query.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ fn cache() -> &'static RwLock<FxHashMap<BrowserslistQuery, EngineTargets>> {
1818
}
1919

2020
impl BrowserslistQuery {
21+
/// # Errors
22+
/// When the query is invalid.
23+
///
24+
/// # Panics
25+
/// When the rwlock is poisoned.
2126
pub fn exec(&self) -> Result<EngineTargets, String> {
2227
if let Some(v) = cache().read().unwrap().get(self) {
2328
return Ok(v.clone());
File renamed without changes.

crates/oxc_transformer/src/options/engine_targets.rs renamed to crates/oxc_compat/src/engine_targets.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ use std::{
44
str::FromStr,
55
};
66

7-
use browserslist::Version;
7+
pub use browserslist::Version;
8+
use oxc_syntax::es_target::ESTarget;
89
use rustc_hash::FxHashMap;
910
use serde::Deserialize;
1011

12+
use crate::browserslist_query::BrowserslistQuery;
13+
use crate::{babel_targets::BabelTargets, es_target::ESVersion};
14+
1115
use super::{
12-
BrowserslistQuery,
13-
babel::BabelTargets,
14-
engine::Engine,
16+
Engine,
1517
es_features::{ESFeature, features},
1618
};
1719

@@ -46,11 +48,15 @@ impl EngineTargets {
4648
BrowserslistQuery::Single(query.to_string()).exec()
4749
}
4850

49-
/// Returns true if all fields are [None].
51+
/// Returns true if all fields are empty.
5052
pub fn is_any_target(&self) -> bool {
5153
self.0.is_empty()
5254
}
5355

56+
/// Check if the target engines support the given ES feature.
57+
///
58+
/// Returns `true` if the feature is NOT supported (needs transformation),
59+
/// `false` if the feature IS supported (can be used natively).
5460
pub fn has_feature(&self, feature: ESFeature) -> bool {
5561
let feature_engine_targets = &features()[&feature];
5662
for (engine, feature_version) in feature_engine_targets.iter() {
@@ -88,4 +94,42 @@ impl EngineTargets {
8894
}
8995
engine_targets
9096
}
97+
98+
/// # Errors
99+
///
100+
/// * When the query failed to parse.
101+
pub fn from_target(s: &str) -> Result<Self, String> {
102+
if s.contains(',') {
103+
Self::from_target_list(&s.split(',').collect::<Vec<_>>())
104+
} else {
105+
Self::from_target_list(&[s])
106+
}
107+
}
108+
109+
/// # Errors
110+
///
111+
/// * When the query failed to parse.
112+
pub fn from_target_list<S: AsRef<str>>(list: &[S]) -> Result<Self, String> {
113+
let mut es_target = None;
114+
let mut engine_targets = EngineTargets::default();
115+
116+
for s in list {
117+
let s = s.as_ref();
118+
// Parse `esXXXX`.
119+
if let Ok(target) = ESTarget::from_str(s) {
120+
if let Some(target) = es_target {
121+
return Err(format!("'{target}' is already specified."));
122+
}
123+
es_target = Some(target);
124+
} else {
125+
// Parse `chromeXX`, `edgeXX` etc.
126+
let (engine, version) = Engine::parse_name_and_version(s)?;
127+
if engine_targets.insert(engine, version).is_some() {
128+
return Err(format!("'{s}' is already specified."));
129+
}
130+
}
131+
}
132+
engine_targets.insert(Engine::Es, es_target.unwrap_or(ESTarget::default()).version());
133+
Ok(engine_targets)
134+
}
91135
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)