Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(es/minfiier): Improve comment testing #9164

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
586 changes: 473 additions & 113 deletions crates/swc_ecma_minifier/tests/benches-full/d3.js

Large diffs are not rendered by default.

11,821 changes: 9,671 additions & 2,150 deletions crates/swc_ecma_minifier/tests/benches-full/echarts.js

Large diffs are not rendered by default.

2,090 changes: 1,764 additions & 326 deletions crates/swc_ecma_minifier/tests/benches-full/jquery.js

Large diffs are not rendered by default.

8,864 changes: 8,333 additions & 531 deletions crates/swc_ecma_minifier/tests/benches-full/lodash.js

Large diffs are not rendered by default.

505 changes: 401 additions & 104 deletions crates/swc_ecma_minifier/tests/benches-full/moment.js

Large diffs are not rendered by default.

466 changes: 393 additions & 73 deletions crates/swc_ecma_minifier/tests/benches-full/react.js

Large diffs are not rendered by default.

1,383 changes: 1,235 additions & 148 deletions crates/swc_ecma_minifier/tests/benches-full/terser.js

Large diffs are not rendered by default.

2,787 changes: 2,124 additions & 663 deletions crates/swc_ecma_minifier/tests/benches-full/three.js

Large diffs are not rendered by default.

10,826 changes: 7,472 additions & 3,354 deletions crates/swc_ecma_minifier/tests/benches-full/victory.js

Large diffs are not rendered by default.

1,498 changes: 1,168 additions & 330 deletions crates/swc_ecma_minifier/tests/benches-full/vue.js

Large diffs are not rendered by default.

