Expand description
§Code Replacement and Transformation
Tools for replacing and transforming matched AST nodes with new content.
§Core Concepts
Replacer- Trait for generating replacement content from matched nodes- Template-based replacement using meta-variables (e.g.,
"let $VAR = $VALUE") - Structural replacement using other AST nodes
- Automatic indentation handling to preserve code formatting
§Built-in Replacers
Several types implement Replacer out of the box:
&str- Template strings with meta-variable substitutionRoot- Replace with entire AST treesNode- Replace with specific nodes
§Examples
§Template Replacement
let mut ast = Language::Tsx.ast_grep("var x = 42;");
// Replace using a template string
ast.replace("var $NAME = $VALUE", "const $NAME = $VALUE");
println!("{}", ast.generate()); // "const x = 42;"§Structural Replacement
let mut target = Language::Tsx.ast_grep("old_function();");
let replacement = Language::Tsx.ast_grep("new_function(42)");
// Replace with another AST
target.replace("old_function()", replacement);
println!("{}", target.generate()); // "new_function(42);"Re-exports§
pub use crate::source::Content;
Enums§
Traits§
- Replacer
- Generate replacement content for matched AST nodes.