ast-grep-cli

0.42.1 · active · verified Sat Apr 11

ast-grep-cli is a Python package that provides a command-line interface (CLI) wrapper for the powerful `ast-grep` tool. `ast-grep` enables structural search and rewrite of code across various programming languages using abstract syntax tree (AST) patterns. The project is actively developed with frequent minor releases, often weekly or bi-weekly, incorporating new features, bug fixes, and language support updates. The current version is 0.42.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ast-grep-cli` from Python by invoking the `sg` command via `subprocess.run()`. It shows both a search operation with JSON output and a dry-run rewrite example. Always prefer using `--json` output for robust parsing of `ast-grep` results in scripts.

import subprocess
import os
import json

# Create a dummy file for demonstration
dummy_js_code = """
function greet(name) {
    console.log("Hello, " + name);
}
greet("World");
"""
with open("example.js", "w") as f:
    f.write(dummy_js_code)

try:
    # Example 1: Search for 'console.log($$$)' pattern and get JSON output
    print("--- Searching for console.log statements (JSON output) ---")
    command_search = [
        "sg",
        "run",
        "--pattern",
        "console.log($$$)",
        "--lang",
        "javascript",
        "--path",
        "example.js",
        "--json" # Use JSON output for programmatic parsing
    ]
    search_result = subprocess.run(
        command_search,
        capture_output=True,
        text=True,
        check=False # Do not raise CalledProcessError if no matches (returns non-zero exit code)
    )
    print(f"Search Exit Code: {search_result.returncode}")
    if search_result.stdout:
        try:
            json_output = json.loads(search_result.stdout)
            print("Parsed JSON Output:", json.dumps(json_output, indent=2))
        except json.JSONDecodeError:
            print("Stdout (not valid JSON):", search_result.stdout)
    if search_result.stderr:
        print(f"Search Stderr:\n{search_result.stderr}")

    # Example 2: Perform a dry-run rewrite from 'console.log' to 'alert'
    print("\n--- Performing a dry-run rewrite (stdout output) ---")
    command_rewrite_dry_run = [
        "sg",
        "run",
        "--pattern",
        "console.log($$$)",
        "--rewrite",
        "alert($$$)",
        "--lang",
        "javascript",
        "--path",
        "example.js",
        "--dry-run" # Show changes without modifying the file
    ]
    rewrite_result = subprocess.run(
        command_rewrite_dry_run,
        capture_output=True,
        text=True,
        check=False
    )
    print(f"Rewrite Dry-Run Exit Code: {rewrite_result.returncode}")
    print(f"Rewrite Dry-Run Stdout:\n{rewrite_result.stdout}")
    if rewrite_result.stderr:
        print(f"Rewrite Dry-Run Stderr:\n{rewrite_result.stderr}")

except FileNotFoundError:
    print("Error: 'sg' command not found. Ensure ast-grep-cli is installed and in your PATH.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    if os.path.exists("example.js"):
        os.remove("example.js")

view raw JSON →