Diagnostic üretimi
- TypeScript kursu
- Uzman
- Ders 28/35
- 5 dk
Özel diagnostic üreticisini eklersin.
Diagnostic yapısı
Bir ts.Diagnostic nesnesi file, start, length, category, code ve messageText alanlarını taşır. Plugin içinde kural ihlali bulunca bu alanlarla bir diagnostic üretirsin.
const diag: ts.Diagnostic = {
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.Warning,
code: 9001,
messageText: "Missing type annotation",
};Diagnostic üretimi
Plugin içinde diagnostic üretmek için bir extras dizisine ts.Diagnostic nesneleri eklersin. Sonra ls'nin prior diagnostics ile birleştirip döndürürsün.
const extras: ts.Diagnostic[] = [];
extras.push({
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.Warning,
code: 9001,
messageText: "Missing type annotation",
});
return [...prior, ...extras];Uygulama Özel diagnostic ekle
Bir plugin yaz: getSemanticDiagnostics'i sar. Önce ls'nin diagnostics'ini al (prior). SourceFile'ı ls.getProgram().getSourceFile(fileName) ile al. AST'yı gez: tür eklemesi olmayan VariableDeclaration düğümleri için bir ts.Diagnostic üret (category: Warning). prior ve extras'ı birleştir. Test: kaynak let x = 1; let y: number = 2;; toplam diagnostic sayısını yazdır.
Başlangıç kodu
import * as ts from "typescript";
// Bir plugin yaz: Semantic diagnostics metodunu sar
// - Önce ls'nin diagnostics'ini al (prior)
// - SourceFile'ı ls.getProgram().getSourceFile ile al
// - AST'yı gez: tür eklemesi olmayan VariableDeclaration bul
// - Her biri için bir Diagnostic üret (category: Warning)
// - prior ve extras'ı birleştir
// Test yaz ve toplam diagnostic sayısını yazdır.
// kodunu buraya yazÖrnek çözüm
import ts from "typescript";
function createPlugin(info: ts.server.PluginCreateInfo): ts.LanguageService {
const ls = info.languageService;
return {
...ls,
getSemanticDiagnostics: function(fileName: string) {
const prior = ls.getSemanticDiagnostics(fileName);
const sf = ls.getProgram().getSourceFile(fileName);
if (!sf) return prior;
const extras: ts.Diagnostic[] = [];
function visit(node: ts.Node) {
if (ts.isVariableDeclaration(node) && !node.type) {
extras.push({
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.Warning,
code: 9001,
messageText: "Missing type annotation",
});
}
ts.forEachChild(node, visit);
}
visit(sf);
return [...prior, ...extras];
},
};
}
const sf = ts.createSourceFile("app.ts", "let x = 1; let y: number = 2;", ts.ScriptTarget.Latest, true);
const fakeLs = {
getSemanticDiagnostics: function() { return [] as ts.Diagnostic[]; },
getProgram: function() { return { getSourceFile: function(n: string) { return n === "app.ts" ? sf : undefined; } } as unknown as ts.Program; },
} as unknown as ts.LanguageService;
const info = { languageService: fakeLs, config: {} } as unknown as ts.server.PluginCreateInfo;
const plugin = createPlugin(info);
const diags = plugin.getSemanticDiagnostics("app.ts");
console.log(diags.length);Beklenen çıktı
1
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 ts.DiagnosticCategory.Warning ne anlama gelir?
- Derleyici hatası
- Bir öneri
- Bilgi mesajı
- Uyarı, derlemeyi durdurmaz Doğru cevap
Açıklama: Warning kategorisi uyarı verir ama derlemeyi durdurmaz; Error durdurur, Suggestion öneridir, Message bilgidir.
2 Diagnostic üret
extras.push({
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.____,
code: 9001,
messageText: "Missing type annotation",
});
extras.push({
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.Warning,
code: 9001,
messageText: "Missing type annotation",
});
Açıklama: Warning kategorisi uyarı verir; Error derlemeyi durdurur, Info ve Fatal DiagnosticCategory üzerinde yoktur.
3 Diagnostic parçaları
| Kavram | Anlamı |
|---|---|
file |
Kaynak dosyayı taşır |
start |
Başlangıç offseti |
category |
Önem derecesi |
messageText |
Mesaj metni |