mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
Supported in [esbuild](https://esbuild.github.io/try/#dAAwLjI0LjAALS10YXJnZXQ9ZXM2ADEqKjI)
86 lines
2.9 KiB
Rust
86 lines
2.9 KiB
Rust
use std::str::FromStr;
|
|
|
|
use crate::{codegen, test};
|
|
use oxc_span::SourceType;
|
|
use oxc_transformer::{ESTarget, TransformOptions};
|
|
|
|
#[test]
|
|
fn es_target() {
|
|
use std::fmt::Write;
|
|
|
|
let cases = [
|
|
("es5", "() => {}"),
|
|
("es6", "a ** b"),
|
|
("es2015", "a ** b"),
|
|
("es2016", "async function foo() {}"),
|
|
("es2017", "({ ...x })"),
|
|
("es2018", "try {} catch {}"),
|
|
("es2019", "a?.b"),
|
|
("es2019", "a ?? b"),
|
|
("es2020", "a ||= b"),
|
|
("es2019", "1n ** 2n"), // test target error
|
|
("es2021", "class foo { static {} }"),
|
|
("es2021", "class Foo { #a; }"), // Plugin not ready
|
|
];
|
|
|
|
// Test no transformation for esnext.
|
|
let options = TransformOptions::from(ESTarget::from_str("esnext").unwrap());
|
|
for (_, case) in cases {
|
|
assert_eq!(test(case, &options), Ok(codegen(case, SourceType::mjs())));
|
|
}
|
|
|
|
let snapshot =
|
|
cases.into_iter().enumerate().fold(String::new(), |mut w, (i, (target, case))| {
|
|
let options = TransformOptions::from_target(target).unwrap();
|
|
let result = match test(case, &options) {
|
|
Ok(code) => code,
|
|
Err(errors) => errors
|
|
.into_iter()
|
|
.map(|err| format!("{:?}", err.with_source_code(case.to_string())))
|
|
.collect::<Vec<_>>()
|
|
.join("\n"),
|
|
};
|
|
write!(w, "########## {i} {target}\n{case}\n----------\n{result}\n").unwrap();
|
|
w
|
|
});
|
|
|
|
#[cfg(not(miri))]
|
|
{
|
|
insta::with_settings!({ prepend_module_to_snapshot => false, snapshot_suffix => "", omit_expression => true }, {
|
|
insta::assert_snapshot!("es_target", snapshot);
|
|
});
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn no_class_properties() {
|
|
let code = "class Foo { #a; }";
|
|
let options = TransformOptions::from(ESTarget::from_str("es2015").unwrap());
|
|
assert_eq!(test(code, &options), Ok(codegen(code, SourceType::mjs())));
|
|
}
|
|
|
|
#[test]
|
|
fn target_list_pass() {
|
|
// https://vite.dev/config/build-options.html#build-target
|
|
let target = "es2020,edge88,firefox78,chrome87,safari14";
|
|
let result = TransformOptions::from_target(target).unwrap();
|
|
assert!(!result.env.es2019.optional_catch_binding);
|
|
assert!(!result.env.es2020.nullish_coalescing_operator);
|
|
assert!(result.env.es2021.logical_assignment_operators);
|
|
assert!(result.env.es2022.class_static_block);
|
|
}
|
|
|
|
#[test]
|
|
fn target_list_fail() {
|
|
let targets = [
|
|
("asdf", "Invalid target 'asdf'."),
|
|
("es2020,es2020", "'es2020' is already specified."),
|
|
("chrome1,chrome1", "'chrome1' is already specified."),
|
|
("chromeXXX", "All version numbers must be in the format \"X\", \"X.Y\", or \"X.Y.Z\" where X, Y, and Z are non-negative integers."),
|
|
];
|
|
|
|
for (target, expected) in targets {
|
|
let result = TransformOptions::from_target(target);
|
|
assert_eq!(result.unwrap_err().to_string(), expected);
|
|
}
|
|
}
|