Skip to content

Commit

Permalink
First draft of the language service plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
raqystyle committed Feb 7, 2023
1 parent fe2c1d3 commit 1d7332c
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
"typescript.tsc.autoDetect": "off",
"typescript.tsserver.log": "normal"
}
Empty file.
11 changes: 11 additions & 0 deletions packages/language-service-plugin/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Language Service Plugin

[Reference](https://github.com/microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin)

To test this plugin out, compile the code and open the `example` folder in a VSCode instance:

```
code ./example
```

Also, please, make sure the workspace version of TypeScript is used. Use the `TypeScript: Select TypeScript version` command for this.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsserver.log": "normal",
"typescript.tsdk": "node_modules/typescript/lib"
}
9 changes: 9 additions & 0 deletions packages/language-service-plugin/example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let a;

export function A(b: number): number {
if (true) {
return 1;
} {
3;
}
}
8 changes: 8 additions & 0 deletions packages/language-service-plugin/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@total-typescript/language-service-plugin-example",
"version": "0.1.0",
"dependencies": {
"ts-error-translator-tssplugin": "file:..",
"typescript": "^4.8.3"
}
}
7 changes: 7 additions & 0 deletions packages/language-service-plugin/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"plugins": [{
"name": "ts-error-translator-tssplugin"
}]
}
}
16 changes: 16 additions & 0 deletions packages/language-service-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ts-error-translator-tssplugin",
"version": "0.1.0",
"license": "MIT",
"main": "./out/index.js",
"private": true,
"files": ["./out/**"],
"scripts": {
"dev": "tsc --watch",
"build": "tsc"
},
"devDependencies": {
"tsconfig": "workspace:*",
"typescript": "^4.5.3"
}
}
32 changes: 32 additions & 0 deletions packages/language-service-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function init(modules: { typescript: typeof import("typescript/lib/tsserverlibrary") }) {
const ts = modules.typescript;

function enrichDiagnostic(diagnostic: ts.Diagnostic): ts.Diagnostic {
// diagnostic.code: number
// diagnostic.category === ts.DiagnosticCategory (Error, Message, Suggestion, Warning)
diagnostic.messageText = `${diagnostic.messageText}\n\nHello from plugin!\n`
return diagnostic;
}

function create(info: ts.server.PluginCreateInfo) {
// Set up decorator object
const proxy: ts.LanguageService = Object.create(null);

for (let k of Object.keys(info.languageService) as Array<keyof ts.LanguageService>) {
const x = info.languageService[k]!;
// @ts-expect-error - JS runtime trickery which is tricky to type tersely
proxy[k] = (...args: Array<{}>) => x.apply(info.languageService, args);
}

proxy.getSemanticDiagnostics = (filename) => {
return info.languageService.getSemanticDiagnostics(filename).map(enrichDiagnostic);
}

return proxy;
}

return { create };

}

export = init;
19 changes: 19 additions & 0 deletions packages/language-service-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"module": "commonjs",
"target": "ES2019",
"outDir": "./out",
"rootDir": "src",
"strict": true,
"declaration": true,
"sourceMap": true,
"skipLibCheck": true
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
118 changes: 70 additions & 48 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
- 'packages/*'
- 'packages/language-service-plugin/*'
- 'apps/*'

0 comments on commit 1d7332c

Please sign in to comment.