Skip to content

Commit

Permalink
refactor(core): imrpove parse error reporintg when multiple tokens ar…
Browse files Browse the repository at this point in the history
…e suggested as solution
  • Loading branch information
matteo-cristino committed May 9, 2024
1 parent 3bbcfc5 commit 3c7f1c5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions pkg/core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export class ParseError extends Error {
* ```
*/
static wrong(have: Token, wantFirst: string, ...wantRest: string[]) {
const wants = [wantFirst, ...wantRest].join(', ');
const wantsColored = [wantFirst, ...wantRest].map((x) => suggestedColor(x)).join(' or ');
const haveRaw = have.raw;
return new ParseError(
`at ${have.lineNo}:${have.start + 1}-${have.end + 1
}\n ${errorColor(haveRaw)} may be ${suggestedColor(wants)}`,
}\n ${errorColor(haveRaw)} may be ${wantsColored}`,
);
}

Expand Down Expand Up @@ -74,15 +74,15 @@ export class ParseError extends Error {
* which means that there exist no prior token.
*/
static missing(prevTokenOrLineNo: Token | number, wantFirst: string, ...wantRest: string[]) {
const wants = [wantFirst, ...wantRest].map((x) => x).join(' ');
const wantsColored = [wantFirst, ...wantRest].map((x) => missingColor(x)).join(', ');
if (typeof prevTokenOrLineNo == 'number') {
const lineNo = prevTokenOrLineNo;
return new ParseError(`at ${lineNo}\n missing one of: ${missingColor(wants)}`);
return new ParseError(`at ${lineNo}\n missing one of: ${wantsColored}`);
}
const token = prevTokenOrLineNo;
return new ParseError(
`at ${token.lineNo}:${token.start + 1}-${token.end + 1
}\n must be followed by one of: ${missingColor(wants)}`,
}\n must be followed by one of: ${wantsColored}`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/core/test/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Error colors:
- ${extraColor('extra words')}
ParseError: at 2:1-9
${errorColor('Gibberish')} may be ${suggestedColor('given, then')}
${errorColor('Gibberish')} may be ${suggestedColor('given')} or ${suggestedColor('then')}
ParseError: at 2:11-17
${errorColor('connect')} may be ${suggestedColor('I')}
Expand Down

0 comments on commit 3c7f1c5

Please sign in to comment.