Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully implement EmptyStatement #1151

Merged
merged 6 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,3 +1519,13 @@ fn test_strict_mode_dup_func_parameters() {

assert!(string.starts_with("Uncaught \"SyntaxError\": "));
}

#[test]
fn test_empty_statement() {
let src = r#"
;;;let a = 10;;
if(a) ;
a
"#;
assert_eq!(&exec(src), "10");
}
14 changes: 14 additions & 0 deletions boa/src/syntax/ast/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ pub enum Node {

/// A 'while {...}' node. [More information](./iteration/struct.WhileLoop.html).
WhileLoop(WhileLoop),

/// A empty node.
///
/// Empty statement do nothing, just return undefined.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#prod-EmptyStatement
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Empty
Empty,
}

impl Display for Node {
Expand Down Expand Up @@ -274,6 +286,7 @@ impl Node {
Self::AsyncFunctionDecl(ref decl) => decl.display(f, indentation),
Self::AsyncFunctionExpr(ref expr) => expr.display(f, indentation),
Self::AwaitExpr(ref expr) => expr.display(f, indentation),
Self::Empty => write!(f, ";"),
}
}
}
Expand Down Expand Up @@ -338,6 +351,7 @@ impl Executable for Node {
Node::Try(ref try_node) => try_node.run(context),
Node::Break(ref break_node) => break_node.run(context),
Node::Continue(ref continue_node) => continue_node.run(context),
Node::Empty => Ok(Value::Undefined),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions boa/src/syntax/parser/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ where
.parse(cursor)
.map(Node::from)
}
TokenKind::Punctuator(Punctuator::Semicolon) => {
// parse the EmptyStatement
cursor.next().expect("semicolon disappeared");
Ok(Node::Empty)
}
TokenKind::Identifier(_) => {
// Labelled Statement check
cursor.set_goal(InputElement::Div);
Expand Down
27 changes: 23 additions & 4 deletions boa/src/syntax/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use super::Parser;
use crate::syntax::ast::{
node::{
field::GetConstField, ArrowFunctionDecl, Assign, BinOp, Call, FormalParameter,
FunctionDecl, Identifier, LetDecl, LetDeclList, New, Node, Return, StatementList, UnaryOp,
VarDecl, VarDeclList,
FunctionDecl, Identifier, If, LetDecl, LetDeclList, New, Node, Return, StatementList,
UnaryOp, VarDecl, VarDeclList,
},
op::{self, CompOp, LogOp, NumOp},
Const,
Expand Down Expand Up @@ -209,7 +209,7 @@ fn assignment_line_terminator() {
let s = r#"
let a = 3;

a =
a =
5;
"#;

Expand All @@ -232,7 +232,7 @@ fn assignment_multiline_terminator() {
let a = 3;


a =
a =


5;
Expand Down Expand Up @@ -292,3 +292,22 @@ fn spread_in_arrow_function() {
],
);
}

#[test]
fn empty_statement() {
check_parser(
r"
;;var a = 10;
if(a) ;
",
vec![
Node::Empty,
VarDeclList::from(vec![VarDecl::new("a", Node::from(Const::from(10)))]).into(),
Node::If(If::new::<_, _, Node, _>(
Identifier::from("a"),
Node::Empty,
None,
)),
],
);
}