Skip to content

Commit 6b23c28

Browse files
authored
fix(utils): throw errors for unexpected situations (#197)
1 parent 2361f90 commit 6b23c28

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/utils.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,29 @@ export function fitSpans(
234234
export function findFrontChar(regex: RegExp, index: number, text: string) {
235235
let i = index;
236236
while (!regex.test(text[i])) {
237-
i--;
237+
// istanbul ignore next
238+
if (--i < 0) {
239+
throw new Error(
240+
`Cannot find front char ${regex} from index ${index} in ${JSON.stringify(
241+
text,
242+
)}`,
243+
);
244+
}
238245
}
239246
return i;
240247
}
241248

242249
export function findBackChar(regex: RegExp, index: number, text: string) {
243250
let i = index;
244251
while (!regex.test(text[i])) {
245-
i++;
252+
// istanbul ignore next
253+
if (++i >= text.length) {
254+
throw new Error(
255+
`Cannot find back char ${regex} from index ${index} in ${JSON.stringify(
256+
text,
257+
)}`,
258+
);
259+
}
246260
}
247261
return i;
248262
}

0 commit comments

Comments
 (0)