Skip to content

Commit 234a24c

Browse files
authoredAug 28, 2024··
fix(ast)!: merge UsingDeclaration into VariableDeclaration (#5270)
relate #2854
1 parent 15b87ad commit 234a24c

File tree

29 files changed

+279
-676
lines changed

29 files changed

+279
-676
lines changed
 

‎crates/oxc_ast/src/ast/js.rs

+8-24
Original file line numberDiff line numberDiff line change
@@ -1197,13 +1197,12 @@ pub enum Declaration<'a> {
11971197
#[visit(args(flags = ScopeFlags::Function))]
11981198
FunctionDeclaration(Box<'a, Function<'a>>) = 33,
11991199
ClassDeclaration(Box<'a, Class<'a>>) = 34,
1200-
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 35,
12011200

1202-
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 36,
1203-
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 37,
1204-
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 38,
1205-
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 39,
1206-
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 40,
1201+
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 35,
1202+
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 36,
1203+
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 37,
1204+
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 38,
1205+
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 39,
12071206
}
12081207

12091208
/// Macro for matching `Declaration`'s variants.
@@ -1213,7 +1212,6 @@ macro_rules! match_declaration {
12131212
$ty::VariableDeclaration(_)
12141213
| $ty::FunctionDeclaration(_)
12151214
| $ty::ClassDeclaration(_)
1216-
| $ty::UsingDeclaration(_)
12171215
| $ty::TSTypeAliasDeclaration(_)
12181216
| $ty::TSInterfaceDeclaration(_)
12191217
| $ty::TSEnumDeclaration(_)
@@ -1248,6 +1246,9 @@ pub enum VariableDeclarationKind {
12481246
Var = 0,
12491247
Const = 1,
12501248
Let = 2,
1249+
Using = 3,
1250+
#[serde(rename = "await using")]
1251+
AwaitUsing = 4,
12511252
}
12521253

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

1268-
/// Using Declaration
1269-
/// * <https://github.com/tc39/proposal-explicit-resource-management>
1270-
#[ast(visit)]
1271-
#[derive(Debug, Hash)]
1272-
#[generate_derive(CloneIn, GetSpan, GetSpanMut)]
1273-
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
1274-
#[serde(tag = "type", rename_all = "camelCase")]
1275-
pub struct UsingDeclaration<'a> {
1276-
#[serde(flatten)]
1277-
pub span: Span,
1278-
pub is_await: bool,
1279-
#[serde(default)]
1280-
pub declarations: Vec<'a, VariableDeclarator<'a>>,
1281-
}
1282-
12831269
/// Empty Statement
12841270
#[ast(visit)]
12851271
#[derive(Debug, Hash)]
@@ -1375,7 +1361,6 @@ inherit_variants! {
13751361
#[serde(untagged)]
13761362
pub enum ForStatementInit<'a> {
13771363
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 64,
1378-
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 65,
13791364
// `Expression` variants added here by `inherit_variants!` macro
13801365
@inherit Expression
13811366
}
@@ -1412,7 +1397,6 @@ inherit_variants! {
14121397
#[serde(untagged)]
14131398
pub enum ForStatementLeft<'a> {
14141399
VariableDeclaration(Box<'a, VariableDeclaration<'a>>) = 16,
1415-
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 17,
14161400
// `AssignmentTarget` variants added here by `inherit_variants!` macro
14171401
@inherit AssignmentTarget
14181402
}