100 changes: 81 additions & 19 deletions crates/swc_ecma_minifier/tests/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use anyhow::Error;
use once_cell::sync::Lazy;
use serde::Deserialize;
use swc_common::{
comments::SingleThreadedComments,
comments::{Comments, SingleThreadedComments},
errors::{Handler, HANDLER},
sync::Lrc,
util::take::Take,
Expand Down Expand Up @@ -144,6 +144,7 @@ fn run(
handler: &Handler,
input: &Path,
config: &str,
comments: Option<&dyn Comments>,
mangle: Option<TestMangleOptions>,
skip_hygiene: bool,
) -> Option<Program> {
Expand All @@ -153,7 +154,6 @@ fn run(
let (_module, mut config) = parse_compressor_config(cm.clone(), config);

let fm = cm.load_file(input).expect("failed to load input.js");
let comments = SingleThreadedComments::default();

eprintln!("---- {} -----\n{}", Color::Green.paint("Input"), fm.src);

Expand Down Expand Up @@ -315,6 +315,8 @@ fn custom_fixture(input: PathBuf) {
eprintln!("---- {} -----\n{}", Color::Green.paint("Config"), config);

testing::run_test2(false, |cm, handler| {
let comments = SingleThreadedComments::default();

let mangle = dir.join("mangle.json");
let mangle = read_to_string(mangle).ok();
if let Some(mangle) = &mangle {
Expand All @@ -327,13 +329,21 @@ fn custom_fixture(input: PathBuf) {
let mangle: Option<TestMangleOptions> =
mangle.map(|s| serde_json::from_str(&s).expect("failed to deserialize mangle.json"));

let output = run(cm.clone(), &handler, &input, &config, mangle, false);
let output = run(
cm.clone(),
&handler,
&input,
&config,
Some(&comments),
mangle,
false,
);
let output_module = match output {
Some(v) => v,
None => return Ok(()),
};

let output = print(cm, &[output_module], false, false);
let output = print(cm, &[output_module], Some(&comments), false, false);

eprintln!("---- {} -----\n{}", Color::Green.paint("Output"), output);

Expand All @@ -356,13 +366,23 @@ fn projects(input: PathBuf) {
eprintln!("---- {} -----\n{}", Color::Green.paint("Config"), config);

testing::run_test2(false, |cm, handler| {
let output = run(cm.clone(), &handler, &input, &config, None, false);
let comments = SingleThreadedComments::default();

let output = run(
cm.clone(),
&handler,
&input,
&config,
Some(&comments),
None,
false,
);
let output_module = match output {
Some(v) => v,
None => return Ok(()),
};

let output = print(cm.clone(), &[output_module], false, false);
let output = print(cm.clone(), &[output_module], Some(&comments), false, false);

eprintln!("---- {} -----\n{}", Color::Green.paint("Output"), output);

Expand All @@ -374,6 +394,7 @@ fn projects(input: PathBuf) {
&handler,
&input,
r#"{ "defaults": true, "toplevel": true, "passes": 3 }"#,
Some(&comments),
Some(TestMangleOptions::Normal(MangleOptions {
top_level: Some(true),
..Default::default()
Expand All @@ -385,7 +406,7 @@ fn projects(input: PathBuf) {
None => return Ok(()),
};

print(cm, &[output_module], true, true)
print(cm, &[output_module], Some(&comments), true, true)
};

eprintln!(
Expand Down Expand Up @@ -422,11 +443,14 @@ fn projects_bench(input: PathBuf) {
.join("benches-full");

testing::run_test2(false, |cm, handler| {
let comments = SingleThreadedComments::default();

let output = run(
cm.clone(),
&handler,
&input,
r#"{ "defaults": true, "toplevel": false, "passes": 3 }"#,
Some(&comments),
None,
false,
);
Expand All @@ -435,7 +459,7 @@ fn projects_bench(input: PathBuf) {
None => return Ok(()),
};

let output = print(cm, &[output_module], false, false);
let output = print(cm, &[output_module], Some(&comments), false, false);

eprintln!("---- {} -----\n{}", Color::Green.paint("Output"), output);

Expand Down Expand Up @@ -473,15 +497,31 @@ fn fixture(input: PathBuf) {
);
}

let comments = SingleThreadedComments::default();

let mangle: Option<TestMangleOptions> = mangle.map(|s| TestMangleOptions::parse(&s));

let output = run(cm.clone(), &handler, &input, &config, mangle, false);
let output = run(
cm.clone(),
&handler,
&input,
&config,
Some(&comments),
mangle,
false,
);
let output_program = match output {
Some(v) => v,
None => return Ok(()),
};

let output = print(cm.clone(), &[output_program.clone()], false, false);
let output = print(
cm.clone(),
&[output_program.clone()],
Some(&comments),
false,
false,
);

eprintln!("---- {} -----\n{}", Color::Green.paint("Output"), output);

Expand Down Expand Up @@ -521,13 +561,19 @@ fn fixture(input: PathBuf) {
return Ok(());
}

if print(cm.clone(), &[actual], false, false)
== print(cm.clone(), &[normalized_expected], false, false)
if print(cm.clone(), &[actual], Some(&comments), false, false)
== print(
cm.clone(),
&[normalized_expected],
Some(&comments),
false,
false,
)
{
return Ok(());
}

print(cm.clone(), &[expected], false, false)
print(cm.clone(), &[expected], Some(&comments), false, false)
};
{
// Check output.teraer.js
Expand Down Expand Up @@ -570,13 +616,19 @@ fn fixture(input: PathBuf) {
return Some(());
}

if print(cm.clone(), &[actual], false, false)
== print(cm.clone(), &[normalized_expected], false, false)
if print(cm.clone(), &[actual], Some(&comments), false, false)
== print(
cm.clone(),
&[normalized_expected],
Some(&comments),
false,
false,
)
{
return Some(());
}

print(cm.clone(), &[expected], false, false)
print(cm.clone(), &[expected], Some(&comments), false, false)
};

if output == expected {
Expand Down Expand Up @@ -623,7 +675,13 @@ fn fixture(input: PathBuf) {
}
}

let output_str = print(cm, &[drop_span(output_program)], false, false);
let output_str = print(
cm,
&[drop_span(output_program)],
Some(&comments),
false,
false,
);

if env::var("UPDATE").map(|s| s == "1").unwrap_or(false) {
let _ = catch_unwind(|| {
Expand All @@ -643,6 +701,7 @@ fn fixture(input: PathBuf) {
fn print<N: swc_ecma_codegen::Node>(
cm: Lrc<SourceMap>,
nodes: &[N],
comments: Option<&dyn Comments>,
minify: bool,
skip_semi: bool,
) -> String {
Expand All @@ -657,7 +716,7 @@ fn print<N: swc_ecma_codegen::Node>(
let mut emitter = Emitter {
cfg: swc_ecma_codegen::Config::default().with_minify(minify),
cm,
comments: None,
comments,
wr,
};

Expand All @@ -675,12 +734,15 @@ fn full(input: PathBuf) {
let config = find_config(dir);
eprintln!("---- {} -----\n{}", Color::Green.paint("Config"), config);

let comments = SingleThreadedComments::default();

testing::run_test2(false, |cm, handler| {
let output = run(
cm.clone(),
&handler,
&input,
&config,
Some(&comments),
Some(TestMangleOptions::Normal(MangleOptions {
top_level: Some(true),
..Default::default()
Expand All @@ -692,7 +754,7 @@ fn full(input: PathBuf) {
None => return Ok(()),
};

let output = print(cm, &[output_module], true, true);
let output = print(cm, &[output_module], Some(&comments), true, true);

eprintln!("---- {} -----\n{}", Color::Green.paint("Output"), output);

Expand Down
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/check/1/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ let foobar = "foo";
export const foo = foobar;
let foobarCopy = foobar += "bar";
foobar += "foo", console.log(foobarCopy);
// export function external1() {
// return internal() + foobar;
// }
// export function external2() {
// foobar += ".";
// }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var ClassB, obj, value;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) throw TypeError("Cannot call a class as a function");
}
module.exports = (obj = ClassB = function() {
module.exports = (obj = ClassB = /*#__PURE__*/ function() {
"use strict";
var protoProps;
function ClassB() {
Expand Down
Loading
Loading