Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/.generated_ast_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ src:
- 'crates/oxc_ast/src/generated/derive_clone_in.rs'
- 'crates/oxc_ast/src/generated/derive_get_span.rs'
- 'crates/oxc_ast/src/generated/derive_get_span_mut.rs'
- 'crates/oxc_ast/src/generated/derive_symbol_traits.rs'
- 'crates/oxc_ast/src/generated/visit.rs'
- 'crates/oxc_ast/src/generated/visit_mut.rs'
- 'tasks/ast_codegen/src/**'
12 changes: 12 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,7 @@ pub struct VariableDeclarator<'a> {
pub span: Span,
#[serde(skip)]
pub kind: VariableDeclarationKind,
#[symbol(binding, recurse)]
pub id: BindingPattern<'a>,
pub init: Option<Expression<'a>>,
pub definite: bool,
Expand Down Expand Up @@ -1563,6 +1564,7 @@ pub struct CatchClause<'a> {
pub struct CatchParameter<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding, recurse)]
pub pattern: BindingPattern<'a>,
}

Expand All @@ -1589,6 +1591,7 @@ pub struct BindingPattern<'a> {
#[serde(flatten)]
#[tsify(type = "(BindingIdentifier | ObjectPattern | ArrayPattern | AssignmentPattern)")]
#[span]
#[symbol(binding, recurse)]
pub kind: BindingPatternKind<'a>,
pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
pub optional: bool,
Expand Down Expand Up @@ -1621,6 +1624,7 @@ pub enum BindingPatternKind<'a> {
pub struct AssignmentPattern<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding, recurse)]
pub left: BindingPattern<'a>,
pub right: Expression<'a>,
}
Expand Down Expand Up @@ -1649,6 +1653,7 @@ pub struct BindingProperty<'a> {
#[serde(flatten)]
pub span: Span,
pub key: PropertyKey<'a>,
#[symbol(binding, recurse)]
pub value: BindingPattern<'a>,
pub shorthand: bool,
pub computed: bool,
Expand Down Expand Up @@ -1677,6 +1682,7 @@ pub struct ArrayPattern<'a> {
pub struct BindingRestElement<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding, recurse)]
pub argument: BindingPattern<'a>,
}

Expand All @@ -1695,6 +1701,7 @@ pub struct Function<'a> {
pub r#type: FunctionType,
#[serde(flatten)]
pub span: Span,
#[symbol]
pub id: Option<BindingIdentifier<'a>>,
pub generator: bool,
pub r#async: bool,
Expand Down Expand Up @@ -1762,6 +1769,7 @@ pub struct FormalParameter<'a> {
#[serde(flatten)]
pub span: Span,
pub decorators: Vec<'a, Decorator<'a>>,
#[symbol(binding, recurse)]
pub pattern: BindingPattern<'a>,
pub accessibility: Option<TSAccessibility>,
pub readonly: bool,
Expand Down Expand Up @@ -1858,6 +1866,7 @@ pub struct Class<'a> {
/// ```
pub decorators: Vec<'a, Decorator<'a>>,
/// Class identifier, AKA the name
#[symbol]
pub id: Option<BindingIdentifier<'a>>,
#[scope(enter_before)]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
Expand Down Expand Up @@ -2355,6 +2364,7 @@ pub struct ImportSpecifier<'a> {
/// import { Foo as Bar } from 'foo';
/// // ^^^
/// ```
#[symbol]
pub local: BindingIdentifier<'a>,
pub import_kind: ImportOrExportKind,
}
Expand All @@ -2375,6 +2385,7 @@ pub struct ImportDefaultSpecifier<'a> {
#[serde(flatten)]
pub span: Span,
/// The name of the imported symbol.
#[symbol]
pub local: BindingIdentifier<'a>,
}

