Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser)!: parse TSImportAttributes as ObjectExpression #9902

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 1 addition & 29 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,41 +1279,13 @@ pub enum TSTypeQueryExprName<'a> {
pub struct TSImportType<'a> {
pub span: Span,
pub argument: TSType<'a>,
pub options: Option<Box<'a, TSImportAttributes<'a>>>,
pub options: Option<Box<'a, ObjectExpression<'a>>>,
pub qualifier: Option<TSTypeName<'a>>,
pub type_arguments: Option<Box<'a, TSTypeParameterInstantiation<'a>>>,
/// `true` for `typeof import("foo")`
pub is_type_of: bool,
}

#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
pub struct TSImportAttributes<'a> {
pub span: Span,
pub attributes_keyword: IdentifierName<'a>, // `with` or `assert`
pub elements: Vec<'a, TSImportAttribute<'a>>,
}

#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
// Pluralize as `TSImportAttributeList` to avoid naming clash with `TSImportAttributes`.
#[plural(TSImportAttributeList)]
pub struct TSImportAttribute<'a> {
pub span: Span,
pub name: TSImportAttributeName<'a>,
pub value: Expression<'a>,
}

#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
pub enum TSImportAttributeName<'a> {
Identifier(IdentifierName<'a>) = 0,
StringLiteral(StringLiteral<'a>) = 1,
}

/// TypeScript Function Type
///
/// ## Examples
Expand Down
30 changes: 0 additions & 30 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,21 +1263,6 @@ const _: () = {
assert!(offset_of!(TSImportType, type_arguments) == 48);
assert!(offset_of!(TSImportType, is_type_of) == 56);

assert!(size_of::<TSImportAttributes>() == 64);
assert!(align_of::<TSImportAttributes>() == 8);
assert!(offset_of!(TSImportAttributes, span) == 0);
assert!(offset_of!(TSImportAttributes, attributes_keyword) == 8);
assert!(offset_of!(TSImportAttributes, elements) == 32);

assert!(size_of::<TSImportAttribute>() == 72);
assert!(align_of::<TSImportAttribute>() == 8);
assert!(offset_of!(TSImportAttribute, span) == 0);
assert!(offset_of!(TSImportAttribute, name) == 8);
assert!(offset_of!(TSImportAttribute, value) == 56);

assert!(size_of::<TSImportAttributeName>() == 48);
assert!(align_of::<TSImportAttributeName>() == 8);

assert!(size_of::<TSFunctionType>() == 48);
assert!(align_of::<TSFunctionType>() == 8);
assert!(offset_of!(TSFunctionType, span) == 0);
Expand Down Expand Up @@ -2668,21 +2653,6 @@ const _: () = {
assert!(offset_of!(TSImportType, type_arguments) == 28);
assert!(offset_of!(TSImportType, is_type_of) == 32);

assert!(size_of::<TSImportAttributes>() == 40);
assert!(align_of::<TSImportAttributes>() == 4);
assert!(offset_of!(TSImportAttributes, span) == 0);
assert!(offset_of!(TSImportAttributes, attributes_keyword) == 8);
assert!(offset_of!(TSImportAttributes, elements) == 24);

assert!(size_of::<TSImportAttribute>() == 44);
assert!(align_of::<TSImportAttribute>() == 4);
assert!(offset_of!(TSImportAttribute, span) == 0);
assert!(offset_of!(TSImportAttribute, name) == 8);
assert!(offset_of!(TSImportAttribute, value) == 36);

assert!(size_of::<TSImportAttributeName>() == 28);
assert!(align_of::<TSImportAttributeName>() == 4);

assert!(size_of::<TSFunctionType>() == 28);
assert!(align_of::<TSFunctionType>() == 4);
assert!(offset_of!(TSFunctionType, span) == 0);
Expand Down
116 changes: 4 additions & 112 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10323,7 +10323,7 @@ impl<'a> AstBuilder<'a> {
is_type_of: bool,
) -> TSType<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
TSType::TSImportType(self.alloc_ts_import_type(
Expand Down Expand Up @@ -13435,7 +13435,7 @@ impl<'a> AstBuilder<'a> {
is_type_of: bool,
) -> TSTypeQueryExprName<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
TSTypeQueryExprName::TSImportType(self.alloc_ts_import_type(
Expand Down Expand Up @@ -13470,7 +13470,7 @@ impl<'a> AstBuilder<'a> {
is_type_of: bool,
) -> TSImportType<'a>
where
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
TSImportType {
Expand Down Expand Up @@ -13505,7 +13505,7 @@ impl<'a> AstBuilder<'a> {
is_type_of: bool,
) -> Box<'a, TSImportType<'a>>
where
T1: IntoIn<'a, Option<Box<'a, TSImportAttributes<'a>>>>,
T1: IntoIn<'a, Option<Box<'a, ObjectExpression<'a>>>>,
T2: IntoIn<'a, Option<Box<'a, TSTypeParameterInstantiation<'a>>>>,
{
Box::new_in(
Expand All @@ -13514,114 +13514,6 @@ impl<'a> AstBuilder<'a> {
)
}

/// Build a [`TSImportAttributes`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_import_attributes`] instead.
///
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `attributes_keyword`
/// * `elements`
#[inline]
pub fn ts_import_attributes(
self,
span: Span,
attributes_keyword: IdentifierName<'a>,
elements: Vec<'a, TSImportAttribute<'a>>,
) -> TSImportAttributes<'a> {
TSImportAttributes { span, attributes_keyword, elements }
}

/// Build a [`TSImportAttributes`], and store it in the memory arena.
///
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::ts_import_attributes`] instead.
///
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `attributes_keyword`
/// * `elements`
#[inline]
pub fn alloc_ts_import_attributes(
self,
span: Span,
attributes_keyword: IdentifierName<'a>,
elements: Vec<'a, TSImportAttribute<'a>>,
) -> Box<'a, TSImportAttributes<'a>> {
Box::new_in(self.ts_import_attributes(span, attributes_keyword, elements), self.allocator)
}

/// Build a [`TSImportAttribute`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_import_attribute`] instead.
///
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `name`
/// * `value`
#[inline]
pub fn ts_import_attribute(
self,
span: Span,
name: TSImportAttributeName<'a>,
value: Expression<'a>,
) -> TSImportAttribute<'a> {
TSImportAttribute { span, name, value }
}

/// Build a [`TSImportAttribute`], and store it in the memory arena.
///
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::ts_import_attribute`] instead.
///
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `name`
/// * `value`
#[inline]
pub fn alloc_ts_import_attribute(
self,
span: Span,
name: TSImportAttributeName<'a>,
value: Expression<'a>,
) -> Box<'a, TSImportAttribute<'a>> {
Box::new_in(self.ts_import_attribute(span, name, value), self.allocator)
}

/// Build a [`TSImportAttributeName::Identifier`].
///
/// ## Parameters
/// * `span`: The [`Span`] covering this node
/// * `name`
#[inline]
pub fn ts_import_attribute_name_identifier<A>(
self,
span: Span,
name: A,
) -> TSImportAttributeName<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
TSImportAttributeName::Identifier(self.identifier_name(span, name))
}

/// Build a [`TSImportAttributeName::StringLiteral`].
///
/// ## Parameters
/// * `span`: Node location in source code
/// * `value`: The value of the string.
/// * `raw`: The raw string as it appears in source code.
#[inline]
pub fn ts_import_attribute_name_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> TSImportAttributeName<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
TSImportAttributeName::StringLiteral(self.string_literal(span, value, raw))
}

/// Build a [`TSFunctionType`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_function_type`] instead.
Expand Down
36 changes: 0 additions & 36 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3859,42 +3859,6 @@ impl<'new_alloc> CloneIn<'new_alloc> for TSImportType<'_> {
}
}

impl<'new_alloc> CloneIn<'new_alloc> for TSImportAttributes<'_> {
type Cloned = TSImportAttributes<'new_alloc>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
TSImportAttributes {
span: CloneIn::clone_in(&self.span, allocator),
attributes_keyword: CloneIn::clone_in(&self.attributes_keyword, allocator),
elements: CloneIn::clone_in(&self.elements, allocator),
}
}
}

impl<'new_alloc> CloneIn<'new_alloc> for TSImportAttribute<'_> {
type Cloned = TSImportAttribute<'new_alloc>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
TSImportAttribute {
span: CloneIn::clone_in(&self.span, allocator),
name: CloneIn::clone_in(&self.name, allocator),
value: CloneIn::clone_in(&self.value, allocator),
}
}
}

