Skip to content

Commit

Permalink
Fix get/set as short method name in object (#1072)
Browse files Browse the repository at this point in the history
Co-authored-by: tofpie <tofpie@users.noreply.github.com>
  • Loading branch information
tofpie and tofpie committed Jan 16, 2021
1 parent e0f226a commit 1098b41
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ where
let _timer = BoaProfiler::global().start_event("MethodDefinition", "Parsing");

let (methodkind, prop_name, params) = match self.identifier.as_str() {
idn @ "get" | idn @ "set" => {
idn @ "get" | idn @ "set"
if matches!(
cursor.peek(0)?.map(|t| t.kind()),
Some(&TokenKind::Identifier(_)) | Some(&TokenKind::Keyword(_))
| Some(&TokenKind::BooleanLiteral(_)) | Some(&TokenKind::NullLiteral)
| Some(&TokenKind::NumericLiteral(_))
) =>
{
let prop_name = cursor.next()?.ok_or(ParseError::AbruptEnd)?.to_string();
cursor.expect(
TokenKind::Punctuator(Punctuator::OpenParen),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,45 @@ fn check_object_setter() {
.into()],
);
}

#[test]
fn check_object_short_function_get() {
let object_properties = vec![PropertyDefinition::method_definition(
MethodDefinitionKind::Ordinary,
"get",
FunctionExpr::new(None, vec![], vec![]),
)];

check_parser(
"const x = {
get() {}
};
",
vec![ConstDeclList::from(vec![ConstDecl::new(
"x",
Some(Object::from(object_properties)),
)])
.into()],
);
}

#[test]
fn check_object_short_function_set() {
let object_properties = vec![PropertyDefinition::method_definition(
MethodDefinitionKind::Ordinary,
"set",
FunctionExpr::new(None, vec![], vec![]),
)];

check_parser(
"const x = {
set() {}
};
",
vec![ConstDeclList::from(vec![ConstDecl::new(
"x",
Some(Object::from(object_properties)),
)])
.into()],
);
}

0 comments on commit 1098b41

Please sign in to comment.