diff --git a/src/parser/parser.test.ts b/src/parser/parser.test.ts index 20ea7e3..c1a9894 100644 --- a/src/parser/parser.test.ts +++ b/src/parser/parser.test.ts @@ -653,4 +653,15 @@ Interval: 2:00 90% `), ).toThrowErrorMatchingInlineSnapshot(`"Comment offset is larger than interval length at line 5 char 5"`); }); + + it("throws error when negative comment offset is outside of interval", () => { + expect(() => + parse(` +Name: My Workout +Interval: 2:00 90% + @ 0:00 Find your rythm. + @ -3:10 Try to settle in for the effort +`), + ).toThrowErrorMatchingInlineSnapshot(`"Negative 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 94e8ddb..0830450 100644 --- a/src/parser/validate.ts +++ b/src/parser/validate.ts @@ -1,15 +1,14 @@ -import { Workout, Interval, Comment } from "../ast"; +import { Workout, Interval } from "../ast"; import { ValidationError } from "./ValidationError"; -const isCommentWithinInterval = (comment: Comment, interval: Interval): boolean => { - return comment.offset.seconds < interval.duration.seconds; -}; - const validateCommentOffsets = (interval: Interval) => { for (const comment of interval.comments) { - if (!isCommentWithinInterval(comment, interval)) { + if (comment.offset.seconds >= interval.duration.seconds) { throw new ValidationError(`Comment offset is larger than interval length`, comment.loc); } + if (comment.offset.seconds < 0) { + throw new ValidationError(`Negative comment offset is larger than interval length`, comment.loc); + } } };