Directory Compare FCRS-1.0 /llms.txt
← Back to Directory

ast-grep

$ sg Community Low Risk
ast-grep (OSS) · Files & Search · MIT · macos / linux / windows

ast-grep (sg) matches and rewrites code by syntax tree structure rather than brittle regexes. It defaults to read-only diff/match previews unless --update-all is passed and emits structured --json results with exact AST node positions.

FCRS-1.0 Score
93 /100
Excellent
Medium Confidence
Zero-Friction Install
brew install ast-grep
CLI vs MCP Decision CLI Preferred vs MCP

Direct sg run --json invokes Tree-sitter locally in milliseconds without running a background language server or MCP daemon.

Agent-Readiness Checklist

✓ JSON Output
— Field Filtering
✓ Non-Interactive
✓ Dry-Run
✓ Read-Only Mode
✓ NO_COLOR Clean
— Self-Describing
— Official SKILL.md

Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.

FCRS-1.0 6-Dimension Breakdown

Agent-Readiness 24 / 25

Supports --json output, --color never, non-interactive execution, and safe diff-only previews by default.

  • Native --json match output and --update-all opt-in for writes
Trust & Safety 22 / 25

Read-only unless --update-all (-U) is explicitly passed; local-only with zero network calls.

  • Diff preview is default when --rewrite is specified
Setup & Portability 14 / 15

Available via brew, npm, pip, cargo, and prebuilt binaries across macOS, Linux, and Windows.

  • Multi-ecosystem distribution (brew, npm, pypi, cargo)
Utility & Efficiency 15 / 15

Eliminates multi-line regex fragility by matching syntax trees and metavariables cleanly.

  • Tree-sitter AST pattern matching across dozens of languages
Maintenance & Lineage 9 / 10

Actively maintained OSS project (ast-grep/ast-grep) with rapid language and rule-engine support.

  • Active GitHub repository under MIT license
Docs & Verifiability 9 / 10

Well-structured guide, rule playground, and CLI reference documentation.

  • Detailed official guide at ast-grep.github.io

Security, Auth & Guardrails

Auth Modes
No Auth
Credential Storage

None required; operates strictly on local source files.

Side Effects
Read-Only Local Write
Cost & Quotas

100% free, offline, and open-source (MIT).

Copyable Guardrail for CLAUDE.md / AGENTS.md
# ast-grep (sg) Agent Rules
- Always preview rewrites with `sg run --pattern ... --rewrite ...` before adding `--update-all`.
- Specify `--lang` and `--json` for deterministic AST matching and parsing.

Agent-Ready Command Recipes

Safe Read-Only Queries

Search AST pattern with structured JSON output
sg run --pattern 'console.log($$$ARGS)' --lang ts --json src/

Emits JSON matches with file paths, byte/line spans, and captured metavariables.

Preview structural rewrite without modifying files
sg run --pattern 'var $A = $B' --rewrite 'let $A = $B' --lang js src/

Prints a read-only diff preview; files remain untouched without --update-all.

Controlled Writes & Previews

Apply reviewed structural rewrite across matched files
sg run --pattern 'oldApi($$$ARGS)' --rewrite 'newApi($$$ARGS)' --lang ts --update-all src/

Writes AST-safe replacements in-place only when --update-all is explicitly supplied.

Strengths for Agents

  • + Native --json output returns exact AST node spans and captured metavariables.
  • + Rewrites preview diffs by default and require explicit --update-all to touch disk.

Caveats & Pitfalls

  • ! The sg binary name can collide with newgrp/sg on some Linux distributions (use ast-grep alias if needed).

Tested Agent Hosts

Claude Code Codex CLI Gemini CLI