Source files and AST traversal
Concept and boundary
Parse source text into a SourceFile and walk structural children with forEachChild. The parser does not resolve imported names or establish type correctness. Collect identifier text only after checking its node kind. A truthy callback return stops the traversal early.
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: for-each-child
Revisit reference fixture ts-api-for-each-child (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const x = 1; function twice(n: number) { return n * 2; }", ts.ScriptTarget.ES2022, true);const names = [];function visit(node) { if (ts.isIdentifier(node)) names.push(node.text); ts.forEachChild(node, visit);}visit(source);assert.deepEqual(names, ["x", "twice", "n", "n"]);console.log(names.join(","));Expected stdout (exit 0):
x,twice,n,nWorkshop 2: create-source-file
Revisit reference fixture ts-api-create-source-file (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const source = ts.createSourceFile("input.ts", "const count = 7;", ts.ScriptTarget.ES2022, true);const declaration = source.statements[0].declarationList.declarations[0];assert(ts.isVariableDeclaration(declaration));assert.equal(declaration.name.getText(source), "count");console.log(declaration.name.getText(source), declaration.initializer.getText(source));Expected stdout (exit 0):
count 7Practice and recap
Collect only identifiers belonging to variable declarations; contrast with collecting every identifier.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Source files and AST traversal — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Source files and AST traversal?
What does workshop 1 (for-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 = 1; function twice(n: number) { return n * 2; }", ts.ScriptTarget.ES2022, true);const names = [];function visit(node) { if (ts.isIdentifier(node)) names.push(node.text); ts.forEachChild(node, visit);}visit(source);assert.deepEqual(names, ["x", "twice", "n", "n"]);console.log(names.join(","));What does workshop 2 (create-source-file) 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 = 7;", ts.ScriptTarget.ES2022, true);const declaration = source.statements[0].declarationList.declarations[0];assert(ts.isVariableDeclaration(declaration));assert.equal(declaration.name.getText(source), "count");console.log(declaration.name.getText(source), declaration.initializer.getText(source));