From 1d99d6faeea77714fca341419df3cf87323a46e3 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Fri, 22 May 2020 15:27:34 +0200 Subject: [PATCH] Fixed parsing if statement without else block preceded by a newline --- boa/src/syntax/parser/statement/if_stm/mod.rs | 16 +++++----- .../syntax/parser/statement/if_stm/tests.rs | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/boa/src/syntax/parser/statement/if_stm/mod.rs b/boa/src/syntax/parser/statement/if_stm/mod.rs index e20840303f4..7d7f95785bb 100644 --- a/boa/src/syntax/parser/statement/if_stm/mod.rs +++ b/boa/src/syntax/parser/statement/if_stm/mod.rs @@ -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)) diff --git a/boa/src/syntax/parser/statement/if_stm/tests.rs b/boa/src/syntax/parser/statement/if_stm/tests.rs index 8b137891791..eb6fdd7951d 100644 --- a/boa/src/syntax/parser/statement/if_stm/tests.rs +++ b/boa/src/syntax/parser/statement/if_stm/tests.rs @@ -1 +1,31 @@ +use crate::syntax::{ + ast::{ + node::{Block, Node}, + Const, + }, + parser::tests::check_parser, +}; +#[test] +fn if_without_else_block() { + check_parser( + "if (true) {}", + vec![Node::if_node::<_, _, Node, _>( + Const::from(true), + Block::from(Vec::new()), + None, + )], + ); +} + +#[test] +fn if_without_else_block_with_trailing_newline() { + check_parser( + "if (true) {}\n", + vec![Node::if_node::<_, _, Node, _>( + Const::from(true), + Block::from(Vec::new()), + None, + )], + ); +}