Skip to content

Commit

Permalink
Merge 5f486aa into 5cfe529
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored May 22, 2020
2 parents 5cfe529 + 5f486aa commit 3c03b12
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
16 changes: 8 additions & 8 deletions boa/src/syntax/parser/statement/if_stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ impl TokenParser for IfStatement {
let then_stm =
Statement::new(self.allow_yield, self.allow_await, self.allow_return).parse(cursor)?;

let else_stm = match cursor.next() {
Some(else_tok) if else_tok.kind == TokenKind::Keyword(Keyword::Else) => Some(
Statement::new(self.allow_yield, self.allow_await, self.allow_return)
.parse(cursor)?,
),
_ => {
cursor.back();
None
let else_stm = match cursor.peek(0) {
Some(else_tok) if else_tok.kind == TokenKind::Keyword(Keyword::Else) => {
cursor.next();
Some(
Statement::new(self.allow_yield, self.allow_await, self.allow_return)
.parse(cursor)?,
)
}
_ => None,
};

Ok(Node::if_node::<_, _, Node, _>(cond, then_stm, else_stm))
Expand Down
36 changes: 36 additions & 0 deletions boa/src/syntax/parser/statement/if_stm/tests.rs
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
use crate::syntax::{ast::node::Node, parser::tests::check_parser};

#[test]
fn if_with_else_block() {
check_parser(
"if (true) {} else {}",
vec![Node::if_node::<_, _, Node, _>(
Node::const_node(true),
Node::block(Vec::new()),
Some(Node::block(Vec::new())),
)],
);
}

#[test]
fn if_without_else_block() {
check_parser(
"if (true) {}",
vec![Node::if_node::<_, _, Node, _>(
Node::const_node(true),
Node::block(Vec::new()),
None,
)],
);
}

#[test]
fn if_without_else_block_with_trailing_newline() {
check_parser(
"if (true) {}\n",
vec![Node::if_node::<_, _, Node, _>(
Node::const_node(true),
Node::block(Vec::new()),
None,
)],
);
}

0 comments on commit 3c03b12

Please sign in to comment.