Skip to content

Commit d4ca9c9

Browse files
author
Y
authored
fix: throw an explicit error when there is nothing to repeat
1 parent 6fec3ef commit d4ca9c9

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

assembly/__tests__/regex.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,11 @@ describe("use cases", () => {
155155
expect(match.matches[3]).toBe("com");
156156
});
157157
});
158+
159+
describe("error cases", () => {
160+
it("throws an explicit error when there is nothing to repeat", () => {
161+
expect(() => {
162+
let foo = new RegExp("*m", ""); // eslint-disable-line no-invalid-regexp
163+
}).toThrow("Invalid regular expression: Nothing to repeat");
164+
});
165+
});

assembly/parser/parser.ts

+4
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ export class Parser {
251251
nodes.push(this.parseCharacter());
252252
}
253253
} else if (isQuantifier(token)) {
254+
if (nodes.length === 0) {
255+
throw new Error("Invalid regular expression: Nothing to repeat");
256+
}
257+
254258
const expression = nodes.pop();
255259
const quantifier = this.eatToken();
256260
nodes.push(new RepetitionNode(expression, quantifier, this.isGreedy()));

0 commit comments

Comments
 (0)