Skip to content

Commit 1061baa

Browse files
committed
refactor(traverse): separate #[scope] attr (#3901)
Separate out attributes which communicate info to codegen related to scopes into `#[scope]` attr. Before: ```rs #[visited_node(scope(ScopeFlags::empty()))] pub struct BlockStatement<'a> { /* ... */ } ``` After: ```rs #[visited_node] #[scope] pub struct BlockStatement<'a> { /* ... */ } ``` I think this is clearer.
1 parent 5ef28b7 commit 1061baa

File tree

5 files changed

+172
-96
lines changed

5 files changed

+172
-96
lines changed

crates/oxc_ast/src/ast/js.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
2+
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.
3+
14
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
25
#![allow(non_snake_case)]
36

@@ -23,8 +26,9 @@ use serde::Serialize;
2326
#[cfg(feature = "serialize")]
2427
use tsify::Tsify;
2528

26-
#[visited_node(
27-
scope(ScopeFlags::Top),
29+
#[visited_node]
30+
#[scope(
31+
flags(ScopeFlags::Top),
2832
strict_if(self.source_type.is_strict() || self.directives.iter().any(Directive::is_use_strict)),
2933
)]
3034
#[derive(Debug)]
@@ -943,7 +947,8 @@ pub struct Hashbang<'a> {
943947
}
944948

945949
/// Block Statement
946-
#[visited_node(scope(ScopeFlags::empty()))]
950+
#[visited_node]
951+
#[scope]
947952
#[derive(Debug)]
948953
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
949954
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -1099,10 +1104,8 @@ pub struct WhileStatement<'a> {
10991104
}
11001105

11011106
/// For Statement
1102-
#[visited_node(
1103-
scope(ScopeFlags::empty()),
1104-
scope_if(self.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration)),
1105-
)]
1107+
#[visited_node]
1108+
#[scope(if(self.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration)))]
11061109
#[derive(Debug)]
11071110
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
11081111
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -1136,7 +1139,8 @@ pub enum ForStatementInit<'a> {
11361139
}
11371140

11381141
/// For-In Statement
1139-
#[visited_node(scope(ScopeFlags::empty()), scope_if(self.left.is_lexical_declaration()))]
1142+
#[visited_node]
1143+
#[scope(if(self.left.is_lexical_declaration()))]
11401144
#[derive(Debug)]
11411145
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
11421146
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -1168,7 +1172,8 @@ pub enum ForStatementLeft<'a> {
11681172
}
11691173
}
11701174
/// For-Of Statement
1171-
#[visited_node(scope(ScopeFlags::empty()), scope_if(self.left.is_lexical_declaration()))]
1175+
#[visited_node]
1176+
#[scope(if(self.left.is_lexical_declaration()))]
11721177
#[derive(Debug)]
11731178
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
11741179
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -1228,7 +1233,8 @@ pub struct WithStatement<'a> {
12281233
}
12291234

12301235
/// Switch Statement
1231-
#[visited_node(scope(ScopeFlags::empty()))]
1236+
#[visited_node]
1237+
#[scope]
12321238
#[derive(Debug)]
12331239
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
12341240
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -1288,7 +1294,8 @@ pub struct TryStatement<'a> {
12881294
pub finalizer: Option<Box<'a, BlockStatement<'a>>>,
12891295
}
12901296

1291-
#[visited_node(scope(ScopeFlags::empty()), scope_if(self.param.is_some()))]
1297+
#[visited_node]
1298+
#[scope(if(self.param.is_some()))]
12921299
#[derive(Debug)]
12931300
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
12941301
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -1422,9 +1429,10 @@ pub struct BindingRestElement<'a> {
14221429
}
14231430

14241431
/// Function Definitions
1425-
#[visited_node(
1432+
#[visited_node]
1433+
#[scope(
14261434
// TODO: `ScopeFlags::Function` is not correct if this is a `MethodDefinition`
1427-
scope(ScopeFlags::Function),
1435+
flags(ScopeFlags::Function),
14281436
strict_if(self.body.as_ref().is_some_and(|body| body.has_use_strict_directive())),
14291437
)]
14301438
#[derive(Debug)]
@@ -1530,8 +1538,9 @@ pub struct FunctionBody<'a> {
15301538
}
15311539

