Browse Source

Add early error for `await` in class static blocks (#3019)

pull/3020/head
raskad 1 year ago committed by GitHub
parent
commit
b502d25a8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 83
      boa_parser/src/parser/statement/declaration/hoistable/class_decl/mod.rs

83
boa_parser/src/parser/statement/declaration/hoistable/class_decl/mod.rs

@ -659,17 +659,14 @@ where
let strict = cursor.strict();
cursor.set_strict(true);
let position = cursor.peek(0, interner).or_abrupt()?.span().start();
let statement_list = StatementList::new(
false,
true,
false,
&FUNCTION_BREAK_TOKENS,
false,
false,
)
.parse(cursor, interner)?;
let statement_list =
StatementList::new(false, true, false, &FUNCTION_BREAK_TOKENS, false, true)
.parse(cursor, interner)?;
let mut lexical_names = FxHashSet::default();
// It is a Syntax Error if the LexicallyDeclaredNames of
// ClassStaticBlockStatementList contains any duplicate entries.
for name in &lexically_declared_names(&statement_list) {
if !lexical_names.insert(*name) {
return Err(Error::general(
@ -679,6 +676,9 @@ where
}
}
// It is a Syntax Error if any element of the LexicallyDeclaredNames of
// ClassStaticBlockStatementList also occurs in the VarDeclaredNames of
// ClassStaticBlockStatementList.
for name in var_declared_names(&statement_list) {
if lexical_names.contains(&name) {
return Err(Error::general(
@ -688,6 +688,41 @@ where
}
}
// It is a Syntax Error if ContainsDuplicateLabels of
// ClassStaticBlockStatementList with argument « » is true.
// It is a Syntax Error if ContainsUndefinedBreakTarget of
// ClassStaticBlockStatementList with argument « » is true.
// It is a Syntax Error if ContainsUndefinedContinueTarget of
// ClassStaticBlockStatementList with arguments « » and « » is true.
check_labels(&statement_list).map_err(|error| {
Error::lex(LexError::Syntax(error.message(interner).into(), position))
})?;
// It is a Syntax Error if ContainsArguments of ClassStaticBlockStatementList is true.
if contains_arguments(&statement_list) {
return Err(Error::general(
"'arguments' not allowed in class static block",
position,
));
}
// It is a Syntax Error if ClassStaticBlockStatementList Contains SuperCall is true.
if contains(&statement_list, ContainsSymbol::SuperCall) {
return Err(Error::general("invalid super usage", position));
}
// It is a Syntax Error if ClassStaticBlockStatementList Contains await is true.
if contains(&statement_list, ContainsSymbol::AwaitExpression) {
return Err(Error::general("invalid await usage", position));
}
if contains_invalid_object_literal(&statement_list) {
return Err(Error::lex(LexError::Syntax(
"invalid object literal in class static block statement list".into(),
position,
)));
}
cursor.expect(
TokenKind::Punctuator(Punctuator::CloseBlock),
"class definition",
@ -1346,37 +1381,7 @@ where
));
}
}
// ClassStaticBlockBody : ClassStaticBlockStatementList
function::ClassElement::StaticBlock(block) => {
for node in &**block.statements() {
// It is a Syntax Error if ContainsArguments of ClassStaticBlockStatementList is true.
if contains_arguments(node) {
return Err(Error::general(
"'arguments' not allowed in class static block",
position,
));
}
// It is a Syntax Error if ClassStaticBlockStatementList Contains SuperCall is true.
if contains(node, ContainsSymbol::SuperCall) {
return Err(Error::general("invalid super usage", position));
}
}
if let Err(error) = check_labels(block) {
return Err(Error::lex(LexError::Syntax(
error.message(interner).into(),
position,
)));
}
if contains_invalid_object_literal(block) {
return Err(Error::lex(LexError::Syntax(
"invalid object literal in class static block statement list".into(),
position,
)));
}
}
_ => {}
}

Loading…
Cancel
Save