TypeScript
Diagnostic üretimi
Ö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.
Örnek çözümü göster
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);
Kendini test et
-
ts.DiagnosticCategory.Warning ne anlama gelir?
- Derleyici hatası
- Bir öneri
- Bilgi mesajı
- Uyarı, derlemeyi durdurmaz
Cevabı göster
Uyarı, derlemeyi durdurmaz — Warning kategorisi uyarı verir ama derlemeyi durdurmaz; Error durdurur, Suggestion öneridir, Message bilgidir.
-
Diagnostic üret
extras.push({
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.____,
code: 9001,
messageText: "Missing type annotation",
});
Cevabı göster
extras.push({
file: sf,
start: node.getStart(sf),
length: node.getWidth(sf),
category: ts.DiagnosticCategory.Warning,
code: 9001,
messageText: "Missing type annotation",
});
-
Diagnostic parçaları
- file
- Kaynak dosyayı taşır
- start
- Başlangıç offseti
- category
- Önem derecesi
- messageText
- Mesaj metni
Sıradaki ders: Code fix