AST Grep
Use AST-aware search when the question is about code shape, not just text. rg remains the default for plain strings; ast-grep is for syntax.
When To Use
Use this skill when:
- Finding call sites by syntactic shape.
- Searching for imports, JSX props, function calls, decorators, object patterns, assignments, or API usage.
- Planning or applying a mechanical codemod.
- Refactoring repeated code structures across files.
- Avoiding false positives from comments, strings, generated files, or unrelated identifiers.
Skip this skill when:
- A simple
rgorrg --filesquery answers the question. - The language is unsupported or the repo lacks enough files for AST matching to pay off.
- The task needs semantic call flow or impact analysis; use
codegraphfirst when available.
Workflow
- Define the code shape in words before writing the pattern.
- Start with
rgorrg --filesif needed to identify languages and candidate directories. - Choose the language/parser explicitly when the tool requires it.
- Test the AST pattern on a small scope first.
- Inspect matches before changing files.
- For codemods:
- run a dry run or JSON/listing mode first.
- apply changes to the narrowest safe file set.
- inspect the diff.
- run tests/typecheck/lint relevant to the changed language.
Pattern Discipline
- Prefer concrete syntax over broad wildcards.
- Exclude generated/build/vendor directories.
- Avoid matching comments and strings unless that is the target.
- Keep replacement patterns minimal.
- If the pattern is brittle, stop and use manual edits or CodeGraph-assisted navigation.
Tool Notes
Common command shapes:
ast-grep --lang ts -p '<pattern>' <path>
ast-grep --lang tsx -p '<pattern>' <path>
ast-grep --lang js -p '<pattern>' <path>
Use the installed tool's help output for exact flags. Some versions differ on JSON, rewrite, and update flags.
Guardrails
- Do not apply a codemod without inspecting candidate matches.
- Do not run broad rewrites over the whole home directory or unrelated workspaces.
- Do not treat a clean AST search as proof of runtime behavior.
- Preserve unrelated user changes.
Closeout
AST-grep:
- Shape searched:
- Scope:
- Matches:
- Changes applied:
- Validation: