{"record":{"id":"4c1ad448b76bade9","repo":"karatelabs/karate","slug":"cannot-access-node-gettext-before-initialization","errorCode":null,"errorMessage":"cannot access '${node.getText()}' before initialization","messagePattern":"cannot access '(.+?)' before initialization","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":2583,"sourceCode":"        }\n        String message = node.toStringError(rawMessage);\n        boolean authored = context.isErrorAuthored();\n        return new EngineException(message, null, errorName, jsMessage,\n                authored, authored ? errorThrown : null, authored ? context.getErrorAuthoredLine() : 0);\n    }\n\n    private static Object evalRefExpr(Node node, CoreContext context) {\n        if (node.getFirst().type == NodeType.FN_ARROW_EXPR) { // arrow function\n            return evalFnArrowExpr(node.getFirst(), context);\n        }\n        int slot = node.slot;\n        if (slot >= 0) {\n            Object[] frame = context.frame;\n            if (frame != null) {\n                Object v = frame[slot];\n                if (v != SlotTable.UNDECLARED) {\n                    if (v == SlotTable.TDZ) {\n                        throw SlotTable.tdzError(node.getText());\n                    }\n                    return v;\n                }\n            }\n        }\n        return evalRefExprByName(node, context);\n    }\n\n    // The name-keyed tail of evalRefExpr, outlined so the slot fast path above\n    // stays small enough to inline reliably — an at-threshold hot method here\n    // flips whole benchmark rows run to run depending on JIT timing.\n    private static Object evalRefExprByName(Node node, CoreContext context) {\n        String varName = node.getText();\n        if (\"this\".equals(varName)) {\n            return context.getThisObject();\n        }\n        if (context.callArgs != null && \"arguments\".equals(varName)) {\n            JsArray a = context.argumentsForRead();","sourceCodeStart":2565,"sourceCodeEnd":2601,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L2565-L2601","documentation":"Thrown when reading a let/const binding in its temporal dead zone — after the slot exists (script has entered its scope) but before the initializer has executed. SlotTable.TDZ marks the un-initialized slot; the produced ReferenceError names the variable.","triggerScenarios":"Referencing a `let`/`const` variable earlier in the same scope than its declaration statement executes — e.g. using it inside a function called before the declaration line, or in its own initializer.","commonSituations":"Hoisting assumptions carried over from `var`; functions invoked during setup that touch bindings declared later; circular module-like top-level code in embedded scripts.","solutions":["Move the `let`/`const` declaration above all code that reads it in the same scope.","Rename the variable if a same-scope `var`/outer binding was shadowed unintentionally.","Defer function calls that read the binding until after its initializer runs."],"exampleFix":"// before\nconsole.log(x);\nlet x = 1;\n// after\nlet x = 1;\nconsole.log(x);","handlingStrategy":"validation","validationCode":"if (typeof x === 'undefined' && declaredLater) throw new Error('x read before its let/const initialization');","typeGuard":"function isInitialized(fn) { try { fn(); return true; } catch (e) { return !String(e).includes('before initialization'); } }","tryCatchPattern":"try { use(x); } catch (e) { if (String(e).includes('before initialization')) { /* defer or redeclare */ } }","preventionTips":["Declare let/const at the top of the scope","Don't call functions that read bindings before their initializers run","Replace TDZ-prone lazy patterns with functions returning values"],"tags":["javascript","tdz","let","const"],"backgroundTag":"access-before-initialization","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}