Skip to content

Commit

Permalink
feat(ast): methods on AST nodes to get scope_id etc (#7127)
Browse files Browse the repository at this point in the history
Add getter and setter methods to all AST types which have a `ScopeId`, `SymbolId` or `ReferenceId` field to get the contents of that field.

Before:

```rs
let symbol_id = ident.symbol_id.get().unwrap();
```

After:

```rs
let symbol_id = ident.symbol_id();
```

This allows removing boilerplate code from the transformer, and discouraging the anti-pattern of treating these fields as if they may contain either `Some` or `None` (after semantic, they will always be `Some`).
  • Loading branch information
overlookmotel committed Nov 5, 2024
1 parent 843bce4 commit cc8a191
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/.generated_ast_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ src:
- 'crates/oxc_ast/src/generated/assert_layouts.rs'
- 'crates/oxc_ast/src/generated/ast_kind.rs'
- 'crates/oxc_ast/src/generated/ast_builder.rs'
- 'crates/oxc_ast/src/generated/get_id.rs'
- 'crates/oxc_ast/src/generated/visit.rs'
- 'crates/oxc_ast/src/generated/visit_mut.rs'
- 'npm/oxc-types/types.d.ts'
Expand Down
17 changes: 1 addition & 16 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::{borrow::Cow, fmt};

use oxc_allocator::{Address, Box, FromIn, GetAddress, Vec};
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::{
operator::UnaryOperator, reference::ReferenceId, scope::ScopeFlags, symbol::SymbolId,
};
use oxc_syntax::{operator::UnaryOperator, scope::ScopeFlags, symbol::SymbolId};

use crate::ast::*;

Expand Down Expand Up @@ -293,19 +291,6 @@ impl<'a> fmt::Display for IdentifierName<'a> {
}
}

impl<'a> IdentifierReference<'a> {
/// Get `ReferenceId` of `IdentifierReference`.
///
/// Only use this method on a post-semantic AST where `ReferenceId`s are always defined.
///
/// # Panics
/// Panics if `reference_id` is `None`.
#[inline]
pub fn reference_id(&self) -> ReferenceId {
self.reference_id.get().unwrap()
}
}

impl<'a> fmt::Display for IdentifierReference<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.name.fmt(f)
Expand Down
Loading

0 comments on commit cc8a191

Please sign in to comment.