diff --git a/boa_engine/src/syntax/parser/expression/primary/mod.rs b/boa_engine/src/syntax/parser/expression/primary/mod.rs index efd69d4f69f..72788a88095 100644 --- a/boa_engine/src/syntax/parser/expression/primary/mod.rs +++ b/boa_engine/src/syntax/parser/expression/primary/mod.rs @@ -142,6 +142,7 @@ where TokenKind::BooleanLiteral(boolean) => Ok(Const::from(*boolean).into()), TokenKind::NullLiteral => Ok(Const::Null.into()), TokenKind::Identifier(ident) => Ok(Identifier::new(*ident).into()), + TokenKind::Keyword((Keyword::Let, _)) => Ok(Identifier::new(Sym::LET).into()), TokenKind::Keyword((Keyword::Yield, _)) if self.allow_yield.0 => { // Early Error: It is a Syntax Error if this production has a [Yield] parameter and StringValue of Identifier is "yield". Err(ParseError::general( diff --git a/boa_engine/src/syntax/parser/statement/declaration/lexical.rs b/boa_engine/src/syntax/parser/statement/declaration/lexical.rs index f3368679729..1f67ee2ebd6 100644 --- a/boa_engine/src/syntax/parser/statement/declaration/lexical.rs +++ b/boa_engine/src/syntax/parser/statement/declaration/lexical.rs @@ -10,12 +10,14 @@ use crate::syntax::{ ast::{ node::{ - declaration::{Declaration, DeclarationList}, - Node, + declaration::{ + Declaration, DeclarationList, DeclarationPatternArray, DeclarationPatternObject, + }, + DeclarationPattern, Node, }, Keyword, Punctuator, }, - lexer::TokenKind, + lexer::{Error as LexError, TokenKind}, parser::{ cursor::{Cursor, SemicolonResult}, expression::Initializer, @@ -23,7 +25,7 @@ use crate::syntax::{ AllowAwait, AllowIn, AllowYield, ParseError, ParseResult, TokenParser, }, }; -use boa_interner::Interner; +use boa_interner::{Interner, Sym}; use boa_profiler::Profiler; use std::io::Read; @@ -268,6 +270,7 @@ where let _timer = Profiler::global().start_event("LexicalBinding", "Parsing"); let peek_token = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?; + let position = peek_token.span().start(); match peek_token.kind() { TokenKind::Punctuator(Punctuator::OpenBlock) => { @@ -293,7 +296,17 @@ where None }; - Ok(Declaration::new_with_object_pattern(bindings, init)) + let declaration = + DeclarationPattern::Object(DeclarationPatternObject::new(bindings, init)); + + if declaration.idents().contains(&Sym::LET) { + return Err(ParseError::lex(LexError::Syntax( + "'let' is disallowed as a lexically bound name".into(), + position, + ))); + } + + Ok(Declaration::Pattern(declaration)) } TokenKind::Punctuator(Punctuator::OpenBracket) => { let bindings = @@ -318,12 +331,29 @@ where None }; - Ok(Declaration::new_with_array_pattern(bindings, init)) + let declaration = + DeclarationPattern::Array(DeclarationPatternArray::new(bindings, init)); + + if declaration.idents().contains(&Sym::LET) { + return Err(ParseError::lex(LexError::Syntax( + "'let' is disallowed as a lexically bound name".into(), + position, + ))); + } + + Ok(Declaration::Pattern(declaration)) } _ => { let ident = BindingIdentifier::new(self.allow_yield, self.allow_await) .parse(cursor, interner)?; + if ident == Sym::LET { + return Err(ParseError::lex(LexError::Syntax( + "'let' is disallowed as a lexically bound name".into(), + position, + ))); + } + let init = if let Some(t) = cursor.peek(0, interner)? { if *t.kind() == TokenKind::Punctuator(Punctuator::Assign) { Some( diff --git a/boa_engine/src/syntax/parser/statement/mod.rs b/boa_engine/src/syntax/parser/statement/mod.rs index bda1529a47d..40829b0b0d6 100644 --- a/boa_engine/src/syntax/parser/statement/mod.rs +++ b/boa_engine/src/syntax/parser/statement/mod.rs @@ -554,6 +554,13 @@ where next_token.span().start(), ))) } + TokenKind::Keyword((Keyword::Let, _)) if cursor.strict_mode() => { + Err(ParseError::lex(LexError::Syntax( + "unexpected identifier 'let' in strict mode".into(), + next_token.span().start(), + ))) + } + TokenKind::Keyword((Keyword::Let, _)) => Ok(Sym::LET), TokenKind::Identifier(ref s) => Ok(*s), TokenKind::Keyword((Keyword::Yield, _)) if self.allow_yield.0 => { // Early Error: It is a Syntax Error if this production has a [Yield] parameter and StringValue of Identifier is "yield".