2019-11-30 05:12:34 +03:00
|
|
|
use std::path::Path;
|
2020-07-31 12:49:07 +03:00
|
|
|
use swc_common::input::StringInput;
|
|
|
|
use swc_ecma_ast::Module;
|
|
|
|
use swc_ecma_parser::{lexer::Lexer, PResult, Parser, Syntax};
|
2019-11-30 05:12:34 +03:00
|
|
|
use testing::NormalizedOutput;
|
|
|
|
|
|
|
|
fn with_parser<F, Ret>(file_name: &str, f: F) -> Result<Ret, NormalizedOutput>
|
|
|
|
where
|
2020-07-31 12:49:07 +03:00
|
|
|
F: FnOnce(&mut Parser<Lexer<StringInput>>) -> PResult<Ret>,
|
2019-11-30 05:12:34 +03:00
|
|
|
{
|
|
|
|
let output = ::testing::run_test(false, |cm, handler| {
|
2019-12-13 09:21:25 +03:00
|
|
|
let fm = cm
|
|
|
|
.load_file(Path::new(file_name))
|
|
|
|
.unwrap_or_else(|e| panic!("failed to load {}: {}", file_name, e));
|
2019-11-30 05:12:34 +03:00
|
|
|
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2019-12-13 09:21:25 +03:00
|
|
|
if file_name.ends_with(".ts") {
|
|
|
|
Syntax::Typescript(Default::default())
|
|
|
|
} else {
|
|
|
|
Syntax::default()
|
|
|
|
},
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2019-12-13 09:21:25 +03:00
|
|
|
(&*fm).into(),
|
|
|
|
None,
|
2019-12-14 12:51:08 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut p = Parser::new_from(lexer);
|
|
|
|
let res = f(&mut p).map_err(|e| e.into_diagnostic(&handler).emit());
|
|
|
|
|
|
|
|
for e in p.take_errors() {
|
|
|
|
e.into_diagnostic(&handler).emit()
|
|
|
|
}
|
2019-11-30 05:12:34 +03:00
|
|
|
|
2019-12-13 09:21:25 +03:00
|
|
|
if handler.has_errors() {
|
|
|
|
return Err(());
|
|
|
|
}
|
2019-11-30 05:12:34 +03:00
|
|
|
|
2019-12-13 09:21:25 +03:00
|
|
|
res
|
2019-11-30 05:12:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn color_js() {
|
|
|
|
with_parser("tests/serde/colors.js", |p| {
|
|
|
|
let m = p.parse_module()?;
|
|
|
|
|
|
|
|
let s = serde_json::to_string(&m).expect("failed to serialize");
|
|
|
|
let _: Module = serde_json::from_str(&s).expect("failed to deserialize");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn color_ts() {
|
|
|
|
with_parser("tests/serde/colors.ts", |p| {
|
|
|
|
let m = p.parse_module()?;
|
|
|
|
|
|
|
|
let s = serde_json::to_string(&m).expect("failed to serialize");
|
|
|
|
let _: Module = serde_json::from_str(&s).expect("failed to deserialize");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn super_js() {
|
|
|
|
with_parser("tests/serde/supers.js", |p| {
|
|
|
|
let m = p.parse_module()?;
|
|
|
|
|
|
|
|
let s = serde_json::to_string(&m).expect("failed to serialize");
|
|
|
|
let _: Module = serde_json::from_str(&s).expect("failed to deserialize");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.expect("failed");
|
|
|
|
}
|