{"record":{"id":"b764e03826611fa2","repo":"different-ai/openwork","slug":"cannot-access-name-before-initialization","errorCode":null,"errorMessage":"Cannot access '${name}' before initialization.","messagePattern":"Cannot access '(.+?)' before initialization\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/interpreter/runtime.ts","lineNumber":3281,"sourceCode":"    // anything else already present is a genuine duplicate declaration.\n    const existing = scope.get(name)\n    if (existing && existing.initialized !== false) {\n      throw new InterpreterRuntimeError(`Identifier '${name}' has already been declared.`, node)\n    }\n\n    scope.set(name, { mutable, value, initialized: true })\n  }\n\n  private getIdentifierValue(name: string, node: AstNode): unknown {\n    const binding = this.resolveBinding(name)\n\n    if (!binding) {\n      throw new InterpreterRuntimeError(`Unknown identifier '${name}'.`, node).as(\"ReferenceError\")\n    }\n\n    // A parameter default that forward-references a later (not-yet-bound) parameter - JS TDZ.\n    if (binding.initialized === false) {\n      throw new InterpreterRuntimeError(`Cannot access '${name}' before initialization.`, node).as(\"ReferenceError\")\n    }\n\n    return binding.value\n  }\n\n  private setIdentifierValue(name: string, value: unknown, node: AstNode): unknown {\n    const binding = this.resolveBinding(name)\n\n    if (!binding) {\n      throw new InterpreterRuntimeError(`Unknown identifier '${name}'.`, node).as(\"ReferenceError\")\n    }\n\n    if (!binding.mutable) {\n      throw new InterpreterRuntimeError(`Cannot assign to constant '${name}'.`, node).as(\"TypeError\")\n    }\n\n    binding.value = value\n    return value","sourceCodeStart":3263,"sourceCodeEnd":3299,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/interpreter/runtime.ts#L3263-L3299","documentation":"Reading an identifier whose binding exists but is not yet initialized reproduces JavaScript's temporal dead zone (TDZ) as a ReferenceError. This occurs specifically for parameter default expressions that forward-reference a later parameter of the same function — the binding is registered but flagged initialized === false until its default is evaluated.","triggerScenarios":"A function parameter default referencing a parameter declared after it: `function f(a = b, b = 2) {}`. Evaluating the default for `a` reads `b` before it is bound.","commonSituations":"Porting JS code with out-of-order parameter defaults; LLM-generated function signatures that reference later parameters from earlier defaults.","solutions":["Reorder parameters so defaults only reference earlier parameters: `function f(b = 2, a = b)`.","Inline the default expression instead of referencing the later parameter.","Compute the value inside the function body: `function f(a, b = 2) { a = a ?? b; }`."],"exampleFix":"// before\nfunction f(a = b, b = 2) { ... }\n// after\nfunction f(b = 2, a = b) { ... }","handlingStrategy":"try-catch","validationCode":"// statically reject parameter defaults referencing later params\nfor (let i = 0; i < params.length; i++) {\n  for (let j = i + 1; j < params.length; j++) {\n    if (defaultRefs(params[i], params[j].name)) throw new Error(`default of '${params[i].name}' references later param '${params[j].name}'`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  const result = await interpret(src);\n} catch (e) {\n  if (e instanceof InterpreterRuntimeError && e.message.endsWith(\"before initialization.\")) {\n    // report a TDZ at e.node; suggest reordering parameter defaults\n  }\n  throw e;\n}","preventionTips":["Only reference earlier parameters in parameter defaults.","Inline default expressions instead of cross-referencing parameters.","Handle the fallback inside the body with `a = a ?? b`.","Surface e.node line/column to users when reporting."],"tags":["codemode","interpreter","tdz","reference-error"],"backgroundTag":"access-before-initialization","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}