Skip to content

Commit

Permalink
Set "Computed" property on method definitions in object literals
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Jan 21, 2023
1 parent 17fd568 commit 2e4cbb9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
22 changes: 22 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5727,6 +5727,28 @@ func TestAsyncFunc(t *testing.T) {
testAsyncFunc(SCRIPT, valueTrue, t)
}

func TestObjectLiteralComputedMethodKeys(t *testing.T) {
_, err := Compile("", `
({
["__proto__"]() {},
["__proto__"]() {}
})
`, false)
if err != nil {
t.Fatal(err)
}

_, err = Compile("", `
({
get ["__proto__"]() {},
get ["__proto__"]() {}
})
`, false)
if err != nil {
t.Fatal(err)
}
}

/*
func TestBabel(t *testing.T) {
src, err := os.ReadFile("babel7.js")
Expand Down
16 changes: 9 additions & 7 deletions parser/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,10 @@ func (self *_parser) parseObjectProperty() ast.Property {
switch {
case self.token == token.LEFT_PARENTHESIS:
return &ast.PropertyKeyed{
Key: value,
Kind: ast.PropertyKindMethod,
Value: self.parseMethodDefinition(keyStartIdx, ast.PropertyKindMethod, false),
Key: value,
Kind: ast.PropertyKindMethod,
Value: self.parseMethodDefinition(keyStartIdx, ast.PropertyKindMethod, false),
Computed: tkn == token.ILLEGAL,
}
case self.token == token.COMMA || self.token == token.RIGHT_BRACE || self.token == token.ASSIGN: // shorthand property
if self.isBindingId(tkn) {
Expand All @@ -428,7 +429,7 @@ func (self *_parser) parseObjectProperty() ast.Property {
self.errorUnexpectedToken(self.token)
}
case (literal == "get" || literal == "set" || tkn == token.ASYNC) && self.token != token.COLON:
_, _, keyValue, _ := self.parseObjectPropertyKey()
_, _, keyValue, tkn1 := self.parseObjectPropertyKey()
if keyValue == nil {
return nil
}
Expand All @@ -445,9 +446,10 @@ func (self *_parser) parseObjectProperty() ast.Property {
}

return &ast.PropertyKeyed{
Key: keyValue,
Kind: kind,
Value: self.parseMethodDefinition(keyStartIdx, kind, async),
Key: keyValue,
Kind: kind,
Value: self.parseMethodDefinition(keyStartIdx, kind, async),
Computed: tkn1 == token.ILLEGAL,
}
}
}
Expand Down

0 comments on commit 2e4cbb9

Please sign in to comment.