Skip to content

Commit a9909e4

Browse files
committed
Allow parameter names to be quoted
1 parent 0e3b169 commit a9909e4

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/index.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const DEFAULT_DELIMITER = "/";
22
const NOOP_VALUE = (value: string) => value;
3-
const ID_CHAR = /^\p{XID_Continue}$/u;
3+
const ID_START = /^[$_\p{ID_Start}]$/u;
4+
const ID_CONTINUE = /^[$\u200c\u200d\p{ID_Continue}]$/u;
45
const DEBUG_URL = "https://git.new/pathToRegexpError";
56

67
/**
@@ -144,12 +145,35 @@ function lexer(str: string) {
144145
if (value === ":") {
145146
let name = "";
146147

147-
while (ID_CHAR.test(chars[++i])) {
148+
if (ID_START.test(chars[++i])) {
148149
name += chars[i];
150+
while (ID_CONTINUE.test(chars[++i])) {
151+
name += chars[i];
152+
}
153+
} else if (chars[i] === '"') {
154+
let pos = i;
155+
156+
while (i < chars.length) {
157+
if (chars[++i] === '"') {
158+
i++;
159+
pos = 0;
160+
break;
161+
}
162+
163+
if (chars[i] === "\\") {
164+
name += chars[++i];
165+
} else {
166+
name += chars[i];
167+
}
168+
}
169+
170+
if (pos) {
171+
throw new TypeError(`Unterminated quote at ${pos}: ${DEBUG_URL}`);
172+
}
149173
}
150174

151175
if (!name) {
152-
throw new TypeError(`Missing parameter name at ${i}`);
176+
throw new TypeError(`Missing parameter name at ${i}: ${DEBUG_URL}`);
153177
}
154178

155179
tokens.push({ type: "NAME", index: i, value: name });

0 commit comments

Comments
 (0)