Skip to content

Commit

Permalink
feat: allow to get current StmtInfo in visiting ast (#370)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Nov 27, 2023
1 parent d0056f1 commit d962268
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl NormalModule {
);

let render_kind = RenderKind::from_wrap_kind(&self_linking_info.wrap_kind);
let mut renderer = AstRenderer::new(base, render_kind);
let mut renderer = AstRenderer::new(base, &self.stmt_infos, render_kind);
renderer.render();

source.prepend(format!("// {}\n", self.pretty_path));
Expand Down
6 changes: 5 additions & 1 deletion crates/rolldown/src/bundler/renderer/impl_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ impl<'ast, 'r> Visit<'ast> for AstRenderer<'r> {
for directive in &program.directives {
self.visit_directive(directive);
}
self.visit_statements(&program.body);
for (stmt_idx, stmt) in program.body.iter().enumerate() {
self.current_stmt_info.next();
debug_assert!(self.current_stmt_info.get().stmt_idx == Some(stmt_idx));
self.visit_statement(stmt);
}
}

fn visit_binding_identifier(&mut self, ident: &oxc::ast::ast::BindingIdentifier) {
Expand Down
30 changes: 28 additions & 2 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc::{
ast::Visit,
span::{Atom, GetSpan, Span},
};
use rolldown_common::{ExportsKind, SymbolRef, WrapKind};
use rolldown_common::{ExportsKind, StmtInfo, StmtInfoId, StmtInfos, SymbolRef, WrapKind};
use rolldown_oxc::BindingIdentifierExt;
use rolldown_utils::MagicStringExt;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -88,21 +88,47 @@ impl RenderKind {
}
}

#[derive(Debug)]
struct CurrentStmtInfo<'a> {
stmts_infos: &'a StmtInfos,
current_idx: StmtInfoId,
}

impl<'a> CurrentStmtInfo<'a> {
pub fn new(stmts_infos: &'a StmtInfos) -> Self {
Self { stmts_infos, current_idx: StmtInfoId::from_raw(0) }
}

pub fn get(&self) -> &StmtInfo {
// 0 represents the facade namespace declaration statement, so it should not be used here.
debug_assert!(self.current_idx.raw() != 0);
let stmt_info = &self.stmts_infos[self.current_idx];
debug_assert!(stmt_info.stmt_idx.is_some(), "Facade statement should not be used here");
stmt_info
}

pub fn next(&mut self) {
self.current_idx = StmtInfoId::from_raw(self.current_idx.raw() + 1);
}
}

#[derive(Debug)]
pub struct AstRenderer<'r> {
ctx: AstRenderContext<'r>,
wrapped_esm_ctx: WrappedEsmCtx,
kind: RenderKind,
indentor: String,
current_stmt_info: CurrentStmtInfo<'r>,
}

impl<'r> AstRenderer<'r> {
pub fn new(ctx: AstRenderContext<'r>, kind: RenderKind) -> Self {
pub fn new(ctx: AstRenderContext<'r>, stmts_infos: &'r StmtInfos, kind: RenderKind) -> Self {
Self {
kind,
indentor: ctx.source.guessed_indentor().to_string(),
ctx,
wrapped_esm_ctx: WrappedEsmCtx::default(),
current_stmt_info: CurrentStmtInfo::new(stmts_infos),
}
}
}
Expand Down

0 comments on commit d962268

Please sign in to comment.