Skip to content

Commit

Permalink
Parse class private async generator methods
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Aug 5, 2022
1 parent fc885f1 commit 038b19e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ where
let position = token.span().start();

if let TokenKind::Punctuator(Punctuator::Mul) = token.kind() {
let (property_name, method) =
let (class_element_name, method) =
AsyncGeneratorMethod::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)?;

Expand All @@ -191,6 +191,18 @@ where
return Err(ParseError::general("invalid super usage", position));
}

let property_name =
if let object::ClassElementName::PropertyName(property_name) =
class_element_name
{
property_name
} else {
return Err(ParseError::general(
"private identifiers not allowed in object literal",
position,
));
};

return Ok(object::PropertyDefinition::method_definition(
method,
property_name,
Expand Down Expand Up @@ -751,7 +763,7 @@ impl<R> TokenParser<R> for AsyncGeneratorMethod
where
R: Read,
{
type Output = (object::PropertyName, MethodDefinition);
type Output = (object::ClassElementName, MethodDefinition);

fn parse(
self,
Expand All @@ -765,8 +777,8 @@ where
interner,
)?;

let property_name =
PropertyName::new(self.allow_yield, self.allow_await).parse(cursor, interner)?;
let name =
ClassElementName::new(self.allow_yield, self.allow_await).parse(cursor, interner)?;

let params = UniqueFormalParameters::new(true, true).parse(cursor, interner)?;

Expand Down Expand Up @@ -809,7 +821,7 @@ where
}

Ok((
property_name,
name,
MethodDefinition::AsyncGenerator(AsyncGeneratorExpr::new(None, params, body)),
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::syntax::{
self,
declaration::class_decl::ClassElement as ClassElementNode,
function_contains_super, has_direct_super,
object::{MethodDefinition, PropertyName::Literal},
object::{ClassElementName, MethodDefinition, PropertyName::Literal},
Class, ContainsSymbol, FormalParameterList, FunctionExpr,
},
Keyword, Punctuator,
Expand Down Expand Up @@ -736,30 +736,44 @@ where
TokenKind::Punctuator(Punctuator::Mul) => {
let token = cursor.peek(1, interner)?.ok_or(ParseError::AbruptEnd)?;
let name_position = token.span().start();
if let TokenKind::Identifier(Sym::CONSTRUCTOR) = token.kind() {
return Err(ParseError::general(
"class constructor may not be a generator method",
token.span().start(),
));
match token.kind() {
TokenKind::Identifier(Sym::CONSTRUCTOR)
| TokenKind::PrivateIdentifier(Sym::CONSTRUCTOR) => {
return Err(ParseError::general(
"class constructor may not be a generator method",
token.span().start(),
));
}
_ => {}
}
let strict = cursor.strict_mode();
cursor.set_strict_mode(true);
let (property_name, method) =
let (class_element_name, method) =
AsyncGeneratorMethod::new(self.allow_yield, self.allow_await)
.parse(cursor, interner)?;
cursor.set_strict_mode(strict);
if r#static {
if let Some(name) = property_name.prop_name() {
if name == Sym::PROTOTYPE {
match class_element_name {
ClassElementName::PropertyName(property_name) if r#static => {
if let Some(Sym::PROTOTYPE) = property_name.prop_name() {
return Err(ParseError::general(
"class may not have static method definitions named 'prototype'",
name_position,
));
"class may not have static method definitions named 'prototype'",
name_position,
));
}
ClassElementNode::StaticMethodDefinition(property_name, method)
}
ClassElementName::PropertyName(property_name) => {
ClassElementNode::MethodDefinition(property_name, method)
}
ClassElementName::PrivateIdentifier(private_ident) if r#static => {
ClassElementNode::PrivateStaticMethodDefinition(
private_ident,
method,
)
}
ClassElementName::PrivateIdentifier(private_ident) => {
ClassElementNode::PrivateMethodDefinition(private_ident, method)
}
ClassElementNode::StaticMethodDefinition(property_name, method)
} else {
ClassElementNode::MethodDefinition(property_name, method)
}
}
TokenKind::Identifier(Sym::CONSTRUCTOR) => {
Expand Down

0 comments on commit 038b19e

Please sign in to comment.