Skip to content

Commit

Permalink
Fixed panic when parsing invalid object property keys. Fixes #376.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Mar 31, 2022
1 parent a18ffb9 commit 8a7c4f4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
50 changes: 21 additions & 29 deletions parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ func (self *_parser) parseObjectProperty() ast.Property {
}
keyStartIdx := self.idx
literal, parsedLiteral, value, tkn := self.parseObjectPropertyKey()
if value == nil {
return nil
}
if tkn == token.IDENTIFIER || tkn == token.STRING || tkn == token.KEYWORD || tkn == token.ILLEGAL {
switch {
case self.token == token.LEFT_PARENTHESIS:
Expand Down Expand Up @@ -367,54 +370,43 @@ func (self *_parser) parseObjectProperty() ast.Property {
Initializer: initializer,
}
}
case literal == "get" && self.token != token.COLON:
case (literal == "get" || literal == "set") && self.token != token.COLON:
_, _, keyValue, _ := self.parseObjectPropertyKey()
if keyValue == nil {
return nil
}
var kind ast.PropertyKind
idx1 := self.idx
parameterList := self.parseFunctionParameterList()
if len(parameterList.List) > 0 || parameterList.Rest != nil {
self.error(idx1, "Getter must not have any formal parameters.")
}
node := &ast.FunctionLiteral{
Function: keyStartIdx,
ParameterList: parameterList,
}
node.Body, node.DeclarationList = self.parseFunctionBlock()
node.Source = self.slice(keyStartIdx, node.Body.Idx1())
return &ast.PropertyKeyed{
Key: keyValue,
Kind: ast.PropertyKindGet,
Value: node,
if literal == "get" {
kind = ast.PropertyKindGet
if len(parameterList.List) > 0 || parameterList.Rest != nil {
self.error(idx1, "Getter must not have any formal parameters.")
}
} else {
kind = ast.PropertyKindSet
}
case literal == "set" && self.token != token.COLON:
_, _, keyValue, _ := self.parseObjectPropertyKey()
parameterList := self.parseFunctionParameterList()

node := &ast.FunctionLiteral{
Function: keyStartIdx,
ParameterList: parameterList,
}

node.Body, node.DeclarationList = self.parseFunctionBlock()
node.Source = self.slice(keyStartIdx, node.Body.Idx1())

return &ast.PropertyKeyed{
Key: keyValue,
Kind: ast.PropertyKindSet,
Kind: kind,
Value: node,
}
}
}

self.expect(token.COLON)
if value != nil {
return &ast.PropertyKeyed{
Key: value,
Kind: ast.PropertyKindValue,
Value: self.parseAssignmentExpression(),
Computed: tkn == token.ILLEGAL,
}
return &ast.PropertyKeyed{
Key: value,
Kind: ast.PropertyKindValue,
Value: self.parseAssignmentExpression(),
Computed: tkn == token.ILLEGAL,
}
return nil
}

func (self *_parser) parseObjectLiteral() *ast.ObjectLiteral {
Expand Down
3 changes: 3 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ func TestParserErr(t *testing.T) {
test("`", "(anonymous): Line 1:2 Unexpected end of input")
test(" `", "(anonymous): Line 1:3 Unexpected end of input")
test("` ", "(anonymous): Line 1:3 Unexpected end of input")
test(`var{..(`, "(anonymous): Line 1:7 Unexpected token ILLEGAL")
test(`var{get..(`, "(anonymous): Line 1:10 Unexpected token ILLEGAL")
test(`var{set..(`, "(anonymous): Line 1:10 Unexpected token ILLEGAL")
})
}

Expand Down

0 comments on commit 8a7c4f4

Please sign in to comment.