ast-grep
Overview
astGrep(pattern, callback)
Search part of the code (single AST node) using ast-grep and iterate over found nodes in the callback or returned commands. Creates separate context for each found node, which could be reused later.
callback
Parameters
pattern
string | readonly string[] | NapiConfig | AstGrepAPI
requiredThere are multiple ways to define a pattern:
- String pattern, e.g.
console.log($$$A)
; by defaultrelaxed
strictness algorithm is used, meaning that comments and non-significant syntax constructs (like single and double quotes for JavaScript) will be ignored. - Object pattern, using NapiConfig, which will be passed as is to
ast-grep
engine. - Template literal, using YAML format inside. It is a syntax sugar for
NapiConfig
object, so you can copy rules from ast-grep playground and paste them here. - Object pattern, using
AstGrepAPI
. In this case request is sent toast-grep
CLI and you can use all its features, likefix
. But there is no programmatic access to the results, it is one-way operation. It has similar syntax toNapiConfig
, but with additionalid
field.
callback
(subcommands: Subcommands) => Promise<void> | void
A callback which will be executed for each repository. First argument is an object with subcommands. It can be destructured to get access to subcommands.