Skip to content

Commit

Permalink
fix endIndex calculation in parseError
Browse files Browse the repository at this point in the history
  • Loading branch information
dmmulroy committed Feb 27, 2024
1 parent 4790d2f commit c6ad317
Show file tree
Hide file tree
Showing 3 changed files with 918 additions and 854 deletions.
21 changes: 18 additions & 3 deletions packages/engine/src/__tests__/engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { on } from 'events';
import { describe, expect, it } from 'vitest';
import { fillBodyWithItems } from '../getImprovedMessage';
import { parseErrors, parseErrorsWithDb } from '../parseErrors';
Expand Down Expand Up @@ -27,7 +28,7 @@ describe('parseErrors', () => {
"code": 2707,
"error": "Generic type '{0}' requires between {1} and {2} type arguments.",
"parseInfo": {
"endIndex": 7,
"endIndex": 65,
"items": [
"T",
"5",
Expand All @@ -41,7 +42,7 @@ describe('parseErrors', () => {
"code": 2739,
"error": "Type '{0}' is missing the following properties from type '{1}': {2}",
"parseInfo": {
"endIndex": 72,
"endIndex": 142,
"items": [
"B",
"A",
Expand All @@ -55,7 +56,7 @@ describe('parseErrors', () => {
"code": 2749,
"error": "'{0}' refers to a value, but is being used as a type here. Did you mean 'typeof {0}'?",
"parseInfo": {
"endIndex": 149,
"endIndex": 230,
"items": [
"T",
],
Expand Down Expand Up @@ -165,6 +166,20 @@ describe('parseErrors', () => {

expect(result[0].parseInfo.items).toEqual([]);
});

it('should properly calculate the endIndex as the startIndex + the length of the match', () => {
const input = `Type '{ foo: number; }' is not assignable to type '{ foo: string; }'.
Types of property 'foo' are incompatible.
Type 'number' is not assignable to type 'string'.`;

const errors = parseErrors(input);
expect(errors.length).toEqual(3);
const thirdError = errors[2];
const { startIndex } = thirdError.parseInfo;
const { endIndex } = thirdError.parseInfo;
expect(startIndex).toEqual(124);
expect(endIndex).toEqual(startIndex + thirdError.parseInfo.rawError.length);
});
});

describe('fillBodyWithItems', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/engine/src/parseErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export const parseErrorsWithDb = (db: TsErrorMessageDb, message: string) => {

if (match) {
match.forEach((matchElem) => {
const startIndex = message.indexOf(matchElem);
const endIndex = startIndex ?? 0 + matchElem.length;
const startIndex = message.indexOf(matchElem) ?? 0;
const endIndex = startIndex + matchElem.length;
const key = `${startIndex}_${endIndex}`;

const items = associateMatchedParameters(
Expand Down
Loading

0 comments on commit c6ad317

Please sign in to comment.