Challenge 1: Compiler API
- TypeScript course
- Expert
- Lesson 32/35
- 6 min
Solve an analysis task with the Compiler API.
Analysis with the Compiler API
In this challenge you will analyze a source file with the Compiler API. Parse the source with ts.createSourceFile, walk the AST with ts.forEachChild, and recognize functions with ts.isFunctionDeclaration. You will extract function parameter counts and names.
Practice Sum parameter counts
Create a SourceFile from function a(x: number) {} function b(y: string, z: number) {}. Write a visit function that sums the parameter count of each FunctionDeclaration. Walk with ts.forEachChild and print the total.
Starter code
import * as ts from "typescript";
// Kaynak: function a(x: number) {} function b(y: string, z: number) {}
// SourceFile oluştur
// visit fonksiyonu yaz: FunctionDeclaration bul
// Her fonksiyonun parametre sayısını topla
// forEachChild ile gez
// Toplamı yazdır
// kodunu buraya yazSample solution
import ts from "typescript";
const sourceText = "function a(x: number) {} function b(y: string, z: number) {}";
const sf = ts.createSourceFile("app.ts", sourceText, ts.ScriptTarget.Latest, true);
let total = 0;
function visit(node: ts.Node) {
if (ts.isFunctionDeclaration(node)) {
total += node.parameters.length;
}
ts.forEachChild(node, visit);
}
visit(sf);
console.log(total);Expected output
3
Practice List function names
Create a SourceFile from function topla() {} function cikar() {}. Write a visit function that prints each FunctionDeclaration's name via node.name.text. Walk with ts.forEachChild.
Starter code
import * as ts from "typescript";
// Kaynak: function topla() {} function cikar() {}
// SourceFile oluştur
// visit fonksiyonu yaz: FunctionDeclaration bul
// Her fonksiyonun adını yazdır
// forEachChild ile gez
// kodunu buraya yazSample solution
import ts from "typescript";
const sourceText = "function topla() {} function cikar() {}";
const sf = ts.createSourceFile("app.ts", sourceText, ts.ScriptTarget.Latest, true);
function visit(node: ts.Node) {
if (ts.isFunctionDeclaration(node) && node.name) {
console.log(node.name.text);
}
ts.forEachChild(node, visit);
}
visit(sf);Expected output
topla cikar
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How do you access the parameter count of a FunctionDeclaration?
- node.arguments.length
- node.parameters.length Correct answer
- node.params.length
- node.args.length
Explanation: FunctionDeclaration.parameters is a ReadonlyArray<ParameterDeclaration>; its count is taken with length. params, args or arguments properties do not exist.
2 Get the function name
if (ts.isFunctionDeclaration(node) && node.name) {
console.log(node.name.____);
}
if (ts.isFunctionDeclaration(node) && node.name) {
console.log(node.name.text);
}
Explanation: Identifier.text gives the textual value of the identifier; value, name and key are not properties on Identifier.
3 Compiler API analysis parts
| Term | Meaning |
|---|---|
ts.createSourceFile |
Parses the source |
ts.forEachChild |
Walks the AST |
ts.isFunctionDeclaration |
Recognizes functions |
node.parameters |
Accesses parameters |