fix(api/rust): Fix transform comment api (#3962)

This commit is contained in:
Donny/강동윤 2022-03-11 14:46:49 +09:00 committed by GitHub
parent 3014d7a876
commit fcbd3c5c58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 14 deletions

View File

@ -925,8 +925,8 @@ impl Compiler {
program: Option<Program>,
handler: &Handler,
opts: &Options,
custom_before_pass: impl FnOnce(&Program) -> P1,
custom_after_pass: impl FnOnce(&Program) -> P2,
custom_before_pass: impl FnOnce(&Program, &SingleThreadedComments) -> P1,
custom_after_pass: impl FnOnce(&Program, &SingleThreadedComments) -> P2,
) -> Result<TransformOutput, Error>
where
P1: swc_ecma_visit::Fold,
@ -942,7 +942,7 @@ impl Compiler {
opts,
&fm.name,
Some(&comments),
custom_before_pass,
|program| custom_before_pass(program, &comments),
)
})?;
let config = match config {
@ -952,7 +952,7 @@ impl Compiler {
}
};
let pass = chain!(config.pass, custom_after_pass(&config.program));
let pass = chain!(config.pass, custom_after_pass(&config.program, &comments));
let config = BuiltInput {
program: config.program,
@ -989,7 +989,7 @@ 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, |_, _| noop(), |_, _| noop())
}
#[tracing::instrument(level = "info", skip_all)]
@ -1127,7 +1127,14 @@ impl Compiler {
let loc = self.cm.lookup_char_pos(program.span().lo());
let fm = loc.file;
self.process_js_with_custom_pass(fm, Some(program), handler, opts, |_| noop(), |_| noop())
self.process_js_with_custom_pass(
fm,
Some(program),
handler,
opts,
|_, _| noop(),
|_, _| noop(),
)
}
#[tracing::instrument(level = "info", skip_all)]

View File

@ -4,7 +4,7 @@ use swc::{
},
Compiler,
};
use swc_common::FileName;
use swc_common::{comments::SingleThreadedComments, FileName};
use swc_ecma_ast::*;
use swc_ecma_parser::{EsConfig, Syntax, TsConfig};
use swc_ecma_transforms::{modules::common_js, pass::noop};
@ -50,8 +50,8 @@ fn test_visit_mut() {
..Default::default()
},
|_| as_folder(PanicOnVisit),
|_| noop(),
|_, _| as_folder(PanicOnVisit),
|_, _| noop(),
);
assert_ne!(res.unwrap().code, "console.log(5 as const)");
@ -101,8 +101,11 @@ fn shopify_1_check_filename() {
is_module: IsModule::Bool(true),
..Default::default()
},
|_| noop(),
|_| noop(),
|_, _: &SingleThreadedComments| {
// Ensure comment API
noop()
},
|_, _: &SingleThreadedComments| noop(),
);
if res.is_err() {
@ -181,7 +184,8 @@ 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, |_, _| noop(), |_, _| noop());
if res.is_err() {
return Err(());
@ -242,7 +246,8 @@ 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, |_, _| noop(), |_, _| noop());
if res.is_err() {
return Err(());
@ -298,7 +303,8 @@ 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, |_, _| noop(), |_, _| noop());
if res.is_err() {
return Err(());

View File

@ -43,6 +43,12 @@ use testing::{assert_eq, find_executable, NormalizedOutput};
pub struct Tester<'a> {
pub cm: Lrc<SourceMap>,
pub handler: &'a Handler,
/// This will be changed to [SingleThreadedComments] once `cargo-mono`
/// supports correct bumping logic, or we need to make a breaking change in
/// a upstream crate of this crate.
///
/// Although type is `Rc<SingleThreadedComments>`, it's fine to clone
/// `SingleThreadedComments` without `Rc`.
pub comments: Rc<SingleThreadedComments>,
}

View File

@ -129,6 +129,7 @@
"sabi",
"seqs",
"sess",
"shopify",
"skippable",
"smallvec",
"spanx",