2020-02-17 09:56:41 +03:00
|
|
|
use rayon::prelude::*;
|
2019-11-26 04:08:48 +03:00
|
|
|
use std::path::Path;
|
2020-02-17 09:56:41 +03:00
|
|
|
use swc::{
|
2020-05-19 14:24:00 +03:00
|
|
|
config::{Config, Options, SourceMapsConfig},
|
2020-02-17 09:56:41 +03:00
|
|
|
Compiler,
|
|
|
|
};
|
2020-05-19 14:24:00 +03:00
|
|
|
use swc_ecmascript::preset_env;
|
2019-11-26 04:08:48 +03:00
|
|
|
use testing::{NormalizedOutput, StdErr, Tester};
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
fn file(f: &str) -> Result<NormalizedOutput, StdErr> {
|
2020-05-19 14:24:00 +03:00
|
|
|
file_with_opt(
|
|
|
|
f,
|
|
|
|
Options {
|
|
|
|
swcrc: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_with_opt(f: &str, options: Options) -> Result<NormalizedOutput, StdErr> {
|
2019-11-26 04:08:48 +03:00
|
|
|
Tester::new().print_errors(|cm, handler| {
|
|
|
|
let c = Compiler::new(cm.clone(), handler);
|
|
|
|
|
|
|
|
let fm = cm.load_file(Path::new(f)).expect("failed to load file");
|
|
|
|
let s = c.process_js_file(
|
|
|
|
fm,
|
2019-11-29 17:46:06 +03:00
|
|
|
&Options {
|
2019-12-11 09:07:21 +03:00
|
|
|
is_module: true,
|
2020-05-19 14:24:00 +03:00
|
|
|
..options
|
2019-11-26 04:08:48 +03:00
|
|
|
},
|
|
|
|
);
|
2019-12-28 05:25:54 +03:00
|
|
|
|
2019-11-26 04:08:48 +03:00
|
|
|
match s {
|
2019-12-28 05:25:54 +03:00
|
|
|
Ok(v) => {
|
|
|
|
if c.handler.has_errors() {
|
|
|
|
Err(())
|
|
|
|
} else {
|
|
|
|
Ok(v.code.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => panic!("Error: {}", err),
|
2019-11-26 04:08:48 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn project(dir: &str) {
|
|
|
|
Tester::new()
|
|
|
|
.print_errors(|cm, handler| {
|
|
|
|
let c = Compiler::new(cm.clone(), handler);
|
|
|
|
|
|
|
|
for entry in WalkDir::new(dir) {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if entry.metadata().unwrap().is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
println!("File: {}", entry.path().to_string_lossy());
|
|
|
|
|
|
|
|
if !entry.file_name().to_string_lossy().ends_with(".ts")
|
|
|
|
&& !entry.file_name().to_string_lossy().ends_with(".js")
|
|
|
|
&& !entry.file_name().to_string_lossy().ends_with(".tsx")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let fm = cm.load_file(entry.path()).expect("failed to load file");
|
2019-12-11 17:28:35 +03:00
|
|
|
match c.process_js_file(
|
2019-11-26 04:08:48 +03:00
|
|
|
fm,
|
2019-11-29 17:46:06 +03:00
|
|
|
&Options {
|
2019-11-26 04:08:48 +03:00
|
|
|
swcrc: true,
|
2019-12-11 09:07:21 +03:00
|
|
|
is_module: true,
|
|
|
|
|
2019-11-26 04:08:48 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
2019-12-11 17:28:35 +03:00
|
|
|
) {
|
|
|
|
Ok(..) => {}
|
2020-03-27 08:40:01 +03:00
|
|
|
Err(ref err) if format!("{:?}", err).contains("not matched") => {}
|
|
|
|
Err(err) => panic!("Error: {:?}", err),
|
2019-12-11 17:28:35 +03:00
|
|
|
}
|
2019-11-26 04:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.map(|_| ())
|
2019-12-11 17:28:35 +03:00
|
|
|
.expect("failed");
|
2019-11-26 04:08:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-17 09:56:41 +03:00
|
|
|
fn par_project(dir: &str) {
|
|
|
|
Tester::new()
|
|
|
|
.print_errors(|cm, handler| {
|
|
|
|
let c = Compiler::new(cm.clone(), handler);
|
|
|
|
|
|
|
|
let entries = WalkDir::new(dir)
|
|
|
|
.into_iter()
|
|
|
|
.map(|entry| entry.unwrap())
|
|
|
|
.filter(|e| {
|
|
|
|
if e.metadata().unwrap().is_dir() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !e.file_name().to_string_lossy().ends_with(".ts")
|
|
|
|
&& !e.file_name().to_string_lossy().ends_with(".js")
|
|
|
|
&& !e.file_name().to_string_lossy().ends_with(".tsx")
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
entries.into_par_iter().for_each(|entry| {
|
|
|
|
let fm = cm.load_file(entry.path()).expect(
|
|
|
|
"failed to load
|
|
|
|
file",
|
|
|
|
);
|
|
|
|
let _ = c.process_js_file(
|
|
|
|
fm,
|
|
|
|
&Options {
|
|
|
|
swcrc: true,
|
|
|
|
is_module: true,
|
|
|
|
source_maps: Some(SourceMapsConfig::Bool(true)),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if c.handler.has_errors() {
|
|
|
|
Err(())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|_| ())
|
|
|
|
.expect("");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn angular_core() {
|
|
|
|
par_project("integration-tests/angular/packages/core/src");
|
|
|
|
}
|
2019-12-11 17:28:35 +03:00
|
|
|
|
2019-11-29 17:46:06 +03:00
|
|
|
//#[test]
|
|
|
|
//fn rxjs() {
|
2020-02-17 09:56:41 +03:00
|
|
|
// par_project("integration-tests/rxjs/repo/src");
|
2019-11-29 17:46:06 +03:00
|
|
|
//}
|
|
|
|
//
|
|
|
|
//#[test]
|
|
|
|
//fn webpack() {
|
2020-02-17 09:56:41 +03:00
|
|
|
// par_project("integration-tests/webpack/repo/lib");
|
2019-11-29 17:46:06 +03:00
|
|
|
//}
|
2019-11-26 04:08:48 +03:00
|
|
|
|
|
|
|
/// should respect modules config in .swcrc
|
|
|
|
#[test]
|
|
|
|
fn issue_225() {
|
|
|
|
let s = file("tests/projects/issue-225/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(s.contains("function _interopRequireDefault"));
|
|
|
|
assert!(s.contains("var _foo = _interopRequireDefault(require('foo'))"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// should handle exportNamespaceFrom configured by .swcrc
|
|
|
|
#[test]
|
|
|
|
fn issue_226() {
|
|
|
|
let s = file("tests/projects/issue-226/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(s.contains("import * as _Foo from 'bar';"));
|
|
|
|
assert!(s.contains("export { _Foo as Foo }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// should handle react correctly
|
|
|
|
#[test]
|
|
|
|
fn issue_351() {
|
|
|
|
let s = file("tests/projects/issue-351/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(s.contains(".default.createElement('div', null);"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// should handle cjs imports
|
|
|
|
#[test]
|
|
|
|
fn issue_389() {
|
|
|
|
let s = file("tests/projects/issue-389/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(s.contains(".default.bar = true"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// should handle comments in arrow expression
|
|
|
|
#[test]
|
|
|
|
fn issue_406() {
|
|
|
|
let s = file("tests/projects/issue-406/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(s.contains("return true"));
|
|
|
|
}
|
|
|
|
|
2019-12-01 11:32:44 +03:00
|
|
|
#[test]
|
|
|
|
fn issue_409_1() {
|
|
|
|
let s = file("tests/projects/issue-409-1/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(s.contains("JSON.parse"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_409_2() {
|
|
|
|
let s = file("tests/projects/issue-409-2/input.js").unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
|
|
|
|
assert!(!s.contains("JSON.parse"));
|
|
|
|
}
|
|
|
|
|
2019-11-26 04:08:48 +03:00
|
|
|
/// should handle multiple entries in swcrc
|
|
|
|
#[test]
|
|
|
|
fn issue_414() {
|
|
|
|
let s1 = file("tests/projects/issue-414/a.js").unwrap();
|
|
|
|
println!("{}", s1);
|
|
|
|
assert!(s1.contains("require('foo')"));
|
|
|
|
|
|
|
|
let s2 = file("tests/projects/issue-414/b.ts").unwrap();
|
|
|
|
println!("{}", s2);
|
2020-03-02 14:49:08 +03:00
|
|
|
assert!(s2.contains("define("));
|
|
|
|
assert!(s2.contains("function(_bar) {"));
|
2019-11-26 04:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// should handle comments in return statement
|
|
|
|
#[test]
|
|
|
|
fn issue_415() {
|
|
|
|
let s = file("tests/projects/issue-415/input.js").unwrap();
|
|
|
|
|
|
|
|
assert!(s.replace(" ", "").contains("return(/*#__PURE__*/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_466_1() {
|
|
|
|
project("tests/projects/issue-466-1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_466_2() {
|
|
|
|
project("tests/projects/issue-466-2");
|
|
|
|
}
|
2019-12-09 15:02:51 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_467() {
|
|
|
|
project("tests/projects/issue-467/");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_468() {
|
|
|
|
file("tests/projects/issue-468/input.ts").expect("failed to parse typescript");
|
|
|
|
}
|
2019-12-24 11:01:32 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_528() {
|
|
|
|
let f = file("tests/projects/issue-528/input.js")
|
|
|
|
.unwrap()
|
|
|
|
.replace(" ", "");
|
|
|
|
let f = f.trim();
|
|
|
|
|
|
|
|
println!("{}", f);
|
|
|
|
assert_eq!(
|
|
|
|
f,
|
|
|
|
"\
|
|
|
|
//bar
|
|
|
|
[
|
|
|
|
//foo
|
|
|
|
a,
|
|
|
|
//baz
|
|
|
|
//bar
|
|
|
|
b
|
|
|
|
];"
|
|
|
|
);
|
|
|
|
}
|
2019-12-28 05:25:54 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_entry_chrome_49() {
|
|
|
|
let f = file("tests/env/entry/chrome-49/input.js")
|
|
|
|
.unwrap()
|
|
|
|
.replace(" ", "");
|
|
|
|
let f = f.trim();
|
|
|
|
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert_eq!(f.lines().count(), 76);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_entry_chrome_71() {
|
|
|
|
let f = file("tests/env/entry/chrome-71/input.js")
|
|
|
|
.unwrap()
|
|
|
|
.replace(" ", "");
|
|
|
|
let f = f.trim();
|
|
|
|
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert_eq!(f.lines().count(), 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_query_chrome_71() {
|
|
|
|
let f = file("tests/env/query/chrome-71/input.js")
|
|
|
|
.unwrap()
|
|
|
|
.replace(" ", "");
|
|
|
|
let f = f.trim();
|
|
|
|
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert_eq!(f.lines().count(), 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn project_env() {
|
|
|
|
project("tests/projects/env/");
|
|
|
|
}
|
2019-12-28 16:02:58 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_532() {
|
|
|
|
let f = file("tests/projects/issue-532-block/input.js")
|
|
|
|
.unwrap()
|
|
|
|
.replace(" ", "");
|
|
|
|
let f = f.trim();
|
|
|
|
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
f,
|
|
|
|
"\
|
|
|
|
/*pre:1*/
|
|
|
|
test();
|
|
|
|
test(123/*post:3*/
|
|
|
|
);
|
|
|
|
test(/*pre:4*/
|
|
|
|
123);
|
|
|
|
test(/*pre:5*/
|
|
|
|
123/*post:6*/
|
|
|
|
);
|
|
|
|
test(/*pre:7*/
|
|
|
|
123,/*pre:8*/
|
|
|
|
456);
|
|
|
|
test(123/*post:9*/
|
|
|
|
,456/*post:10*/
|
|
|
|
);"
|
|
|
|
)
|
|
|
|
}
|
2020-01-23 17:23:24 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_602() {
|
|
|
|
let f = file("tests/projects/issue-602/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(!f.contains("undefined1"));
|
|
|
|
}
|
2020-01-24 08:54:52 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_604_1() {
|
|
|
|
let f = file("tests/projects/issue-604-1/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("_loop(i)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_605() {
|
|
|
|
let f = file("tests/projects/issue-605/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("Object.keys(_c)"));
|
|
|
|
}
|
2020-02-07 08:33:26 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn await_expr() {
|
|
|
|
let f = file("tests/projects/await-expression/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("await"));
|
|
|
|
assert!(f.contains("test"));
|
|
|
|
}
|
2020-02-08 04:49:00 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn await_expr_2() {
|
|
|
|
let f = file("tests/projects/await-expression-2/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("await"));
|
|
|
|
assert!(f.contains("test"));
|
|
|
|
}
|
2020-02-12 18:07:45 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_config() {
|
|
|
|
project("tests/projects/issue-655")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_658() {
|
|
|
|
project("tests/projects/issue-658")
|
|
|
|
}
|
2020-05-09 06:22:45 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_763() {
|
|
|
|
let f = file("tests/projects/issue-763/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("_consts.RESOURCE_WEBSITE"));
|
|
|
|
assert!(f.contains("_consts.RESOURCE_FACEBOOK"));
|
|
|
|
assert!(f.contains("_consts.RESOURCE_INSTAGRAM"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_763_2() {
|
|
|
|
let f = file("tests/projects/issue-763-2/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("_consts.RESOURCE_WEBSITE"));
|
|
|
|
assert!(f.contains("_consts.RESOURCE_FACEBOOK"));
|
|
|
|
assert!(f.contains("_consts.RESOURCE_INSTAGRAM"));
|
|
|
|
}
|
2020-05-18 17:25:41 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_779_1() {
|
|
|
|
let f = file("tests/projects/issue-779-1/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("require('core-js/modules/es.array-buffer.constructor');"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_779_2() {
|
|
|
|
let f = file("tests/projects/issue-779-2/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(f.contains("require('core-js');"));
|
|
|
|
}
|
2020-05-19 14:24:00 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_783() {
|
|
|
|
let f = file("tests/projects/issue-783/input.js").unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(!f.contains("require('core-js');"));
|
|
|
|
assert!(f.contains("require('core-js/modules/es.array-buffer.constructor');"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_783_core_js_2() {
|
|
|
|
let f = file_with_opt(
|
|
|
|
"tests/projects/issue-783/input.js",
|
|
|
|
Options {
|
|
|
|
swcrc: false,
|
|
|
|
config: Some(Config {
|
|
|
|
env: Some(preset_env::Config {
|
|
|
|
core_js: Some("2".parse().unwrap()),
|
|
|
|
mode: Some(preset_env::Mode::Entry),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
!f.contains("'core-js'"),
|
|
|
|
"import of `core-js` should be transformed"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_783_core_js_3() {
|
|
|
|
let f = file_with_opt(
|
|
|
|
"tests/projects/issue-783/input.js",
|
|
|
|
Options {
|
|
|
|
swcrc: false,
|
|
|
|
config: Some(Config {
|
|
|
|
env: Some(preset_env::Config {
|
|
|
|
mode: Some(preset_env::Mode::Entry),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
println!("{}", f);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
!f.contains("'core-js'"),
|
|
|
|
"import of `core-js` should be transformed"
|
|
|
|
);
|
|
|
|
}
|