ast-grep adds a parameterized util
Introduced 2026-03-16 · score 0.94
HOW TO USE IT
When a rule needs a reusable global utility customized for different contexts, define parameters on the utility and pass a rule object for every parameter through the caller’s `matches` rule.
# utils/audit-log-call.yml
id: audit-log-call
arguments: [logger-rule]
language: TypeScript
rule:
pattern: $OBJ.$METHOD($$ARGS)
all:
- has:
kind: member_expression
has:
field: object
matches: logger-rule
- has:
field: arguments
has:
kind: string
# rules/audit-logging.yml
id: no-console-string
language: TypeScript
rule:
matches:
audit-log-call:
logger-rule: { regex: ^console$ }WHY THIS HELPS
Use ast-grep's parameterized util while maintaining or changing repository code.
REQUIREMENTS AND LIMITATIONS
- Version: 0.42.0
- arguments: Utility declaration list of parameter names. Every declared parameter must be supplied at the call site; there are no optional parameters.
- <parameter name> (for example, logger-rule) (required): At the call site, provide a full ast-grep rule object as the value; arguments are rules, not strings.
- matches (required): In the utility body, a bare parameter name in `matches` refers to the bound rule. At the call site, `matches` names the parameterized global utility and maps each argument name to its rule object.
- Prerequisite: Use ast-grep 0.42 parameterized utilities; the release documentation labels this feature experimental.
- Prerequisite: Parameters can be declared only by global utility rules in separate YAML files under the `utils/` directory; the utility has `id`, `language`, and `rule`.
- Prerequisite: All declared arguments must be supplied. The documentation does not state that parameters are mutually exclusive.
FIRST-PARTY EVIDENCE
Check the installed version and the source before using it. Entry JSON