I am developing an SWC plugin in Rust.
[dependencies]
swc_core = "0.75.*"
swc_ecma_parser = "0.133.8"
I need to replace the argument in console.log("hello")
with console.log("world")
fn visit_mut_call_expr(&mut self, n: &mut CallExpr) {
n.args = "new argument";
}
n.args
accepts a Vec
vector with data type ExprOrSpread
.
I don't understand what ExprOrSpread
is. Any value returns an error.
It works like this:
fn visit_mut_call_expr(&mut self, n: &mut CallExpr) {
let mut vector: Vec<ExprOrSpread> = vec![];
n.args = vector;
}
I replace console.log("hello")
with console.log()
.
How do I insert the string "world"?
In most cases, you're interested in traits such as From<Expr>
, which allow you to transform things into each other. And builders or factories to create a new one.
use swc_ecma_ast::*;
use swc_ecma_utils::ExprFactory;
fn visit_mut_call_expr(&mut self, n: &mut CallExpr) {
let first = Lit::Str("world".into());
n.args = vec![first.as_arg()];
}