Detect overlapping comments

This commit is contained in:
Rene Saarsoo 2020-12-26 17:31:01 +02:00
parent 5aa11ddb3d
commit bfae2b12e0
2 changed files with 33 additions and 3 deletions

View File

@ -664,4 +664,30 @@ Interval: 2:00 90%
`),
).toThrowErrorMatchingInlineSnapshot(`"Negative comment offset is larger than interval length at line 5 char 5"`);
});
it("throws error when comment offset is the same as another comment offset", () => {
expect(() =>
parse(`
Name: My Workout
Interval: 2:00 90%
@ 0:00 First comment
@ 1:00 Comment
@ 1:00 Overlapping comment
@ 1:50 Last comment
`),
).toThrowErrorMatchingInlineSnapshot(`"Comment overlaps previous comment at line 6 char 5"`);
});
it("throws error when comment offset is greater than next comment offset", () => {
expect(() =>
parse(`
Name: My Workout
Interval: 2:00 90%
@ 0:00 First comment
@ 1:20 Comment
@ 1:00 Misplaced comment
@ 1:50 Last comment
`),
).toThrowErrorMatchingInlineSnapshot(`"Comment overlaps previous comment at line 6 char 5"`);
});
});

View File

@ -1,14 +1,18 @@
import { Workout, Interval } from "../ast";
import { ValidationError } from "./ValidationError";
const validateCommentOffsets = (interval: Interval) => {
for (const comment of interval.comments) {
if (comment.offset.seconds >= interval.duration.seconds) {
const validateCommentOffsets = ({ comments, duration }: Interval) => {
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
if (comment.offset.seconds >= 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);
}
if (i > 0 && comment.offset.seconds <= comments[i - 1].offset.seconds) {
throw new ValidationError(`Comment overlaps previous comment`, comment.loc);
}
}
};