Summary - Transformer
- TypeScript course
- Expert
- Lesson 8/35
- 4 min
Write a logging injector with a transformer.
Logging injector summary
A logging injector transformer combines three pieces: you produce a console.log(...) statement with ts.factory, apply the transformer to the source with ts.transform, and print the transformed source with ts.createPrinter. The injector can add a statement at the top of the file or at the start of each function body.
const result = ts.transform(sf, [transformer]);
const printer = ts.createPrinter();
process.stdout.write(printer.printFile(result.transformed[0]));Practice Logging injector
Create a SourceFile from const x = 1;. Write a transformer that adds a console.log("called") statement at the start of the source file. Produce the statement with ts.factory, apply with ts.transform, and print with ts.createPrinter.
Starter code
import * as ts from "typescript";
// Kaynak: const x = 1;
// SourceFile oluştur
// Bir transformer yaz: kaynak dosyanın başına console.log("called") deyimi ekle
// transform ile uygula
// Printer ile sonucu yazdır
// kodunu buraya yazSample solution
import ts from "typescript";
const sourceText = "const x = 1;";
const sf = ts.createSourceFile("app.ts", sourceText, ts.ScriptTarget.Latest, true);
const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
return (node) => {
const logStmt = ts.factory.createExpressionStatement(
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("console"),
ts.factory.createIdentifier("log")
),
undefined,
[ts.factory.createStringLiteral("called")]
)
);
return ts.factory.updateSourceFile(node, [logStmt, ...node.statements]);
};
};
const result = ts.transform(sf, [transformer]);
const printer = ts.createPrinter();
process.stdout.write(printer.printFile(result.transformed[0] as ts.SourceFile));
result.dispose();Expected output
console.log("called");
const x = 1;
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which trio comes together for a logging injector?
- getSourceFile + getText + console.log
- createSourceFile + JSON.stringify + writeFile
- getTypeChecker + writeFile + readFile
- ts.factory + ts.transform + ts.createPrinter Correct answer
Explanation: You produce the log statement with ts.factory, apply it with ts.transform, and print the result with ts.createPrinter. Together they give a full injector.
2 Create a printer
const result = ts.transform(sf, [transformer]);
const printer = ts.____;
const result = ts.transform(sf, [transformer]);
const printer = ts.createPrinter();
Explanation: ts.createPrinter() returns a Printer object; createPrinter without parentheses is the function itself, and newPrinter and printer do not exist on the factory.
3 Transformer summary
| Term | Meaning |
|---|---|
ts.factory |
Produces AST nodes |
ts.transform |
Applies the transformer |
ts.createPrinter |
Writes a node to text |
visitEachChild |
Transforms child nodes |