{"record":{"id":"fff1ea54f076d48a","repo":"karatelabs/karate","slug":"return-is-not-allowed-in-a-class-static-initialization-block","errorCode":null,"errorMessage":"'return' is not allowed in a class static initialization block","messagePattern":"'return' is not allowed in 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":695,"sourceCode":"        TokenType prev = null;\n        for (int i = 0; i <= last; i++) {\n            Node child = node.get(i);\n            if (child.isToken()) {\n                TokenType type = child.token.type;\n                // `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            }","sourceCodeStart":677,"sourceCodeEnd":713,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L677-L713","documentation":"`return` cannot appear in a class static initialization block — the block is not a function, so there is nothing to return from. The Karate JS parser throws this when a RETURN_STMT is found while walking a static-block body without the SB_FN (inside-nested-function) flag.","triggerScenarios":"Parsing `class C { static { return x; } }` — a return statement directly inside a static block (returns inside nested functions within the block are fine).","commonSituations":"Converting an IIFE or constructor body into a static block and keeping its `return`; copy-pasting function code into a static initializer; early-exit patterns carried over from function bodies.","solutions":["Remove the `return` and express the early exit with `if`/`throw` around the remaining statements.","Wrap the logic in a static method or nested function where `return` is valid: `static { init(); }`.","Use a labeled block with `break` if you need an early exit to the end of the block."],"exampleFix":"// before\nclass C { static { if (!ready) return; setup(); } }\n// after\nclass C { static { if (ready) setup(); } }","handlingStrategy":"validation","validationCode":"// flag top-level return in static block source (naive: any return not inside a nested function)\nfunction noBareReturn(src) { return !/(^|[{;\\s])return\\b/.test(src.replace(/function[^{]*\\{[\\s\\S]*?\\}/g, '')); }","typeGuard":null,"tryCatchPattern":"try { karate.eval(blockSrc); } catch (e) { if (String(e).includes(\"'return' is not allowed\")) { /* replace with if/throw control flow */ } }","preventionTips":["Treat static blocks as plain statement sequences, not function bodies.","Move complex initializers with early exits into static methods.","Use `if` inversion instead of early `return` in initializer code."],"tags":["javascript","parser","syntax-error","static-block","return-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"}