apply_codemod
Apply structural or regex find-and-replace to modify code. Preview changes with dry-run before applying.
Instructions
Structural (AST-aware) or regex find-and-replace across files. Default engine "auto": when the pattern is an ast-grep pattern (concrete syntax with $META metavariables, e.g. "foo($$$ARGS)") supported code files (.ts/.tsx/.js/.jsx) are matched syntactically — so occurrences inside strings and comments are NOT touched. Plain regex patterns fall back to the text engine. Dry-run by default — first call shows preview (with engine_used), second call with dry_run=false applies. Potentially destructive. Always preview with dry_run=true first. Returns JSON: { success, engine_used, dry_run, matches, files_modified, total_replacements, total_files }.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| engine | No | Engine: "auto" (default — AST for ast-grep patterns on supported code files, else regex), "ast" (force ast-grep), "regex" (force text regex). | |
| dry_run | No | Preview changes without writing (default: true). Set to false to apply. | |
| pattern | Yes | Pattern to match. ast-grep pattern (e.g. "foo($$$ARGS)", "console.log($A)") for the AST engine, or a JavaScript regex for the text engine. | |
| multiline | No | Regex engine only: multiline mode (dot matches newlines, patterns span lines) | |
| replacement | Yes | Replacement template. AST engine: substitute captured metavariables ($A, $$$ARGS, or positional $1/$2). Regex engine: $1, $2 capture groups. | |
| file_pattern | Yes | Glob pattern for files to scan (e.g. "tests/**/*.test.ts", "src/**/*.py") | |
| confirm_large | No | Required when >20 files affected. Acknowledges large-scale change. | |
| filter_content | No | Only process files containing this substring (narrows scope) |