Skip to content

Commit

Permalink
fix: throw an explicit error when there is nothing to repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
Y authored Feb 26, 2021
1 parent 6fec3ef commit d4ca9c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions assembly/__tests__/regex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ describe("use cases", () => {
expect(match.matches[3]).toBe("com");
});
});

describe("error cases", () => {
it("throws an explicit error when there is nothing to repeat", () => {
expect(() => {
let foo = new RegExp("*m", ""); // eslint-disable-line no-invalid-regexp
}).toThrow("Invalid regular expression: Nothing to repeat");
});
});
4 changes: 4 additions & 0 deletions assembly/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ export class Parser {
nodes.push(this.parseCharacter());
}
} else if (isQuantifier(token)) {
if (nodes.length === 0) {
throw new Error("Invalid regular expression: Nothing to repeat");
}

const expression = nodes.pop();
const quantifier = this.eatToken();
nodes.push(new RepetitionNode(expression, quantifier, this.isGreedy()));
Expand Down

0 comments on commit d4ca9c9

Please sign in to comment.