agentleFS
fmindfmind/dot/skills/ast-grep/SKILL.md

ast-grep

Skill10 starsChanged 56 days agoMITView on GitHub

Search, outline, and rewrite code structurally with ast-grep rules.

13 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use research, tdd and 1 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • DockerSkill

    research

    Research a documentation topic — locate affected files, understand the problem, identify what to change. Use when investigating an issue, a question, or a topic before writing a fix. Triggers on: "research issue 1234", "investigate what needs changing for #500", "what files are affected by #200", "where is X documented", "is our docs page about Y accurate", "look into how we document Z".

    Also in 3 projects that use this

  • AWSSkill

    tdd

    Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.

    Also in 3 projects that use this

  • MicrosoftSkill

    skill-creator

    Guide for creating effective GitHub Copilot skills (.github/skills/) in this repository. Use when creating a new skill, updating an existing skill, or when asked about skill structure, format, or best practices for the vscode-documentdb project.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 13 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Rewrite in two steps: add -r to see the diff, then --update-all (or -i for an interactive session).

    line 33
  2. Very specific:

    Wire into the gate: run ast-grep scan inside check:lint (see mise) so hooks and CI apply the same rules.

    line 35

“Common” is measured: kept by at least 6 in 10 of the 13 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

ast-grep

Structural code search and rewrite: a pattern is real code with meta-variables, matched against the syntax tree. A call-expression pattern distinguishes a call from similar text inside a string or comment; string and comment nodes can also be matched deliberately. Use it where rg gives false positives; plain text search stays with rg.

Commands

ast-grep outline -l python src/                                                 # symbols, imports, and members per file
ast-grep run -p 'print($$$ARGS)' -l python src/                                      # scope search to the relevant source
ast-grep run -p 'print($$$ARGS)' -r 'logger.info($$$ARGS)' -l python src/             # dry run: prints the diff, changes nothing
ast-grep run -p 'print($$$ARGS)' -r 'logger.info($$$ARGS)' -l python --update-all src/ # apply after reviewing the dry run (-i to confirm per hunk)
ast-grep run -p 'os.getenv($KEY)' -l python --json=compact src/                       # structured output after narrowing paths
ast-grep scan                                                                    # every rule in sgconfig.yml
ast-grep scan -r rules/no-print.yml --format github                              # one rule file; GitHub annotations in CI

Workflow

  1. Write the pattern as code: $NAME matches one node, $$$NAME a sequence (arguments, statements), $_ a node without binding; always pass -l <lang> so the pattern parses in the right grammar, and use --debug-query=ast when a pattern that should match does not.
  2. Search first: map unfamiliar files with ast-grep outline before reading full source; pass the relevant path or --globs; use --files-with-matches when only filenames are needed, then read selected matches with -C 2. Use JSON only for structured processing. Widen scope deliberately, and use --no-ignore vcs (or hidden; the flag requires a value) only when skipped files are relevant.
  3. Rewrite in two steps: add -r to see the diff, then --update-all (or -i for an interactive session); captured meta-variables are reused in the replacement.
  4. Promote to a rule: for a lint or a repeated refactor, ast-grep new project scaffolds sgconfig.yml and rules/; a rule file has id, language, rule (pattern, kind, inside, has, not), optional fix, severity, and message; ast-grep test runs its valid and invalid cases.
  5. Wire into the gate: run ast-grep scan inside check:lint (see mise) so hooks and CI apply the same rules.

Gotchas

  • Meta-variables are uppercase: $a is plain text; $A, $ARGS, $_ are meta-variables.
  • Pattern must be a complete node: foo( does not parse; match foo($$$) and narrow with --selector.
  • Rewrites replace the whole node: -r replaces the whole matched node, not a substring inside it.
  • Syntax is not name resolution: inspect imports, aliases, and shadowed names before rewriting; identical syntax can refer to different functions.
  • Pass -l for inline patterns: pass -l python for inline patterns; under scan, the .py extension selects the grammar.

Official Skills

Upstream: ast-grep/agent-skill ships ast-grep (structural search) and ast-grep-outline (codebase map); preview the same-name upstream ast-grep instead of installing it (vendor-skill policy); the rest follow the same policy.

Documentation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.