SD
SWC Developers•6mo ago
w00t

How can I rewrite this code for the moved SyntaxContext?

I'm upgrading the Qwik optimizer and this code no longer works because expr.span no longer has the SyntaxContext. However, the only other property is expr.expr which is a Box<Expr>. How can I get at the syntax context to add the mark? Or is this the wrong approach and should I be doing one of https://swc.rs/docs/plugin/ecmascript/cheatsheet#deleting-node ? I'm not sure how to apply that though, since it marks by making the name invalid and then removes, but here I need to retain what's marked. I don't quite understand how it works, but it removes statements that aren't used. The full code is at https://github.com/QwikDev/qwik/blob/b0802ca6dc448a82454dfba6b46f7392e0401c05/packages/qwik/src/optimizer/core/src/clean_side_effects.rs
pub struct CleanMarker {
pub mark: Mark,
}

impl Treeshaker {
pub fn new() -> Self {
let mark = Mark::new();
Self {
marker: CleanMarker { mark },
cleaner: CleanSideEffects {
did_drop: false,
mark,
},
}
}
}

impl VisitMut for CleanMarker {
fn visit_mut_module_item(&mut self, node: &mut ast::ModuleItem) {
if let ast::ModuleItem::Stmt(ast::Stmt::Expr(expr)) = node {
expr.span = expr.span.apply_mark(self.mark);
}
}
}

impl VisitMut for CleanSideEffects {
fn visit_mut_module(&mut self, node: &mut ast::Module) {
node.body.retain(|item| {
if item.span().has_mark(self.mark) {
return true;
}
...
pub struct CleanMarker {
pub mark: Mark,
}

impl Treeshaker {
pub fn new() -> Self {
let mark = Mark::new();
Self {
marker: CleanMarker { mark },
cleaner: CleanSideEffects {
did_drop: false,
mark,
},
}
}
}

impl VisitMut for CleanMarker {
fn visit_mut_module_item(&mut self, node: &mut ast::ModuleItem) {
if let ast::ModuleItem::Stmt(ast::Stmt::Expr(expr)) = node {
expr.span = expr.span.apply_mark(self.mark);
}
}
}

impl VisitMut for CleanSideEffects {
fn visit_mut_module(&mut self, node: &mut ast::Module) {
node.body.retain(|item| {
if item.span().has_mark(self.mark) {
return true;
}
...
GitHub
qwik/packages/qwik/src/optimizer/core/src/clean_side_effects.rs at ...
Instant-loading web apps, without effort. Contribute to QwikDev/qwik development by creating an account on GitHub.
Plugin cheatsheet – SWC
SWC is an extensible Rust-based platform for the next generation of fast developer tools. It's used by tools like Next.js, Parcel, and Deno, as well as companies like Vercel, ByteDance, Tencent, Shopify, and more.
4 Replies
kdy1
kdy1•6mo ago
I think you can make the node invalid in the else statement for your ast::ModuleItem::Stmt(ast::Stmt::Expr(expr))
w00t
w00tOP•6mo ago
hmm that will basically kill all import statements. I think I figured out how it works. It gets used here: https://github.com/QwikDev/qwik/blob/b0802ca6dc448a82454dfba6b46f7392e0401c05/packages/qwik/src/optimizer/core/src/parse.rs#L334-L361 First it marks all top-level statements in the module. Then it simplifes, which will remove all unused statements and assignments but leave possibly impure expressions. Anything that was simplified and moved to toplevel will not have been marked, and if they are function calls/new constructions, they will be forcibly removed. So I only need to mark top level call/new expressions, not all expressions. Hmm.
GitHub
qwik/packages/qwik/src/optimizer/core/src/parse.rs at b0802ca6dc448...
Instant-loading web apps, without effort. Contribute to QwikDev/qwik development by creating an account on GitHub.
kdy1
kdy1•6mo ago
Call / New has syntax context
w00t
w00tOP•6mo ago
yes it works now 🙂

Did you find this page helpful?