{"record":{"id":"dcf49ca353d8d60d","repo":"evanw/esbuild","slug":"multiple-default-clauses-are-not-allowed","errorCode":null,"errorMessage":"Multiple default clauses are not allowed","messagePattern":"Multiple default clauses are not allowed","errorType":"panic","errorClass":"LexerPanic","httpStatus":null,"severity":"error","filePath":"internal/js_parser/js_parser.go","lineNumber":7611,"sourceCode":"\t\tp.pushScopeForParsePass(js_ast.ScopeBlock, bodyLoc)\n\t\tdefer p.popScope()\n\n\t\tp.lexer.Expect(js_lexer.TOpenBrace)\n\t\tcases := []js_ast.Case{}\n\t\tfoundDefault := false\n\t\tswitchScopeStart := len(p.scopesInOrder)\n\t\tvar caseScopeMap map[*js_ast.Scope]struct{}\n\n\t\tfor p.lexer.Token != js_lexer.TCloseBrace {\n\t\t\tvar value js_ast.Expr\n\t\t\tbody := []js_ast.Stmt{}\n\t\t\tcaseLoc := p.saveExprCommentsHere()\n\t\t\tcaseScopeStart := len(p.scopesInOrder)\n\n\t\t\tif p.lexer.Token == js_lexer.TDefault {\n\t\t\t\tif foundDefault {\n\t\t\t\t\tp.log.AddError(&p.tracker, p.lexer.Range(), \"Multiple default clauses are not allowed\")\n\t\t\t\t\tpanic(js_lexer.LexerPanic{})\n\t\t\t\t}\n\t\t\t\tfoundDefault = true\n\t\t\t\tp.lexer.Next()\n\t\t\t\tp.lexer.Expect(js_lexer.TColon)\n\t\t\t} else {\n\t\t\t\tp.lexer.Expect(js_lexer.TCase)\n\t\t\t\tvalue = p.parseExpr(js_ast.LLowest)\n\t\t\t\tp.lexer.Expect(js_lexer.TColon)\n\t\t\t}\n\n\t\t\t// Keep track of any scopes created by case values. This can happen if\n\t\t\t// code uses anonymous functions inside a case value. For example:\n\t\t\t//\n\t\t\t//   switch (x) {\n\t\t\t//     case y.map(z => -z).join(':'):\n\t\t\t//       return y\n\t\t\t//   }\n\t\t\t//","sourceCodeStart":7593,"sourceCodeEnd":7629,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/internal/js_parser/js_parser.go#L7593-L7629","documentation":"A switch statement may contain at most one 'default' clause. esbuild tracks 'foundDefault' and, on encountering a second 'default' (js_parser.go:7609), reports 'Multiple default clauses are not allowed'.","triggerScenarios":"switch (x) { default: a(); default: b(); } — two default clauses in one switch body.","commonSituations":"Merging two switch bodies; copy-paste duplication; faulty code generation.","solutions":["Remove the extra 'default' clause.","If both branches are needed, convert one to an explicit 'case'.","Merge the bodies of the duplicate defaults into a single clause."],"exampleFix":"// before\nswitch (x) {\n  default: a();\n  default: b();\n}\n// after\nswitch (x) {\n  default: a();\n}","handlingStrategy":"validation","validationCode":"import \"github.com/evanw/esbuild/pkg/api\"\nres := api.Transform(src, api.TransformOptions{Loader: api.LoaderJS})\nfor _, m := range res.Errors {\n  if strings.Contains(m.Text, \"Multiple default clauses\") {\n    // two 'default:' in one switch; m.Location marks the second\n  }\n}","typeGuard":"// Count 'default' clauses per switch block; flag >1.\nfunc switchHasMultipleDefaults(src string) bool {\n  depth, defaults := 0, 0\n  for i := 0; i < len(src); i++ {\n    switch src[i] {\n    case '{': depth++\n    case '}': if depth > 0 { depth--; if depth == 0 { if defaults > 1 { return true }; defaults = 0 } }\n    }\n    if depth > 0 && strings.HasPrefix(src[i:], \"default\") { defaults++ }\n  }\n  return false\n}","tryCatchPattern":null,"preventionTips":["Merge duplicated default branches into one.","Use eslint/tsc to flag duplicate switch defaults.","When merging switch bodies, deduplicate default clauses."],"tags":["syntax-error","parser","switch"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}