Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Allow let as variable declaration name #2044

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boa_engine/src/syntax/parser/expression/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
42 changes: 36 additions & 6 deletions boa_engine/src/syntax/parser/statement/declaration/lexical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
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,
statement::{ArrayBindingPattern, BindingIdentifier, ObjectBindingPattern},
AllowAwait, AllowIn, AllowYield, ParseError, ParseResult, TokenParser,
},
};
use boa_interner::Interner;
use boa_interner::{Interner, Sym};
use boa_profiler::Profiler;
use std::io::Read;

Expand Down Expand Up @@ -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) => {
Expand All @@ -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 =
Expand All @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions boa_engine/src/syntax/parser/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down