Skip to content

Commit

Permalink
refactor(es): Accept custom instance of Comments (#6290)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Oct 29, 2022
1 parent 41e0698 commit 1024a55
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions crates/binding_macros/src/wasm.rs
Expand Up @@ -327,6 +327,7 @@ macro_rules! build_transform_sync {
None,
handler,
&opts,
Default::default(),
$before_pass,
$after_pass,
), "failed to process js file"
Expand Down
25 changes: 17 additions & 8 deletions crates/swc/src/lib.rs
Expand Up @@ -851,15 +851,15 @@ impl Compiler {
program: Option<Program>,
handler: &Handler,
opts: &Options,
custom_before_pass: impl FnOnce(&Program, &SingleThreadedComments) -> P1,
custom_after_pass: impl FnOnce(&Program, &SingleThreadedComments) -> P2,
comments: SingleThreadedComments,
custom_before_pass: impl FnOnce(&Program) -> P1,
custom_after_pass: impl FnOnce(&Program) -> P2,
) -> Result<TransformOutput, Error>
where
P1: swc_ecma_visit::Fold,
P2: swc_ecma_visit::Fold,
{
self.run(|| -> Result<_, Error> {
let comments = SingleThreadedComments::default();
let config = self.run(|| {
self.parse_js_as_input(
fm.clone(),
Expand All @@ -868,7 +868,7 @@ impl Compiler {
opts,
&fm.name,
Some(&comments),
|program| custom_before_pass(program, &comments),
|program| custom_before_pass(program),
)
})?;
let config = match config {
Expand All @@ -878,7 +878,7 @@ impl Compiler {
}
};

let pass = chain!(config.pass, custom_after_pass(&config.program, &comments));
let pass = chain!(config.pass, custom_after_pass(&config.program));

let config = BuiltInput {
program: config.program,
Expand Down Expand Up @@ -917,7 +917,15 @@ impl Compiler {
handler: &Handler,
opts: &Options,
) -> Result<TransformOutput, Error> {
self.process_js_with_custom_pass(fm, None, handler, opts, |_, _| noop(), |_, _| noop())
self.process_js_with_custom_pass(
fm,
None,
handler,
opts,
SingleThreadedComments::default(),
|_| noop(),
|_| noop(),
)
}

#[tracing::instrument(level = "info", skip_all)]
Expand Down Expand Up @@ -1091,8 +1099,9 @@ impl Compiler {
Some(program),
handler,
opts,
|_, _| noop(),
|_, _| noop(),
SingleThreadedComments::default(),
|_| noop(),
|_| noop(),
)
}

Expand Down
43 changes: 33 additions & 10 deletions crates/swc/tests/rust_api.rs
Expand Up @@ -50,8 +50,9 @@ fn test_visit_mut() {

..Default::default()
},
|_, _| as_folder(PanicOnVisit),
|_, _| noop(),
SingleThreadedComments::default(),
|_| as_folder(PanicOnVisit),
|_| noop(),
);

assert_ne!(res.unwrap().code, "console.log(5 as const)");
Expand Down Expand Up @@ -101,11 +102,12 @@ fn shopify_1_check_filename() {
},
..Default::default()
},
|_, _: &SingleThreadedComments| {
SingleThreadedComments::default(),
|_| {
// Ensure comment API
noop()
},
|_, _: &SingleThreadedComments| noop(),
|_| noop(),
);

if res.is_err() {
Expand Down Expand Up @@ -181,8 +183,15 @@ fn shopify_2_same_opt() {
.into(),
);

let res =
c.process_js_with_custom_pass(fm, None, &handler, &opts, |_, _| noop(), |_, _| noop());
let res = c.process_js_with_custom_pass(
fm,
None,
&handler,
&opts,
SingleThreadedComments::default(),
|_| noop(),
|_| noop(),
);

if res.is_err() {
return Err(());
Expand Down Expand Up @@ -243,8 +252,15 @@ fn shopify_3_reduce_defaults() {
.into(),
);

let res =
c.process_js_with_custom_pass(fm, None, &handler, &opts, |_, _| noop(), |_, _| noop());
let res = c.process_js_with_custom_pass(
fm,
None,
&handler,
&opts,
SingleThreadedComments::default(),
|_| noop(),
|_| noop(),
);

if res.is_err() {
return Err(());
Expand Down Expand Up @@ -300,8 +316,15 @@ fn shopify_4_reduce_more() {
.into(),
);

let res =
c.process_js_with_custom_pass(fm, None, &handler, &opts, |_, _| noop(), |_, _| noop());
let res = c.process_js_with_custom_pass(
fm,
None,
&handler,
&opts,
SingleThreadedComments::default(),
|_| noop(),
|_| noop(),
);

if res.is_err() {
return Err(());
Expand Down

1 comment on commit 1024a55

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 1024a55 Previous: 4eab2ed Ratio
es/full/bugs-1 356067 ns/iter (± 43971) 357027 ns/iter (± 36382) 1.00
es/full/minify/libraries/antd 1859111974 ns/iter (± 14784413) 1822176677 ns/iter (± 12780498) 1.02
es/full/minify/libraries/d3 463355625 ns/iter (± 32126132) 407193008 ns/iter (± 6441771) 1.14
es/full/minify/libraries/echarts 1635317767 ns/iter (± 37714106) 1536015044 ns/iter (± 17592898) 1.06
es/full/minify/libraries/jquery 111886114 ns/iter (± 2504388) 101900818 ns/iter (± 1670739) 1.10
es/full/minify/libraries/lodash 151225285 ns/iter (± 8765299) 119727178 ns/iter (± 4067255) 1.26
es/full/minify/libraries/moment 72264617 ns/iter (± 5370681) 57229767 ns/iter (± 847143) 1.26
es/full/minify/libraries/react 24802712 ns/iter (± 1502754) 19451227 ns/iter (± 360642) 1.28
es/full/minify/libraries/terser 362972484 ns/iter (± 13027433) 295598359 ns/iter (± 8343699) 1.23
es/full/minify/libraries/three 646026264 ns/iter (± 35595182) 546846146 ns/iter (± 16036182) 1.18
es/full/minify/libraries/typescript 3795528050 ns/iter (± 89836935) 3334542431 ns/iter (± 27866776) 1.14
es/full/minify/libraries/victory 896609770 ns/iter (± 55945265) 798174381 ns/iter (± 3908514) 1.12
es/full/minify/libraries/vue 182554178 ns/iter (± 13773073) 148193859 ns/iter (± 2519141) 1.23
es/full/codegen/es3 35545 ns/iter (± 2323) 33821 ns/iter (± 479) 1.05
es/full/codegen/es5 34983 ns/iter (± 2212) 33938 ns/iter (± 508) 1.03
es/full/codegen/es2015 34629 ns/iter (± 1635) 33910 ns/iter (± 611) 1.02
es/full/codegen/es2016 34901 ns/iter (± 2660) 33821 ns/iter (± 684) 1.03
es/full/codegen/es2017 35094 ns/iter (± 1802) 33898 ns/iter (± 713) 1.04
es/full/codegen/es2018 34604 ns/iter (± 1833) 33884 ns/iter (± 918) 1.02
es/full/codegen/es2019 34468 ns/iter (± 1173) 33846 ns/iter (± 476) 1.02
es/full/codegen/es2020 34658 ns/iter (± 1279) 33958 ns/iter (± 569) 1.02
es/full/all/es3 205519690 ns/iter (± 11365300) 187297186 ns/iter (± 5117678) 1.10
es/full/all/es5 196142869 ns/iter (± 7785188) 177931751 ns/iter (± 3109096) 1.10
es/full/all/es2015 155767285 ns/iter (± 6217327) 142598673 ns/iter (± 2632108) 1.09
es/full/all/es2016 154437144 ns/iter (± 5923843) 141160728 ns/iter (± 3413724) 1.09
es/full/all/es2017 155672702 ns/iter (± 6139698) 141045852 ns/iter (± 3087148) 1.10
es/full/all/es2018 154282845 ns/iter (± 6468276) 139349694 ns/iter (± 2482481) 1.11
es/full/all/es2019 145273363 ns/iter (± 7142148) 139030456 ns/iter (± 3091370) 1.04
es/full/all/es2020 139509565 ns/iter (± 4405715) 134050799 ns/iter (± 2880427) 1.04
es/full/parser 729222 ns/iter (± 26256) 710978 ns/iter (± 18759) 1.03
es/full/base/fixer 26383 ns/iter (± 595) 26585 ns/iter (± 1026) 0.99
es/full/base/resolver_and_hygiene 92248 ns/iter (± 3414) 91937 ns/iter (± 4978) 1.00
serialization of ast node 216 ns/iter (± 4) 207 ns/iter (± 3) 1.04
serialization of serde 216 ns/iter (± 4) 208 ns/iter (± 4) 1.04

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.