Skip to content

Commit

Permalink
fix(ast)!: merge UsingDeclaration into VariableDeclaration (#5270)
Browse files Browse the repository at this point in the history
relate #2854
  • Loading branch information
sxzz committed Aug 28, 2024
1 parent 15b87ad commit 234a24c
Show file tree
Hide file tree
Showing 29 changed files with 279 additions and 676 deletions.
32 changes: 8 additions & 24 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,13 +1197,12 @@ pub enum Declaration<'a> {
#[visit(args(flags = ScopeFlags::Function))]
FunctionDeclaration(Box<'a, Function<'a>>) = 33,
ClassDeclaration(Box<'a, Class<'a>>) = 34,
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 35,

TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 36,
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 37,
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 38,
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 39,
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 40,
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 35,
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 36,
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 37,
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 38,
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 39,
}

/// Macro for matching `Declaration`'s variants.
Expand All @@ -1213,7 +1212,6 @@ macro_rules! match_declaration {
$ty::VariableDeclaration(_)
| $ty::FunctionDeclaration(_)
| $ty::ClassDeclaration(_)
| $ty::UsingDeclaration(_)
| $ty::TSTypeAliasDeclaration(_)
| $ty::TSInterfaceDeclaration(_)
| $ty::TSEnumDeclaration(_)
Expand Down Expand Up @@ -1248,6 +1246,9 @@ pub enum VariableDeclarationKind {
Var = 0,
Const = 1,
Let = 2,
Using = 3,
#[serde(rename = "await using")]
AwaitUsing = 4,
}

#[ast(visit)]
Expand All @@ -1265,21 +1266,6 @@ pub struct VariableDeclarator<'a> {
pub definite: bool,
}

/// Using Declaration
/// * <https://github.com/tc39/proposal-explicit-resource-management>
#[ast(visit)]
#[derive(Debug, Hash)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[serde(tag = "type", rename_all = "camelCase")]
pub struct UsingDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
pub is_await: bool,
#[serde(default)]
pub declarations: Vec<'a, VariableDeclarator<'a>>,
}

/// Empty Statement
#[ast(visit)]
#[derive(Debug, Hash)]
Expand Down Expand Up @@ -1375,7 +1361,6 @@ inherit_variants! {
#[serde(untagged)]
pub enum ForStatementInit<'a> {
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 64,
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 65,
// `Expression` variants added here by `inherit_variants!` macro
@inherit Expression
}
Expand Down Expand Up @@ -1412,7 +1397,6 @@ inherit_variants! {
#[serde(untagged)]
pub enum ForStatementLeft<'a> {
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 16,
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 17,
// `AssignmentTarget` variants added here by `inherit_variants!` macro
@inherit AssignmentTarget
}
Expand Down
13 changes: 5 additions & 8 deletions crates/oxc_ast/src/ast/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,17 @@ macro_rules! inherit_variants {
FunctionDeclaration(Box<'a, Function<'a>>) = 33,
/// Inherited from [`Declaration`]
ClassDeclaration(Box<'a, Class<'a>>) = 34,
/// Inherited from [`Declaration`]
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 35,

/// Inherited from [`Declaration`]
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 36,
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 35,
/// Inherited from [`Declaration`]
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 37,
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 36,
/// Inherited from [`Declaration`]
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 38,
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 37,
/// Inherited from [`Declaration`]
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 39,
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 38,
/// Inherited from [`Declaration`]
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 40,
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 39,

$($rest)*
}
Expand All @@ -470,7 +468,6 @@ macro_rules! inherit_variants {
VariableDeclaration,
FunctionDeclaration,
ClassDeclaration,
UsingDeclaration,
TSTypeAliasDeclaration,
TSInterfaceDeclaration,
TSEnumDeclaration,
Expand Down
9 changes: 7 additions & 2 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ impl<'a> Declaration<'a> {
Self::VariableDeclaration(decl) => decl.is_typescript_syntax(),
Self::FunctionDeclaration(func) => func.is_typescript_syntax(),
Self::ClassDeclaration(class) => class.is_typescript_syntax(),
Self::UsingDeclaration(_) => false,
_ => true,
}
}
Expand All @@ -789,7 +788,7 @@ impl<'a> Declaration<'a> {
Declaration::TSTypeAliasDeclaration(decl) => decl.declare,
Declaration::TSModuleDeclaration(decl) => decl.declare,
Declaration::TSInterfaceDeclaration(decl) => decl.declare,
_ => false,
Declaration::TSImportEqualsDeclaration(_) => false,
}
}
}
Expand Down Expand Up @@ -817,11 +816,17 @@ impl VariableDeclarationKind {
matches!(self, Self::Const | Self::Let)
}

