LibJS: Allow parenthesized dynamic import under new

Parenthesized import calls are valid callees for new expressions. The
unparenthesized form remains a syntax error, but the grouped expression
must parse and then fail at runtime if the produced Promise is not a
constructor.

Add syntax coverage for both forms.
This commit is contained in:
Andreas Kling 2026-05-22 02:14:35 +02:00 committed by Andreas Kling
parent f6b1625e12
commit 8b64f0079b
2 changed files with 7 additions and 1 deletions

View file

@ -1189,6 +1189,7 @@ impl Parser<'_> {
return self.expression(start, ExpressionKind::MetaProperty(MetaPropertyType::NewTarget));
}
let callee_starts_with_grouping_paren = self.match_token(TokenType::ParenOpen);
let callee = if self.match_token(TokenType::New) {
self.parse_new_expression()
} else {
@ -1196,7 +1197,7 @@ impl Parser<'_> {
self.parse_expression(PRECEDENCE_MEMBER, Associativity::Right, forbidden)
};
if matches!(callee.inner, ExpressionKind::ImportCall(_)) {
if matches!(callee.inner, ExpressionKind::ImportCall(_)) && !callee_starts_with_grouping_paren {
self.syntax_error("Cannot call new on dynamic import");
}

View file

@ -5,6 +5,11 @@ describe("parsing", () => {
expect("import('a', {options: true})").toEval();
});
test("parenthesized import call can be used as a new callee", () => {
expect("new import('a')").not.toEval();
expect("new (import('a'), function() {})").toEval();
});
test("does not crash on unexpected tokens after import", () => {
expect("f = import('a')").toEval();