Skip to content

Commit

Permalink
package the plugin for distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
raqystyle committed Feb 7, 2023
1 parent 46ecaa7 commit 57fbc9c
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 24 deletions.
26 changes: 18 additions & 8 deletions packages/language-service-plugin/example/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
let a;

export function A(b: number): number {
if (true) {
return 1;
} {
3;
}
}
return b;
}
A("test")


let a: { m: number[] };
let b = { m: [""] };
a = b;

// Extra Properties

type A = { m: number };
const w: A = { m: 10, n: "" };

// Union Assignments

type Thing = "none" | { name: string };
const e: Thing = { name: 0 };
2 changes: 1 addition & 1 deletion packages/language-service-plugin/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"version": "0.1.0",
"dependencies": {
"ts-error-translator-tssplugin": "file:..",
"typescript": "^4.8.3"
"typescript": "^4.5.3"
}
}
14 changes: 12 additions & 2 deletions packages/language-service-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
"license": "MIT",
"main": "./out/index.js",
"private": true,
"files": ["./out/**"],
"files": [
"./out/**"
],
"scripts": {
"dev": "tsc --watch",
"build": "tsc"
"build": "rollup -c rollup.config.mjs"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^11.0.0",
"rollup": "^3.14.0",
"tsconfig": "workspace:*",
"typescript": "^4.5.3"
},
"dependencies": {
"@total-typescript/error-translation-engine": "workspace:*"
}
}
21 changes: 21 additions & 0 deletions packages/language-service-plugin/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

export default {
input: 'src/index.ts',
output: {
file: 'out/index.js',
sourcemap: true,
format: 'cjs'
},
plugins: [
nodeResolve({
extensions: ['.mjs', '.js', '.json', '.node', '.ts']
}),
commonjs({ extensions: ['.js', '.ts'] }),
typescript(),
json()
]
};
31 changes: 26 additions & 5 deletions packages/language-service-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
function init(modules: { typescript: typeof import("typescript/lib/tsserverlibrary") }) {
import { parseErrors } from '@total-typescript/error-translation-engine';

export default function init(modules: { typescript: typeof import("typescript/lib/tsserverlibrary") }) {
const ts = modules.typescript;

function withParsedError(messageText: string): string {
const parsed = parseErrors(messageText);
const allMessages = parsed.map(err => err.error).join('\n\n');
return `${messageText}\n\nIn other words,\n${allMessages}\n`;
}

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`
if (typeof diagnostic.messageText === 'string') {
diagnostic.messageText = withParsedError(diagnostic.messageText);
return diagnostic;
}
if (diagnostic.messageText.category === ts.DiagnosticCategory.Error) {
const msg = withParsedError(diagnostic.messageText.messageText);
diagnostic.messageText.messageText = msg;

const nextMesgs = diagnostic.messageText.next;
if (nextMesgs) {
for (let i = 0; i < nextMesgs.length; i++) {
let nextMsg = nextMesgs[i];
nextMesgs[i].messageText = withParsedError(nextMsg.messageText);
}
}
}
return diagnostic;
}

Expand All @@ -29,4 +50,4 @@ function init(modules: { typescript: typeof import("typescript/lib/tsserverlibra

}

export = init;
// export = init;
11 changes: 5 additions & 6 deletions packages/language-service-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"module": "commonjs",
"target": "ES2019",
"module": "ESNext",
"target": "ESNext",
"outDir": "./out",
"rootDir": "src",
"strict": true,
"declaration": true,
"sourceMap": true,
"skipLibCheck": true
"sourceMap": true
},
"include": [
"src"
"src/**/*"
],
"exclude": [
"node_modules"
"node_modules", "out"
]
}
Loading

0 comments on commit 57fbc9c

Please sign in to comment.