From 6b7a4525b4d8633b6520987738ba04a732e1d6b2 Mon Sep 17 00:00:00 2001 From: Gregorio Litenstein Date: Wed, 24 Jul 2024 22:17:56 -0400 Subject: [PATCH] Throw on unmatched closing paren (#286) Co-authored-by: Blake Embrey --- src/index.spec.ts | 12 ++++++++++++ src/index.ts | 4 ++++ 2 files changed, 16 insertions(+) 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++] }); }