// A name (any Unicode identifier) or a number literal, read as one token. const WORD = /^(?:[\p{ID_Start}$_][\p{ID_Continue}$‌‍]*|0[xXoObB][\da-fA-F_]+n?|\d[\d_]*(?:\.[\d_]*)?(?:[eE][+-]?\d+)?n?|\.\d[\d_]*(?:[eE][+-]?\d+)?)/u; const REGEX_AFTER_KEYWORD = new Set([ "return", "typeof", "instanceof", "in", "new", "delete", "void", "throw", "case", "do", "else", "yield", "await", ]); /** * The source of a function's parameter list, with comments removed and every string, * template and regular expression literal replaced by `""`, so that none of their * characters can open, close or split the list. */ function parameterListSource(src: string): string | undefined { let out = ""; let depth = 0; // Whether a `/` here starts a regular expression rather than a division. let regexAllowed = true; let i = 0; while (i < src.length) { const ch = src[i]; const next = src[i + 1]; if (ch === "/" && next === "/") { const end = src.indexOf("\n", i); i = end === -1 ? src.length : end; continue; } if (ch === "/" && next === "*") { const end = src.indexOf("*/", i + 2); i = end === -1 ? src.length : end + 2; continue; } if (ch === "'" || ch === '"' || ch === "`") { i = skipQuoted(src, i, ch); if (depth > 0) out += '""'; regexAllowed = false; continue; } if (ch === "/" && regexAllowed) { i = skipRegex(src, i); if (depth > 0) out += '""'; regexAllowed = false; continue; } const word = WORD.exec(src.slice(i, i + 256))?.[0]; if (word) { if (depth > 0) out += word; // A `/` after a name or a number divides, except after a keyword that starts an // expression (`return /x/`). regexAllowed = REGEX_AFTER_KEYWORD.has(word) && !/\.\s*$/.test(src.slice(Math.max(0, i - 64), i)); i += word.length; continue; } if (ch === "(") { if (depth > 0) out += ch; depth++; } else if (ch === ")") { depth--; if (depth === 0) return out; if (depth > 0) out += ch; } else if (depth > 0) { out += ch; } if (!/\s/.test(ch)) { // After a closing bracket or a postfix `++`/`--` a `/` divides; after any other // punctuator (an operator, `(`, `,`, `=`, `[`, `{`) it starts a regular expression. const postfix = (ch === "+" || ch === "-") && src[i - 1] === ch; regexAllowed = !postfix && !/[)\]}]/.test(ch); } i++; } return undefined; } function skipQuoted(src: string, start: number, quote: string): number { let i = start + 1; while (i < src.length && src[i] !== quote) { if (src[i] === "\\") i++; else if (quote !== "`" && src[i] === "\n") break; i++; } return i + 1; } function skipRegex(src: string, start: number): number { let i = start + 1; let inClass = false; while (i < src.length && src[i] !== "\n") { const ch = src[i]; if (ch === "\\") i++; else if (ch === "[") inClass = true; else if (ch === "]") inClass = false; else if (ch === "/" && !inClass) break; i++; } i++; while (i < src.length && /[a-z]/i.test(src[i])) i++; return i; } /** * The names a v1 task job binds its arguments by, in order: the script parser names an * object pattern `anon`, and an array pattern has no name (`""`). * @internal */ export function getParamNames(func: Function): string[] { const paramStr = parameterListSource(func.toString())?.trim(); if (!paramStr) return []; // Split on commas at depth 0 (skip nested parens, brackets, braces) const params: string[] = []; let current = ""; let d = 0; for (const ch of paramStr) { if ("({[".includes(ch)) d++; else if (")}]".includes(ch)) d--; if (ch === "," && d === 0) { params.push(current.trim()); current = ""; } else { current += ch; } } if (current.trim()) params.push(current.trim()); let anon = 0; return params.map((p) => { p = p.replace(/^\.\.\./, ""); if (p.startsWith("{")) return `anon${++anon}`; if (p.startsWith("[")) return ""; p = p.split("=")[0]; // Strip type annotation (e.g. "x: number" -> "x", "x?: string" -> "x") const colonIdx = p.indexOf(":"); if (colonIdx !== -1) p = p.slice(0, colonIdx); return p.replace(/\?$/, "").trim(); }); }