AST editing
AST editing is the practice of modifying source code by transforming its parsed abstract syntax tree (AST) — addressing structural nodes like functions, classes, and parameters — rather than matching and replacing raw text.
An abstract syntax tree is the structured representation a compiler or parser builds from source code: every function, class, statement, and expression becomes a typed node with known boundaries and relationships. AST editing operates on that tree. Instead of "find this string and swap it for that string," an AST edit says "rename this method," "change this function's return type," or "add this parameter," then serializes the modified tree back to text. The change targets a node, not a line or a character offset.
This matters because text-level editing carries failure modes that compound on real codebases. Whitespace and formatting differences cause silent match failures; a target string that appears multiple times creates ambiguity; and a replacement that lands mid-expression can leave unbalanced braces or broken syntax. Because an AST edit is anchored to a named symbol, it is immune to line drift — reformatting the file, adding imports above the target, or changing indentation does not move the node. The edit is also structurally aware, so it can refuse or repair changes that would produce invalid syntax.
AST editing is long-established in developer tooling. Refactoring engines, codemod frameworks like jscodeshift, and language-specific libraries such as ts-morph (TypeScript), libcst (Python), and Roslyn (C#) all manipulate syntax trees to perform safe, repeatable transformations across large codebases. Parsers like Tree-sitter make incremental AST construction fast enough to run on every keystroke. The core trade-off is generality: AST tooling must be written per language and per grammar, which is why text editing remains the universal fallback for files a given parser does not cover.
In the context of AI coding agents, AST editing is increasingly relevant because a model's confidence in raw string matches degrades over a long session — the file keeps changing since the model last read it, so the 60th text edit is far riskier than the first. Structural edits hold steady, letting an agent apply a change and move on instead of re-reading and re-verifying after every mutation.