Skip to content

Commit

Permalink
refactor(traverse): separate #[scope] attr (#3901)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
overlookmotel committed Jun 26, 2024
1 parent 5ef28b7 commit 1061baa
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 96 deletions.
45 changes: 28 additions & 17 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.

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

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

#[visited_node(
scope(ScopeFlags::Top),
#[visited_node]
#[scope(
flags(ScopeFlags::Top),
strict_if(self.source_type.is_strict() || self.directives.iter().any(Directive::is_use_strict)),
)]
#[derive(Debug)]
Expand Down Expand Up @@ -943,7 +947,8 @@ pub struct Hashbang<'a> {
}

/// Block Statement
#[visited_node(scope(ScopeFlags::empty()))]
#[visited_node]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down Expand Up @@ -1099,10 +1104,8 @@ pub struct WhileStatement<'a> {
}

/// For Statement
#[visited_node(
scope(ScopeFlags::empty()),
scope_if(self.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration)),
)]
#[visited_node]
#[scope(if(self.init.as_ref().is_some_and(ForStatementInit::is_lexical_declaration)))]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down Expand Up @@ -1136,7 +1139,8 @@ pub enum ForStatementInit<'a> {
}

/// For-In Statement
#[visited_node(scope(ScopeFlags::empty()), scope_if(self.left.is_lexical_declaration()))]
#[visited_node]
#[scope(if(self.left.is_lexical_declaration()))]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down Expand Up @@ -1168,7 +1172,8 @@ pub enum ForStatementLeft<'a> {
}
}
/// For-Of Statement
#[visited_node(scope(ScopeFlags::empty()), scope_if(self.left.is_lexical_declaration()))]
#[visited_node]
#[scope(if(self.left.is_lexical_declaration()))]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down Expand Up @@ -1228,7 +1233,8 @@ pub struct WithStatement<'a> {
}

/// Switch Statement
#[visited_node(scope(ScopeFlags::empty()))]
#[visited_node]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down Expand Up @@ -1288,7 +1294,8 @@ pub struct TryStatement<'a> {
pub finalizer: Option<Box<'a, BlockStatement<'a>>>,
}

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

/// Function Definitions
#[visited_node(
#[visited_node]
#[scope(
// TODO: `ScopeFlags::Function` is not correct if this is a `MethodDefinition`
scope(ScopeFlags::Function),
flags(ScopeFlags::Function),
strict_if(self.body.as_ref().is_some_and(|body| body.has_use_strict_directive())),
)]
#[derive(Debug)]
Expand Down Expand Up @@ -1530,8 +1538,9 @@ pub struct FunctionBody<'a> {
}

/// Arrow Function Definitions
#[visited_node(
scope(ScopeFlags::Function | ScopeFlags::Arrow),
#[visited_node]
#[scope(
flags(ScopeFlags::Function | ScopeFlags::Arrow),
strict_if(self.body.has_use_strict_directive()),
)]
#[derive(Debug)]
Expand Down Expand Up @@ -1565,7 +1574,8 @@ pub struct YieldExpression<'a> {
}

/// Class Definitions
#[visited_node(scope(ScopeFlags::StrictMode))]
#[visited_node]
#[scope(flags(ScopeFlags::StrictMode))]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
Expand Down Expand Up @@ -1690,7 +1700,8 @@ pub struct PrivateIdentifier<'a> {
pub name: Atom<'a>,
}

#[visited_node(scope(ScopeFlags::ClassStaticBlock))]
#[visited_node]
#[scope(flags(ScopeFlags::ClassStaticBlock))]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! [JSX](https://facebook.github.io/jsx)
// NB: `#[visited_node]` attribute on AST nodes does not do anything to the code in this file.
// It is purely a marker for codegen used in `oxc_traverse`. See docs in that crate.
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.

// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Literals
// NB: `#[visited_node]` attribute on AST nodes does not do anything to the code in this file.
// It is purely a marker for codegen used in `oxc_traverse`. See docs in that crate.
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.

// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]
Expand Down
15 changes: 9 additions & 6 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! [AST Spec](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/ast-spec)
//! [Archived TypeScript spec](https://github.com/microsoft/TypeScript/blob/3c99d50da5a579d9fa92d02664b1b66d4ff55944/doc/spec-ARCHIVED.md)
// NB: `#[visited_node]` attribute on AST nodes does not do anything to the code in this file.
// It is purely a marker for codegen used in `oxc_traverse`. See docs in that crate.
// NB: `#[visited_node]` and `#[scope]` attributes on AST nodes do not do anything to the code in this file.
// They are purely markers for codegen used in `oxc_traverse`. See docs in that crate.

// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]
Expand Down Expand Up @@ -46,7 +46,8 @@ pub struct TSThisParameter<'a> {
/// Enum Declaration
///
/// `const_opt` enum `BindingIdentifier` { `EnumBody_opt` }
#[visited_node(scope(ScopeFlags::empty()))]
#[visited_node]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
Expand Down Expand Up @@ -562,7 +563,8 @@ pub struct TSTypeParameterInstantiation<'a> {
pub params: Vec<'a, TSType<'a>>,
}

#[visited_node(scope(ScopeFlags::empty()))]
#[visited_node]
#[scope]
#[derive(Debug)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
Expand Down Expand Up @@ -783,8 +785,9 @@ pub enum TSTypePredicateName<'a> {
This(TSThisType),
}

#[visited_node(
scope(ScopeFlags::TsModuleBlock),
#[visited_node]
#[scope(
flags(ScopeFlags::TsModuleBlock),
strict_if(self.body.as_ref().is_some_and(|body| body.is_strict())),
)]
#[derive(Debug)]
Expand Down
Loading

0 comments on commit 1061baa

Please sign in to comment.