Skip to content

Commit 9a78879

Browse files
authored
Error on trailing backslash (#434)
1 parent 7f05876 commit 9a78879

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/index.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
* Dynamically generate the entire test suite.
2020
*/
2121
describe("path-to-regexp", () => {
22-
describe("ParseError", () => {
22+
describe("PathError", () => {
2323
it("should contain original path and debug url", () => {
2424
const error = new PathError(
2525
"Unexpected end at index 7, expected }",
@@ -77,6 +77,36 @@ describe("path-to-regexp", () => {
7777
new PathError("Unterminated quote at index 2", '/:"foo'),
7878
);
7979
});
80+
81+
it("should throw on eol backslash", () => {
82+
expect(() => parse("/foo\\")).toThrow(
83+
new PathError("Unexpected end after \\ at index 5", "/foo\\"),
84+
);
85+
});
86+
87+
it("should throw on eol backslash in group", () => {
88+
expect(() => parse("/foo/{bar\\")).toThrow(
89+
new PathError("Unexpected end after \\ at index 10", "/foo/{bar\\"),
90+
);
91+
});
92+
93+
it("should throw on eol backslash after param", () => {
94+
expect(() => parse("/foo/:bar\\")).toThrow(
95+
new PathError("Unexpected end after \\ at index 10", "/foo/:bar\\"),
96+
);
97+
});
98+
99+
it("should throw on eol backslash after wildcard", () => {
100+
expect(() => parse("/foo/*bar\\")).toThrow(
101+
new PathError("Unexpected end after \\ at index 10", "/foo/*bar\\"),
102+
);
103+
});
104+
105+
it("should throw on eol backslash in quoted param", () => {
106+
expect(() => parse('/foo/:"bar\\')).toThrow(
107+
new PathError("Unterminated quote at index 6", '/foo/:"bar\\'),
108+
);
109+
});
80110
});
81111

82112
describe("compile errors", () => {

src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ type TokenType =
6464
| "wildcard"
6565
| "param"
6666
| "char"
67-
| "escape"
6867
| "end"
6968
// Reserved for use or ambiguous due to past use.
7069
| "("
@@ -220,10 +219,14 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
220219
while (index < chars.length) {
221220
const value = chars[index++];
222221

223-
if (SIMPLE_TOKENS.includes(value)) {
222+
if (value === "\\") {
223+
if (index === chars.length) {
224+
throw new PathError(`Unexpected end after \\ at index ${index}`, str);
225+
}
226+
227+
tokens.push({ type: "char", index, value: chars[index++] });
228+
} else if (SIMPLE_TOKENS.includes(value)) {
224229
tokens.push({ type: value as TokenType, index, value });
225-
} else if (value === "\\") {
226-
tokens.push({ type: "escape", index, value: chars[index++] });
227230
} else if (value === ":") {
228231
tokens.push({ type: "param", index, value: name() });
229232
} else if (value === "*") {
@@ -242,11 +245,11 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
242245
const token = tokens[pos++];
243246
if (token.type === endType) break;
244247

245-
if (token.type === "char" || token.type === "escape") {
248+
if (token.type === "char") {
246249
let path = token.value;
247250
let cur = tokens[pos];
248251

249-
while (cur.type === "char" || cur.type === "escape") {
252+
while (cur.type === "char") {
250253
path += cur.value;
251254
cur = tokens[++pos];
252255
}

0 commit comments

Comments
 (0)