impl<'new_alloc> CloneIn<'new_alloc> for TSImportAttributeName<'_> {
type Cloned = TSImportAttributeName<'new_alloc>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
match self {
Self::Identifier(it) => {
TSImportAttributeName::Identifier(CloneIn::clone_in(it, allocator))
}
Self::StringLiteral(it) => {
TSImportAttributeName::StringLiteral(CloneIn::clone_in(it, allocator))
}
}
}
}

impl<'new_alloc> CloneIn<'new_alloc> for TSFunctionType<'_> {
type Cloned = TSFunctionType<'new_alloc>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Expand Down
24 changes: 0 additions & 24 deletions crates/oxc_ast/src/generated/derive_content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2305,30 +2305,6 @@ impl ContentEq for TSImportType<'_> {
}
}

impl ContentEq for TSImportAttributes<'_> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.attributes_keyword, &other.attributes_keyword)
&& ContentEq::content_eq(&self.elements, &other.elements)
}
}

impl ContentEq for TSImportAttribute<'_> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.name, &other.name)
&& ContentEq::content_eq(&self.value, &other.value)
}
}

impl ContentEq for TSImportAttributeName<'_> {
fn content_eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Identifier(a), Self::Identifier(b)) => a.content_eq(b),
(Self::StringLiteral(a), Self::StringLiteral(b)) => a.content_eq(b),
_ => false,
}
}
}

