Replies: 2 comments 6 replies
-
You may able to provide custom visitor into transform chain, next.js have some reference example like |
Beta Was this translation helpful? Give feedback.
1 reply
-
use std::{fs, sync::Arc};
use swc::{config::Options, ecmascript::ast::ModuleItem};
use swc_common::{
errors::{ColorConfig, Handler},
FileName, SourceMap, DUMMY_SP,
};
use swc_ecma_parser::{Syntax, TsConfig};
use swc_ecmascript::{
ast::Lit,
transforms::pass::noop,
visit::{as_folder, Fold},
};
use swc_ecmascript::{
ast::{Expr, Number},
visit::{VisitMut, VisitMutWith},
};
fn main() {
let cm = Arc::<SourceMap>::default();
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));
let c = swc::Compiler::new(cm.clone());
let fm = cm.new_source_file(
FileName::Custom("test.js".into()),
include_str!("./main.ts").into(),
);
let result = c.process_js_with_custom_pass(
fm,
None,
&handler,
&Options::default(),
|_| noop(),
|_| my_visitor(),
);
println!("{:#?}", result.unwrap());
}
fn my_visitor() -> impl Fold {
as_folder(MyVisitor)
}
struct MyVisitor;
impl VisitMut for MyVisitor {
fn visit_mut_expr(&mut self, expr: &mut Expr) {
expr.visit_mut_children_with(self);
*expr = Expr::Lit(Lit::Num(Number { // here change expr to 0
span: DUMMY_SP,
value: 0.0,
}))
}
} you can find more example of VisitMut and Fold in |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
PuruVJ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have this code
I have the AST in body variable. Now, I want to walk this AST, akin to estree-walker by Rich Haris
How do I walk this in Rust in the best possible way?
Beta Was this translation helpful? Give feedback.
All reactions