as variable)\n- Or use single quotes: `'$VAR'` works in most shells\n\n**Example:**\n\n```\n# Correct: escaped $\nast-grep scan --inline-rules \"rule: {pattern: 'console.log(\\$ARG)'}\" .\n\n# Or use single quotes\nast-grep scan --inline-rules 'rule: {pattern: \"console.log($ARG)\"}' .\n```\n\n## Common Use Cases\n\n### Find Functions with Specific Content\n\nFind async functions that use await:\n\n```\nast-grep scan --inline-rules \"id: async-await\nlanguage: javascript\nrule:\n all:\n - kind: function_declaration\n - has:\n pattern: await \\$EXPR\n stopBy: end\" /path/to/project\n```\n\n### Find Code Inside Specific Contexts\n\nFind console.log inside class methods:\n\n```\nast-grep scan --inline-rules \"id: console-in-class\nlanguage: javascript\nrule:\n pattern: console.log(\\$\\$\\$)\n inside:\n kind: method_definition\n stopBy: end\" /path/to/project\n```\n\n### Find Code Missing Expected Patterns\n\nFind async functions without try-catch:\n\n```\nast-grep scan --inline-rules \"id: async-no-trycatch\nlanguage: javascript\nrule:\n all:\n - kind: function_declaration\n - has:\n pattern: await \\$EXPR\n stopBy: end\n - not:\n has:\n pattern: try { \\$\\$\\$ } catch (\\$E) { \\$\\$\\$ }\n stopBy: end\" /path/to/project\n```\n\n## Resources\n\n### references/\n\nContains detailed documentation for ast-grep rule syntax:\n\n- `rule_reference.md`: Comprehensive ast-grep rule documentation covering atomic rules, relational rules, composite rules, and metavariables\n\nLoad these references when detailed rule syntax information is needed.","repositoryUrl":"https://github.com/ast-grep/agent-skill","originalUrl":"https://skills.sh/ast-grep/agent-skill/ast-grep","originalInstalls":410,"platformInstalls":0,"isVerified":true,"importedBy":null,"createdAt":"2026-02-10T03:38:59.265Z","updatedAt":null,"deletedAt":null,"totalInstalls":410}}
This skill helps translate natural language queries into ast-grep rules for structural code search. ast-grep uses Abstract Syntax Tree (AST) patterns to match code based on its structure rather than just text, enabling powerful and precise code search across large codebases. Use this skill when users: - Need to search for code patterns using structural matching (e.g., "find all async functions that don't have error handling")
// test_example.js async function example() { const result = await fetchData(); return result; }
stopBy: end for relational rules (inside, has) to ensure search goes to the end of the directionpattern for simple structureskind with has/inside for complex structuresall, any, or notid: async-with-await language: javascript rule: kind: function_declaration has: pattern: await $EXPR stopBy: end
references/rule_reference.md for comprehensive rule documentation.echo "async function test() { await fetch(); }" | ast-grep scan --inline-rules "id: test language: javascript rule: kind: function_declaration has: pattern: await \$EXPR stopBy: end" --stdin `**Option B: Test with rule files (recommended for complex rules)**` ast-grep scan --rule test_rule.yml test_example.js
stopBy: end to relational rules if not present--debug-query to understand the AST structure (see below)kind values are correct for the languageast-grep run --pattern 'console.log($ARG)' --lang javascript /path/to/projectast-grep scan --rule my_rule.yml /path/to/projectast-grep scan --inline-rules "id: my-rule language: javascript rule: pattern: \$PATTERN" /path/to/project
ast-grep run --pattern 'async function example() { await fetch(); }' \ --lang javascript \ --debug-query=cst
cst: Concrete Syntax Tree (shows all nodes including punctuation)ast: Abstract Syntax Tree (shows only named nodes)pattern: Shows how ast-grep interprets your patternkind values for nodes# See the structure of your target code ast-grep run --pattern 'class User { constructor() {} }' \ --lang javascript \ --debug-query=cst # See how ast-grep interprets your pattern ast-grep run --pattern 'class $NAME { $$BODY }' \ --lang javascript \ --debug-query=pattern
echo "const x = await fetch();" | ast-grep scan --inline-rules "id: test language: javascript rule: pattern: await \$EXPR" --stdin `**Add --json for structured output:**` echo "const x = await fetch();" | ast-grep scan --inline-rules "..." --stdin --json
# Basic pattern search ast-grep run --pattern 'console.log($ARG)' --lang javascript . # Search specific files ast-grep run --pattern 'class $NAME' --lang python /path/to/project # JSON output for programmatic use ast-grep run --pattern 'function $NAME($$)' --lang javascript --json .
# With rule file ast-grep scan --rule my_rule.yml /path/to/project # With inline rules ast-grep scan --inline-rules "id: find-async language: javascript rule: kind: function_declaration has: pattern: await \$EXPR stopBy: end" /path/to/project # JSON output ast-grep scan --rule my_rule.yml --json /path/to/project
stopBy: end to ensure complete traversal.stopBy: end unless there's a specific reason not to:has: pattern: await $EXPR stopBy: end
pattern firstkind to match the node typehas, inside) as neededall, any, not) for complex logicconsole.log($ARG))--debug-query=cst to see the actual AST structurekind matches what you expect--inline-rules, escape metavariables in shell commands:\$VAR instead of $VAR (shell interprets $ as variable)'$VAR' works in most shells# Correct: escaped $ ast-grep scan --inline-rules "rule: {pattern: 'console.log(\$ARG)'}" . # Or use single quotes ast-grep scan --inline-rules 'rule: {pattern: "console.log($ARG)"}' .
ast-grep scan --inline-rules "id: async-await language: javascript rule: all: - kind: function_declaration - has: pattern: await \$EXPR stopBy: end" /path/to/project
ast-grep scan --inline-rules "id: console-in-class language: javascript rule: pattern: console.log(\$\$\$) inside: kind: method_definition stopBy: end" /path/to/project
ast-grep scan --inline-rules "id: async-no-trycatch language: javascript rule: all: - kind: function_declaration - has: pattern: await \$EXPR stopBy: end - not: has: pattern: try { \$\$\$ } catch (\$E) { \$\$\$ } stopBy: end" /path/to/project
rule_reference.md: Comprehensive ast-grep rule documentation covering atomic rules, relational rules, composite rules, and metavariables