{"record":{"id":"9b53127ed0931177","repo":"karatelabs/karate","slug":"break-may-not-cross-a-class-static-initialization-block","errorCode":null,"errorMessage":"'break' may not cross a class static initialization block","messagePattern":"'break' may not cross a class static initialization block","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":700,"sourceCode":"                // `x.await` / `x?.await` name a member, they do not reference one\n                if (keepAwait && type == IDENT && prev != DOT && prev != QUES_DOT\n                        && isIdentText(child.token, \"await\")) {\n                    throw new ParserException(\"'await' is a reserved word in a class static initialization block\");\n                }\n                prev = type;\n                continue;\n            }\n            prev = null;\n            int childFlags = flags;\n            switch (child.type) {\n                case RETURN_STMT -> {\n                    if ((flags & SB_FN) == 0) {\n                        throw new ParserException(\"'return' is not allowed in a class static initialization block\");\n                    }\n                }\n                case BREAK_STMT -> {\n                    if ((flags & (SB_FN | SB_LOOP | SB_SWITCH)) == 0 && child.size() == 1) {\n                        throw new ParserException(\"'break' may not cross a class static initialization block\");\n                    }\n                }\n                case CONTINUE_STMT -> {\n                    if ((flags & (SB_FN | SB_LOOP)) == 0 && child.size() == 1) {\n                        throw new ParserException(\"'continue' may not cross a class static initialization block\");\n                    }\n                }\n                case FOR_STMT, WHILE_STMT, DO_WHILE_STMT -> childFlags |= SB_LOOP;\n                case SWITCH_STMT -> childFlags |= SB_SWITCH;\n                case FN_EXPR, FN_ARROW_EXPR -> childFlags |= SB_FN;\n                default -> {\n                }\n            }\n            // A function's parameters are [~Await] along with its body; an arrow's are\n            // not — only the body (always its last child) leaves the reserved region.\n            if (node.type == NodeType.FN_EXPR\n                    || (node.type == NodeType.FN_ARROW_EXPR && i == last)) {\n                childFlags |= SB_AWAIT_OK;","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L682-L718","documentation":"An unlabeled `break` inside a class static initialization block has no enclosing loop or switch to target (the block does not count), so it is an early error. The Karate JS parser throws this when a bare BREAK_STMT (no label) is found in a static block outside any nested function, loop, or switch (SB_FN | SB_LOOP | SB_SWITCH flags unset).","triggerScenarios":"Parsing `class C { static { break; } }` or any unlabeled `break` in a static block not inside a nested `for`/`while`/`switch`/function.","commonSituations":"Copy-pasting loop-exit code into a static initializer; replacing `return` with `break` after hitting the return restriction; breaking out of a block body expecting C-style `break` semantics.","solutions":["Remove the stray `break` and restructure the condition (`if (cond) { ... }` around the rest).","Wrap the breakable logic in an actual loop or switch, or in a nested function using `return`.","If exiting early from a labeled construct, ensure the label refers to a loop/switch inside the block."],"exampleFix":"// before\nclass C { static { for (const x of xs) { if (x.bad) break; use(x); } } } // OK\n// before (invalid)\nclass C { static { if (!ready) break; init(); } }\n// after\nclass C { static { if (ready) init(); } }","handlingStrategy":"validation","validationCode":"// flag unlabeled break in static block source outside loops/switches (naive scan)\nfunction noBareBreak(src) { return !/(^|[{;\\s])break\\s*[;}]/.test(src); }","typeGuard":null,"tryCatchPattern":"try { karate.eval(blockSrc); } catch (e) { if (String(e).includes(\"'break' may not cross a class static initialization block\")) { /* restructure control flow */ } }","preventionTips":["Only use `break` inside loops or switches, even in static blocks.","Replace desired early exits with guarded `if` blocks.","When converting function/IIFE bodies to static blocks, rewrite all control-flow exits."],"tags":["javascript","parser","syntax-error","static-block","break-statement"],"backgroundTag":"javascript-syntax-error","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"}