Skip to content

Commit

Permalink
fix: change process_js_with_custom_pass api
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Oct 23, 2022
1 parent 2dbff69 commit b1ed5ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
5 changes: 4 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ pub struct Options {
#[serde(skip_deserializing, default)]
pub top_level_mark: Option<Mark>,

#[serde(skip_deserializing, default)]
pub unresolved_mark: Option<Mark>,

#[cfg(not(target_arch = "wasm32"))]
#[serde(default = "default_cwd")]
pub cwd: PathBuf,
Expand Down Expand Up @@ -338,7 +341,7 @@ impl Options {
}
});

let unresolved_mark = Mark::new();
let unresolved_mark = self.unresolved_mark.unwrap_or_else(Mark::new);
let top_level_mark = self.top_level_mark.unwrap_or_else(Mark::new);

let es_version = target.unwrap_or_default();
Expand Down
25 changes: 16 additions & 9 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,31 +844,37 @@ impl Compiler {
///
/// This means, you can use `noop_visit_type`, `noop_fold_type` and
/// `noop_visit_mut_type` in your visitor to reduce the binary size.
///
/// You can pass comments that will be using in custom_pass, otherwise
/// a default singleThreadedComments will be used
#[tracing::instrument(level = "info", skip_all)]
pub fn process_js_with_custom_pass<P1, P2>(
&self,
fm: Arc<SourceFile>,
program: Option<Program>,
handler: &Handler,
opts: &Options,
custom_before_pass: impl FnOnce(&Program, &SingleThreadedComments) -> P1,
custom_after_pass: impl FnOnce(&Program, &SingleThreadedComments) -> P2,
comments: Option<&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 default_comments = SingleThreadedComments::default();
let comments = comments.unwrap_or(&default_comments);

let config = self.run(|| {
self.parse_js_as_input(
fm.clone(),
program,
handler,
opts,
&fm.name,
Some(&comments),
|program| custom_before_pass(program, &comments),
Some(comments),
|program| custom_before_pass(program),
)
})?;
let config = match config {
Expand All @@ -878,7 +884,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 +923,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, None, |_| noop(), |_| noop())
}

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

Expand Down
18 changes: 10 additions & 8 deletions crates/swc/tests/rust_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc::{
},
Compiler,
};
use swc_common::{comments::SingleThreadedComments, FileName};
use swc_common::{FileName};
use swc_ecma_ast::*;
use swc_ecma_parser::{EsConfig, Syntax, TsConfig};
use swc_ecma_transforms::{modules::common_js, pass::noop};
Expand Down Expand Up @@ -50,8 +50,9 @@ fn test_visit_mut() {

..Default::default()
},
|_, _| as_folder(PanicOnVisit),
|_, _| noop(),
None,
|_| 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| {
None,
|_| {
// Ensure comment API
noop()
},
|_, _: &SingleThreadedComments| noop(),
|_| noop(),
);

if res.is_err() {
Expand Down Expand Up @@ -182,7 +184,7 @@ fn shopify_2_same_opt() {
);

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

if res.is_err() {
return Err(());
Expand Down Expand Up @@ -244,7 +246,7 @@ fn shopify_3_reduce_defaults() {
);

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

if res.is_err() {
return Err(());
Expand Down Expand Up @@ -301,7 +303,7 @@ fn shopify_4_reduce_more() {
);

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

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

0 comments on commit b1ed5ba

Please sign in to comment.