refactor(es): Accept custom instance of Comments (#6290)

This commit is contained in:
Fy 2022-10-29 15:29:37 +08:00 committed by GitHub
parent 41e0698ed6
commit 1024a552cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 18 deletions

View File

@ -327,6 +327,7 @@ macro_rules! build_transform_sync {
None,
handler,
&opts,
Default::default(),
$before_pass,
$after_pass,
), "failed to process js file"

View File

@ -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(),
@ -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 {
@ -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,
@ -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)]
@ -1091,8 +1099,9 @@ impl Compiler {
Some(program),
handler,
opts,
|_, _| noop(),
|_, _| noop(),
SingleThreadedComments::default(),
|_| noop(),
|_| noop(),
)
}

View File

@ -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)");
@ -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() {
@ -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(());
@ -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(());
@ -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(());