Skip to content

Commit 1d7332c

Browse files
committed
First draft of the language service plugin
1 parent fe2c1d3 commit 1d7332c

File tree

12 files changed

+179
-49
lines changed

12 files changed

+179
-49
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"out": true // set this to false to include "out" folder in search results
88
},
99
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
10-
"typescript.tsc.autoDetect": "off"
10+
"typescript.tsc.autoDetect": "off",
11+
"typescript.tsserver.log": "normal"
1112
}

packages/language-service-plugin/CHANGELOG.md

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Language Service Plugin
2+
3+
[Reference](https://github.com/microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin)
4+
5+
To test this plugin out, compile the code and open the `example` folder in a VSCode instance:
6+
7+
```
8+
code ./example
9+
```
10+
11+
Also, please, make sure the workspace version of TypeScript is used. Use the `TypeScript: Select TypeScript version` command for this.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"typescript.tsserver.log": "normal",
3+
"typescript.tsdk": "node_modules/typescript/lib"
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let a;
2+
3+
export function A(b: number): number {
4+
if (true) {
5+
return 1;
6+
} {
7+
3;
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@total-typescript/language-service-plugin-example",
3+
"version": "0.1.0",
4+
"dependencies": {
5+
"ts-error-translator-tssplugin": "file:..",
6+
"typescript": "^4.8.3"
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"plugins": [{
4+
"name": "ts-error-translator-tssplugin"
5+
}]
6+
}
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ts-error-translator-tssplugin",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"main": "./out/index.js",
6+
"private": true,
7+
"files": ["./out/**"],
8+
"scripts": {
9+
"dev": "tsc --watch",
10+
"build": "tsc"
11+
},
12+
"devDependencies": {
13+
"tsconfig": "workspace:*",
14+
"typescript": "^4.5.3"
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function init(modules: { typescript: typeof import("typescript/lib/tsserverlibrary") }) {
2+
const ts = modules.typescript;
3+
4+
function enrichDiagnostic(diagnostic: ts.Diagnostic): ts.Diagnostic {
5+
// diagnostic.code: number
6+
// diagnostic.category === ts.DiagnosticCategory (Error, Message, Suggestion, Warning)
7+
diagnostic.messageText = `${diagnostic.messageText}\n\nHello from plugin!\n`
8+
return diagnostic;
9+
}
10+
11+
function create(info: ts.server.PluginCreateInfo) {
12+
// Set up decorator object
13+
const proxy: ts.LanguageService = Object.create(null);
14+
15+
for (let k of Object.keys(info.languageService) as Array<keyof ts.LanguageService>) {
16+
const x = info.languageService[k]!;
17+
// @ts-expect-error - JS runtime trickery which is tricky to type tersely
18+
proxy[k] = (...args: Array<{}>) => x.apply(info.languageService, args);
19+
}
20+
21+
proxy.getSemanticDiagnostics = (filename) => {
22+
return info.languageService.getSemanticDiagnostics(filename).map(enrichDiagnostic);
23+
}
24+
25+
return proxy;
26+
}
27+
28+
return { create };
29+
30+
}
31+
32+
export = init;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"extends": "tsconfig/base.json",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"target": "ES2019",
6+
"outDir": "./out",
7+
"rootDir": "src",
8+
"strict": true,
9+
"declaration": true,
10+
"sourceMap": true,
11+
"skipLibCheck": true
12+
},
13+
"include": [
14+
"src"
15+
],
16+
"exclude": [
17+
"node_modules"
18+
]
19+
}

0 commit comments

Comments
 (0)