Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Aug 1, 2023
1 parent f3fe7e2 commit fe2e899
Show file tree
Hide file tree
Showing 10 changed files with 7,489 additions and 121 deletions.
1 change: 1 addition & 0 deletions apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
},
"devDependencies": {
"@total-typescript/error-translation-engine": "workspace:*",
"@total-typescript/error-hint-engine": "workspace:*",
"@types/glob": "^7.2.0",
"@types/mocha": "^9.1.0",
"@types/node": "14.x",
Expand Down
42 changes: 4 additions & 38 deletions apps/vscode/src/humaniseDiagnostic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as vscode from 'vscode';

import {
fillExcerptWithItems,
parseErrors,
} from '@total-typescript/error-translation-engine';
import * as bundleErrors from './bundleErrors.json';
import { findHintsInError } from '@total-typescript/error-hint-engine';
import { Options } from './types';

export const humaniseDiagnostic = (
Expand All @@ -14,47 +10,17 @@ export const humaniseDiagnostic = (
if (diagnostic.source !== 'ts') {
return [];
}
const errors = parseErrors(diagnostic.message);
const hints = findHintsInError(diagnostic.message);

const messageBodies: string[] = [];

errors.forEach((error, index, array) => {
const fullError = (bundleErrors as Record<string, { excerpt: string }>)[
error.code
];

hints.forEach((hint, index, array) => {
const prepended = (str: string) => {
return `- ${str}`;
};

// if (fullError) {
// const { excerpt } = fillExcerptWithItems(
// fullError.excerpt,
// error.parseInfo.items,
// );

// messageBodies.push(
// // TODO: Prettify the excerpt here
// prepended(excerpt),
// );
// } else {
// messageBodies.push(
// // TODO: Prettify the raw error here
// prepended(error.parseInfo.rawError),
// );
// }

// messageBodies.push(
// `'Component' cannot be used as a [JSX component](https://totaltypescript.com/concepts/jsx-component).`,
// `Its return type 'ReactNode' is not a valid JSX element.`,
// `Type 'undefined' is [not assignable to](https://totaltypescript.com/concepts/not-assignable-to) type 'Element | null'.`,
// );
messageBodies.push(prepended(hint));
});

messageBodies.push(
`- The type 'undefined' is [not assignable to](https://totaltypescript.com/concepts/not-assignable-to) type 'Element | null'.`,
'- Only certain types can be used as [JSX elements](https://totaltypescript.com/concepts/jsx-components).',
);

return [new vscode.MarkdownString(messageBodies.join('\n'))];
};
19 changes: 19 additions & 0 deletions packages/error-hint-engine/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @total-typescript/error-translation-engine

## 1.0.3

### Patch Changes

- [#83](https://github.com/mattpocock/ts-error-translator/pull/83) [`6c29b64`](https://github.com/mattpocock/ts-error-translator/commit/6c29b640de019b59fe8c780a8cf139724a6c3efc) Thanks [@eddyw](https://github.com/eddyw)! - Correctly match duplicated matched parameters

## 1.0.2

### Patch Changes

- [#67](https://github.com/mattpocock/ts-error-translator/pull/67) [`dc3d052`](https://github.com/mattpocock/ts-error-translator/commit/dc3d0528b8fa7bd0af38d9f3603d4e836f09e7dd) Thanks [@eddyw](https://github.com/eddyw)! - Add translation for 2761

## 1.0.1

### Patch Changes

- [`d458eab`](https://github.com/mattpocock/ts-error-translator/commit/d458eabd0bd2481867eb69661163de2505411133) Thanks [@mattpocock](https://github.com/mattpocock)! - Tweak to 7006 error
21 changes: 21 additions & 0 deletions packages/error-hint-engine/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@total-typescript/error-hint-engine",
"version": "1.0.3",
"license": "MIT",
"main": "./src/index.ts",
"types": "./src/index.ts",
"private": true,
"scripts": {
"dev": "vitest",
"test": "vitest run"
},
"dependencies": {
"front-matter": "^4.0.2"
},
"devDependencies": {
"typescript": "^4.5.3",
"vitest": "^0.10.0",
"esbuild": "^0.14.38",
"tsconfig": "workspace:*"
}
}
13 changes: 13 additions & 0 deletions packages/error-hint-engine/src/__tests__/engine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';
import { findHintsInError } from '../errorHintEngine';

it('REPL', () => {
const errors = findHintsInError(
`Type 'string' is not assignable to type 'number'.`,
);
expect(errors).toMatchInlineSnapshot(`
[
"[Not Assignable To](https://totaltypescript.com/concepts/assignability): I expected one thing, but you passed another.",
]
`);
});
112 changes: 112 additions & 0 deletions packages/error-hint-engine/src/errorHintEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
export type HintInstruction =
| {
type: 'includes';
includes: string;
hint: string;
}
| {
type: 'regex';
regex: RegExp;
hint: string;
};

export type TransformedHintInstruction = {
type: 'regex';
regex: RegExp;
hint: string;
};

const hintNamespace = (() => {
const hintInstructions: HintInstruction[] = [
{
type: 'includes',
includes: ' not assignable to ',
hint: `[Not Assignable To](https://totaltypescript.com/concepts/assignability): I expected one thing, but you passed another.`,
},
{
type: 'includes',
includes: 'parameter',
hint: `[Parameters](https://totaltypescript.com/concepts/parameters) are the names you give to the variables that a function takes.`,
},
{
type: 'regex',
regex: /(?<!type )argument/gi,
hint: `[Arguments](https://totaltypescript.com/concepts/arguments) are the values you pass to a function when you call it.`,
},
{
type: 'includes',
includes: 'Type argument',
hint: `[Type Arguments](https://totaltypescript.com/concepts/type-arguments) are the types you pass to a generic type, function or class.`,
},
{
type: 'includes',
includes: ' used before its ',
hint: `[Used Before Its](https://totaltypescript.com/concepts/used-before-declared): You tried to use something before you declared it.`,
},
{
type: 'includes',
includes: ` implicitly has an 'any' type`,
hint: `[Implicit Any](https://totaltypescript.com/concepts/implicit-any): I couldn't figure out what type something is, so I defaulted it to \`any\`.`,
},
{
type: 'includes',
includes: `Unused '@ts-expect-error' directive`,
hint: `You used a \`@ts-expect-error\`, but I didn't find any errors on the following line.`,
},
{
type: 'includes',
includes: 'Cannot assign to',
hint: `You're trying to modify something that can't be modified.`,
},
{
type: 'includes',
includes: ' comparable to ',
hint: `You'll usually see this error when you're trying to use 'as' to cast between two types that aren't compatible.`,
},
{
type: 'includes',
includes: `'React' refers to a UMD global`,
hint: 'You either need to import React, or fix the `jsx` property of your `tsconfig.json`. [Full guide](https://www.totaltypescript.com/react-refers-to-a-umd-global).',
},
{
type: 'includes',
includes: `JSX.IntrinsicElements`,
hint: '[JSX.IntrinsicElements](https://www.totaltypescript.com/what-is-jsx-intrinsicelements) is a type that describes the valid HTML elements you can use in JSX.',
},
{
type: 'includes',
includes: `does not exist on type 'JSX.IntrinsicElements'`,
hint: `You're trying to use an HTML element that doesn't exist on React's types.`,
},
];

const transformedHintInstructions: TransformedHintInstruction[] =
hintInstructions.map((instruction) => {
if (instruction.type === 'includes') {
return {
hint: instruction.hint,
type: 'regex',
regex: new RegExp(instruction.includes, 'ig'),
};
}
return instruction;
});

return {
transformedHintInstructions,
};
})();

export const findHintsInError = (err: string): string[] => {
const hints: string[] = [];

hintNamespace.transformedHintInstructions.forEach((instruction) => {
if (instruction.type === 'regex') {
if (instruction.regex.test(err)) {
hints.push(instruction.hint);
}
}
});

return hints;
};
1 change: 1 addition & 0 deletions packages/error-hint-engine/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './errorHintEngine';
Loading

0 comments on commit fe2e899

Please sign in to comment.