From f628f4cc8cd5be5af3430339be25086ee2975c2c Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Fri, 6 Mar 2020 21:58:27 +0100 Subject: [PATCH] Fixed empty returns (#251) (#257) --- boa/src/syntax/parser/mod.rs | 8 +++++- boa/src/syntax/parser/tests.rs | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/boa/src/syntax/parser/mod.rs b/boa/src/syntax/parser/mod.rs index 7fdd712033..e28aa6cdf9 100644 --- a/boa/src/syntax/parser/mod.rs +++ b/boa/src/syntax/parser/mod.rs @@ -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 { diff --git a/boa/src/syntax/parser/tests.rs b/boa/src/syntax/parser/tests.rs index b327c4e56f..7669e96fb8 100644 --- a/boa/src/syntax/parser/tests.rs +++ b/boa/src/syntax/parser/tests.rs @@ -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( @@ -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, + ))]))), + ))], + ); }