{"record":{"id":"414e47402a668523","repo":"karatelabs/karate","slug":"label-already-in-scope-name","errorCode":null,"errorMessage":"label already in scope: ${name}","messagePattern":"label already in scope: (.+?)","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":403,"sourceCode":"            }\n            return false;\n        }\n    }\n\n    /**\n     * §14.13.1 label early errors at a single node (no recursion — {@link #earlyErrors}\n     * drives the traversal): a label may not be nested inside a statement already carrying\n     * it, {@code break} / {@code continue} may only name an enclosing label, and\n     * {@code continue} additionally needs that label to name an iteration statement.\n     * Returns the label chain visible to this node's children — nested function bodies see\n     * none of them, since labels do not cross a function boundary.\n     */\n    private Label labelNodeChecks(Node node, Label labels) {\n        switch (node.type) {\n            case LABELLED_STMT -> {\n                String name = node.getFirst().getText();\n                if (Label.contains(labels, name, false)) {\n                    throw new ParserException(\"label already in scope: \" + name);\n                }\n                return new Label(name, labelsIterationStatement(node), labels);\n            }\n            // §15.7.1 makes a static initialization block a boundary of the same kind\n            // as a function body: nothing inside it may name a label outside it.\n            case FN_EXPR, FN_ARROW_EXPR, CLASS_STATIC_BLOCK -> {\n                return null;\n            }\n            case BREAK_STMT -> {\n                String target = labelReference(node);\n                if (target != null && !Label.contains(labels, target, false)) {\n                    throw new ParserException(\"break references an undefined label: \" + target);\n                }\n            }\n            case CONTINUE_STMT -> {\n                String target = labelReference(node);\n                if (target != null) {\n                    if (!Label.contains(labels, target, false)) {","sourceCodeStart":385,"sourceCodeEnd":421,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L385-L421","documentation":"A statement label must be unique within its scope; re-declaring the same label in nested (non-function) code is a static error. labelNodeChecks maintains a linked Label chain and rejects a LABELLED_STMT whose name already appears in the chain. Function bodies, arrow functions, and class static blocks reset the chain (labels outside are invisible, so re-use there is fine).","triggerScenarios":"Writing `outer: ... outer: ...` where the second `outer` is nested within the first's statement (not separated by a function boundary) — Label.contains(labels, name, false) finds the prior binding.","commonSituations":"Copy-pasted loop blocks each carrying the same label inside one function; wrapping existing labeled code in another labeled block; template-generated loops reusing label names.","solutions":["Rename the inner (or outer) label to a unique name","Move the inner labeled statement into its own function, which resets label scope","Remove the redundant inner label if `break`/`continue` there can target the outer label legally"],"exampleFix":"// before\nouter: for (;;) { outer: for (;;) { break outer; } }\n// after\nouter: for (;;) { inner: for (;;) { break inner; } }","handlingStrategy":"validation","validationCode":"// Collect labels and flag re-declarations within one function scope:\nconst labels = [...src.matchAll(/([A-Za-z_$][\\w$]*):\\s*(?:for|while|do|\\{|switch|if)/g)].map(m=>m[1]);\nconst dup = labels.filter((l,i)=>labels.indexOf(l)!==i);","typeGuard":null,"tryCatchPattern":"try { eval(js); } catch (e) { if (String(e).includes('label already in scope')) { /* rename the inner label */ } throw e; }","preventionTips":["Give nested loops distinct, descriptive label names","Extract nested labeled code into functions to reset label scope","Avoid reusing generic labels like `loop`/`outer` across nested blocks"],"tags":["javascript","parser","labels","scope"],"backgroundTag":"duplicate-declaration","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"}