Browse Source

Implement `ComputedPropertyName` in `ObjectLiteral` (#1514)

pull/1509/head
raskad 3 years ago committed by GitHub
parent
commit
1687ecf877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      boa/src/syntax/ast/node/mod.rs
  2. 15
      boa/src/syntax/ast/node/object/mod.rs
  3. 12
      boa/src/syntax/parser/expression/primary/object_initializer/mod.rs

10
boa/src/syntax/ast/node/mod.rs

@ -484,6 +484,16 @@ pub enum PropertyDefinition {
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Property_definitions
Property(Box<str>, Node),
/// Binds a computed property name to a JavaScript value.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#prod-ComputedPropertyName
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Property_definitions
ComputedPropertyName(Node, Node),
/// A property of an object can also refer to a function or a getter or setter method.
///
/// More information:

15
boa/src/syntax/ast/node/object/mod.rs

@ -64,6 +64,11 @@ impl Object {
value.display_no_indent(f, indent + 1)?;
writeln!(f, ",")?;
}
PropertyDefinition::ComputedPropertyName(key, value) => {
writeln!(f, "{}{},", indentation, key)?;
value.display_no_indent(f, indent + 1)?;
writeln!(f, ",")?;
}
PropertyDefinition::SpreadObject(key) => {
writeln!(f, "{}...{},", indentation, key)?;
}
@ -104,6 +109,16 @@ impl Executable for Object {
.configurable(true),
);
}
PropertyDefinition::ComputedPropertyName(key, value) => {
obj.set_property(
key.run(context)?.to_property_key(context)?,
PropertyDescriptor::builder()
.value(value.run(context)?)
.writable(true)
.enumerable(true)
.configurable(true),
);
}
PropertyDefinition::MethodDefinition(kind, name, func) => match kind {
MethodDefinitionKind::Ordinary => {
obj.set_property(

12
boa/src/syntax/parser/expression/primary/object_initializer/mod.rs

@ -135,6 +135,18 @@ where
return Ok(node::PropertyDefinition::SpreadObject(node));
}
// ComputedPropertyName
// https://tc39.es/ecma262/#prod-ComputedPropertyName
if cursor.next_if(Punctuator::OpenBracket)?.is_some() {
let node = AssignmentExpression::new(false, self.allow_yield, self.allow_await)
.parse(cursor)?;
cursor.expect(Punctuator::CloseBracket, "expected token ']'")?;
cursor.expect(Punctuator::Colon, "expected token ':'")?;
let val = AssignmentExpression::new(false, self.allow_yield, self.allow_await)
.parse(cursor)?;
return Ok(node::PropertyDefinition::ComputedPropertyName(node, val));
}
// Peek for '}' or ',' to indicate shorthand property name
if let Some(next_token) = cursor.peek(1)? {
match next_token.kind() {

Loading…
Cancel
Save