test(es/minifier): Enable script mode for terser_exec and mangle (#8474)

This commit is contained in:
Austaras 2024-01-02 14:00:26 +08:00 committed by GitHub
parent 0ba043bd5f
commit b676e75cdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 116 additions and 44 deletions

View File

@ -15,14 +15,14 @@ use swc_ecma_minifier::{
optimize,
option::{ExtraOptions, MangleOptions, ManglePropertiesOptions, MinifyOptions},
};
use swc_ecma_parser::parse_file_as_module;
use swc_ecma_parser::parse_file_as_program;
use swc_ecma_transforms_base::{fixer::paren_remover, resolver};
use swc_ecma_utils::drop_span;
use swc_ecma_visit::VisitMutWith;
use testing::{assert_eq, NormalizedOutput};
use tracing::warn;
fn print(cm: Lrc<SourceMap>, m: &Module, minify: bool) -> String {
fn print(cm: Lrc<SourceMap>, p: &Program, minify: bool) -> String {
let mut buf = vec![];
{
@ -39,19 +39,19 @@ fn print(cm: Lrc<SourceMap>, m: &Module, minify: bool) -> String {
wr,
};
emitter.emit_module(m).unwrap();
emitter.emit_program(p).unwrap();
}
String::from_utf8(buf).unwrap()
}
fn parse(handler: &Handler, cm: Lrc<SourceMap>, path: &Path) -> Result<Module, ()> {
fn parse(handler: &Handler, cm: Lrc<SourceMap>, path: &Path) -> Result<Program, ()> {
let fm = cm.load_file(path).unwrap();
parse_fm(handler, fm)
}
fn parse_fm(handler: &Handler, fm: Lrc<SourceFile>) -> Result<Module, ()> {
parse_file_as_module(
fn parse_fm(handler: &Handler, fm: Lrc<SourceFile>) -> Result<Program, ()> {
parse_file_as_program(
&fm,
Default::default(),
EsVersion::latest(),
@ -88,8 +88,8 @@ fn snapshot_compress_fixture(input: PathBuf) {
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
let m = optimize(
m.into(),
let p = optimize(
m,
cm.clone(),
None,
None,
@ -105,13 +105,12 @@ fn snapshot_compress_fixture(input: PathBuf) {
unresolved_mark,
top_level_mark,
},
)
.expect_module();
);
if output_path.exists() {
// Compare AST, and mark test as a success if ast is identical.
let mut actual = m.clone();
let mut actual = p.clone();
actual.visit_mut_with(&mut paren_remover(None));
actual = drop_span(actual);
@ -131,7 +130,7 @@ fn snapshot_compress_fixture(input: PathBuf) {
}
}
let mangled = print(cm, &m, false);
let mangled = print(cm, &p, false);
NormalizedOutput::from(mangled)
.compare_to_file(output_path)
@ -144,15 +143,15 @@ fn snapshot_compress_fixture(input: PathBuf) {
#[testing::fixture("tests/mangle/**/input.js")]
fn fixture(input: PathBuf) {
testing::run_test2(false, |cm, handler| {
let mut m = parse(&handler, cm.clone(), &input)?;
let mut p = parse(&handler, cm.clone(), &input)?;
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
p.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
let m = optimize(
m.into(),
let p = optimize(
p,
cm.clone(),
None,
None,
@ -168,10 +167,9 @@ fn fixture(input: PathBuf) {
unresolved_mark,
top_level_mark,
},
)
.expect_module();
);
let mangled = print(cm, &m, false);
let mangled = print(cm, &p, false);
NormalizedOutput::from(mangled)
.compare_to_file(input.parent().unwrap().join("output.js"))
@ -186,13 +184,13 @@ fn assert_mangled(src: &str, expected: &str, opts: MangleOptions) {
testing::run_test2(false, |cm, handler| {
let fm = cm.new_source_file(FileName::Anon, src.into());
let m = parse_fm(&handler, fm)?;
let p = parse_fm(&handler, fm)?;
let unresolved_mark = Mark::fresh(Mark::root());
let top_level_mark = Mark::fresh(Mark::root());
let m = optimize(
m.into(),
p,
cm.clone(),
None,
None,
@ -205,8 +203,7 @@ fn assert_mangled(src: &str, expected: &str, opts: MangleOptions) {
unresolved_mark,
top_level_mark,
},
)
.expect_module();
);
let mangled = print(cm, &m, false);

View File

@ -5,4 +5,5 @@ var await = function (x) {
console.log("await", x);
};
async(1);
await 2;
// prettier-ignore
await(2);

View File

@ -0,0 +1,8 @@
var o = function(o) {
console.log("async", o);
};
var a = function(o) {
console.log("await", o);
};
o(1);
await 2;

View File

@ -0,0 +1,8 @@
var o = function(o) {
console.log("async", o);
};
var a = function(o) {
console.log("await", o);
};
o(1);
await (2);

View File

@ -0,0 +1,32 @@
function a(a) {
console.log(a.async + " " + a.await);
}
var n = "Async", c = "Await";
a({
async: n
});
a({
await: c
});
a({
async: n,
await: c
});
a({
await: c,
async: n
});
a({
async: n
});
a({
await: c
});
a({
async: n,
await: c
});
a({
await: c,
async: n
});

View File

@ -1,7 +1,7 @@
switch(function() {
return xxx;
return n;
}){
case xxx:
case n:
for(; console.log("FAIL");){
function n() {}
}

View File

@ -1,7 +1,7 @@
switch(function() {
return xxx;
return n;
}){
case xxx:
case n:
if (console.log("FAIL")) {
function n() {}
}

View File

@ -1,11 +1,11 @@
while(!((a && bar) || o + "0")){
while(!((a && o) || e + "0")){
console.log("unreachable");
var a;
function r() {}
function o() {}
}
for(var o = 10, e; o && (e || o) && !typeof o; ++o){
for(var e = 10, r; e && (r || e) && !typeof e; ++e){
asdf();
a();
var f;
}
bar();
o();

View File

@ -1,9 +1,9 @@
console.log(typeof f, typeof e, 1);
if (console.log(typeof f, typeof e, 2)) console.log(typeof f, typeof e, 3);
console.log(typeof o, typeof e, 1);
if (console.log(typeof o, typeof e, 2)) console.log(typeof o, typeof e, 3);
else {
console.log(typeof o, typeof e, 4);
function o() {}
console.log(typeof o, typeof e, 5);
}
function e() {}
console.log(typeof f, typeof e, 6);
console.log(typeof o, typeof e, 6);

View File

@ -0,0 +1,19 @@
var e = 0, t = 1, l = 2, a = 3, s = 4, i = 5, n = 6, r = 7, u = 8;
console.log({
foo: e,
0: 0,
NaN,
async: t,
await: l,
false: false,
implements: a,
null: null,
package: s,
private: i,
protected: n,
static: r,
this: this,
true: true,
undefined,
yield: u
});

View File

@ -0,0 +1,5 @@
var n = function*() {
function n() {
yield = 1;
}
};

View File

@ -57,6 +57,9 @@ use testing::assert_eq;
// annex B
"blocks/issue_1672_if",
"blocks/issue_1672_for",
// parser error
"arrow/async_identifiers",
"async/async_identifiers"
)
)]
fn terser_exec(input: PathBuf) {
@ -168,7 +171,7 @@ fn parse_compressor_config(cm: Lrc<SourceMap>, s: &str) -> Result<(bool, Compres
Ok((c.module, c.into_config(cm)))
}
fn run(cm: Lrc<SourceMap>, handler: &Handler, input: &Path, config: &str) -> Option<Module> {
fn run(cm: Lrc<SourceMap>, handler: &Handler, input: &Path, config: &str) -> Option<Program> {
HANDLER.set(handler, || {
let (_module, config) = parse_compressor_config(cm.clone(), config).ok()?;
if config.ie8 {
@ -197,15 +200,15 @@ fn run(cm: Lrc<SourceMap>, handler: &Handler, input: &Path, config: &str) -> Opt
let mut parser = Parser::new_from(lexer);
let program = parser
.parse_module()
.parse_program()
.map_err(|err| {
err.into_diagnostic(handler).emit();
})
.map(|mut module| {
module.visit_mut_with(&mut paren_remover(Some(&comments)));
module.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
.map(|mut program| {
program.visit_mut_with(&mut paren_remover(Some(&comments)));
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
module
program
});
// Ignore parser errors.
@ -218,7 +221,7 @@ fn run(cm: Lrc<SourceMap>, handler: &Handler, input: &Path, config: &str) -> Opt
let optimization_start = Instant::now();
let mut output = optimize(
program.into(),
program,
cm,
Some(&comments),
None,
@ -231,8 +234,7 @@ fn run(cm: Lrc<SourceMap>, handler: &Handler, input: &Path, config: &str) -> Opt
unresolved_mark,
top_level_mark,
},
)
.expect_module();
);
let end = Instant::now();
tracing::info!(
"optimize({}) took {:?}",