Skip to content

Commit

Permalink
Add early errors for LexicalDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Aug 10, 2023
1 parent 3a6715a commit f58311d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions boa_parser/src/parser/statement/declaration/lexical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ast::operations::bound_names;
use boa_ast::{self as ast, declaration::Variable, pattern::Pattern, Keyword, Punctuator};
use boa_interner::{Interner, Sym};
use boa_profiler::Profiler;
use rustc_hash::FxHashSet;
use std::io::Read;

/// Parses a lexical declaration.
Expand Down Expand Up @@ -99,6 +100,25 @@ where
cursor.expect_semicolon("lexical declaration", interner)?;
}

// It is a Syntax Error if the BoundNames of BindingList contains "let".
// It is a Syntax Error if the BoundNames of BindingList contains any duplicate entries.
let bound_names = bound_names(&lexical_declaration);
let mut names = FxHashSet::default();
for name in bound_names {
if name.sym() == Sym::LET {
return Err(Error::general(
"'let' is disallowed as a lexically bound name",
tok.span().start(),
));
}
if !names.insert(name) {
return Err(Error::general(
"lexical name declared multiple times",
tok.span().start(),
));
}
}

Ok(lexical_declaration)
}
}
Expand Down
11 changes: 11 additions & 0 deletions boa_parser/src/parser/statement/declaration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,14 @@ fn multiple_const_declaration() {
interner,
);
}

/// Checks `LexicalDeclaration` early errors.
#[test]
fn lexical_declaration_early_errors() {
check_invalid_script("let let = 0");
check_invalid_script("let a = 0, a = 0");
check_invalid_script("const a = 0, a = 0");
check_invalid_script("for (let let = 0; ; ) {}");
check_invalid_script("for (let a = 0, a = 0; ; ) {}");
check_invalid_script("for (const a = 0, a = 0; ; ) {}");
}

0 comments on commit f58311d

Please sign in to comment.