Detect overlapping comments
This commit is contained in:
parent
5aa11ddb3d
commit
bfae2b12e0
|
|
@ -664,4 +664,30 @@ Interval: 2:00 90%
|
||||||
`),
|
`),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`"Negative comment offset is larger than interval length at line 5 char 5"`);
|
).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"`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,18 @@
|
||||||
import { Workout, Interval } from "../ast";
|
import { Workout, Interval } from "../ast";
|
||||||
import { ValidationError } from "./ValidationError";
|
import { ValidationError } from "./ValidationError";
|
||||||
|
|
||||||
const validateCommentOffsets = (interval: Interval) => {
|
const validateCommentOffsets = ({ comments, duration }: Interval) => {
|
||||||
for (const comment of interval.comments) {
|
for (let i = 0; i < comments.length; i++) {
|
||||||
if (comment.offset.seconds >= interval.duration.seconds) {
|
const comment = comments[i];
|
||||||
|
if (comment.offset.seconds >= duration.seconds) {
|
||||||
throw new ValidationError(`Comment offset is larger than interval length`, comment.loc);
|
throw new ValidationError(`Comment offset is larger than interval length`, comment.loc);
|
||||||
}
|
}
|
||||||
if (comment.offset.seconds < 0) {
|
if (comment.offset.seconds < 0) {
|
||||||
throw new ValidationError(`Negative comment offset is larger than interval length`, comment.loc);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue