
The technique of finding code by its structure rather than its raw text. Source code is first parsed into a tree representation of its grammatical structure—and patterns are then matched against that tree, so matches ignore formatting, whitespace, and variable names that don’t affect meaning.
How it works
A parser turns code into an AST, where each node represents a construct like a function call, loop, or assignment. Instead of searching for a text string, an AST matcher searches for a shape: for example, any call to eval() with a variable argument. Because the match is structural, it finds every equivalent instance regardless of how the code is written—foo( x ), foo(x), and foo(\n x\n) all match the same pattern.
Why it matters
Text search and regexes break on the countless ways the same code can be written. AST matching is precise and reliable, which is why it powers linters, codemods, refactoring tools, and static analysis. Tools like ast-grep, ESLint, and Semgrep use it to detect patterns and rewrite code safely at scale.
Example: To flag every console.log call, a regex might miss console .log() or match it inside a comment. An AST matcher targets the actual call expression node, catching all real calls and nothing else.