AST editing
AST-native editing for TypeScript and JavaScript. Address symbols by kind and name, mutate them structurally with 65+ atomic operations across three tiers.
ast_edit is the first AI coding tool to edit TypeScript and JavaScript through the AST instead of text matching. It addresses symbols by kind and name, mutates them structurally, and writes the result back. Micro-edits cost a handful of tokens and never fail on whitespace.
You don't invoke ast_edit directly. The agent picks it automatically for TypeScript and JavaScript files. This page is a reference for what it can do.
Origin
The AST-first approach comes from my own Master's thesis in Computer Science, *Typed vs Untyped Programming Languages* (co-authored with Artur Matusiak, 2022), and its reference implementation JS Typer. The thesis demonstrated that programmatic JavaScript-to-TypeScript conversion could be done entirely through ts-morph AST mutations rather than string manipulation. ast_edit is the direct descendant of that work, applied to AI coding.
Examples
Change a function's return type:
ast_edit({
path: "src/api.ts",
action: "set_return_type",
target: "function",
name: "fetchUser",
value: "Promise",
})Make a function async and add a parameter - one atomic batch:
ast_edit({
path: "src/api.ts",
operations: [
{ action: "set_async", target: "function", name: "fetchUser", value: "true" },
{ action: "add_parameter", target: "function", name: "fetchUser", value: "cache: boolean" },
{ action: "add_named_import", value: "./types", newCode: "User" },
],
})Rename a class method:
ast_edit({
path: "src/store.ts",
action: "rename",
target: "method",
name: "UserStore.fetch",
value: "load",
})Replace a whole function body:
ast_edit({
path: "src/util.ts",
action: "set_body",
target: "function",
name: "parseInput",
newCode: "return JSON.parse(raw);",
})Batch edits are all-or-nothing - if any fails, none are applied.
Targets
| Target | Addressed by |
|---|---|
function, class, interface, type, enum, variable, constant | name |
method, property | "ClassName.member" or "member" |
arrow_function | name (targets const fn = () => {}) |
constructor | ClassName |
Action reference
Supported files
.ts, .tsx, .js, .jsx, .mts, .cts, .mjs, .cjs. For other files the agent falls back to edit_file / multi_edit.
Safety
- File snapshotted before the edit - rejects if changed on disk mid-flight.
- Atomic batches: all or none.
undo_editreversesast_editthe same asedit_file.- Pre/post diagnostics - new type errors flow back to the agent immediately.