private_ident! not working? And what is the correct way to write a plugin? #4848
-
Hello! I'm new to swc and I want to write an swc plugin: I found And I also find the testing process is not convenient (I tried I also found the plugin created by use swc_plugin::{ast::*, plugin_transform, utils::private_ident, TransformPluginProgramMetadata};
pub struct TransformVisitor {
uses_import_meta: bool,
}
impl TransformVisitor {
pub fn new() -> Self {
Self {
uses_import_meta: false,
}
}
}
impl Fold for TransformVisitor {
// https://rustdoc.swc.rs/swc_ecma_visit/trait.Fold.html
fn fold_expr(&mut self, n: Expr) -> Expr {
match n {
Expr::MetaProp(meta) if meta.kind == MetaPropKind::ImportMeta => {
self.uses_import_meta = true;
Expr::Ident(private_ident!("import_meta"))
}
_ => n.fold_children_with(self),
}
}
}
impl VisitMut for TransformVisitor {
// https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
}
#[plugin_transform]
pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
program.fold_with(&mut as_folder(TransformVisitor::new()))
}
#[cfg(test)]
mod tests {
use std::fs::{read_to_string, write};
use std::{path::PathBuf, rc::Rc};
use swc_common::comments::SingleThreadedComments;
use swc_ecma_parser::{Syntax};
use swc_ecma_transforms_testing::{test, Tester};
use crate::TransformVisitor;
#[testing::fixture("tests/fixtures/**/input.js")]
fn test(input: PathBuf) {
let output = input.with_file_name("output.js");
Tester::run(|tester| {
let actual = tester.apply_transform(
TransformVisitor::new(),
"output.js",
Syntax::Es(Default::default()),
read_to_string(&input).unwrap().as_str(),
)?;
let result = tester.print(&actual, &Rc::new(SingleThreadedComments::default()));
write(&output, result).unwrap();
Ok(())
})
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
-
If you want to use |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
hygiene
pass will rename identifiers createdprivate_ident
iff there's a conflict.