SWC CommunitySC
SWC Community2y ago
4 replies
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;
            }
...
GitHub
Instant-loading web apps, without effort. Contribute to QwikDev/qwik development by creating an account on GitHub.
qwik/packages/qwik/src/optimizer/core/src/clean_side_effects.rs at ...
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.
Plugin cheatsheet – SWC
Was this page helpful?