Skip to content

Commit c538efa

Browse files
authored
fix(ast)!: ImportExpression only allows one option argument (#10432)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
1 parent c0f0369 commit c538efa

File tree

18 files changed

+115
-153
lines changed

18 files changed

+115
-153
lines changed

crates/oxc_ast/src/ast/js.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,8 +2351,7 @@ pub struct AccessorProperty<'a> {
23512351
pub struct ImportExpression<'a> {
23522352
pub span: Span,
23532353
pub source: Expression<'a>,
2354-
#[estree(via = ImportExpressionOptions)]
2355-
pub options: Vec<'a, Expression<'a>>,
2354+
pub options: Option<Expression<'a>>,
23562355
#[estree(skip)]
23572356
pub phase: Option<ImportPhase>,
23582357
}

crates/oxc_ast/src/generated/assert_layouts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,12 @@ const _: () = {
658658
assert!(offset_of!(AccessorProperty, type_annotation) == 88);
659659
assert!(offset_of!(AccessorProperty, accessibility) == 96);
660660

661-
assert!(size_of::<ImportExpression>() == 64);
661+
assert!(size_of::<ImportExpression>() == 48);
662662
assert!(align_of::<ImportExpression>() == 8);
663663
assert!(offset_of!(ImportExpression, span) == 0);
664664
assert!(offset_of!(ImportExpression, source) == 8);
665665
assert!(offset_of!(ImportExpression, options) == 24);
666-
assert!(offset_of!(ImportExpression, phase) == 56);
666+
assert!(offset_of!(ImportExpression, phase) == 40);
667667

668668
assert!(size_of::<ImportDeclaration>() == 112);
669669
assert!(align_of::<ImportDeclaration>() == 8);
@@ -2051,12 +2051,12 @@ const _: () = {
20512051
assert!(offset_of!(AccessorProperty, type_annotation) == 48);
20522052
assert!(offset_of!(AccessorProperty, accessibility) == 52);
20532053

2054-
assert!(size_of::<ImportExpression>() == 36);
2054+
assert!(size_of::<ImportExpression>() == 28);
20552055
assert!(align_of::<ImportExpression>() == 4);
20562056
assert!(offset_of!(ImportExpression, span) == 0);
20572057
assert!(offset_of!(ImportExpression, source) == 8);
20582058
assert!(offset_of!(ImportExpression, options) == 16);
2059-
assert!(offset_of!(ImportExpression, phase) == 32);
2059+
assert!(offset_of!(ImportExpression, phase) == 24);
20602060

20612061
assert!(size_of::<ImportDeclaration>() == 64);
20622062
assert!(align_of::<ImportDeclaration>() == 4);

crates/oxc_ast/src/generated/ast_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ impl<'a> AstBuilder<'a> {
800800
self,
801801
span: Span,
802802
source: Expression<'a>,
803-
options: Vec<'a, Expression<'a>>,
803+
options: Option<Expression<'a>>,
804804
phase: Option<ImportPhase>,
805805
) -> Expression<'a> {
806806
Expression::ImportExpression(self.alloc_import_expression(span, source, options, phase))
@@ -7334,7 +7334,7 @@ impl<'a> AstBuilder<'a> {
73347334
self,
73357335
span: Span,
73367336
source: Expression<'a>,
7337-
options: Vec<'a, Expression<'a>>,
7337+
options: Option<Expression<'a>>,
73387338
phase: Option<ImportPhase>,
73397339
) -> ImportExpression<'a> {
73407340
ImportExpression { span, source, options, phase }
@@ -7355,7 +7355,7 @@ impl<'a> AstBuilder<'a> {
73557355
self,
73567356
span: Span,
73577357
source: Expression<'a>,
7358-
options: Vec<'a, Expression<'a>>,
7358+
options: Option<Expression<'a>>,
73597359
phase: Option<ImportPhase>,
73607360
) -> Box<'a, ImportExpression<'a>> {
73617361
Box::new_in(self.import_expression(span, source, options, phase), self.allocator)

crates/oxc_ast/src/generated/derive_estree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ impl ESTree for ImportExpression<'_> {
16511651
state.serialize_field("start", &self.span.start);
16521652
state.serialize_field("end", &self.span.end);
16531653
state.serialize_field("source", &self.source);
1654-
state.serialize_field("options", &crate::serialize::ImportExpressionOptions(self));
1654+
state.serialize_field("options", &self.options);
16551655
state.end();
16561656
}
16571657
}

crates/oxc_ast/src/serialize.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -730,29 +730,6 @@ impl ESTree for AssignmentTargetPropertyIdentifierValue<'_> {
730730
}
731731
}
732732

