2021-08-03 18:52:47 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-06-13 06:24:30 +03:00
|
|
|
use swc_common::input::SourceFileInput;
|
|
|
|
use swc_ecma_ast::EsVersion;
|
2021-08-03 18:52:47 +03:00
|
|
|
use swc_ecma_codegen::{
|
|
|
|
text_writer::{JsWriter, WriteJs},
|
|
|
|
Emitter,
|
|
|
|
};
|
2021-06-13 06:24:30 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
|
|
|
use testing::{run_test2, NormalizedOutput};
|
|
|
|
|
2021-08-03 18:52:47 +03:00
|
|
|
fn run(input: &Path, minify: bool) {
|
2021-06-13 06:24:30 +03:00
|
|
|
let dir = input.parent().unwrap();
|
2021-08-03 18:52:47 +03:00
|
|
|
let output = if minify {
|
|
|
|
dir.join(format!(
|
|
|
|
"output.min.{}",
|
|
|
|
input.extension().unwrap().to_string_lossy()
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
dir.join(format!(
|
|
|
|
"output.{}",
|
|
|
|
input.extension().unwrap().to_string_lossy()
|
|
|
|
))
|
|
|
|
};
|
2021-06-13 06:24:30 +03:00
|
|
|
|
|
|
|
run_test2(false, |cm, _| {
|
2022-01-13 17:06:11 +03:00
|
|
|
let fm = cm.load_file(input).unwrap();
|
2021-06-13 06:24:30 +03:00
|
|
|
|
|
|
|
let lexer = Lexer::new(
|
|
|
|
Syntax::Typescript(Default::default()),
|
|
|
|
EsVersion::latest(),
|
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let m = parser
|
|
|
|
.parse_module()
|
|
|
|
.expect("failed to parse input as a module");
|
|
|
|
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
2021-08-03 18:52:47 +03:00
|
|
|
let mut wr =
|
|
|
|
Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None)) as Box<dyn WriteJs>;
|
|
|
|
|
|
|
|
if minify {
|
|
|
|
wr = Box::new(swc_ecma_codegen::text_writer::omit_trailing_semi(wr));
|
|
|
|
}
|
|
|
|
|
2021-06-13 06:24:30 +03:00
|
|
|
let mut emitter = Emitter {
|
2021-08-03 18:52:47 +03:00
|
|
|
cfg: swc_ecma_codegen::Config { minify },
|
2022-01-13 17:06:11 +03:00
|
|
|
cm,
|
2021-06-13 06:24:30 +03:00
|
|
|
comments: None,
|
2021-08-03 18:52:47 +03:00
|
|
|
wr,
|
2021-06-13 06:24:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(&m).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
NormalizedOutput::from(String::from_utf8(buf).unwrap())
|
|
|
|
.compare_to_file(&output)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
2021-08-03 18:52:47 +03:00
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/**/input.ts")]
|
|
|
|
fn ts(input: PathBuf) {
|
|
|
|
run(&input, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/**/input.js")]
|
|
|
|
fn js(input: PathBuf) {
|
|
|
|
run(&input, false);
|
|
|
|
run(&input, true);
|
|
|
|
}
|