Skip to content

Commit c631291

Browse files
committed
feat(parser)!: parse TSImportAttributes as ObjectExpression (#9902)
closes #9662 closes #9663 closes #9670
1 parent 2e023ab commit c631291

File tree

23 files changed

+65
-962
lines changed

23 files changed

+65
-962
lines changed

crates/oxc_ast/src/ast/ts.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1279,41 +1279,13 @@ pub enum TSTypeQueryExprName<'a> {
12791279
pub struct TSImportType<'a> {
12801280
pub span: Span,
12811281
pub argument: TSType<'a>,
1282-
pub options: Option<Box<'a, TSImportAttributes<'a>>>,
1282+
pub options: Option<Box<'a, ObjectExpression<'a>>>,
12831283
pub qualifier: Option<TSTypeName<'a>>,
12841284
pub type_arguments: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
12851285
/// `true` for `typeof import("foo")`
12861286
pub is_type_of: bool,
12871287
}
12881288

1289-
#[ast(visit)]
1290-
#[derive(Debug)]
1291-
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
1292-
pub struct TSImportAttributes<'a> {
1293-
pub span: Span,
1294-
pub attributes_keyword: IdentifierName<'a>, // `with` or `assert`
1295-
pub elements: Vec<'a, TSImportAttribute<'a>>,
1296-
}
1297-
1298-
#[ast(visit)]
1299-
#[derive(Debug)]
1300-
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
1301-
// Pluralize as `TSImportAttributeList` to avoid naming clash with `TSImportAttributes`.
1302-
#[plural(TSImportAttributeList)]
1303-
pub struct TSImportAttribute<'a> {
1304-
pub span: Span,
1305-
pub name: TSImportAttributeName<'a>,
1306-
pub value: Expression<'a>,
1307-
}
1308-
1309-
#[ast(visit)]
1310-
#[derive(Debug)]
1311-
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
1312-
pub enum TSImportAttributeName<'a> {
1313-
Identifier(IdentifierName<'a>) = 0,
1314-
StringLiteral(StringLiteral<'a>) = 1,
1315-
}
1316-
13171289
/// TypeScript Function Type
13181290
///
13191291
/// ## Examples

crates/oxc_ast/src/generated/assert_layouts.rs

-30
Original file line numberDiff line numberDiff line change
@@ -1263,21 +1263,6 @@ const _: () = {
12631263
assert!(offset_of!(TSImportType, type_arguments) == 48);
12641264
assert!(offset_of!(TSImportType, is_type_of) == 56);
12651265

1266-
assert!(size_of::<TSImportAttributes>() == 64);
1267-
assert!(align_of::<TSImportAttributes>() == 8);
1268-
assert!(offset_of!(TSImportAttributes, span) == 0);
1269-
assert!(offset_of!(TSImportAttributes, attributes_keyword) == 8);
1270-
assert!(offset_of!(TSImportAttributes, elements) == 32);
1271-
1272-
assert!(size_of::<TSImportAttribute>() == 72);
1273-
assert!(align_of::<TSImportAttribute>() == 8);
1274-
assert!(offset_of!(TSImportAttribute, span) == 0);
1275-
assert!(offset_of!(TSImportAttribute, name) == 8);
1276-
assert!(offset_of!(TSImportAttribute, value) == 56);
1277-
1278-
assert!(size_of::<TSImportAttributeName>() == 48);
1279-
assert!(align_of::<TSImportAttributeName>() == 8);
1280-
12811266
assert!(size_of::<TSFunctionType>() == 48);
12821267
assert!(align_of::<TSFunctionType>() == 8);
12831268
assert!(offset_of!(TSFunctionType, span) == 0);
@@ -2668,21 +2653,6 @@ const _: () = {
26682653
assert!(offset_of!(TSImportType, type_arguments) == 28);
26692654
assert!(offset_of!(TSImportType, is_type_of) == 32);
26702655

2671-
assert!(size_of::<TSImportAttributes>() == 40);
2672-
assert!(align_of::<TSImportAttributes>() == 4);
2673-
assert!(offset_of!(TSImportAttributes, span) == 0);
2674-
assert!(offset_of!(TSImportAttributes, attributes_keyword) == 8);
2675-
assert!(offset_of!(TSImportAttributes, elements) == 24);
2676-
2677-
assert!(size_of::<TSImportAttribute>() == 44);
2678-
assert!(align_of::<TSImportAttribute>() == 4);
2679-
assert!(offset_of!(TSImportAttribute, span) == 0);
2680-
assert!(offset_of!(TSImportAttribute, name) == 8);
2681-
assert!(offset_of!(TSImportAttribute, value) == 36);
2682-
2683-
assert!(size_of::<TSImportAttributeName>() == 28);
2684-
assert!(align_of::<TSImportAttributeName>() == 4);
2685-
26862656
assert!(size_of::<TSFunctionType>() == 28);
26872657
assert!(align_of::<TSFunctionType>() == 4);
26882658
assert!(offset_of!(TSFunctionType, span) == 0);

crates/oxc_ast/src/generated/ast_builder.rs

+4-112
Original file line numberDiff line numberDiff line change
@@ -10323,7 +10323,7 @@ impl<'a> AstBuilder<'a> {
1032310323
is_type_of: bool,
1032410324
) -> TSType<'a>
1032510325
where
10326-
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
10326+
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
1032710327
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
1032810328
{
1032910329
TSType::TSImportType(self.alloc_ts_import_type(
@@ -13435,7 +13435,7 @@ impl<'a> AstBuilder<'a> {
1343513435
is_type_of: bool,
1343613436
) -> TSTypeQueryExprName<'a>
1343713437
where
13438-
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
13438+
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
1343913439
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
1344013440
{
1344113441
TSTypeQueryExprName::TSImportType(self.alloc_ts_import_type(
@@ -13470,7 +13470,7 @@ impl<'a> AstBuilder<'a> {
1347013470
is_type_of: bool,
1347113471
) -> TSImportType<'a>
1347213472
where
13473-
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
13473+
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
1347413474
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
1347513475
{
1347613476
TSImportType {
@@ -13505,7 +13505,7 @@ impl<'a> AstBuilder<'a> {
1350513505
is_type_of: bool,
1350613506
) -> Box<'a, TSImportType<'a>>
1350713507
where
13508-
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
13508+
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
1350913509
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
1351013510
{
1351113511
Box::new_in(
@@ -13514,114 +13514,6 @@ impl<'a> AstBuilder<'a> {
1351413514
)
1351513515
}
1351613516

13517-
/// Build a [`TSImportAttributes`].
13518-
///
13519-
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_import_attributes`] instead.
13520-
///
13521-
/// ## Parameters
13522-
/// * `span`: The [`Span`] covering this node
13523-
/// * `attributes_keyword`
13524-
/// * `elements`
13525-
#[inline]
13526-
pub fn ts_import_attributes(
13527-
self,
13528-
span: Span,
13529-
attributes_keyword: IdentifierName<'a>,
13530-
elements: Vec<'a, TSImportAttribute<'a>>,
13531-
) -> TSImportAttributes<'a> {
13532-
TSImportAttributes { span, attributes_keyword, elements }
13533-
}
13534-
13535-
/// Build a [`TSImportAttributes`], and store it in the memory arena.
13536-
///
13537-
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::ts_import_attributes`] instead.
13538-
///
13539-
/// ## Parameters
13540-
/// * `span`: The [`Span`] covering this node
13541-
/// * `attributes_keyword`
13542-
/// * `elements`
13543-
#[inline]
13544-
pub fn alloc_ts_import_attributes(
13545-
self,
13546-
span: Span,
13547-
attributes_keyword: IdentifierName<'a>,
13548-
elements: Vec<'a, TSImportAttribute<'a>>,
13549-
) -> Box<'a, TSImportAttributes<'a>> {
13550-
Box::new_in(self.ts_import_attributes(span, attributes_keyword, elements), self.allocator)
13551-
}
13552-
13553-
/// Build a [`TSImportAttribute`].
13554-
///
13555-
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_import_attribute`] instead.
13556-
///
13557-
/// ## Parameters
13558-
/// * `span`: The [`Span`] covering this node
13559-
/// * `name`
13560-
/// * `value`
13561-
#[inline]
13562-
pub fn ts_import_attribute(
13563-
self,
13564-
span: Span,
13565-
name: TSImportAttributeName<'a>,
13566-
value: Expression<'a>,
13567-
) -> TSImportAttribute<'a> {
13568-
TSImportAttribute { span, name, value }
13569-
}
13570-
13571-
/// Build a [`TSImportAttribute`], and store it in the memory arena.
13572-
///
13573-
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::ts_import_attribute`] instead.
13574-
///
13575-
/// ## Parameters
13576-
/// * `span`: The [`Span`] covering this node
13577-
/// * `name`
13578-
/// * `value`
13579-
#[inline]
13580-
pub fn alloc_ts_import_attribute(
13581-
self,
13582-
span: Span,
13583-
name: TSImportAttributeName<'a>,
13584-
value: Expression<'a>,
13585-
) -> Box<'a, TSImportAttribute<'a>> {
13586-
Box::new_in(self.ts_import_attribute(span, name, value), self.allocator)
13587-
}
13588-
13589-
/// Build a [`TSImportAttributeName::Identifier`].
13590-
///
13591-
/// ## Parameters
13592-
/// * `span`: The [`Span`] covering this node
13593-
/// * `name`
13594-
#[inline]
13595-
pub fn ts_import_attribute_name_identifier<A>(
13596-
self,
13597-
span: Span,
13598-
name: A,
13599-
) -> TSImportAttributeName<'a>
13600-
where
13601-
A: IntoIn<'a, Atom<'a>>,
13602-
{
13603-
TSImportAttributeName::Identifier(self.identifier_name(span, name))
13604-
}
13605-
13606-
/// Build a [`TSImportAttributeName::StringLiteral`].
13607-
///
13608-
/// ## Parameters
13609-
/// * `span`: Node location in source code
13610-
/// * `value`: The value of the string.
13611-
/// * `raw`: The raw string as it appears in source code.
13612-
#[inline]
13613-
pub fn ts_import_attribute_name_string_literal<A>(
13614-
self,
13615-
span: Span,
13616-
value: A,
13617-
raw: Option<Atom<'a>>,
13618-
) -> TSImportAttributeName<'a>
13619-
where
13620-
A: IntoIn<'a, Atom<'a>>,
13621-
{
13622-
TSImportAttributeName::StringLiteral(self.string_literal(span, value, raw))
13623-
}
13624-
1362513517
/// Build a [`TSFunctionType`].
1362613518
///
1362713519
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_function_type`] instead.

crates/oxc_ast/src/generated/derive_clone_in.rs

-36
Original file line numberDiff line numberDiff line change
@@ -3859,42 +3859,6 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSImportType<'_> {
38593859
}
38603860
}
38613861

3862-
impl<'new_alloc> CloneIn<'new_alloc> for TSImportAttributes<'_> {
3863-
type Cloned = TSImportAttributes<'new_alloc>;
3864-
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
3865-
TSImportAttributes {
3866-
span: CloneIn::clone_in(&self.span, allocator),
3867-
attributes_keyword: CloneIn::clone_in(&self.attributes_keyword, allocator),
3868-
elements: CloneIn::clone_in(&self.elements, allocator),
3869-
}
3870-
}
3871-
}
3872-
3873-
impl<'new_alloc> CloneIn<'new_alloc> for TSImportAttribute<'_> {
3874-
type Cloned = TSImportAttribute<'new_alloc>;
3875-
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
3876-
TSImportAttribute {
3877-
span: CloneIn::clone_in(&self.span, allocator),
3878-
name: CloneIn::clone_in(&self.name, allocator),
3879-
value: CloneIn::clone_in(&self.value, allocator),
3880-
}
3881-
}
3882-
}
3883-
3884-
impl<'new_alloc> CloneIn<'new_alloc> for TSImportAttributeName<'_> {
3885-
type Cloned = TSImportAttributeName<'new_alloc>;
3886-
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
3887-
match self {
3888-
Self::Identifier(it) => {
3889-
TSImportAttributeName::Identifier(CloneIn::clone_in(it, allocator))
3890-
}
3891-
Self::StringLiteral(it) => {
3892-
TSImportAttributeName::StringLiteral(CloneIn::clone_in(it, allocator))
3893-
}
3894-
}
3895-
}
3896-
}
3897-
38983862
impl<'new_alloc> CloneIn<'new_alloc> for TSFunctionType<'_> {
38993863
type Cloned = TSFunctionType<'new_alloc>;
39003864
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {

crates/oxc_ast/src/generated/derive_content_eq.rs

-24
Original file line numberDiff line numberDiff line change
@@ -2305,30 +2305,6 @@ impl ContentEq for TSImportType<'_> {
23052305
}
23062306
}
23072307

2308-
impl ContentEq for TSImportAttributes<'_> {
2309-
fn content_eq(&self, other: &Self) -> bool {
2310-
ContentEq::content_eq(&self.attributes_keyword, &other.attributes_keyword)
2311-
&& ContentEq::content_eq(&self.elements, &other.elements)
2312-
}
2313-
}
2314-
2315-
impl ContentEq for TSImportAttribute<'_> {
2316-
fn content_eq(&self, other: &Self) -> bool {
2317-
ContentEq::content_eq(&self.name, &other.name)
2318-
&& ContentEq::content_eq(&self.value, &other.value)
2319-
}
2320-
}
2321-
2322-
impl ContentEq for TSImportAttributeName<'_> {
2323-
fn content_eq(&self, other: &Self) -> bool {
2324-
match (self, other) {
2325-
(Self::Identifier(a), Self::Identifier(b)) => a.content_eq(b),
2326-
(Self::StringLiteral(a), Self::StringLiteral(b)) => a.content_eq(b),
2327-
_ => false,
2328-
}
2329-
}
2330-
}
2331-
23322308
impl ContentEq for TSFunctionType<'_> {
23332309
fn content_eq(&self, other: &Self) -> bool {
23342310
ContentEq::content_eq(&self.type_parameters, &other.type_parameters)

crates/oxc_ast/src/generated/derive_estree.rs

-33
Original file line numberDiff line numberDiff line change
@@ -3111,39 +3111,6 @@ impl ESTree for TSImportType<'_> {
31113111
}
31123112
}
31133113

3114-
impl ESTree for TSImportAttributes<'_> {
3115-
fn serialize<S: Serializer>(&self, serializer: S) {
3116-
let mut state = serializer.serialize_struct();
3117-
state.serialize_field("type", &JsonSafeString("TSImportAttributes"));
3118-
state.serialize_field("start", &self.span.start);
3119-
state.serialize_field("end", &self.span.end);
3120-
state.serialize_field("attributesKeyword", &self.attributes_keyword);
3121-
state.serialize_field("elements", &self.elements);
3122-
state.end();
3123-
}
3124-
}
3125-
3126-
impl ESTree for TSImportAttribute<'_> {
3127-
fn serialize<S: Serializer>(&self, serializer: S) {
3128-
let mut state = serializer.serialize_struct();
3129-
state.serialize_field("type", &JsonSafeString("TSImportAttribute"));
3130-
state.serialize_field("start", &self.span.start);
3131-
state.serialize_field("end", &self.span.end);
3132-
state.serialize_field("name", &self.name);
3133-
state.serialize_field("value", &self.value);
3134-
state.end();
3135-
}
3136-
}
3137-
3138-
impl ESTree for TSImportAttributeName<'_> {
3139-
fn serialize<S: Serializer>(&self, serializer: S) {
3140-
match self {
3141-
Self::Identifier(it) => it.serialize(serializer),
3142-
Self::StringLiteral(it) => it.serialize(serializer),
3143-
}
3144-
}
3145-
}
3146-
31473114
impl ESTree for TSFunctionType<'_> {
31483115
fn serialize<S: Serializer>(&self, serializer: S) {
31493116
let mut state = serializer.serialize_struct();

crates/oxc_ast/src/generated/derive_get_span.rs

-23
Original file line numberDiff line numberDiff line change
@@ -2015,29 +2015,6 @@ impl GetSpan for TSImportType<'_> {
20152015
}
20162016
}
20172017

2018-
impl GetSpan for TSImportAttributes<'_> {
2019-
#[inline]
2020-
fn span(&self) -> Span {
2021-
self.span
2022-
}
2023-
}
2024-
2025-
impl GetSpan for TSImportAttribute<'_> {
2026-
#[inline]
2027-
fn span(&self) -> Span {
2028-
self.span
2029-
}
2030-
}
2031-
2032-
impl GetSpan for TSImportAttributeName<'_> {
2033-
fn span(&self) -> Span {
2034-
match self {
2035-
Self::Identifier(it) => GetSpan::span(it),
2036-
Self::StringLiteral(it) => GetSpan::span(it),
2037-
}
2038-
}
2039-
}
2040-
20412018
impl GetSpan for TSFunctionType<'_> {
20422019
#[inline]
20432020
fn span(&self) -> Span {

0 commit comments

Comments
 (0)