Skip to content

Commit

Permalink
fix(transformer): add more engines to EngineTargets
Browse files Browse the repository at this point in the history
* `android`
* `electron`
* `samsung`
* `opera_mobile`
  • Loading branch information
Boshen committed Nov 5, 2024
1 parent 8340243 commit a6237ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
9 changes: 3 additions & 6 deletions crates/oxc_transformer/src/options/babel/env/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ impl TryFrom<BabelTargets> for EngineTargets {
if key == "safari" && v == "tp" {
continue;
}
// TODO: Some keys are not implemented yet.
// <https://babel.dev/docs/options#targets>:
// Supported environments: android, chrome, deno, edge, electron, firefox, ie, ios, node, opera, rhino, safari, samsung.
let Ok(target) = targets.get_version_mut(&key) else {
continue;
};
let target = targets.get_version_mut(&key).map_err(|()| {
oxc_diagnostics::Error::msg(format!("target {key} is not implemented yet."))
})?;
match Version::parse(&v) {
Ok(version) => {
target.replace(version);
Expand Down
22 changes: 22 additions & 0 deletions crates/oxc_transformer/src/options/engine_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ use oxc_diagnostics::Error;
use super::{babel::BabelTargets, BrowserslistQuery};

/// A map of engine names to minimum supported versions.
///
/// <https://github.com/babel/babel/blob/main/packages/babel-helper-compilation-targets/src/options.ts>
#[derive(Debug, Default, Clone, Eq, PartialEq, Deserialize)]
#[serde(try_from = "BabelTargets")]
pub struct EngineTargets {
android: Option<Version>, // not in esbuild
chrome: Option<Version>,
deno: Option<Version>,
edge: Option<Version>,
electron: Option<Version>, // not in esbuild
firefox: Option<Version>,
hermes: Option<Version>,
ie: Option<Version>,
ios: Option<Version>,
node: Option<Version>,
opera: Option<Version>,
opera_mobile: Option<Version>, // not in esbuild
rhino: Option<Version>,
safari: Option<Version>,
samsung: Option<Version>, // not in esbuild
}

impl EngineTargets {
Expand All @@ -36,6 +42,9 @@ impl EngineTargets {
}

pub fn should_enable(&self, targets: &EngineTargets) -> bool {
if let (Some(v1), Some(v2)) = (&self.android, &targets.android) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.chrome, &targets.chrome) {
return v1 < v2;
}
Expand All @@ -45,6 +54,9 @@ impl EngineTargets {
if let (Some(v1), Some(v2)) = (&self.edge, &targets.edge) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.electron, &targets.electron) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.firefox, &targets.firefox) {
return v1 < v2;
}
Expand All @@ -63,12 +75,18 @@ impl EngineTargets {
if let (Some(v1), Some(v2)) = (&self.opera, &targets.opera) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.opera_mobile, &targets.opera_mobile) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.rhino, &targets.rhino) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.safari, &targets.safari) {
return v1 < v2;
}
if let (Some(v1), Some(v2)) = (&self.samsung, &targets.samsung) {
return v1 < v2;
}
false
}

Expand All @@ -91,17 +109,21 @@ impl EngineTargets {

pub(crate) fn get_version_mut(&mut self, key: &str) -> Result<&mut Option<Version>, ()> {
match key {
"android" => Ok(&mut self.android),
"chrome" | "and_chr" => Ok(&mut self.chrome),
"deno" => Ok(&mut self.deno),
"edge" => Ok(&mut self.edge),
"electron" => Ok(&mut self.electron),
"firefox" | "and_ff" => Ok(&mut self.firefox),
"hermes" => Ok(&mut self.hermes),
"ie" | "ie_mob" => Ok(&mut self.ie),
"ios" | "ios_saf" => Ok(&mut self.ios),
"node" => Ok(&mut self.node),
"opera" | "op_mob" => Ok(&mut self.opera),
"opera_mobile" => Ok(&mut self.opera_mobile),
"rhino" => Ok(&mut self.rhino),
"safari" => Ok(&mut self.safari),
"samsung" => Ok(&mut self.samsung),
_ => Err(()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/snapshots/babel_exec.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ AssertionError: expected [Function Object] to be [Function Bar] // Object.is equ
❯ ../../node_modules/.pnpm/@babel[email protected]/node_modules/@babel/runtime/helpers/asyncToGenerator.js:14:12
❯ Bar.test fixtures/babel-preset-env-test-fixtures-plugins-integration-regression-7064-exec.test.js:14:6
❯ fixtures/babel-preset-env-test-fixtures-plugins-integration-regression-7064-exec.test.js:20:12
❯ ../../node_modules/.pnpm/@vitest[email protected].2/node_modules/@vitest/runner/dist/index.js:146:14
❯ ../../node_modules/.pnpm/@vitest[email protected].4/node_modules/@vitest/runner/dist/index.js:146:14

This error originated in "fixtures/babel-preset-env-test-fixtures-plugins-integration-regression-7064-exec.test.js" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
The latest test that might've caused the error is "fixtures/babel-preset-env-test-fixtures-plugins-integration-regression-7064-exec.test.js". It might mean one of the following:
Expand Down

0 comments on commit a6237ff

Please sign in to comment.