From 2279946a233c64d7f4c7fa7aa131ecba690ddedf Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Thu, 17 Aug 2023 14:25:59 +0100 Subject: [PATCH] Made it so the error translator only translates errors, not low-priority messages like unused variables. --- .changeset/slow-peaches-teach.md | 5 +++++ apps/vscode/src/bundleErrors.json | 18 ++++++++--------- apps/vscode/src/initDiagnostics.ts | 32 +++++++++++++++++------------- packages/engine/src/parseErrors.ts | 2 -- 4 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 .changeset/slow-peaches-teach.md diff --git a/.changeset/slow-peaches-teach.md b/.changeset/slow-peaches-teach.md new file mode 100644 index 0000000..ec8ca94 --- /dev/null +++ b/.changeset/slow-peaches-teach.md @@ -0,0 +1,5 @@ +--- +'ts-error-translator': minor +--- + +Made it so the error translator only translates errors, not low-priority messages like unused variables. diff --git a/apps/vscode/src/bundleErrors.json b/apps/vscode/src/bundleErrors.json index 26ede44..7c61fd3 100644 --- a/apps/vscode/src/bundleErrors.json +++ b/apps/vscode/src/bundleErrors.json @@ -60,7 +60,7 @@ "code": "2314" }, "2322": { - "body": "I was expecting a type matching A, but instead you passed B.\n", + "body": "I was expecting a type matching '{1}', but instead you passed '{0}'.\n", "code": "2322" }, "2324": { @@ -76,7 +76,7 @@ "code": "2327" }, "2339": { - "body": "You're trying to access '{0}' on an object that doesn't contain it. [Learn more.](https://totaltypescript.com)\n", + "body": "You're trying to access '{0}' on an object that doesn't contain it. [Learn more](https://totaltypescript.com/concepts/property-does-not-exist-on-type).\n", "code": "2339" }, "2344": { @@ -96,7 +96,7 @@ "code": "2352" }, "2353": { - "body": "You can't pass property '{0}' to object '{1}'.\n", + "body": "You can't pass property '{0}' to type '{1}'.\n", "code": "2353" }, "2355": { @@ -112,7 +112,7 @@ "code": "2414" }, "2451": { - "body": "'{0}' has already been declared - you can't declare it again.\n", + "body": "'{0}' has already been declared - you can't declare it again. [Learn more](https://www.totaltypescript.com/cannot-redeclare-block-scoped-variable).\n", "code": "2451" }, "2551": { @@ -128,7 +128,7 @@ "code": "2554" }, "2571": { - "body": "I don't know what type this object is.\n", + "body": "I don't know what type this object is, so I've defaulted it to 'unknown'. [Learn more](https://www.totaltypescript.com/concepts/object-is-of-type-unknown).\n", "code": "2571" }, "2590": { @@ -136,7 +136,7 @@ "code": "2590" }, "2741": { - "body": "You haven't passed all the required properties to '{2}' - '{1}' is missing the '{0}' property\n", + "body": "You haven't passed all the required properties to '{2}' - '{1}' is missing the '{0}' property.\n", "code": "2741" }, "2749": { @@ -164,7 +164,7 @@ "code": "6142" }, "7006": { - "body": "I don't know what type '{0}' is supposed to be, so I've defaulted it to '{1}'. Your tsconfig file says I should throw an error here.\n", + "body": "I don't know what type '{0}' is supposed to be, so I've defaulted it to '{1}'. Your `tsconfig.json` file says I should throw an error here. [Learn more](https://www.totaltypescript.com/tutorials/beginners-typescript/beginner-s-typescript-section/implicit-any-type-error).\n", "code": "7006" }, "7057": { @@ -176,11 +176,11 @@ "code": "7061" }, "8016": { - "body": "You can't use type assertions because this isn't a TypeScript file\n", + "body": "You can't use type assertions because this isn't a TypeScript file.\n", "code": "8016" }, "17004": { - "body": "You can't use JSX yet because you haven't added `jsx` to your `tsconfig.json`.\n", + "body": "You can't use JSX yet because you haven't added `jsx` to your `tsconfig.json`. [Learn more](https://www.totaltypescript.com/cannot-use-jsx-unless-the-jsx-flag-is-provided).\n", "code": "17004" }, "18004": { diff --git a/apps/vscode/src/initDiagnostics.ts b/apps/vscode/src/initDiagnostics.ts index 088f341..fb79bc2 100644 --- a/apps/vscode/src/initDiagnostics.ts +++ b/apps/vscode/src/initDiagnostics.ts @@ -11,14 +11,14 @@ const languages = [ 'astro', ]; +type UriStoreMember = { + range: vscode.Range; + severity: vscode.DiagnosticSeverity; + contents: vscode.MarkdownString[]; +}; + export const initDiagnostics = (context: vscode.ExtensionContext) => { - const uriStore: Record< - vscode.Uri['path'], - { - range: vscode.Range; - contents: vscode.MarkdownString[]; - }[] - > = {}; + const uriStore: Record = {}; const hoverProvider: vscode.HoverProvider = { provideHover: (document, position) => { @@ -28,9 +28,13 @@ export const initDiagnostics = (context: vscode.ExtensionContext) => { return null; } - const itemsInRange = itemsInUriStore.filter((item) => { - return item.range.contains(position); - }); + const itemsInRange = itemsInUriStore + // Only show errors + .filter((item) => item.severity === vscode.DiagnosticSeverity.Error) + .filter((item) => { + return item.range.contains(position); + }); + return itemsInRange[0]; }, }; @@ -51,20 +55,20 @@ export const initDiagnostics = (context: vscode.ExtensionContext) => { e.uris.forEach((uri) => { const diagnostics = vscode.languages.getDiagnostics(uri); - const items: { - range: vscode.Range; - contents: vscode.MarkdownString[]; - }[] = []; + const items: UriStoreMember[] = []; + diagnostics.forEach((diagnostic) => { if (diagnostic.source !== 'ts') { return; } + const humanizedVersion = humaniseDiagnostic(diagnostic); if (humanizedVersion) { items.push({ range: diagnostic.range, contents: humanizedVersion, + severity: diagnostic.severity, }); } }); diff --git a/packages/engine/src/parseErrors.ts b/packages/engine/src/parseErrors.ts index 693f983..2214039 100644 --- a/packages/engine/src/parseErrors.ts +++ b/packages/engine/src/parseErrors.ts @@ -76,8 +76,6 @@ export interface ErrorInfo extends ErrorInfoWithoutImprovedError { } | null; } -export interface ParseErrorsOpts {} - export const parseErrorsWithDb = (db: TsErrorMessageDb, message: string) => { const errorMessageByKey: Record = {};