From ea00d550fe1527cdb1020594a5a06df459eb780e Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sat, 21 Nov 2020 21:04:58 +0200 Subject: [PATCH] Use comment source location in validation error --- src/parser/ValidationError.ts | 6 ++++-- src/parser/parser.test.ts | 4 +--- src/parser/validate.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/parser/ValidationError.ts b/src/parser/ValidationError.ts index c56501d..a872b16 100644 --- a/src/parser/ValidationError.ts +++ b/src/parser/ValidationError.ts @@ -1,5 +1,7 @@ +import { SourceLocation } from "./tokenizer"; + export class ValidationError extends Error { - constructor(msg: string) { - super(msg); + constructor(msg: string, { row, col }: SourceLocation) { + super(`${msg} at line ${row + 1} char ${col + 1}`); } } diff --git a/src/parser/parser.test.ts b/src/parser/parser.test.ts index c28e4a9..5b4c635 100644 --- a/src/parser/parser.test.ts +++ b/src/parser/parser.test.ts @@ -563,8 +563,6 @@ Interval: 2:00 90% @ 0:00 Find your rythm. @ 3:10 Try to settle in for the effort `), - ).toThrowErrorMatchingInlineSnapshot( - `"Comment \\"@ 190 Try to settle in for the effort\\" has offset outside of interval"`, - ); + ).toThrowErrorMatchingInlineSnapshot(`"Comment offset is larger than interval length at line 5 char 5"`); }); }); diff --git a/src/parser/validate.ts b/src/parser/validate.ts index c5ce2d8..94e8ddb 100644 --- a/src/parser/validate.ts +++ b/src/parser/validate.ts @@ -8,7 +8,7 @@ const isCommentWithinInterval = (comment: Comment, interval: Interval): boolean const validateCommentOffsets = (interval: Interval) => { for (const comment of interval.comments) { if (!isCommentWithinInterval(comment, interval)) { - throw new ValidationError(`Comment "@ ${comment.offset.seconds} ${comment.text}" has offset outside of interval`); + throw new ValidationError(`Comment offset is larger than interval length`, comment.loc); } } };