Skip to content

Commit

Permalink
JS: support ellipsis operator anywhere in arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Feb 6, 2021
1 parent 87e4257 commit 2cc8037
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
17 changes: 9 additions & 8 deletions js/ast.go
Expand Up @@ -1064,10 +1064,14 @@ func (n ImportMetaExpr) String() string {
return "(import.meta)"
}

type Arg struct {
Value IExpr
Rest bool
}

// Args is a list of arguments as used by new and call expressions.
type Args struct {
List []IExpr
Rest IExpr // can be nil
List []Arg
}

func (n Args) String() string {
Expand All @@ -1076,13 +1080,10 @@ func (n Args) String() string {
if i != 0 {
s += ", "
}
s += item.String()
}
if n.Rest != nil {
if len(n.List) != 0 {
s += ", "
if item.Rest {
s += "..."
}
s += "..." + n.Rest.String()
s += item.Value.String()
}
return s + ")"
}
Expand Down
17 changes: 8 additions & 9 deletions js/parse.go
Expand Up @@ -1315,21 +1315,20 @@ func (p *Parser) parseTemplateLiteral(precLeft OpPrec) (template TemplateExpr) {
func (p *Parser) parseArguments() (args Args) {
// assume we're on (
p.next()
args.List = make([]IExpr, 0, 4)
args.List = make([]Arg, 0, 4)
for {
if p.tt == EllipsisToken {
rest := p.tt == EllipsisToken
if rest {
p.next()
args.Rest = p.parseExpression(OpAssign)
if p.tt == CommaToken {
p.next()
}
break
}

if p.tt == CloseParenToken || p.tt == ErrorToken {
break
}
args.List = append(args.List, p.parseExpression(OpAssign))
args.List = append(args.List, Arg{
Value: p.parseExpression(OpAssign),
Rest: rest,
})
if p.tt == CommaToken {
p.next()
}
Expand Down Expand Up @@ -1572,7 +1571,7 @@ func (p *Parser) parseExpression(prec OpPrec) IExpr {
newExpr := &NewExpr{p.parseExpression(OpNew), nil}
if p.tt == OpenParenToken {
args := p.parseArguments()
if len(args.List) != 0 || args.Rest != nil {
if len(args.List) != 0 {
newExpr.Args = &args
}
precLeft = OpMember
Expand Down
1 change: 1 addition & 0 deletions js/parse_test.go
Expand Up @@ -250,6 +250,7 @@ func TestParse(t *testing.T) {
{"x = a?.(b)", "Stmt(x=(a?.(b)))"},
{"x = super(a)", "Stmt(x=(super(a)))"},
{"x = a(a,b,...c,)", "Stmt(x=(a(a, b, ...c)))"},
{"x = a(...a,...b)", "Stmt(x=(a(...a, ...b)))"},
{"x = new a", "Stmt(x=(new a))"},
{"x = new a()", "Stmt(x=(new a))"},
{"x = new a(b)", "Stmt(x=(new a(b)))"},
Expand Down

0 comments on commit 2cc8037

Please sign in to comment.