pub fn is_await(&self) -> bool {
matches!(self, Self::AwaitUsing)
}

pub fn as_str(&self) -> &'static str {
match self {
Self::Var => "var",
Self::Const => "const",
Self::Let => "let",
Self::Using => "using",
Self::AwaitUsing => "await using",
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ impl<'a> AstKind<'a> {
)
.into(),

Self::UsingDeclaration(_) => "UsingDeclaration".into(),

Self::IdentifierName(x) => format!("IdentifierName({})", x.name).into(),
Self::IdentifierReference(x) => format!("IdentifierReference({})", x.name).into(),
Self::BindingIdentifier(x) => format!("BindingIdentifier({})", x.name).into(),
Expand Down
12 changes: 0 additions & 12 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,6 @@ const _: () = {
assert!(offset_of!(VariableDeclarator, init) == 48usize);
assert!(offset_of!(VariableDeclarator, definite) == 64usize);

assert!(size_of::<UsingDeclaration>() == 48usize);
assert!(align_of::<UsingDeclaration>() == 8usize);
assert!(offset_of!(UsingDeclaration, span) == 0usize);
assert!(offset_of!(UsingDeclaration, is_await) == 8usize);
assert!(offset_of!(UsingDeclaration, declarations) == 16usize);

assert!(size_of::<EmptyStatement>() == 8usize);
assert!(align_of::<EmptyStatement>() == 4usize);
assert!(offset_of!(EmptyStatement, span) == 0usize);
Expand Down Expand Up @@ -1776,12 +1770,6 @@ const _: () = {
assert!(offset_of!(VariableDeclarator, init) == 28usize);
assert!(offset_of!(VariableDeclarator, definite) == 36usize);

assert!(size_of::<UsingDeclaration>() == 28usize);
assert!(align_of::<UsingDeclaration>() == 4usize);
assert!(offset_of!(UsingDeclaration, span) == 0usize);
assert!(offset_of!(UsingDeclaration, is_await) == 8usize);
assert!(offset_of!(UsingDeclaration, declarations) == 12usize);

assert!(size_of::<EmptyStatement>() == 8usize);
assert!(align_of::<EmptyStatement>() == 4usize);
assert!(offset_of!(EmptyStatement, span) == 0usize);
Expand Down
129 changes: 0 additions & 129 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4292,37 +4292,6 @@ impl<'a> AstBuilder<'a> {
Declaration::ClassDeclaration(inner.into_in(self.allocator))
}

/// Build a [`Declaration::UsingDeclaration`]
///
/// This node contains a [`UsingDeclaration`] that will be stored in the memory arena.
///
/// ## Parameters
/// - span: The [`Span`] covering this node
/// - is_await
/// - declarations
#[inline]
pub fn declaration_using(
self,
span: Span,
is_await: bool,
declarations: Vec<'a, VariableDeclarator<'a>>,
) -> Declaration<'a> {
Declaration::UsingDeclaration(self.alloc(self.using_declaration(
span,
is_await,
declarations,
)))
}

/// Convert a [`UsingDeclaration`] into a [`Declaration::UsingDeclaration`]
#[inline]
pub fn declaration_from_using<T>(self, inner: T) -> Declaration<'a>
where
T: IntoIn<'a, Box<'a, UsingDeclaration<'a>>>,
{
Declaration::UsingDeclaration(inner.into_in(self.allocator))
}

/// Build a [`Declaration::TSTypeAliasDeclaration`]
///
/// This node contains a [`TSTypeAliasDeclaration`] that will be stored in the memory arena.
Expand Down Expand Up @@ -4591,42 +4560,6 @@ impl<'a> AstBuilder<'a> {
Box::new_in(self.variable_declarator(span, kind, id, init, definite), self.allocator)
}

/// Builds a [`UsingDeclaration`]
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_using_declaration`] instead.
///
/// ## Parameters
/// - span: The [`Span`] covering this node
/// - is_await
/// - declarations
#[inline]
pub fn using_declaration(
self,
span: Span,
is_await: bool,
declarations: Vec<'a, VariableDeclarator<'a>>,
) -> UsingDeclaration<'a> {
UsingDeclaration { span, is_await, declarations }
}

/// Builds a [`UsingDeclaration`] and stores it in the memory arena.
///
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::using_declaration`] instead.
///
/// ## Parameters
/// - span: The [`Span`] covering this node
/// - is_await
/// - declarations
#[inline]
pub fn alloc_using_declaration(
self,
span: Span,
is_await: bool,
declarations: Vec<'a, VariableDeclarator<'a>>,
) -> Box<'a, UsingDeclaration<'a>> {
Box::new_in(self.using_declaration(span, is_await, declarations), self.allocator)
}

/// Builds a [`EmptyStatement`]
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_empty_statement`] instead.
Expand Down Expand Up @@ -4871,37 +4804,6 @@ impl<'a> AstBuilder<'a> {
ForStatementInit::VariableDeclaration(inner.into_in(self.allocator))
}

/// Build a [`ForStatementInit::UsingDeclaration`]
///
/// This node contains a [`UsingDeclaration`] that will be stored in the memory arena.
///
/// ## Parameters
/// - span: The [`Span`] covering this node
/// - is_await
/// - declarations
#[inline]
pub fn for_statement_init_using_declaration(
self,
span: Span,
is_await: bool,
declarations: Vec<'a, VariableDeclarator<'a>>,
) -> ForStatementInit<'a> {
ForStatementInit::UsingDeclaration(self.alloc(self.using_declaration(
span,
is_await,
declarations,
)))
}

/// Convert a [`UsingDeclaration`] into a [`ForStatementInit::UsingDeclaration`]
#[inline]
pub fn for_statement_init_from_using_declaration<T>(self, inner: T) -> ForStatementInit<'a>
where
T: IntoIn<'a, Box<'a, UsingDeclaration<'a>>>,
{
ForStatementInit::UsingDeclaration(inner.into_in(self.allocator))
}

#[inline]
pub fn for_statement_init_expression(self, inner: Expression<'a>) -> ForStatementInit<'a> {
ForStatementInit::from(inner)
Expand Down Expand Up @@ -4981,37 +4883,6 @@ impl<'a> AstBuilder<'a> {
ForStatementLeft::VariableDeclaration(inner.into_in(self.allocator))
}

/// Build a [`ForStatementLeft::UsingDeclaration`]
///
/// This node contains a [`UsingDeclaration`] that will be stored in the memory arena.
///
/// ## Parameters
/// - span: The [`Span`] covering this node
/// - is_await
/// - declarations
#[inline]
pub fn for_statement_left_using_declaration(
self,
span: Span,
is_await: bool,
declarations: Vec<'a, VariableDeclarator<'a>>,
) -> ForStatementLeft<'a> {
ForStatementLeft::UsingDeclaration(self.alloc(self.using_declaration(
span,
is_await,
declarations,
)))
}

/// Convert a [`UsingDeclaration`] into a [`ForStatementLeft::UsingDeclaration`]
#[inline]
pub fn for_statement_left_from_using_declaration<T>(self, inner: T) -> ForStatementLeft<'a>
where
T: IntoIn<'a, Box<'a, UsingDeclaration<'a>>>,
{
ForStatementLeft::UsingDeclaration(inner.into_in(self.allocator))
}

#[inline]
pub fn for_statement_left_assignment_target(
self,
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/generated/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub enum AstType {
BlockStatement,
VariableDeclaration,
VariableDeclarator,
UsingDeclaration,
EmptyStatement,
ExpressionStatement,
IfStatement,
Expand Down Expand Up @@ -231,7 +230,6 @@ pub enum AstKind<'a> {
BlockStatement(&'a BlockStatement<'a>),
VariableDeclaration(&'a VariableDeclaration<'a>),
VariableDeclarator(&'a VariableDeclarator<'a>),
UsingDeclaration(&'a UsingDeclaration<'a>),
EmptyStatement(&'a EmptyStatement),
ExpressionStatement(&'a ExpressionStatement<'a>),
IfStatement(&'a IfStatement<'a>),
Expand Down Expand Up @@ -406,7 +404,6 @@ impl<'a> GetSpan for AstKind<'a> {
Self::BlockStatement(it) => it.span(),
Self::VariableDeclaration(it) => it.span(),
Self::VariableDeclarator(it) => it.span(),
Self::UsingDeclaration(it) => it.span(),
Self::EmptyStatement(it) => it.span(),
Self::ExpressionStatement(it) => it.span(),
Self::IfStatement(it) => it.span(),
Expand Down
Loading

0 comments on commit 234a24c

Please sign in to comment.