Skip to main content
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<User>",
})
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

TargetAddressed by
function, class, interface, type, enum, variable, constantname
method, property"ClassName.member" or "member"
arrow_functionname (targets const fn = () => {})
constructorClassName

Action reference

ActionEffect
set_typeVariable/property/parameter type annotation
set_return_typeFunction return type
set_initializer / remove_initializerVariable initializer
set_valueEnum member value
set_async / set_generatorToggle async / function*
set_export / set_default_exportToggle export modifiers
set_abstract / set_static / set_readonly / set_scope / set_optional / set_overrides / set_ambientClass-member modifiers
set_const_enum / set_declaration_kindMisc modifiers
renameDeclaration-only rename
removeDelete the declaration
add_parameter / remove_parameterFunction/method parameters
ActionEffect
set_bodyReplace function/method body
add_statement / insert_statement / remove_statementStatement-level
add_property / remove_propertyInterface/class properties
add_method / remove_method / add_constructor / add_getter / add_setterClass members
add_member / remove_memberEnum / type-literal members
add_decorator / remove_decoratorDecorators
add_overloadFunction overload signatures
set_extends / add_extends / remove_extends / add_implements / remove_implementsInheritance
add_type_parameterGeneric type parameters
add_jsdoc / remove_jsdocJSDoc blocks
unwrap, set_structure, extract_interfaceStructural rewrites
ActionEffect
replaceReplace a whole symbol
replace_in_bodyAST-anchored substring replace (unique match only)
ActionEffect
create_fileNew file (pass newCode as content)
add_import / remove_import / add_named_import / remove_named_importImports
set_module_specifierChange import path
add_export_declaration / add_named_reexportRe-exports
add_namespaceNamespace declarations
organize_imports / fix_missing_imports / fix_unusedCleanup
add_function / add_class / add_interface / add_type_alias / add_enum / add_variableAppend top-level declarations
insert_textInsert at an anchor ("after-imports", "before-exports", index)

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_edit reverses ast_edit the same as edit_file.
  • Pre/post diagnostics — new type errors flow back to the agent immediately.