2021-11-26 09:04:41 +03:00
|
|
|
use std::{fs, path::PathBuf};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-12-15 12:33:35 +03:00
|
|
|
use swc_common::{chain, Mark, Span};
|
2021-11-30 04:04:08 +03:00
|
|
|
use swc_ecma_ast::*;
|
|
|
|
use swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax, TsConfig};
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::resolver;
|
2021-11-26 09:04:41 +03:00
|
|
|
use swc_ecma_transforms_testing::test_fixture;
|
2021-12-03 12:54:28 +03:00
|
|
|
use swc_ecma_visit::{as_folder, VisitMut, VisitMutWith};
|
2021-11-26 09:04:41 +03:00
|
|
|
use swc_webpack_ast::reducer::ast_reducer;
|
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/**/input.js")]
|
|
|
|
fn fixture(input: PathBuf) {
|
|
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
|
|
|
|
test_fixture(
|
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
&|_| {
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2021-11-26 09:04:41 +03:00
|
|
|
|
|
|
|
chain!(
|
2022-04-26 10:38:50 +03:00
|
|
|
resolver(unresolved_mark, top_level_mark, false),
|
|
|
|
as_folder(ast_reducer(unresolved_mark)),
|
2021-12-03 12:54:28 +03:00
|
|
|
as_folder(AssertValid)
|
2021-11-26 09:04:41 +03:00
|
|
|
)
|
|
|
|
},
|
|
|
|
&input,
|
|
|
|
&output,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-30 04:04:08 +03:00
|
|
|
struct AssertValid;
|
|
|
|
|
2021-12-03 12:54:28 +03:00
|
|
|
impl VisitMut for AssertValid {
|
2021-12-15 12:33:35 +03:00
|
|
|
fn visit_mut_array_pat(&mut self, a: &mut ArrayPat) {
|
|
|
|
if a.optional {
|
|
|
|
panic!("found an optional pattern: {:?}", a)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.visit_mut_children_with(self);
|
|
|
|
}
|
|
|
|
|
2021-12-03 12:54:28 +03:00
|
|
|
fn visit_mut_empty_stmt(&mut self, i: &mut EmptyStmt) {
|
2021-11-30 12:16:05 +03:00
|
|
|
panic!("found empty: {:?}", i)
|
|
|
|
}
|
2021-12-03 12:54:28 +03:00
|
|
|
|
|
|
|
fn visit_mut_ident(&mut self, i: &mut Ident) {
|
2021-12-15 12:33:35 +03:00
|
|
|
if i.optional {
|
|
|
|
panic!("found an optional pattern: {:?}", i)
|
2021-12-03 12:54:28 +03:00
|
|
|
}
|
2021-12-15 12:33:35 +03:00
|
|
|
|
|
|
|
i.visit_mut_children_with(self);
|
2021-12-03 12:54:28 +03:00
|
|
|
}
|
2021-12-04 13:28:18 +03:00
|
|
|
|
2021-12-14 11:28:51 +03:00
|
|
|
fn visit_mut_if_stmt(&mut self, s: &mut IfStmt) {
|
|
|
|
s.test.visit_mut_with(self);
|
|
|
|
if !s.cons.is_empty() {
|
|
|
|
s.cons.visit_mut_with(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
s.alt.visit_mut_with(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mut_invalid(&mut self, i: &mut Invalid) {
|
|
|
|
panic!("found invalid: {:?}", i)
|
|
|
|
}
|
|
|
|
|
2021-12-04 13:28:18 +03:00
|
|
|
fn visit_mut_module(&mut self, m: &mut Module) {
|
|
|
|
dbg!(&*m);
|
|
|
|
|
2021-12-15 12:33:35 +03:00
|
|
|
m.body.visit_mut_with(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mut_object_pat(&mut self, pat: &mut ObjectPat) {
|
|
|
|
if pat.optional {
|
|
|
|
panic!("found a optional pattern: {:?}", pat)
|
|
|
|
}
|
|
|
|
|
|
|
|
pat.visit_mut_children_with(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mut_span(&mut self, sp: &mut Span) {
|
|
|
|
assert!(!sp.is_dummy());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mut_ts_type(&mut self, ty: &mut TsType) {
|
|
|
|
panic!("found a typescript type: {:?}", ty)
|
2021-12-04 13:28:18 +03:00
|
|
|
}
|
2021-11-30 04:04:08 +03:00
|
|
|
}
|
|
|
|
|
2022-01-28 13:11:30 +03:00
|
|
|
#[testing::fixture("../swc_ecma_parser/tests/tsc/**/*.ts")]
|
|
|
|
#[testing::fixture("../swc_ecma_parser/tests/typescript/**/*.ts")]
|
2021-12-20 08:05:57 +03:00
|
|
|
#[testing::fixture("../swc/tests/tsc-references/**/*.js")]
|
2021-11-30 04:04:08 +03:00
|
|
|
fn assert_no_invalid(input: PathBuf) {
|
|
|
|
testing::run_test(false, |cm, _handler| {
|
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
|
|
|
|
let mut module = {
|
|
|
|
let mut p = Parser::new(
|
|
|
|
Syntax::Typescript(TsConfig {
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
StringInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let res = p.parse_module();
|
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-01-13 17:06:11 +03:00
|
|
|
if !p.take_errors().is_empty() {
|
2021-11-30 04:04:08 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
res.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut pass = {
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2021-11-30 04:04:08 +03:00
|
|
|
|
|
|
|
chain!(
|
2022-04-26 10:38:50 +03:00
|
|
|
resolver(unresolved_mark, top_level_mark, false),
|
2021-11-30 04:04:08 +03:00
|
|
|
as_folder(ast_reducer(top_level_mark))
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
module.visit_mut_with(&mut pass);
|
2021-12-03 12:54:28 +03:00
|
|
|
module.visit_mut_with(&mut AssertValid);
|
2021-11-30 04:04:08 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2021-11-26 09:04:41 +03:00
|
|
|
#[testing::fixture("tests/fixture/**/input.js")]
|
|
|
|
fn test_babelify(input: PathBuf) {
|
|
|
|
let output_path = input.parent().unwrap().join("output.json");
|
|
|
|
|
|
|
|
testing::run_test(false, |cm, handler| {
|
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
|
|
|
|
let module = {
|
|
|
|
let mut p = Parser::new(
|
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
StringInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let res = p
|
|
|
|
.parse_module()
|
2022-01-13 17:06:11 +03:00
|
|
|
.map_err(|e| e.into_diagnostic(handler).emit());
|
2021-11-26 09:04:41 +03:00
|
|
|
|
|
|
|
for e in p.take_errors() {
|
2022-01-13 17:06:11 +03:00
|
|
|
e.into_diagnostic(handler).emit()
|
2021-11-26 09:04:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
res?
|
|
|
|
};
|
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
let s = swc_webpack_ast::webpack_ast(cm, fm, module).unwrap();
|
2021-11-26 09:04:41 +03:00
|
|
|
println!("{} bytes", s.len());
|
|
|
|
|
|
|
|
fs::write(&output_path, s.as_bytes()).unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|