Expand All @@ -2392,6 +2403,7 @@ pub struct ImportDefaultSpecifier<'a> {
pub struct ImportNamespaceSpecifier<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol]
pub local: BindingIdentifier<'a>,
}

Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct TSThisParameter<'a> {
pub struct TSEnumDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding)]
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub members: Vec<'a, TSEnumMember<'a>>,
Expand Down Expand Up @@ -728,6 +729,7 @@ pub struct TSTypeParameterInstantiation<'a> {
pub struct TSTypeParameter<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding)]
pub name: BindingIdentifier<'a>,
pub constraint: Option<TSType<'a>>,
pub default: Option<TSType<'a>>,
Expand Down Expand Up @@ -756,6 +758,7 @@ pub struct TSTypeParameterDeclaration<'a> {
pub struct TSTypeAliasDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding)]
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
Expand Down Expand Up @@ -802,6 +805,7 @@ pub struct TSInterfaceDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
/// The identifier (name) of the interface.
#[symbol(binding)]
pub id: BindingIdentifier<'a>,
#[scope(enter_before)]
pub extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
Expand Down Expand Up @@ -1287,6 +1291,7 @@ pub struct TSTypeAssertion<'a> {
pub struct TSImportEqualsDeclaration<'a> {
#[serde(flatten)]
pub span: Span,
#[symbol(binding)]
pub id: BindingIdentifier<'a>,
pub module_reference: TSModuleReference<'a>,
pub import_kind: ImportOrExportKind,
Expand Down
59 changes: 57 additions & 2 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::ast::*;
use crate::{ast::*, WithBindingIdentifier};

use std::{borrow::Cow, cell::Cell, fmt, hash::Hash};

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

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -363,6 +365,18 @@ impl<'a> BindingIdentifier<'a> {
}
}

impl<'a> WithBindingIdentifier<'a> for BindingIdentifier<'a> {
#[inline]
fn name(&self) -> Option<Atom<'a>> {
Some(self.name.clone())
}

#[inline]
fn symbol_id(&self) -> Option<SymbolId> {
self.symbol_id.get()
}
}

impl<'a> fmt::Display for BindingIdentifier<'a> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -793,6 +807,18 @@ impl<'a> Declaration<'a> {
}
}

impl<'a> WithBindingIdentifier<'a> for Declaration<'a> {
#[inline]
fn symbol_id(&self) -> Option<SymbolId> {
self.id().and_then(BindingIdentifier::symbol_id)
}

#[inline]
fn name(&self) -> Option<Atom<'a>> {
self.id().and_then(BindingIdentifier::name)
}
}

impl<'a> VariableDeclaration<'a> {
pub fn is_typescript_syntax(&self) -> bool {
self.declare
Expand Down Expand Up @@ -999,6 +1025,23 @@ impl<'a> BindingPatternKind<'a> {
}
}

impl<'a> WithBindingIdentifier<'a> for BindingPatternKind<'a> {
fn symbol_id(&self) -> Option<SymbolId> {
match self {
Self::BindingIdentifier(ident) => ident.symbol_id.get(),
Self::AssignmentPattern(assign) => assign.left.kind.symbol_id(),
_ => None,
}
}
fn name(&self) -> Option<Atom<'a>> {
match self {
Self::BindingIdentifier(ident) => Some(ident.name.clone()),
Self::AssignmentPattern(assign) => assign.left.kind.name(),
_ => None,
}
}
}

impl<'a> ObjectPattern<'a> {
pub fn is_empty(&self) -> bool {
self.properties.is_empty() && self.rest.is_none()
Expand Down Expand Up @@ -1513,6 +1556,18 @@ impl<'a> ImportDeclarationSpecifier<'a> {
}
}

impl<'a> WithBindingIdentifier<'a> for ImportDeclarationSpecifier<'a> {
#[inline]
fn symbol_id(&self) -> Option<SymbolId> {
self.local().symbol_id.get()
}

#[inline]
fn name(&self) -> Option<Atom<'a>> {
Some(self.local().name.clone())
}
}

impl<'a> ImportAttributeKey<'a> {
pub fn as_atom(&self) -> Atom<'a> {
match self {
Expand Down
Loading