{"record":{"id":"840d76f6f7fb313a","repo":"jackwener/OpenCLI","slug":"page-evaluate-string-input-does-not-accept-args-u","errorCode":null,"errorMessage":"page.evaluate string input does not accept args; use page.evaluate(fn, ...args) instead","messagePattern":"page\\.evaluate string input does not accept args; use page\\.evaluate\\(fn, \\.\\.\\.args\\) instead","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/browser/utils.ts","lineNumber":67,"sourceCode":"  // Already an IIFE: `(async () => { ... })()` or `(function() {...})()`\n  if (/^\\([\\s\\S]*\\)\\s*\\(.*\\)\\s*$/.test(code)) return code;\n\n  // Arrow function: `() => ...` or `async () => ...`\n  if (/^(async\\s+)?(\\([^)]*\\)|[A-Za-z_]\\w*)\\s*=>/.test(code)) return `(${code})()`;\n\n  // Function declaration: `function ...` or `async function ...`\n  if (/^(async\\s+)?function[\\s(]/.test(code)) return `(${code})()`;\n\n  // Everything else: bare expression, `new Promise(...)`, etc. → evaluate directly\n  return code;\n}\n\nexport function buildEvaluateExpression(input: string | EvaluateFunction, args: readonly unknown[] = []): string {\n  if (typeof input === 'function') {\n    return serializeFunctionForEval(input, args);\n  }\n  if (args.length > 0) {\n    throw new Error('page.evaluate string input does not accept args; use page.evaluate(fn, ...args) instead');\n  }\n  return wrapForEval(input);\n}\n","sourceCodeStart":49,"sourceCodeEnd":71,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/src/browser/utils.ts#L49-L71","documentation":"buildEvaluateExpression accepts either a function (whose args are serialized into the call) or a pre-written string expression. Strings are treated as complete expressions and the API deliberately refuses to attach arguments to them, because there is no safe way to bind args to an arbitrary string. Passing args with a string input is therefore rejected up front.","triggerScenarios":"Calling buildEvaluateExpression('() => ...', [arg]) or page.evaluate('document.title', someArg) — any string input with args.length > 0.","commonSituations":"Refactoring code from a driver whose evaluate accepted (string, ...args); dynamically building expressions as strings while still passing parameters; copying an old evaluate('fn string', arg) call pattern.","solutions":["Convert the string into a real function and pass args normally: page.evaluate((x) => ..., arg).","If you must keep a string, interpolate the values yourself (safely JSON.stringify them into the expression) and drop the args argument.","Call buildEvaluateExpression(str) with no second argument if the string is already self-contained."],"exampleFix":"// before\nbuildEvaluateExpression('(x) => x + 1', [41]);\n// after\nbuildEvaluateExpression((x) => x + 1, [41]);","handlingStrategy":"validation","validationCode":"function evaluateSafe(input, ...args) {\n  if (typeof input === 'string' && args.length > 0) {\n    throw new TypeError('string evaluate input cannot take args; pass a function instead');\n  }\n  return buildEvaluateExpression(input, args);\n}","typeGuard":"const isEvaluateFunction = (input: string | EvaluateFunction): input is EvaluateFunction => typeof input === 'function';","tryCatchPattern":"try {\n  return buildEvaluateExpression(input, args);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('does not accept args')) {\n    return buildEvaluateExpression(new Function('return (' + input + ')')(), args);\n  }\n  throw err;\n}","preventionTips":["Always pass real functions, not strings, when you need arguments.","If using string expressions, embed parameters yourself before passing.","Audit refactors from other evaluate APIs that accepted (string, ...args)."],"tags":["api-misuse","evaluate","arguments","browser"],"backgroundTag":"invalid-arguments-combination","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}