‎crates/oxc_ast/src/ast/macros.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -439,19 +439,17 @@ macro_rules! inherit_variants {
439439
FunctionDeclaration(Box<'a, Function<'a>>) = 33,
440440
/// Inherited from [`Declaration`]
441441
ClassDeclaration(Box<'a, Class<'a>>) = 34,
442-
/// Inherited from [`Declaration`]
443-
UsingDeclaration(Box<'a, UsingDeclaration<'a>>) = 35,
444442

445443
/// Inherited from [`Declaration`]
446-
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 36,
444+
TSTypeAliasDeclaration(Box<'a, TSTypeAliasDeclaration<'a>>) = 35,
447445
/// Inherited from [`Declaration`]
448-
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 37,
446+
TSInterfaceDeclaration(Box<'a, TSInterfaceDeclaration<'a>>) = 36,
449447
/// Inherited from [`Declaration`]
450-
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 38,
448+
TSEnumDeclaration(Box<'a, TSEnumDeclaration<'a>>) = 37,
451449
/// Inherited from [`Declaration`]
452-
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 39,
450+
TSModuleDeclaration(Box<'a, TSModuleDeclaration<'a>>) = 38,
453451
/// Inherited from [`Declaration`]
454-
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 40,
452+
TSImportEqualsDeclaration(Box<'a, TSImportEqualsDeclaration<'a>>) = 39,
455453

456454
$($rest)*
457455
}
@@ -470,7 +468,6 @@ macro_rules! inherit_variants {
470468
VariableDeclaration,
471469
FunctionDeclaration,
472470
ClassDeclaration,
473-
UsingDeclaration,
474471
TSTypeAliasDeclaration,
475472
TSInterfaceDeclaration,
476473
TSEnumDeclaration,

‎crates/oxc_ast/src/ast_impl/js.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ impl<'a> Declaration<'a> {
763763
Self::VariableDeclaration(decl) => decl.is_typescript_syntax(),
764764
Self::FunctionDeclaration(func) => func.is_typescript_syntax(),
765765
Self::ClassDeclaration(class) => class.is_typescript_syntax(),
766-
Self::UsingDeclaration(_) => false,
767766
_ => true,
768767
}
769768
}
@@ -789,7 +788,7 @@ impl<'a> Declaration<'a> {
789788
Declaration::TSTypeAliasDeclaration(decl) => decl.declare,
790789
Declaration::TSModuleDeclaration(decl) => decl.declare,
791790
Declaration::TSInterfaceDeclaration(decl) => decl.declare,
792-
_ => false,
791+
Declaration::TSImportEqualsDeclaration(_) => false,
793792
}
794793
}
795794
}
@@ -817,11 +816,17 @@ impl VariableDeclarationKind {
817816
matches!(self, Self::Const | Self::Let)
818817
}
819818

819+
pub fn is_await(&self) -> bool {
820+
matches!(self, Self::AwaitUsing)
821+
}
822+
820823
pub fn as_str(&self) -> &'static str {
821824
match self {
822825
Self::Var => "var",
823826
Self::Const => "const",
824827
Self::Let => "let",
828+
Self::Using => "using",
829+
Self::AwaitUsing => "await using",
825830
}
826831
}
827832
}

‎crates/oxc_ast/src/ast_kind_impl.rs

-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ impl<'a> AstKind<'a> {
226226
)
227227
.into(),
228228

229-
Self::UsingDeclaration(_) => "UsingDeclaration".into(),
230-
231229
Self::IdentifierName(x) => format!("IdentifierName({})", x.name).into(),
232230
Self::IdentifierReference(x) => format!("IdentifierReference({})", x.name).into(),
233231
Self::BindingIdentifier(x) => format!("BindingIdentifier({})", x.name).into(),

‎crates/oxc_ast/src/generated/assert_layouts.rs

