diff --git a/src/index.spec.ts b/src/index.spec.ts index 73f2c78..b51dd9b 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -31,6 +31,18 @@ describe("path-to-regexp", () => { ); }); + it("should throw on unmatched )", function () { + expect(() => match("/:fooab)c")).toThrow( + new TypeError("Unmatched ) at 7: https://git.new/pathToRegexpError"), + ); + }); + + it("should throw on unmatched ) after other patterns", function () { + expect(() => match("/:test(\\w+)/:foo(\\d+))")).toThrow( + new TypeError("Unmatched ) at 21: https://git.new/pathToRegexpError"), + ); + }); + it("should throw on missing pattern", () => { expect(() => match("/:foo()")).toThrow( new TypeError( diff --git a/src/index.ts b/src/index.ts index ec4fad1..0d8bc53 100644 --- a/src/index.ts +++ b/src/index.ts @@ -199,6 +199,10 @@ function lexer(str: string) { continue; } + if (value === ")") { + throw new TypeError(`Unmatched ) at ${i}: ${DEBUG_URL}`); + } + tokens.push({ type: "CHAR", index: i, value: chars[i++] }); }