Merge pull request #17931 from overleaf/em-invalid-ranges

Throw error when constructing invalid ranges

GitOrigin-RevId: 9451e0f8d35372610d08530048e7ee2ca1ff2052
This commit is contained in:
Eric Mc Sween 2024-04-24 11:09:53 -04:00 committed by Copybot
parent b8195f537d
commit 0e54a1078f
2 changed files with 14 additions and 0 deletions

View file

@ -1,10 +1,16 @@
// @ts-check // @ts-check
const OError = require('@overleaf/o-error')
class Range { class Range {
/** /**
* @param {number} pos * @param {number} pos
* @param {number} length * @param {number} length
*/ */
constructor(pos, length) { constructor(pos, length) {
if (pos < 0 || length < 0) {
throw new OError('Invalid range', { pos, length })
}
/** @readonly */ /** @readonly */
this.pos = pos this.pos = pos
/** @readonly */ /** @readonly */

View file

@ -30,6 +30,14 @@ describe('Range', function () {
expect(range0length.isEmpty()).to.be.true expect(range0length.isEmpty()).to.be.true
}) })
it('should not create a range with a negative position', function () {
expect(() => new Range(-1, 10)).to.throw
})
it('should not create a range with a negative length', function () {
expect(() => new Range(0, -2)).to.throw
})
describe('overlaps', function () { describe('overlaps', function () {
it('same ranges should overlap', function () { it('same ranges should overlap', function () {
const range1 = new Range(1, 3) const range1 = new Range(1, 3)