AST transformations
Concept and boundary
A TransformerFactory receives a transformation context and returns a root transformer. Use visitEachChild for recursive descent and the context factory for replacement nodes. Return the original node when no change is needed; dispose standalone transformation results after use. This does not re-check newly generated semantics.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: transform
Revisit reference fixture ts-api-transform (historical V24, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const count: number = 7;", ts.ScriptTarget.ES2022, true);const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });const replaceSeven = context => root => { function visit(node) { if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8); return ts.visitEachChild(node, visit, context); } return ts.visitNode(root, visit, ts.isSourceFile);};const result = ts.transform(source, [replaceSeven]);try { assert.equal(source.statements[0].declarationList.declarations[0].initializer.text, "7"); console.log(printer.printFile(result.transformed[0]).trim());} finally { result.dispose(); }Expected stdout (exit 0):
const count: number = 8;Workshop 2: visit-each-child
Revisit reference fixture ts-api-visit-each-child (historical V24, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const x = 7 + 7;", ts.ScriptTarget.ES2022, true);const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });const replaceSeven = context => root => { function visit(node) { if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8); return ts.visitEachChild(node, visit, context); } return ts.visitNode(root, visit, ts.isSourceFile);};const result = ts.transform(source, [replaceSeven]);try { console.log(printer.printFile(result.transformed[0]).trim()); } finally { result.dispose(); }Expected stdout (exit 0):
const x = 8 + 8;Practice and recap
Replace only numeric literal 7 inside a selected function and leave other literals intact.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
AST transformations — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes AST transformations?
What does workshop 1 (transform) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const count: number = 7;", ts.ScriptTarget.ES2022, true);const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });const replaceSeven = context => root => { function visit(node) { if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8); return ts.visitEachChild(node, visit, context); } return ts.visitNode(root, visit, ts.isSourceFile);};const result = ts.transform(source, [replaceSeven]);try { assert.equal(source.statements[0].declarationList.declarations[0].initializer.text, "7"); console.log(printer.printFile(result.transformed[0]).trim());} finally { result.dispose(); }What does workshop 2 (visit-each-child) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const x = 7 + 7;", ts.ScriptTarget.ES2022, true);const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });const replaceSeven = context => root => { function visit(node) { if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8); return ts.visitEachChild(node, visit, context); } return ts.visitNode(root, visit, ts.isSourceFile);};const result = ts.transform(source, [replaceSeven]);try { console.log(printer.printFile(result.transformed[0]).trim()); } finally { result.dispose(); }