Skip to content

Commit

Permalink
Deny const declarations without initializer inside for loops (#1903)
Browse files Browse the repository at this point in the history
<!---
Thank you for contributing to Boa! Please fill out the template below, and remove or add any
information as you feel neccesary.
--->

This Pull Request fixes/closes #1897.

It changes the following:

- Rejects uninitialized const declarations inside the init value of a for loop statement.
- Adds test for the case.
  • Loading branch information
jedel1043 committed Mar 7, 2022
1 parent a49b57b commit 2a6ea9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 3 additions & 5 deletions boa_engine/src/syntax/parser/statement/declaration/lexical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions boa_engine/src/syntax/parser/statement/iteration/for_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
}
}
}
_ => {}
}

Expand Down
8 changes: 7 additions & 1 deletion boa_engine/src/syntax/parser/statement/iteration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;;);");
}

0 comments on commit 2a6ea9d

Please sign in to comment.