diff --git a/boa_engine/src/syntax/parser/statement/declaration/lexical.rs b/boa_engine/src/syntax/parser/statement/declaration/lexical.rs index cccaef84e1f..ba126e1529e 100644 --- a/boa_engine/src/syntax/parser/statement/declaration/lexical.rs +++ b/boa_engine/src/syntax/parser/statement/declaration/lexical.rs @@ -167,11 +167,9 @@ where const_decls.push(decl); } else { let next = cursor.next(interner)?.ok_or(ParseError::AbruptEnd)?; - return Err(ParseError::expected( - ["=".to_owned()], - next.to_string(interner), - next.span(), - "const declaration", + return Err(ParseError::general( + "Expected initializer for const declaration", + next.span().start(), )); } } else { diff --git a/boa_engine/src/syntax/parser/statement/iteration/for_statement.rs b/boa_engine/src/syntax/parser/statement/iteration/for_statement.rs index 2bd25c803ae..059d05da436 100644 --- a/boa_engine/src/syntax/parser/statement/iteration/for_statement.rs +++ b/boa_engine/src/syntax/parser/statement/iteration/for_statement.rs @@ -146,6 +146,18 @@ where return Ok(ForOfLoop::new(init, iterable, body).into()); } + (Some(Node::ConstDeclList(list)), _) => { + // Reject const declarations without initializers inside for loops + for decl in list.as_ref() { + if decl.init().is_none() { + return Err(ParseError::general( + "Expected initializer for const declaration", + // TODO: get exact position of uninitialized const decl + init_position, + )); + } + } + } _ => {} } diff --git a/boa_engine/src/syntax/parser/statement/iteration/tests.rs b/boa_engine/src/syntax/parser/statement/iteration/tests.rs index a6ad01c4296..f021fa11bf5 100644 --- a/boa_engine/src/syntax/parser/statement/iteration/tests.rs +++ b/boa_engine/src/syntax/parser/statement/iteration/tests.rs @@ -7,7 +7,7 @@ use crate::syntax::{ op::{self, AssignOp, CompOp}, Const, }, - parser::tests::check_parser, + parser::tests::{check_invalid, check_parser}, }; use boa_interner::Interner; @@ -179,3 +179,9 @@ fn do_while_spaces() { &mut interner, ); } + +/// Checks rejection of const bindings without init in for loops +#[test] +fn reject_const_no_init_for_loop() { + check_invalid("for (const h;;);"); +}