733-
/// Serializer for `options` field of `ImportExpression`.
734-
///
735-
/// Serialize only the first expression in `options`, or `null` if `options` is empty.
736-
#[ast_meta]
737-
#[estree(
738-
ts_type = "Expression | null",
739-
raw_deser = "
740-
const options = DESER[Vec<Expression>](POS_OFFSET.options);
741-
options.length === 0 ? null : options[0]
742-
"
743-
)]
744-
pub struct ImportExpressionOptions<'a>(pub &'a ImportExpression<'a>);
745-
746-
impl ESTree for ImportExpressionOptions<'_> {
747-
fn serialize<S: Serializer>(&self, serializer: S) {
748-
if let Some(expression) = self.0.options.first() {
749-
expression.serialize(serializer);
750-
} else {
751-
().serialize(serializer);
752-
}
753-
}
754-
}
755-
756733
// Serializers for `with_clause` field of `ImportDeclaration`, `ExportNamedDeclaration`,
757734
// and `ExportAllDeclaration` (which are renamed to `attributes` in ESTree AST).
758735
//

crates/oxc_ast_visit/src/generated/visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2666,7 +2666,9 @@ pub mod walk {
26662666
visitor.enter_node(kind);
26672667
visitor.visit_span(&it.span);
26682668
visitor.visit_expression(&it.source);
2669-
visitor.visit_expressions(&it.options);
2669+
if let Some(options) = &it.options {
2670+
visitor.visit_expression(options);
2671+
}
26702672
visitor.leave_node(kind);
26712673
}
26722674

crates/oxc_ast_visit/src/generated/visit_mut.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,9 @@ pub mod walk_mut {
27672767
visitor.enter_node(kind);
27682768
visitor.visit_span(&mut it.span);
27692769
visitor.visit_expression(&mut it.source);
2770-
visitor.visit_expressions(&mut it.options);
2770+
if let Some(options) = &mut it.options {
2771+
visitor.visit_expression(options);
2772+
}
27712773
visitor.leave_node(kind);
27722774
}
27732775

crates/oxc_codegen/src/gen.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,8 +2087,8 @@ impl GenExpr for ImportExpression<'_> {
20872087
|| p.has_comment(self.source.span().start)
20882088
|| self
20892089
.options
2090-
.first()
2091-
.is_some_and(|argument| p.has_comment(argument.span().start)));
2090+
.as_ref()
2091+
.is_some_and(|options| p.has_comment(options.span().start)));
20922092

20932093
p.wrap(wrap, |p| {
20942094
p.print_space_before_identifier();
@@ -2108,15 +2108,15 @@ impl GenExpr for ImportExpression<'_> {
21082108
p.print_indent();
21092109
}
21102110
self.source.print_expr(p, Precedence::Comma, Context::empty());
2111-
if !self.options.is_empty() {
2111+
if let Some(options) = &self.options {
21122112
p.print_comma();
21132113
if has_comment {
21142114
p.print_soft_newline();
21152115
p.print_indent();
21162116
} else {
21172117
p.print_soft_space();
21182118
}
2119-
p.print_expressions(&self.options, Precedence::Comma, Context::empty());
2119+
options.gen_expr(p, Precedence::Comma, Context::empty());
21202120
}
21212121
if has_comment {
21222122
// Handle `/* comment */);`

crates/oxc_formatter/src/write/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,8 @@ impl<'a> FormatWrite<'a> for AccessorProperty<'a> {
10681068
impl<'a> FormatWrite<'a> for ImportExpression<'a> {
10691069
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
10701070
write!(f, ["import(", self.source])?;
1071-
for option in &self.options {
1072-
write!(f, [",", space(), option])?;
1071+
if let Some(options) = &self.options {
1072+
write!(f, [",", space(), options])?;
10731073
}
10741074
write!(f, ")")
10751075
}

crates/oxc_parser/src/diagnostics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ pub fn private_in_private(span: Span) -> OxcDiagnostic {
514514
OxcDiagnostic::error("Unexpected right-hand side of private-in expression").with_label(span)
515515
}
516516

517+
#[cold]
518+
pub fn import_arguments(span: Span) -> OxcDiagnostic {
519+
OxcDiagnostic::error("Dynamic imports can only accept a module specifier and an optional set of attributes as arguments").with_label(span)
520+
}
521+
517522
// ================================= MODIFIERS =================================
518523

519524
#[cold]

0 commit comments

Comments
 (0)