Meydan okuma 2: Transformer
- TypeScript kursu
- Uzman
- Ders 33/35
- 6 dk
Kendi transformer'ını gerektiren bir görev çözersin.
Transformer ile kod dönüştürme
Bu meydan okumada kendi transformer'ını yazacaksın. ts.factory ile yeni düğümler üret, ts.visitEachChild ile alt düğümleri dönüştür, ts.transform ile uygula. Belirli düğüm türlerini (TrueKeyword, NumericLiteral) değiştireceksin.
Uygulama true'yu false ile değiştir
let a = true; let b = true; kaynağından bir SourceFile oluştur. Bir transformer yaz: TrueKeyword düğümlerini ts.factory.createFalse() ile değiştir. ts.visitEachChild ile alt düğümlere in. ts.transform ile uygula ve ts.createPrinter ile sonucu yazdır.
Başlangıç kodu
import * as ts from "typescript";
// Kaynak: let a = true; let b = true;
// SourceFile oluştur
// Bir transformer yaz: TrueKeyword düğümlerini FalseKeyword ile değiştir
// False düğümü üretmek için factory kullan
// transform ile uygula
// Printer ile sonucu yazdır
// kodunu buraya yazÖrnek çözüm
import ts from "typescript";
const sourceText = "let a = true; let b = true;";
const sf = ts.createSourceFile("app.ts", sourceText, ts.ScriptTarget.Latest, true);
const transformer: ts.TransformerFactory<ts.SourceFile> = function(context: ts.TransformationContext) {
return function visit(node: ts.Node): ts.Node {
if (node.kind === ts.SyntaxKind.TrueKeyword) {
return ts.factory.createFalse();
}
return ts.visitEachChild(node, visit, context);
};
};
const result = ts.transform(sf, [transformer]);
const printer = ts.createPrinter();
process.stdout.write(printer.printFile(result.transformed[0] as ts.SourceFile));
result.dispose();Beklenen çıktı
let a = false; let b = false;
Uygulama Sayıları 0 ile değiştir
let a = 5; let b = 10; let c = 15; kaynağından bir SourceFile oluştur. Bir transformer yaz: NumericLiteral düğümlerini ts.factory.createNumericLiteral("0") ile değiştir. ts.visitEachChild ile alt düğümlere in. ts.transform ile uygula ve ts.createPrinter ile sonucu yazdır.
Başlangıç kodu
import * as ts from "typescript";
// Kaynak: let a = 5; let b = 10; let c = 15;
// SourceFile oluştur
// Bir transformer yaz: NumericLiteral düğümlerini 0 ile değiştir
// Sayısal düğüm üretmek için factory kullan
// transform ile uygula
// Printer ile sonucu yazdır
// kodunu buraya yazÖrnek çözüm
import ts from "typescript";
const sourceText = "let a = 5; let b = 10; let c = 15;";
const sf = ts.createSourceFile("app.ts", sourceText, ts.ScriptTarget.Latest, true);
const transformer: ts.TransformerFactory<ts.SourceFile> = function(context: ts.TransformationContext) {
return function visit(node: ts.Node): ts.Node {
if (ts.isNumericLiteral(node)) {
return ts.factory.createNumericLiteral("0");
}
return ts.visitEachChild(node, visit, context);
};
};
const result = ts.transform(sf, [transformer]);
const printer = ts.createPrinter();
process.stdout.write(printer.printFile(result.transformed[0] as ts.SourceFile));
result.dispose();Beklenen çıktı
let a = 0; let b = 0; let c = 0;
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 Bir transformer'da alt düğümleri dönüştürmek için hangi metod kullanılır?
- ts.forEachChild
- ts.mapChildren
- ts.transformChildren
- ts.visitEachChild Doğru cevap
Açıklama: ts.visitEachChild alt düğümleri visitor ile dönüştürür ve yeni üst düğüm döndürür; forEachChild salt okur gezer, transformChildren ve mapChildren diye metod yoktur.
2 False düğümü üret
if (node.kind === ts.SyntaxKind.TrueKeyword) {
return ts.factory.____();
}
if (node.kind === ts.SyntaxKind.TrueKeyword) {
return ts.factory.createFalse();
}
Açıklama: ts.factory.createFalse bir BooleanLiteral (FalseKeyword) düğümü üretir; createBoolean ve createFalseKeyword factory'de yoktur, createLiteral farklı bir API'dir.
3 Transformer parçaları
| Kavram | Anlamı |
|---|---|
ts.transform |
Transformer'ı uygular |
ts.factory |
Düğüm üretir |
ts.visitEachChild |
Alt düğümleri dönüştürür |
ts.SyntaxKind |
Düğüm türünü verir |