node: handle trailing commas in array literals

This commit is contained in:
mappu 2020-04-11 12:45:26 +12:00
parent 48f3014429
commit 6b8d4d879a

View File

@ -1648,6 +1648,8 @@ func (this *conversionState) convertArrayLiteralCommon(items []node.Node) (strin
keyType := unknownVarType keyType := unknownVarType
valType := unknownVarType valType := unknownVarType
// TODO support unpack operator (...)
isMapType := false isMapType := false
for idx, itm_ := range items { for idx, itm_ := range items {
itm, ok := itm_.(*expr.ArrayItem) itm, ok := itm_.(*expr.ArrayItem)
@ -1655,6 +1657,12 @@ func (this *conversionState) convertArrayLiteralCommon(items []node.Node) (strin
return "", parseErr{itm_, fmt.Errorf("expected ArrayItem")} return "", parseErr{itm_, fmt.Errorf("expected ArrayItem")}
} }
// If there is a trailing comma in the definition,
// the parser produces an empty ArrayItem with no detail information
if itm.Key == nil && itm.Val == nil && idx == len(items)-1 {
break
}
if idx == 0 { if idx == 0 {
isMapType = (itm.Key != nil) isMapType = (itm.Key != nil)
} else { } else {