15321540
/// Arrow Function Definitions
1533-
#[visited_node(
1534-
scope(ScopeFlags::Function | ScopeFlags::Arrow),
1541+
#[visited_node]
1542+
#[scope(
1543+
flags(ScopeFlags::Function | ScopeFlags::Arrow),
15351544
strict_if(self.body.has_use_strict_directive()),
15361545
)]
15371546
#[derive(Debug)]
@@ -1565,7 +1574,8 @@ pub struct YieldExpression<'a> {
15651574
}
15661575

15671576
/// Class Definitions
1568-
#[visited_node(scope(ScopeFlags::StrictMode))]
1577+
#[visited_node]
1578+
#[scope(flags(ScopeFlags::StrictMode))]
15691579
#[derive(Debug)]
15701580
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
15711581
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
@@ -1690,7 +1700,8 @@ pub struct PrivateIdentifier<'a> {
16901700
pub name: Atom<'a>,
16911701
}
16921702

1693-
#[visited_node(scope(ScopeFlags::ClassStaticBlock))]
1703+
#[visited_node]
1704+
#[scope(flags(ScopeFlags::ClassStaticBlock))]
16941705
#[derive(Debug)]
16951706
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
16961707
#[cfg_attr(feature = "serialize", serde(tag = "type"))]

crates/oxc_ast/src/ast/jsx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! [JSX](https://facebook.github.io/jsx)
22
3-
// NB: `#[visited_node]` attribute on AST nodes does not do anything to the code in this file.
4-
// It is purely a marker for codegen used in `oxc_traverse`. See docs in that crate.
3+
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
4+
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.
55

66
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
77
#![allow(non_snake_case)]

crates/oxc_ast/src/ast/literal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Literals
22
3-
// NB: `#[visited_node]` attribute on AST nodes does not do anything to the code in this file.
4-
// It is purely a marker for codegen used in `oxc_traverse`. See docs in that crate.
3+
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
4+
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.
55

66
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
77
#![allow(non_snake_case)]

crates/oxc_ast/src/ast/ts.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! [AST Spec](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/ast-spec)
44
//! [Archived TypeScript spec](https://github.com/microsoft/TypeScript/blob/3c99d50da5a579d9fa92d02664b1b66d4ff55944/doc/spec-ARCHIVED.md)
55
6-
// NB: `#[visited_node]` attribute on AST nodes does not do anything to the code in this file.
7-
// It is purely a marker for codegen used in `oxc_traverse`. See docs in that crate.
6+
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
7+
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.
88

99
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
1010
#![allow(non_snake_case)]
@@ -46,7 +46,8 @@ pub struct TSThisParameter<'a> {
4646
/// Enum Declaration
4747
///
4848
/// `const_opt` enum `BindingIdentifier` { `EnumBody_opt` }
49-
#[visited_node(scope(ScopeFlags::empty()))]
49+
#[visited_node]
50+
#[scope]
5051
#[derive(Debug)]
5152
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
5253
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
@@ -562,7 +563,8 @@ pub struct TSTypeParameterInstantiation<'a> {
562563
pub params: Vec<'a, TSType<'a>>,
563564
}
564565

565-
#[visited_node(scope(ScopeFlags::empty()))]
566+
#[visited_node]
567+
#[scope]
566568
#[derive(Debug)]
567569
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
568570
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
@@ -783,8 +785,9 @@ pub enum TSTypePredicateName<'a> {
783785
This(TSThisType),
784786
}
785787

786-
#[visited_node(
787-
scope(ScopeFlags::TsModuleBlock),
788+
#[visited_node]
789+
#[scope(
790+
flags(ScopeFlags::TsModuleBlock),
788791
strict_if(self.body.as_ref().is_some_and(|body| body.is_strict())),
789792
)]
790793
#[derive(Debug)]

0 commit comments

Comments
 (0)