LibJS: Allow super property lookup and new.target in static init blocks

This commit is contained in:
davidot 2021-12-19 02:06:22 +01:00 committed by Linus Groh
parent 92672b1520
commit 45578f58dc
Notes: sideshowbarker 2024-07-17 22:31:33 +09:00
2 changed files with 28 additions and 1 deletions

View File

@ -1200,6 +1200,7 @@ NonnullRefPtr<ClassExpression> Parser::parse_class_expression(bool expect_class_
TemporaryChange async_function_context_rollback(m_state.await_expression_is_valid, false); TemporaryChange async_function_context_rollback(m_state.await_expression_is_valid, false);
TemporaryChange class_field_initializer_rollback(m_state.in_class_field_initializer, true); TemporaryChange class_field_initializer_rollback(m_state.in_class_field_initializer, true);
TemporaryChange class_static_init_block_rollback(m_state.in_class_static_init_block, true); TemporaryChange class_static_init_block_rollback(m_state.in_class_static_init_block, true);
TemporaryChange super_property_access_rollback(m_state.allow_super_property_lookup, true);
ScopePusher static_init_scope = ScopePusher::static_init_block_scope(*this, *static_init_block); ScopePusher static_init_scope = ScopePusher::static_init_block_scope(*this, *static_init_block);
parse_statement_list(static_init_block); parse_statement_list(static_init_block);
@ -1416,7 +1417,7 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression()
auto new_start = position(); auto new_start = position();
auto new_target_result = try_parse_new_target_expression(); auto new_target_result = try_parse_new_target_expression();
if (!new_target_result.is_null()) { if (!new_target_result.is_null()) {
if (!m_state.in_function_context) if (!m_state.in_function_context && !m_state.in_class_static_init_block)
syntax_error("'new.target' not allowed outside of a function", new_start); syntax_error("'new.target' not allowed outside of a function", new_start);
return { new_target_result.release_nonnull() }; return { new_target_result.release_nonnull() };
} }

View File

@ -46,3 +46,29 @@ test("correct this", () => {
expect(thisValue).toBe(A); expect(thisValue).toBe(A);
}); });
describe("class like constructs can be used inside", () => {
test("can use new.target", () => {
let value = 1;
class C {
static {
value = new.target;
}
}
expect(value).toBeUndefined();
});
test("can use super property lookup", () => {
function parent() {}
parent.val = 3;
let hit = false;
class C extends parent {
static {
hit = true;
expect(super.val).toBe(3);
}
}
expect(hit).toBeTrue();
});
});