impl ContentEq for TSFunctionType<'_> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.type_parameters, &other.type_parameters)
Expand Down
33 changes: 0 additions & 33 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3111,39 +3111,6 @@ impl ESTree for TSImportType<'_> {
}
}

impl ESTree for TSImportAttributes<'_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut state = serializer.serialize_struct();
state.serialize_field("type", &JsonSafeString("TSImportAttributes"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("attributesKeyword", &self.attributes_keyword);
state.serialize_field("elements", &self.elements);
state.end();
}
}

impl ESTree for TSImportAttribute<'_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut state = serializer.serialize_struct();
state.serialize_field("type", &JsonSafeString("TSImportAttribute"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("value", &self.value);
state.end();
}
}

impl ESTree for TSImportAttributeName<'_> {
fn serialize<S: Serializer>(&self, serializer: S) {
match self {
Self::Identifier(it) => it.serialize(serializer),
Self::StringLiteral(it) => it.serialize(serializer),
}
}
}

impl ESTree for TSFunctionType<'_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let mut state = serializer.serialize_struct();
Expand Down
23 changes: 0 additions & 23 deletions crates/oxc_ast/src/generated/derive_get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2015,29 +2015,6 @@ impl GetSpan for TSImportType<'_> {
}
}

impl GetSpan for TSImportAttributes<'_> {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl GetSpan for TSImportAttribute<'_> {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl GetSpan for TSImportAttributeName<'_> {
fn span(&self) -> Span {
match self {
Self::Identifier(it) => GetSpan::span(it),
Self::StringLiteral(it) => GetSpan::span(it),
}
}
}

impl GetSpan for TSFunctionType<'_> {
#[inline]
fn span(&self) -> Span {
Expand Down
Loading
Loading