{"record":{"id":"3a3b162d0e046a62","repo":"karatelabs/karate","slug":"more-than-one-default-clause-in-a-switch","errorCode":null,"errorMessage":"more than one `default` clause in a switch","messagePattern":"more than one `default` clause in a switch","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":572,"sourceCode":"            // A LexicalDeclaration (let/const) and a ClassDeclaration are likewise\n            // Declarations, not Statements, and — unlike FunctionDeclaration — have no\n            // Annex B carve-out for ANY clause, so they are illegal as the body of an\n            // `if`/`else` clause too (§13.6/§14.x). The `for`-init `let` is a direct\n            // FOR_STMT child, not a STATEMENT, so it is correctly ignored.\n            case FOR_STMT, WHILE_STMT, DO_WHILE_STMT -> {\n                checkNoFunctionDeclarationBody(node, \"a loop\");\n                checkNoLexicalOrClassDeclarationBody(node, \"a loop\");\n            }\n            case IF_STMT -> checkNoLexicalOrClassDeclarationBody(node, \"an `if`/`else` clause\");\n            // CaseBlock has at most one DefaultClause (§14.12.1). The grammar accepts\n            // `default` anywhere among the cases, so the count is checked here.\n            case SWITCH_STMT -> {\n                boolean seen = false;\n                for (int i = 0, n = node.size(); i < n; i++) {\n                    Node child = node.get(i);\n                    if (!child.isToken() && child.type == NodeType.DEFAULT_BLOCK) {\n                        if (seen) {\n                            throw new ParserException(\"more than one `default` clause in a switch\");\n                        }\n                        seen = true;\n                    }\n                }\n            }\n            // LabelledItem is a Statement or a FunctionDeclaration; the latter is a strict-mode\n            // early error with only an Annex B.3.1 sloppy carve-out. karate-js rejects it in both\n            // modes: the hoisting a bare `function f(){}` gets does not reach through the\n            // LABELLED_STMT wrapper, so accepting it would bind `f` later than the reader expects.\n            // let/const/class have no carve-out at all.\n            case LABELLED_STMT -> {\n                checkNoFunctionDeclarationBody(node, \"a labelled statement\");\n                checkNoLexicalOrClassDeclarationBody(node, \"a labelled statement\");\n            }\n            default -> {\n            }\n        }\n    }","sourceCodeStart":554,"sourceCodeEnd":590,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L554-L590","documentation":"A `switch` statement may contain at most one `default` clause; more than one is an early error. The Karate JS parser scans SWITCH_STMT children for multiple DEFAULT_BLOCK nodes and throws. Remove or merge the extra `default`.","triggerScenarios":"Parsing a switch statement whose case list contains two or more `default:` clauses, detected in earlyErrorNodeChecks.","commonSituations":"Merging branches from two code paths during refactoring; copy-paste duplicating a switch arm; adding a new fallback without noticing an existing one.","solutions":["Delete the redundant `default:` clause.","Merge the bodies of both `default` clauses into a single one.","Convert one `default` into an explicit `case` matching the intended value."],"exampleFix":"// before\nswitch (x) { case 1: a(); break; default: b(); break; default: c(); }\n// after\nswitch (x) { case 1: a(); break; default: b(); c(); }","handlingStrategy":"validation","validationCode":"// count default clauses in a switch body string\nfunction singleDefault(src) { return (src.match(/\\bdefault\\s*:/g) || []).length <= 1; }","typeGuard":null,"tryCatchPattern":"try { karate.eval(src); } catch (e) { if (String(e).includes(\"more than one `default` clause\")) { /* merge/remove defaults */ } }","preventionTips":["Search for `default:` when editing existing switch statements.","Merge duplicated fallback arms during merges/refactors.","Use lint rules that report duplicate default clauses."],"tags":["javascript","parser","syntax-error","switch-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-19T12:17:13.211Z"}