Просмотр исходного кода

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

Throw error when constructing invalid ranges

GitOrigin-RevId: 9451e0f8d35372610d08530048e7ee2ca1ff2052
Eric Mc Sween 2 лет назад
Родитель
Сommit
0e54a1078f

+ 6 - 0
libraries/overleaf-editor-core/lib/range.js

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

+ 8 - 0
libraries/overleaf-editor-core/test/range.test.js

@@ -30,6 +30,14 @@ describe('Range', function () {
     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 () {
     it('same ranges should overlap', function () {
       const range1 = new Range(1, 3)