Symbol çözümleme
- TypeScript kursu
- Uzman
- Ders 10/35
- 4 dk
Symbol ile tanım çözümlemeyi öğrenirsin.
Sembol çözümleme
Sembol çözümleme, bir tanımlayıcının (Identifier) hangi tanıma karşılık geldiğini bulmaktır. checker.getSymbolAtLocation(node) bir Identifier düğümü için Symbol | undefined döndürür. Sembol, tanımın adını ve tanım listesini taşır.
const sym = checker.getSymbolAtLocation(id);
if (sym) {
console.log(sym.getName());
console.log(sym.declarations?.length);
}Symbol ve declarations
Bir Symbol'ün .declarations özelliği, sembolün tüm tanımlarını içeren bir dizidir. Çoğu durumda tek tanım vardır; ancak fonksiyon aşırı yüklerinde birden fazla olabilir. symbol.getName() tanımın adını verir.
const sym = checker.getSymbolAtLocation(node.name);
console.log(sym?.declarations?.length); // 1
console.log(sym?.getName()); // "topla"Uygulama Fonksiyon sembolünü çöz
function topla(a: number, b: number): number { return a + b; } kaynağı ile bellekten bir program oluştur (noLib: true). TypeChecker al, visit fonksiyonu yaz: her FunctionDeclaration'ın adının sembolünü getSymbolAtLocation ile çöz ve ad: tanımSayisi biçiminde yazdır. ts.forEachChild ile gez.
Başlangıç kodu
import * as ts from "typescript";
// Kaynak: function topla(a: number, b: number): number { return a + b; }
// host ve program oluştur (noLib: true)
// TypeChecker al
// visit fonksiyonu yaz: FunctionDeclaration bulunca sembolünü çöz
// ad: tanımSayisi biçiminde yazdır
// forEachChild ile gez
// kodunu buraya yazÖrnek çözüm
import ts from "typescript";
const fileName = "app.ts";
const sourceText = "function topla(a: number, b: number): number { return a + b; }";
const host: ts.CompilerHost = {
fileExists: (n) => n === fileName,
readFile: (n) => (n === fileName ? sourceText : undefined),
getSourceFile: (n, lang) =>
n === fileName ? ts.createSourceFile(n, sourceText, lang, true) : undefined,
writeFile: () => {},
getDefaultLibFileName: () => "lib.d.ts",
useCaseSensitiveFileNames: () => true,
getCanonicalFileName: (n) => n,
getCurrentDirectory: () => "",
getNewLine: () => "\n",
};
const program = ts.createProgram([fileName], { noLib: true }, host);
const checker = program.getTypeChecker();
const sf = program.getSourceFile(fileName)!;
function visit(node: ts.Node) {
if (ts.isFunctionDeclaration(node) && node.name) {
const sym = checker.getSymbolAtLocation(node.name);
console.log(`${sym?.getName()}: ${sym?.declarations?.length}`);
}
ts.forEachChild(node, visit);
}
visit(sf);Beklenen çıktı
topla: 1
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 checker.getSymbolAtLocation(id) ne döndürür?
- string
- Type
- Symbol | undefined Doğru cevap
- Declaration[]
Açıklama: getSymbolAtLocation bir Identifier için Symbol | undefined döndürür; tanım yoksa undefined verir. Doğrudan string, Type veya Declaration[] döndürmez.
2 Tanım sayısı
const sym = checker.getSymbolAtLocation(node.name);
console.log(sym?.____?.length);
const sym = checker.getSymbolAtLocation(node.name);
console.log(sym?.declarations?.length);
Açıklama: Symbol.declarations bir Declaration[] döndürür; getDeclarations diye yöntem yoktur, declarationList VariableDeclaration'a özeldir, nodes genel bir özellik değildir.
3 Sembol çözümleme parçaları
| Kavram | Anlamı |
|---|---|
checker.getSymbolAtLocation |
Identifier için Symbol verir |
symbol.getName() |
Tanım adını döndürür |
symbol.declarations |
Tanım dizisini verir |
TypeChecker |
Sembol ve tür çözer |