evanw/esbuild · error · LexerPanic
Unexpected "," after rest pattern
Error message
Unexpected "," after rest pattern
What it means
In array destructuring binding, a rest element '...rest' must be the last element and cannot be followed by a comma. When the parser sees a TComma right after a spread in the array-binding loop (js_parser.go:6066), it errors.
Source
Thrown at internal/js_parser/js_parser.go:6068
p.saveExprCommentsHere()
binding := p.parseBinding(parseBindingOpts{})
var defaultValueOrNil js_ast.Expr
if !hasSpread && p.lexer.Token == js_lexer.TEquals {
p.lexer.Next()
defaultValueOrNil = p.parseExpr(js_ast.LComma)
}
items = append(items, js_ast.ArrayBinding{
Binding: binding,
DefaultValueOrNil: defaultValueOrNil,
Loc: itemLoc,
})
// Commas after spread elements are not allowed
if hasSpread && p.lexer.Token == js_lexer.TComma {
p.log.AddError(&p.tracker, p.lexer.Range(), "Unexpected \",\" after rest pattern")
panic(js_lexer.LexerPanic{})
}
}
if p.lexer.Token != js_lexer.TComma {
break
}
if p.lexer.HasNewlineBefore {
isSingleLine = false
}
p.lexer.Next()
if p.lexer.HasNewlineBefore {
isSingleLine = false
}
}
p.allowIn = oldAllowIn
if p.lexer.HasNewlineBefore {View on GitHub (pinned to f6058f8364)
Solutions
- Remove the comma and any elements after '...rest'.
- Make the rest element truly last in the array pattern.
Example fix
// before const [a, ...rest,] = arr // after const [a, ...rest] = arr
Defensive patterns
Strategy: validation
Validate before calling
import "github.com/evanw/esbuild/pkg/api"
res := api.Transform(src, api.TransformOptions{Loader: api.LoaderJS})
for _, m := range res.Errors {
if strings.Contains(m.Text, "Unexpected \",\" after rest pattern") {
// a trailing comma appeared after ...rest in an array binding
}
} Type guard
// Flag '...rest,' (comma after rest) inside an array destructuring binding.
var reRestCommaArray = regexp.MustCompile(`\\[[^\\]]*\\.\\.\\.[^\\]]*,\\s*[\\]\\)]`)
func arrayRestHasTrailingComma(src string) bool { return reRestCommaArray.MatchString(src) } Prevention
- Keep the rest element last in array patterns with no trailing comma.
- Configure prettier/eslint destructuring rules to flag post-rest commas.
- Review any spread in destructuring that is followed by more elements.
When it happens
Trigger: const [a, ...rest,] = arr or ([a, ...b, c] = x) — a comma (and any further elements) after the rest in an array pattern.
Common situations: A trailing comma after spread; additional elements accidentally placed after the rest; formatter inserting trailing commas.
Related errors
- Invalid binding pattern
- Unexpected newline before "=>"
- For loop initializers cannot start with "async of"
- Unexpected ":"
- Unexpected "..."
AI-assisted analysis of evanw/esbuild@f6058f8364 (2026-08-09).
Data as JSON: /api/errors/ca006dd9ca9f5f8d.
Report an issue: GitHub.