TypeScript
Integration
Build the working plugin by combining all parts.
Integrated plugin
A working lint plugin combines four pieces: skeleton (PluginCreateInfo → LanguageService), config reading (info.config), diagnostic production (getSemanticDiagnostics) and code fix (getCodeFixesAtPosition). Both walk the AST with ts.forEachChild.
function createPlugin(info: ts.server.PluginCreateInfo): ts.LanguageService {
const ls = info.languageService;
const prefix = info.config?.prefix ?? "[lint]";
return {
...ls,
getSemanticDiagnostics: function(fileName) { /* ... */ },
getCodeFixesAtPosition: function(fileName) { /* ... */ },
};
}
Working plugin test
To test the plugin you create a mock language service: getSemanticDiagnostics and getCodeFixesAtPosition return empty arrays, getProgram().getSourceFile returns the test source. You call the plugin and verify diagnostic and fix counts.
const fakeLs = {
getSemanticDiagnostics: function() { return []; },
getCodeFixesAtPosition: function() { return []; },
getProgram: function() { return { getSourceFile: ... }; },
} as unknown as ts.LanguageService;
Exercise Integrated lint plugin
Write an integrated plugin: takes PluginCreateInfo, returns LanguageService. Read prefix from info.config?.prefix (default "[lint]"). Wrap getSemanticDiagnostics: produce a Warning diagnostic for each VariableDeclaration without a type annotation, message starting with prefix. Wrap getCodeFixesAtPosition: produce a fix with fixName: "add-any" for the same nodes, appending : any. Walk with ts.forEachChild in both. Test: source let x = 1;, prefix "[L]"; print diagnostic count and first fix name as count fixName.
Show example solution
import ts from "typescript";
function createPlugin(info: ts.server.PluginCreateInfo): ts.LanguageService {
const ls = info.languageService;
const prefix: string = info.config?.prefix ?? "[lint]";
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: prefix + " Missing type annotation",
});
}
ts.forEachChild(node, visit);
}
visit(sf);
return [...prior, ...extras];
},
getCodeFixesAtPosition: function(fileName: string, start: number, end: number, errorCodes: readonly number[], formatOptions: ts.FormatCodeSettings, preferences: ts.UserPreferences) {
const prior = ls.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences);
const sf = ls.getProgram().getSourceFile(fileName);
if (!sf) return prior;
const fixes: ts.CodeFixAction[] = [];
function visit(node: ts.Node) {
if (ts.isVariableDeclaration(node) && !node.type) {
fixes.push({
fixName: "add-any",
description: "Add type annotation",
changes: [{
fileName,
textChanges: [{ span: { start: node.name.getEnd(), length: 0 }, newText: ": any" }],
}],
});
}
ts.forEachChild(node, visit);
}
visit(sf);
return [...prior, ...fixes];
},
};
}
const sf = ts.createSourceFile("app.ts", "let x = 1;", ts.ScriptTarget.Latest, true);
const fakeLs = {
getSemanticDiagnostics: function() { return [] as ts.Diagnostic[]; },
getCodeFixesAtPosition: function() { return [] as ts.CodeFixAction[]; },
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: { prefix: "[L]" } } as unknown as ts.server.PluginCreateInfo;
const plugin = createPlugin(info);
const diags = plugin.getSemanticDiagnostics("app.ts");
const fixes = plugin.getCodeFixesAtPosition("app.ts", 0, 1, [], {} as ts.FormatCodeSettings, {} as ts.UserPreferences);
console.log(diags.length + " " + fixes[0].fixName);
Test yourself
-
Which four pieces does a working lint plugin combine?
- PluginCreateInfo + getSemanticDiagnostics + getCodeFixesAtPosition + info.config
- getProgram + getSourceFile + getText + writeFile
- getTypeChecker + writeFile + readFile + getText
- createSourceFile + JSON.stringify + writeFile + console.log
Show answer
PluginCreateInfo + getSemanticDiagnostics + getCodeFixesAtPosition + info.config — PluginCreateInfo is the plugin input, getSemanticDiagnostics produces diagnostics, getCodeFixesAtPosition provides code fixes, and info.config carries configuration. These four form a working plugin.
-
Return the diagnostic list
return [...prior, ...____];
Show answer
return [...prior, ...extras];
-
Integrated plugin parts
- info.config
- Configuration is read
- getSemanticDiagnostics
- Diagnostics are produced
- getCodeFixesAtPosition
- Fixes are produced
- ts.forEachChild
- AST is walked
Next lesson: Challenge 1: Compiler API