-12
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,6 @@ const _: () = {
370370
assert!(offset_of!(VariableDeclarator, init) == 48usize);
371371
assert!(offset_of!(VariableDeclarator, definite) == 64usize);
372372

373-
assert!(size_of::<UsingDeclaration>() == 48usize);
374-
assert!(align_of::<UsingDeclaration>() == 8usize);
375-
assert!(offset_of!(UsingDeclaration, span) == 0usize);
376-
assert!(offset_of!(UsingDeclaration, is_await) == 8usize);
377-
assert!(offset_of!(UsingDeclaration, declarations) == 16usize);
378-
379373
assert!(size_of::<EmptyStatement>() == 8usize);
380374
assert!(align_of::<EmptyStatement>() == 4usize);
381375
assert!(offset_of!(EmptyStatement, span) == 0usize);
@@ -1776,12 +1770,6 @@ const _: () = {
17761770
assert!(offset_of!(VariableDeclarator, init) == 28usize);
17771771
assert!(offset_of!(VariableDeclarator, definite) == 36usize);
17781772

1779-
assert!(size_of::<UsingDeclaration>() == 28usize);
1780-
assert!(align_of::<UsingDeclaration>() == 4usize);
1781-
assert!(offset_of!(UsingDeclaration, span) == 0usize);
1782-
assert!(offset_of!(UsingDeclaration, is_await) == 8usize);
1783-
assert!(offset_of!(UsingDeclaration, declarations) == 12usize);
1784-
17851773
assert!(size_of::<EmptyStatement>() == 8usize);
17861774
assert!(align_of::<EmptyStatement>() == 4usize);
17871775
assert!(offset_of!(EmptyStatement, span) == 0usize);

‎crates/oxc_ast/src/generated/ast_builder.rs

-129
Original file line numberDiff line numberDiff line change
@@ -4292,37 +4292,6 @@ impl<'a> AstBuilder<'a> {
42924292
Declaration::ClassDeclaration(inner.into_in(self.allocator))
42934293
}
42944294

4295-
/// Build a [`Declaration::UsingDeclaration`]
4296-
///
4297-
/// This node contains a [`UsingDeclaration`] that will be stored in the memory arena.
4298-
///
4299-
/// ## Parameters
4300-
/// - span: The [`Span`] covering this node
4301-
/// - is_await
4302-
/// - declarations
4303-
#[inline]
4304-
pub fn declaration_using(
4305-
self,
4306-
span: Span,
4307-
is_await: bool,
4308-
declarations: Vec<'a, VariableDeclarator<'a>>,
4309-
) -> Declaration<'a> {
4310-
Declaration::UsingDeclaration(self.alloc(self.using_declaration(
4311-
span,
4312-
is_await,
4313-
declarations,
4314-
)))
4315-
}
4316-
4317-
/// Convert a [`UsingDeclaration`] into a [`Declaration::UsingDeclaration`]
4318-
#[inline]
4319-
pub fn declaration_from_using<T>(self, inner: T) -> Declaration<'a>
4320-
where
4321-
T: IntoIn<'a, Box<'a, UsingDeclaration<'a>>>,
4322-
{
4323-
Declaration::UsingDeclaration(inner.into_in(self.allocator))
4324-
}
4325-
43264295
/// Build a [`Declaration::TSTypeAliasDeclaration`]
43274296
///
43284297
/// This node contains a [`TSTypeAliasDeclaration`] that will be stored in the memory arena.
@@ -4591,42 +4560,6 @@ impl<'a> AstBuilder<'a> {
45914560
Box::new_in(self.variable_declarator(span, kind, id, init, definite), self.allocator)
45924561
}
45934562

4594-
/// Builds a [`UsingDeclaration`]
4595-
///
4596-
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_using_declaration`] instead.
4597-
///
4598-
/// ## Parameters
4599-
/// - span: The [`Span`] covering this node
4600-
/// - is_await
4601-
/// - declarations
4602-
#[inline]
4603-
pub fn using_declaration(
4604-
self,
4605-
span: Span,
4606-
is_await: bool,
4607-
declarations: Vec<'a, VariableDeclarator<'a>>,
4608-
) -> UsingDeclaration<'a> {
4609-
UsingDeclaration { span, is_await, declarations }
4610-
}
4611-
4612-
/// Builds a [`UsingDeclaration`] and stores it in the memory arena.
4613-
///
4614-
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::using_declaration`] instead.
4615-
///
4616-
/// ## Parameters
4617-
/// - span: The [`Span`] covering this node
4618-
/// - is_await
4619-
/// - declarations
4620-
#[inline]
4621-
pub fn alloc_using_declaration(
4622-
self,
4623-
span: Span,
4624-
is_await: bool,
4625-
declarations: Vec<'a, VariableDeclarator<'a>>,
4626-
) -> Box<'a, UsingDeclaration<'a>> {
4627-
Box::new_in(self.using_declaration(span, is_await, declarations), self.allocator)
4628-
}
4629-
46304563
/// Builds a [`EmptyStatement`]
46314564
///
46324565
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_empty_statement`] instead.
@@ -4871,37 +4804,6 @@ impl<'a> AstBuilder<'a> {
48714804
ForStatementInit::VariableDeclaration(inner.into_in(self.allocator))
48724805
}
48734806

4874-
/// Build a [`ForStatementInit::UsingDeclaration`]
4875-
///
4876-
/// This node contains a [`UsingDeclaration`] that will be stored in the memory arena.
4877-
///
4878-
/// ## Parameters
4879-
/// - span: The [`Span`] covering this node
4880-
/// - is_await
4881-
/// - declarations
4882-
#[inline]
4883-
pub fn for_statement_init_using_declaration(
4884-
self,
4885-
span: Span,
4886-
is_await: bool,
4887-
declarations: Vec<'a, VariableDeclarator<'a>>,
4888-
) -> ForStatementInit<'a> {
4889-
ForStatementInit::UsingDeclaration(self.alloc(self.using_declaration(
4890-
span,
4891-
is_await,
4892-
declarations,
4893-
)))
4894-
}
4895-
4896-
/// Convert a [`UsingDeclaration`] into a [`ForStatementInit::UsingDeclaration`]
4897-
#[inline]
4898-
pub fn for_statement_init_from_using_declaration<T>(self, inner: T) -> ForStatementInit<'a>
4899-
where
4900-
T: IntoIn<'a, Box<'a, UsingDeclaration<'a>>>,
4901-
{
4902-
ForStatementInit::UsingDeclaration(inner.into_in(self.allocator))
4903-
}
4904-
49054807
#[inline]
49064808
pub fn for_statement_init_expression(self, inner: Expression<'a>) -> ForStatementInit<'a> {
49074809
ForStatementInit::from(inner)
@@ -4981,37 +4883,6 @@ impl<'a> AstBuilder<'a> {
49814883
ForStatementLeft::VariableDeclaration(inner.into_in(self.allocator))
49824884
}
49834885

4984-
/// Build a [`ForStatementLeft::UsingDeclaration`]
4985-
///
4986-
/// This node contains a [`UsingDeclaration`] that will be stored in the memory arena.
4987-
///
4988-
/// ## Parameters
4989-
/// - span: The [`Span`] covering this node
4990-
/// - is_await
4991-
/// - declarations
4992-
#[inline]
4993-
pub fn for_statement_left_using_declaration(
4994-
self,
4995-
span: Span,
4996-
is_await: bool,
4997-
declarations: Vec<'a, VariableDeclarator<'a>>,
4998-
) -> ForStatementLeft<'a> {
4999-
ForStatementLeft::UsingDeclaration(self.alloc(self.using_declaration(
5000-
span,
5001-
is_await,
5002-
declarations,
5003-
)))
5004-
}
5005-
5006-
/// Convert a [`UsingDeclaration`] into a [`ForStatementLeft::UsingDeclaration`]
5007-
#[inline]
5008-
pub fn for_statement_left_from_using_declaration<T>(self, inner: T) -> ForStatementLeft<'a>
5009-
where
5010-
T: IntoIn<'a, Box<'a, UsingDeclaration<'a>>>,
5011-
{
5012-
ForStatementLeft::UsingDeclaration(inner.into_in(self.allocator))
5013-
}
5014-
50154886
#[inline]
50164887
pub fn for_statement_left_assignment_target(
50174888
self,

‎crates/oxc_ast/src/generated/ast_kind.rs

-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub enum AstType {
5757
BlockStatement,
5858
VariableDeclaration,
5959
VariableDeclarator,
60-
UsingDeclaration,
6160
EmptyStatement,
6261
ExpressionStatement,
6362
IfStatement,
@@ -231,7 +230,6 @@ pub enum AstKind<'a> {
231230
BlockStatement(&'a BlockStatement<'a>),
232231
VariableDeclaration(&'a VariableDeclaration<'a>),
233232
VariableDeclarator(&'a VariableDeclarator<'a>),
234-
UsingDeclaration(&'a UsingDeclaration<'a>),
235233
EmptyStatement(&'a EmptyStatement),
236234
ExpressionStatement(&'a ExpressionStatement<'a>),
237235
IfStatement(&'a IfStatement<'a>),
@@ -406,7 +404,6 @@ impl<'a> GetSpan for AstKind<'a> {
406404
Self::BlockStatement(it) => it.span(),
407405
Self::VariableDeclaration(it) => it.span(),
408406
Self::VariableDeclarator(it) => it.span(),
409-
Self::UsingDeclaration(it) => it.span(),
410407
Self::EmptyStatement(it) => it.span(),
411408
Self::ExpressionStatement(it) => it.span(),
412409
Self::IfStatement(it) => it.span(),

0 commit comments

Comments
 (0)
Please sign in to comment.