Skip to content

Commit

Permalink
JS: fail if arrow parameters are a valid expression but invalid as pa…
Browse files Browse the repository at this point in the history
…rameters
  • Loading branch information
tdewolff committed Oct 25, 2023
1 parent 9b8eaa4 commit bf2964b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,9 @@ func (p *Parser) parseParenthesizedExpressionOrArrowFunc(prec OpPrec, async []by
// exprToBinding converts a CoverParenthesizedExpressionAndArrowParameterList into FormalParameters
// Any unbound variables of the parameters (Initializer, ComputedPropertyName) are kept in the parent scope
func (p *Parser) exprToBinding(expr IExpr) (binding IBinding) {
if v, ok := expr.(*Var); ok {
if expr == nil {
// no-op
} else if v, ok := expr.(*Var); ok {
binding = v
} else if array, ok := expr.(*ArrayExpr); ok {
bindingArray := BindingArray{}
Expand Down Expand Up @@ -2272,6 +2274,8 @@ func (p *Parser) exprToBinding(expr IExpr) (binding IBinding) {
bindingObject.List = append(bindingObject.List, BindingObjectItem{Key: item.Name, Value: bindingElement})
}
binding = &bindingObject
} else {
p.failMessage("invalid parameters in arrow function")
}
return
}
Expand Down
3 changes: 3 additions & 0 deletions js/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func TestParse(t *testing.T) {
{"x = async function() {}", "Stmt(x=Decl(async function Params() Stmt({ })))"},
{"x = class {}", "Stmt(x=Decl(class))"},
{"x = class {a(){}}", "Stmt(x=Decl(class Method(a Params() Stmt({ }))))"},
{"x = class A { [(0())]; };", "Stmt(x=Decl(class A Field([(0())])))"},
{"x = a => a++", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return (a++)) })))"},
{"x = a => {a++}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(a++) })))"},
{"x = a => {return}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return) })))"},
Expand Down Expand Up @@ -632,6 +633,8 @@ func TestParseError(t *testing.T) {
{"x = (a, a) =>", "unexpected => in expression"},
{"x = (a, a, ...a) =>", "unexpected ... in expression"},
{"x = (a, ...a) =>", "identifier a has already been declared"},
{"(A,{}%0%{})=>0", "invalid parameters in arrow function"},
{"({}``=1)=>0", "invalid parameters in arrow function"},

// expression precedence
{"x = a + yield b", "unexpected b in expression"},
Expand Down

0 comments on commit bf2964b

Please sign in to comment.