Skip to content

Commit

Permalink
feat: support deep AST parsing in debug (#6919)
Browse files Browse the repository at this point in the history
* feat: support deep AST parsing in debug

* fix
  • Loading branch information
h-a-n-a authored Jun 25, 2024
1 parent ff26c71 commit e015c99
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rustc-hash = { version = "1.1.0" }
schemars = { version = "0.8.16" }
serde = { version = "1.0.197" }
serde_json = { version = "1.0.115" }
stacker = { version = "0.1.15" }
sugar_path = { version = "1.2.0", features = ["cached_current_dir"] }
syn = { version = "2.0.58" }
tokio = { version = "1.37.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_loader_swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rspack_swc_visitors = { path = "../rspack_swc_visitors" }
rspack_util = { path = "../rspack_util" }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
stacker = { workspace = true }
swc_config = { workspace = true }
swc_core = { workspace = true, features = ["base", "ecma_ast", "common"] }
tokio = { workspace = true }
Expand Down
27 changes: 21 additions & 6 deletions crates/rspack_loader_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ impl SwcLoader {
self.identifier = identifier;
self
}
}

pub const SWC_LOADER_IDENTIFIER: &str = "builtin:swc-loader";

#[async_trait::async_trait]
impl Loader<RunnerContext> for SwcLoader {
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
fn loader_impl(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
let resource_path = loader_context.resource_path().to_path_buf();
let Some(content) = std::mem::take(&mut loader_context.content) else {
return Ok(());
Expand Down Expand Up @@ -160,6 +155,26 @@ impl Loader<RunnerContext> for SwcLoader {
}
}

pub const SWC_LOADER_IDENTIFIER: &str = "builtin:swc-loader";

#[async_trait::async_trait]
impl Loader<RunnerContext> for SwcLoader {
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
let inner = || self.loader_impl(loader_context);
#[cfg(debug_assertions)]
{
// Adjust stack to avoid stack overflow.
stacker::maybe_grow(
2 * 1024 * 1024, /* 2mb */
4 * 1024 * 1024, /* 4mb */
inner,
)
}
#[cfg(not(debug_assertions))]
inner()
}
}

impl Identifiable for SwcLoader {
fn identifier(&self) -> Identifier {
self.identifier
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_plugin_javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rspack_regex = { path = "../rspack_regex" }
rspack_util = { path = "../rspack_util" }
rustc-hash = { workspace = true }
serde_json = { workspace = true }
stacker = { workspace = true }
sugar_path = { workspace = true }
swc_core = { workspace = true, features = [
"__parser",
Expand Down
48 changes: 31 additions & 17 deletions crates/rspack_plugin_javascript/src/ast/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,40 @@ fn parse_with_lexer(
lexer: Lexer,
is_module: IsModule,
) -> Result<Program, Vec<parser::error::Error>> {
let mut parser = Parser::new_from(lexer);
let program_result = match is_module {
IsModule::Bool(true) => parser.parse_module().map(Program::Module),
IsModule::Bool(false) => parser.parse_script().map(Program::Script),
IsModule::Unknown => parser.parse_program(),
};
let mut errors = parser.take_errors();
// Using combinator will let rustc unhappy.
match program_result {
Ok(program) => {
if !errors.is_empty() {
return Err(errors);
let inner = || {
let mut parser = Parser::new_from(lexer);
let program_result = match is_module {
IsModule::Bool(true) => parser.parse_module().map(Program::Module),
IsModule::Bool(false) => parser.parse_script().map(Program::Script),
IsModule::Unknown => parser.parse_program(),
};
let mut errors = parser.take_errors();
// Using combinator will let rustc unhappy.
match program_result {
Ok(program) => {
if !errors.is_empty() {
return Err(errors);
}
Ok(program)
}
Err(err) => {
errors.push(err);
Err(errors)
}
Ok(program)
}
Err(err) => {
errors.push(err);
Err(errors)
}
};

#[cfg(debug_assertions)]
{
// Adjust stack to avoid stack overflow.
stacker::maybe_grow(
2 * 1024 * 1024, /* 2mb */
4 * 1024 * 1024, /* 4mb */
inner,
)
}
#[cfg(not(debug_assertions))]
inner()
}

pub fn parse(
Expand Down

1 comment on commit e015c99

@rspack-bot
Copy link

Choose a reason for hiding this comment

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

📝 Ran ecosystem CI: Open

suite result
${{ matrix.suite }} undefined skipped

Please sign in to comment.