Skip to content

Commit

Permalink
Fixed empty returns (boa-dev#251) (boa-dev#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Mar 6, 2020
1 parent 86052d6 commit f628f4c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion boa/src/syntax/parser/mod.rs
Expand Up @@ -222,7 +222,13 @@ impl Parser {

Ok(Expr::new(ExprDef::ConstDecl(vars)))
}
Keyword::Return => Ok(Expr::new(ExprDef::Return(Some(Box::new(self.parse()?))))),
Keyword::Return => match self.get_token(self.pos)?.data {
TokenData::Punctuator(Punctuator::Semicolon)
| TokenData::Punctuator(Punctuator::CloseBlock) => {
Ok(Expr::new(ExprDef::Return(None)))
}
_ => Ok(Expr::new(ExprDef::Return(Some(Box::new(self.parse()?))))),
},
Keyword::New => {
let call = self.parse()?;
match call.def {
Expand Down
48 changes: 48 additions & 0 deletions boa/src/syntax/parser/tests.rs
Expand Up @@ -631,6 +631,28 @@ fn check_function_declarations() {
))],
);

check_parser(
"function foo(a) { return; }",
&[Expr::new(ExprDef::FunctionDecl(
Some(String::from("foo")),
vec![Expr::new(ExprDef::Local(String::from("a")))],
Box::new(Expr::new(ExprDef::Block(vec![Expr::new(ExprDef::Return(
None,
))]))),
))],
);

check_parser(
"function foo(a) { return }",
&[Expr::new(ExprDef::FunctionDecl(
Some(String::from("foo")),
vec![Expr::new(ExprDef::Local(String::from("a")))],
Box::new(Expr::new(ExprDef::Block(vec![Expr::new(ExprDef::Return(
None,
))]))),
))],
);

check_parser(
"function (a, ...b) {}",
&[Expr::new(ExprDef::FunctionDecl(
Expand Down Expand Up @@ -688,4 +710,30 @@ fn check_function_declarations() {
))]))),
))],
);

check_parser(
"(a, b) => { return; }",
&[Expr::new(ExprDef::ArrowFunctionDecl(
vec![
Expr::new(ExprDef::Local(String::from("a"))),
Expr::new(ExprDef::Local(String::from("b"))),
],
Box::new(Expr::new(ExprDef::Block(vec![Expr::new(ExprDef::Return(
None,
))]))),
))],
);

check_parser(
"(a, b) => { return }",
&[Expr::new(ExprDef::ArrowFunctionDecl(
vec![
Expr::new(ExprDef::Local(String::from("a"))),
Expr::new(ExprDef::Local(String::from("b"))),
],
Box::new(Expr::new(ExprDef::Block(vec![Expr::new(ExprDef::Return(
None,
))]))),
))],
);
}

0 comments on commit f628f4c

Please sign in to comment.