Skip to content

Commit

Permalink
Add async arrow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed Jan 12, 2021
1 parent be0e0b3 commit 970534e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//!
//! [mdn]:
//! [spec]: https://tc39.es/ecma262/#prod-AsyncArrowFunction
#[cfg(test)]
mod tests;

use super::ConciseBody;
use crate::{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::syntax::{
ast::node::{
AsyncArrowFunctionDecl, BinOp, FormalParameter, Identifier, LetDecl, LetDeclList, Node,
Return,
},
ast::op::NumOp,
parser::tests::check_parser,
};

/// Checks an arrow function with expression return.
#[test]
fn check_async_arrow() {
check_parser(
"async (a, b) => { return a + b; }",
vec![AsyncArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
],
vec![Return::new(
BinOp::new(NumOp::Add, Identifier::from("a"), Identifier::from("b")),
None,
)
.into()],
)
.into()],
);
}

#[test]
fn check_async_arrow_assignment() {
check_parser(
"let foo = async (a) => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
AsyncArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_async_arrow_assignment_noparenthesis() {
check_parser(
"let foo = async a => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
AsyncArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}
6 changes: 5 additions & 1 deletion boa/src/syntax/parser/expression/assignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ where
let _timer = BoaProfiler::global().start_event("AssignmentExpression", "Parsing");
cursor.set_goal(InputElement::Div);

println!("Assignment expression");
// Arrow function
match cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.kind() {
// Async a => {} or Async function or Async (a) => {}
Expand All @@ -104,9 +105,12 @@ where
.map(Node::AsyncArrowFunctionDecl);
}
TokenKind::Punctuator(Punctuator::OpenParen) => {
println!("OpenParen");
todo!("Async arrow func with () params");
}
_ => {}
_ => {
println!("Async not open Paren / Identifier");
}
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions boa/src/syntax/parser/expression/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,17 @@ where
TokenKind::Keyword(Keyword::Function) => {
FunctionExpression.parse(cursor).map(Node::from)
}
TokenKind::Keyword(Keyword::Async) => AsyncFunctionExpression::new(self.allow_yield)
.parse(cursor)
.map(Node::from),
TokenKind::Keyword(Keyword::Async) => {
println!("Keyword async");
match cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.kind() {
TokenKind::Keyword(Keyword::Function) => {
AsyncFunctionExpression::new(self.allow_yield)
.parse(cursor)
.map(Node::from)
}
_ => Err(ParseError::unexpected(tok.clone(), "primary expression")),
}
}
TokenKind::Punctuator(Punctuator::OpenParen) => {
cursor.set_goal(InputElement::RegExp);
let expr =
Expand Down

0 comments on commit 970534e

Please sign in to comment.