Browse Source

Implement parsing for object shorthand properties (#1324)

pull/1516/head
David M 3 years ago committed by GitHub
parent
commit
d86b56927e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      boa/src/syntax/parser/expression/primary/object_initializer/mod.rs
  2. 62
      boa/src/syntax/parser/expression/primary/object_initializer/tests.rs

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

@ -9,6 +9,7 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use crate::syntax::ast::node::Identifier;
use crate::syntax::lexer::TokenKind; use crate::syntax::lexer::TokenKind;
use crate::{ use crate::{
syntax::{ syntax::{
@ -134,6 +135,27 @@ where
return Ok(node::PropertyDefinition::SpreadObject(node)); return Ok(node::PropertyDefinition::SpreadObject(node));
} }
// Peek for '}' or ',' to indicate shorthand property name
if let Some(next_token) = cursor.peek(1)? {
match next_token.kind() {
TokenKind::Punctuator(Punctuator::CloseBlock)
| TokenKind::Punctuator(Punctuator::Comma) => {
let token = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;
if let TokenKind::Identifier(ident) = token.kind() {
// ident is both the name and value in a shorthand property
let name = ident.to_string();
let value = Identifier::from(ident.to_owned());
cursor.next()?.expect("token vanished"); // Consume the token.
return Ok(node::PropertyDefinition::property(name, value));
} else {
// Anything besides an identifier is a syntax error
return Err(ParseError::unexpected(token.clone(), "object literal"));
}
}
_ => {}
}
}
let prop_name = cursor.next()?.ok_or(ParseError::AbruptEnd)?.to_string(); let prop_name = cursor.next()?.ok_or(ParseError::AbruptEnd)?.to_string();
if cursor.next_if(Punctuator::Colon)?.is_some() { if cursor.next_if(Punctuator::Colon)?.is_some() {
let val = AssignmentExpression::new(true, self.allow_yield, self.allow_await) let val = AssignmentExpression::new(true, self.allow_yield, self.allow_await)

62
boa/src/syntax/parser/expression/primary/object_initializer/tests.rs

@ -1,8 +1,8 @@
use crate::syntax::{ use crate::syntax::{
ast::{ ast::{
node::{ node::{
Declaration, DeclarationList, FormalParameter, FunctionExpr, MethodDefinitionKind, Declaration, DeclarationList, FormalParameter, FunctionExpr, Identifier,
Object, PropertyDefinition, MethodDefinitionKind, Object, PropertyDefinition,
}, },
Const, Const,
}, },
@ -203,3 +203,61 @@ fn check_object_short_function_set() {
.into()], .into()],
); );
} }
#[test]
fn check_object_shorthand_property_names() {
let object_properties = vec![PropertyDefinition::property("a", Identifier::from("a"))];
check_parser(
"const a = true;
const x = { a };
",
vec![
DeclarationList::Const(
vec![Declaration::new("a", Some(Const::from(true).into()))].into(),
)
.into(),
DeclarationList::Const(
vec![Declaration::new(
"x",
Some(Object::from(object_properties).into()),
)]
.into(),
)
.into(),
],
);
}
#[test]
fn check_object_shorthand_multiple_properties() {
let object_properties = vec![
PropertyDefinition::property("a", Identifier::from("a")),
PropertyDefinition::property("b", Identifier::from("b")),
];
check_parser(
"const a = true;
const b = false;
const x = { a, b, };
",
vec![
DeclarationList::Const(
vec![Declaration::new("a", Some(Const::from(true).into()))].into(),
)
.into(),
DeclarationList::Const(
vec![Declaration::new("b", Some(Const::from(false).into()))].into(),
)
.into(),
DeclarationList::Const(
vec![Declaration::new(
"x",
Some(Object::from(object_properties).into()),
)]
.into(),
)
.into(),
],
);
}

Loading…
Cancel
Save