diff --git a/crates/oxc_ast/src/precedence.rs b/crates/oxc_ast/src/precedence.rs index 899336af5..f2b7217cb 100644 --- a/crates/oxc_ast/src/precedence.rs +++ b/crates/oxc_ast/src/precedence.rs @@ -2,8 +2,8 @@ use oxc_syntax::precedence::{GetPrecedence, Precedence}; use crate::ast::{ ArrowExpression, AssignmentExpression, AwaitExpression, BinaryExpression, CallExpression, - ConditionalExpression, Expression, LogicalExpression, MemberExpression, NewExpression, - SequenceExpression, UnaryExpression, UpdateExpression, YieldExpression, + ConditionalExpression, Expression, ImportExpression, LogicalExpression, MemberExpression, + NewExpression, SequenceExpression, UnaryExpression, UpdateExpression, YieldExpression, }; impl<'a> GetPrecedence for Expression<'a> { @@ -97,6 +97,12 @@ impl<'a> GetPrecedence for CallExpression<'a> { } } +impl<'a> GetPrecedence for ImportExpression<'a> { + fn precedence(&self) -> Precedence { + Precedence::Call + } +} + impl<'a> GetPrecedence for NewExpression<'a> { fn precedence(&self) -> Precedence { if self.arguments.is_empty() { diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 8c8c22c51..3741f121a 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -886,7 +886,7 @@ impl<'a, const MINIFY: bool> GenExpr for Expression<'a> { Self::ConditionalExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::AssignmentExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::SequenceExpression(expr) => expr.gen_expr(p, precedence, ctx), - Self::ImportExpression(expr) => expr.gen(p, ctx), + Self::ImportExpression(expr) => expr.gen_expr(p, precedence, ctx), Self::TemplateLiteral(literal) => literal.gen(p, ctx), Self::TaggedTemplateExpression(expr) => expr.gen(p, ctx), Self::Super(sup) => sup.gen(p, ctx), @@ -1738,15 +1738,19 @@ impl<'a, const MINIFY: bool> GenExpr for SequenceExpression<'a> { } } -impl<'a, const MINIFY: bool> Gen for ImportExpression<'a> { - fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) { - p.print_str(b"import("); - self.source.gen_expr(p, Precedence::Assign, Context::default()); - if !self.arguments.is_empty() { - p.print_comma(); - p.print_expressions(&self.arguments, Precedence::Assign, Context::default()); - } - p.print(b')'); +impl<'a, const MINIFY: bool> GenExpr for ImportExpression<'a> { + fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) { + let wrap = precedence > self.precedence() || ctx.has_forbid_call(); + let ctx = ctx.and_forbid_call(false); + p.wrap(wrap, |p| { + p.print_str(b"import("); + self.source.gen_expr(p, Precedence::Assign, ctx); + if !self.arguments.is_empty() { + p.print_comma(); + p.print_expressions(&self.arguments, Precedence::Assign, ctx); + } + p.print(b')'); + }); } } diff --git a/tasks/coverage/codegen_runtime_test262.snap b/tasks/coverage/codegen_runtime_test262.snap index 18a08ed33..9328848a3 100644 --- a/tasks/coverage/codegen_runtime_test262.snap +++ b/tasks/coverage/codegen_runtime_test262.snap @@ -1,6 +1,6 @@ codegen_runtime_test262 Summary: AST Parsed : 19407/19407 (100.00%) -Positive Passed: 19232/19407 (99.10%) +Positive Passed: 19239/19407 (99.13%) Expect to run correctly: "annexB/built-ins/String/prototype/substr/surrogate-pairs.js" But got a runtime error: Test262Error: start: 1 Expected SameValue(«�», «\udf06») to be true @@ -208,24 +208,6 @@ But got a runtime error: TypeError: c[(intermediate value)] is not a function Expect to run correctly: "language/expressions/class/elements/field-definition-accessor-no-line-terminator.js" But got a runtime error: SyntaxError: Unexpected identifier -Expect to run correctly: "language/expressions/class/private-getter-brand-check-multiple-evaluations-of-class-eval-indirect.js" -But got a runtime error: TypeError: _eval is not a constructor - -Expect to run correctly: "language/expressions/class/private-getter-brand-check-multiple-evaluations-of-class-eval.js" -But got a runtime error: TypeError: eval is not a constructor - -Expect to run correctly: "language/expressions/class/private-method-brand-check-multiple-evaluations-of-class-eval-indirect.js" -But got a runtime error: TypeError: _eval is not a constructor - -Expect to run correctly: "language/expressions/class/private-method-brand-check-multiple-evaluations-of-class-eval.js" -But got a runtime error: TypeError: eval is not a constructor - -Expect to run correctly: "language/expressions/class/private-setter-brand-check-multiple-evaluations-of-class-eval-indirect.js" -But got a runtime error: TypeError: _eval is not a constructor - -Expect to run correctly: "language/expressions/class/private-setter-brand-check-multiple-evaluations-of-class-eval.js" -But got a runtime error: TypeError: eval is not a constructor - Expect to run correctly: "language/expressions/compound-assignment/compound-assignment-operator-calls-putvalue-lref--v--1.js" But got a runtime error: Test262Error: Expected a ReferenceError but got a TypeError @@ -313,9 +295,6 @@ But got a runtime error: Test262Error: Expected SameValue(«function then() { [n Expect to run correctly: "language/expressions/dynamic-import/syntax/valid/nested-with-expression-script-code-valid.js" But got a runtime error: Test262Error: Expected SameValue(«function then() { [native code] }», «function then() { [native code] }») to be true -Expect to run correctly: "language/expressions/dynamic-import/syntax/valid/new-covered-expression-is-valid.js" -But got a runtime error: SyntaxError: Cannot use new with import - Expect to run correctly: "language/expressions/dynamic-import/usage-from-eval.js" But got a runtime error: Test262Error: constructor is %Promise% Expected SameValue(«[object Promise]», «[object Promise]») to be true