{"record":{"id":"46dcd0cb24330e45","repo":"karatelabs/karate","slug":"a-class-declaration-may-not-be-the-body-of-where","errorCode":null,"errorMessage":"a class declaration may not be the body of ${where}","messagePattern":"a class declaration may not be the body of (.+?)","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":783,"sourceCode":"    }\n\n    /** Throws if any direct {@code STATEMENT} child of {@code node} is itself a\n     *  LexicalDeclaration ({@code let}/{@code const}) or a ClassDeclaration — the body\n     *  of an `if`/`else`/loop clause is a Statement, and both are Declarations, not\n     *  Statements (§13.6/§14.x). A {@code var} declaration hoists and stays legal; a\n     *  braced body (BLOCK) is fine. Mode-independent — there is no Annex B carve-out\n     *  for these the way there is for FunctionDeclaration in an `if` clause. */\n    private static void checkNoLexicalOrClassDeclarationBody(Node node, String where) {\n        for (int i = 0, n = node.size(); i < n; i++) {\n            Node child = node.get(i);\n            if (child.isToken() || child.type != NodeType.STATEMENT) {\n                continue;\n            }\n            for (int j = 0, m = child.size(); j < m; j++) {\n                Node inner = child.get(j);\n                if (!inner.isToken()) {\n                    if (inner.type == NodeType.CLASS_EXPR) {\n                        throw new ParserException(\n                                \"a class declaration may not be the body of \" + where);\n                    }\n                    if (inner.type == NodeType.VAR_STMT && isLexicalVarStmt(inner)) {\n                        throw new ParserException(\n                                \"a lexical declaration may not be the body of \" + where);\n                    }\n                    break; // first non-token child decides\n                }\n            }\n        }\n    }\n\n    /** True if a {@code VAR_STMT}'s leading keyword token is {@code let} / {@code const}\n     *  (a LexicalDeclaration) rather than {@code var}. The keyword distinction lives on\n     *  VAR_STMT, not on the VAR_DECL children.\n     *  <p>One sloppy-mode subtlety: {@code let} is not a reserved word, so as the body of\n     *  an `if`/loop clause {@code let} followed by a LineTerminator is the identifier\n     *  {@code let} as an ExpressionStatement with ASI inserted before the next line","sourceCodeStart":765,"sourceCodeEnd":801,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L765-L801","documentation":"A class declaration, like a function declaration, is a Declaration rather than a Statement, so it cannot be the unbraced body of `if`/`else`, a loop, or a labelled clause. Karate's parser rejects this unconditionally (there is no sloppy-mode carve-out for classes). The body must be a braced block.","triggerScenarios":"Writing `if (cond) class C {}`, `while (x) class C {}`, or `label: class C {}` in Karate-embedded JS.","commonSituations":"Authors omitting braces around multi-token declarations; code ported from a transpiler that assumed lenient parsing; teaching examples written sloppily.","solutions":["Brace the clause: `if (cond) { class C {} }`.","Declare the class outside the clause and reference it inside.","Use a class expression: `if (cond) { const C = class {}; }`."],"exampleFix":"// before\nif (flag) class Point { constructor(x) { this.x = x; } }\n// after\nif (flag) { class Point { constructor(x) { this.x = x; } } }","handlingStrategy":"validation","validationCode":"function hasUnbracedClassBody(src) {\n  return /(^|[^\\w$])(if|else|for|while|do|\\w+:)\\s*class\\s*[\\w$]*/.test(src);\n}\nif (hasUnbracedClassBody(src)) throw new Error('brace the class declaration body');","typeGuard":null,"tryCatchPattern":"try {\n  karate.eval(script);\n} catch (e) {\n  if (String(e.message).startsWith('a class declaration may not be the body of')) {\n    // rewrite the clause with braces and retry\n  } else throw e;\n}","preventionTips":["Treat class declarations like function declarations: statement-list positions only.","Always brace clause bodies containing any declaration.","Search generated scripts for `if (…) class` / `while (…) class` patterns before shipping."],"tags":["javascript","parser","syntax-error","class-declaration"],"backgroundTag":"invalid-syntax","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}