2021-10-01 15:11:17 +03:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2019-12-28 05:25:54 +03:00
|
|
|
use std::{
|
|
|
|
cmp::Ordering,
|
|
|
|
env,
|
|
|
|
fs::File,
|
|
|
|
io,
|
|
|
|
io::Read,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_json::Value;
|
2021-08-14 13:33:16 +03:00
|
|
|
use swc_common::{
|
2022-05-11 07:36:22 +03:00
|
|
|
chain, collections::AHashMap, comments::SingleThreadedComments, errors::HANDLER,
|
|
|
|
input::StringInput, FromVariant, Mark,
|
2021-08-14 13:33:16 +03:00
|
|
|
};
|
2019-12-28 05:25:54 +03:00
|
|
|
use swc_ecma_ast::*;
|
|
|
|
use swc_ecma_codegen::Emitter;
|
2023-03-30 11:06:02 +03:00
|
|
|
use swc_ecma_parser::{Parser, Syntax};
|
2019-12-28 05:25:54 +03:00
|
|
|
use swc_ecma_preset_env::{preset_env, Config, FeatureOrModule, Mode, Targets, Version};
|
2022-02-26 18:09:02 +03:00
|
|
|
use swc_ecma_transforms::{fixer, helpers};
|
2022-05-11 07:36:22 +03:00
|
|
|
use swc_ecma_utils::drop_span;
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_ecma_visit::{as_folder, FoldWith, VisitMut};
|
|
|
|
use testing::{NormalizedOutput, Tester};
|
2019-12-28 05:25:54 +03:00
|
|
|
|
|
|
|
/// options.json file
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct BabelOptions {
|
|
|
|
presets: Vec<(String, PresetConfig)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
|
|
struct PresetConfig {
|
|
|
|
#[serde(default)]
|
|
|
|
pub use_built_ins: UseBuiltIns,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub corejs: CoreJs,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub modules: ModulesConfig,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub targets: Option<Targets>,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub include: Vec<FeatureOrModule>,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub exclude: Vec<FeatureOrModule>,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub force_all_transforms: bool,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub shipped_proposals: bool,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub config_path: String,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
pub debug: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum CoreJs {
|
|
|
|
Ver(Version),
|
2021-08-14 13:33:16 +03:00
|
|
|
Val(AHashMap<String, Value>),
|
2019-12-28 05:25:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CoreJs {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Ver(Version {
|
|
|
|
major: 2,
|
|
|
|
minor: 0,
|
|
|
|
patch: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum ModulesConfig {
|
|
|
|
Bool(bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ModulesConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
ModulesConfig::Bool(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum UseBuiltIns {
|
|
|
|
Bool(bool),
|
|
|
|
Str(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for UseBuiltIns {
|
|
|
|
fn default() -> Self {
|
|
|
|
UseBuiltIns::Bool(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, FromVariant)]
|
|
|
|
enum Error {
|
|
|
|
Io(io::Error),
|
|
|
|
Var(env::VarError),
|
|
|
|
Json(serde_json::Error),
|
|
|
|
Msg(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec(c: PresetConfig, dir: PathBuf) -> Result<(), Error> {
|
|
|
|
println!("Config: {:?}", c);
|
|
|
|
|
|
|
|
Tester::new()
|
|
|
|
.print_errors(|cm, handler| {
|
2022-01-17 16:20:02 +03:00
|
|
|
let mut pass = chain!(
|
|
|
|
preset_env(
|
|
|
|
Mark::fresh(Mark::root()),
|
|
|
|
Some(SingleThreadedComments::default()),
|
|
|
|
Config {
|
|
|
|
debug: c.debug,
|
|
|
|
mode: match c.use_built_ins {
|
|
|
|
UseBuiltIns::Bool(false) => None,
|
|
|
|
UseBuiltIns::Str(ref s) if s == "usage" => Some(Mode::Usage),
|
|
|
|
UseBuiltIns::Str(ref s) if s == "entry" => Some(Mode::Entry),
|
|
|
|
v => unreachable!("invalid: {:?}", v),
|
|
|
|
},
|
|
|
|
skip: vec![],
|
|
|
|
loose: true,
|
|
|
|
// TODO
|
|
|
|
dynamic_import: true,
|
|
|
|
bugfixes: false,
|
|
|
|
include: c.include,
|
|
|
|
exclude: c.exclude,
|
|
|
|
core_js: match c.corejs {
|
|
|
|
CoreJs::Ver(v) => Some(v),
|
|
|
|
ref s => unimplemented!("Unknown core js version: {:?}", s),
|
|
|
|
},
|
|
|
|
force_all_transforms: c.force_all_transforms,
|
|
|
|
shipped_proposals: c.shipped_proposals,
|
|
|
|
targets: c.targets,
|
|
|
|
path: std::env::current_dir().unwrap(),
|
2020-05-18 17:25:41 +03:00
|
|
|
},
|
2022-06-27 15:32:06 +03:00
|
|
|
Default::default(),
|
|
|
|
&mut Default::default(),
|
2022-01-17 16:20:02 +03:00
|
|
|
),
|
|
|
|
fixer(None)
|
2020-05-18 17:25:41 +03:00
|
|
|
);
|
|
|
|
|
2019-12-28 05:25:54 +03:00
|
|
|
let print = |m: &Module| {
|
|
|
|
let mut buf = vec![];
|
|
|
|
{
|
|
|
|
let mut emitter = Emitter {
|
2022-05-15 08:07:21 +03:00
|
|
|
cfg: swc_ecma_codegen::Config {
|
|
|
|
minify: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-12-28 05:25:54 +03:00
|
|
|
comments: None,
|
|
|
|
cm: cm.clone(),
|
2020-07-23 20:18:22 +03:00
|
|
|
wr: Box::new(swc_ecma_codegen::text_writer::JsWriter::new(
|
2019-12-28 05:25:54 +03:00
|
|
|
cm.clone(),
|
|
|
|
"\n",
|
|
|
|
&mut buf,
|
|
|
|
None,
|
2020-07-23 20:18:22 +03:00
|
|
|
)),
|
2019-12-28 05:25:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(m).expect("failed to emit module");
|
|
|
|
}
|
2021-06-12 08:17:09 +03:00
|
|
|
String::from_utf8(buf).expect("invalid utf8 character detected")
|
2019-12-28 05:25:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let fm = cm
|
|
|
|
.load_file(&dir.join("input.mjs"))
|
|
|
|
.expect("failed to load file");
|
|
|
|
let mut p = Parser::new(
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Es(Default::default()),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-12-28 05:25:54 +03:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
2020-07-25 14:26:04 +03:00
|
|
|
let module = p
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|e| e.into_diagnostic(&handler).emit())?;
|
|
|
|
|
|
|
|
for e in p.take_errors() {
|
|
|
|
e.into_diagnostic(&handler).emit()
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:09:02 +03:00
|
|
|
let actual = helpers::HELPERS.set(&Default::default(), || {
|
|
|
|
HANDLER.set(&handler, || module.fold_with(&mut pass))
|
|
|
|
});
|
2019-12-28 05:25:54 +03:00
|
|
|
|
|
|
|
// debug mode?
|
|
|
|
if dir.join("stdout.txt").exists() {
|
|
|
|
let mut out = read(&dir.join("stdout.txt"));
|
|
|
|
|
|
|
|
if dir.join("stderr.txt").exists() {
|
|
|
|
out.push_str("\n\n");
|
|
|
|
out.push_str(&read(&dir.join("stderr.txt")));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
2021-05-18 18:55:01 +03:00
|
|
|
let actual_src = print(&actual);
|
|
|
|
if let Ok(..) = env::var("UPDATE") {
|
|
|
|
NormalizedOutput::from(actual_src.clone())
|
|
|
|
.compare_to_file(dir.join("output.mjs"))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2019-12-28 05:25:54 +03:00
|
|
|
// It's normal transform test.
|
|
|
|
let expected = {
|
|
|
|
let fm = cm
|
|
|
|
.load_file(&dir.join("output.mjs"))
|
|
|
|
.expect("failed to load output file");
|
|
|
|
|
|
|
|
let mut p = Parser::new(
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Es(Default::default()),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-12-28 05:25:54 +03:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut m = p
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|e| e.into_diagnostic(&handler).emit())?;
|
|
|
|
|
|
|
|
for e in p.take_errors() {
|
|
|
|
e.into_diagnostic(&handler).emit()
|
|
|
|
}
|
2019-12-28 05:25:54 +03:00
|
|
|
|
|
|
|
m.body.sort_by(|a, b| match *a {
|
|
|
|
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
|
|
|
|
ref specifiers,
|
|
|
|
ref src,
|
|
|
|
..
|
|
|
|
})) if specifiers.is_empty() && src.value.starts_with("core-js/modules") => {
|
|
|
|
match *b {
|
|
|
|
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
|
|
|
|
specifiers: ref rs,
|
|
|
|
src: ref rsrc,
|
|
|
|
..
|
|
|
|
})) if rs.is_empty() && rsrc.value.starts_with("core-js/modules") => {
|
|
|
|
src.value.cmp(&rsrc.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => Ordering::Equal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Ordering::Equal,
|
|
|
|
});
|
|
|
|
|
|
|
|
m
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_src = print(&expected);
|
|
|
|
|
2020-12-21 22:27:18 +03:00
|
|
|
if drop_span(actual.fold_with(&mut as_folder(Normalizer)))
|
|
|
|
== drop_span(expected.fold_with(&mut as_folder(Normalizer)))
|
|
|
|
{
|
2020-07-28 15:56:19 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-12-28 05:25:54 +03:00
|
|
|
if actual_src != expected_src {
|
|
|
|
panic!(
|
|
|
|
r#"assertion failed: `(left == right)`
|
|
|
|
{}"#,
|
|
|
|
::testing::diff(&actual_src, &expected_src),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("failed to execute");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(p: &Path) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut f = File::open(p).expect("failed to open file");
|
|
|
|
f.read_to_string(&mut buf).expect("failed to read file");
|
|
|
|
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2021-08-14 09:34:04 +03:00
|
|
|
#[testing::fixture("tests/fixtures/**/input.mjs")]
|
|
|
|
fn fixture(input: PathBuf) {
|
|
|
|
let entry_dir = input.parent().unwrap().to_path_buf();
|
|
|
|
println!("File: {}", entry_dir.display());
|
|
|
|
|
|
|
|
let cfg: BabelOptions =
|
|
|
|
serde_json::from_reader(File::open(entry_dir.join("options.json")).unwrap())
|
|
|
|
.map_err(|err| Error::Msg(format!("failed to parse options.json: {}", err)))
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(cfg.presets.len(), 1);
|
|
|
|
let cfg = cfg.presets.into_iter().map(|v| v.1).next().unwrap();
|
2019-12-28 05:25:54 +03:00
|
|
|
|
2021-08-14 09:34:04 +03:00
|
|
|
exec(cfg, entry_dir).expect("failed to run test")
|
2019-12-28 05:25:54 +03:00
|
|
|
}
|
2020-12-21 22:27:18 +03:00
|
|
|
|
|
|
|
struct Normalizer;
|
|
|
|
|
2022-03-22 10:54:08 +03:00
|
|
|
impl VisitMut for Normalizer {}
|