Skip to content

Commit

Permalink
refactor(ast)!: replace Modifiers with declare on `VariableDeclar…
Browse files Browse the repository at this point in the history
…ation`

part of #2958
  • Loading branch information
Boshen committed Jun 23, 2024
1 parent f029273 commit 8b4c9bf
Show file tree
Hide file tree
Showing 25 changed files with 224 additions and 200 deletions.
5 changes: 2 additions & 3 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]

use crate::ast::*;
use std::cell::Cell;

use oxc_allocator::{Box, Vec};
Expand All @@ -17,6 +16,7 @@ use oxc_syntax::{
};

use super::macros::inherit_variants;
use super::*;

#[cfg(feature = "serialize")]
use serde::Serialize;
Expand Down Expand Up @@ -1000,8 +1000,7 @@ pub struct VariableDeclaration<'a> {
pub span: Span,
pub kind: VariableDeclarationKind,
pub declarations: Vec<'a, VariableDeclarator<'a>>,
/// Valid Modifiers: `export`, `declare`
pub modifiers: Modifiers<'a>,
pub declare: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
127 changes: 127 additions & 0 deletions crates/oxc_ast/src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Silence erroneous warnings from Rust Analyser for `#[derive(Tsify)]`
#![allow(non_snake_case)]
//! AST Definitions
//!
//! # Enum inheritance
Expand Down Expand Up @@ -181,3 +183,128 @@ mod ts;
use macros::inherit_variants;

pub use self::{js::*, jsx::*, literal::*, ts::*};

#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;

use oxc_allocator::Vec;
use oxc_span::Span;

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct Modifier {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub kind: ModifierKind,
}

#[derive(Debug, Default, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(transparent))]
pub struct Modifiers<'a>(Option<Vec<'a, Modifier>>);

impl<'a> Modifiers<'a> {
pub fn new(modifiers: Vec<'a, Modifier>) -> Self {
Self(Some(modifiers))
}

pub fn empty() -> Self {
Self(None)
}

pub fn is_none(&self) -> bool {
self.0.is_none()
}

pub fn contains(&self, target: ModifierKind) -> bool {
self.0
.as_ref()
.map_or(false, |modifiers| modifiers.iter().any(|modifier| modifier.kind == target))
}

pub fn iter(&self) -> impl Iterator<Item = &Modifier> + '_ {
self.0.as_ref().into_iter().flat_map(|modifiers| modifiers.iter())
}

/// Find a modifier by kind
pub fn find(&self, kind: ModifierKind) -> Option<&Modifier> {
self.find_where(|modifier| modifier.kind == kind)
}

pub fn find_where<F>(&self, f: F) -> Option<&Modifier>
where
F: Fn(&Modifier) -> bool,
{
self.0.as_ref().and_then(|modifiers| modifiers.iter().find(|modifier| f(modifier)))
}

pub fn is_contains_declare(&self) -> bool {
self.contains(ModifierKind::Declare)
}

pub fn is_contains_abstract(&self) -> bool {
self.contains(ModifierKind::Abstract)
}

pub fn remove_type_modifiers(&mut self) {
if let Some(list) = &mut self.0 {
list.retain(|m| !m.kind.is_typescript_syntax());
}
}

pub fn add_modifier(&mut self, modifier: Modifier) {
if let Some(list) = self.0.as_mut() {
list.push(modifier);
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
pub enum ModifierKind {
Abstract,
Accessor,
Async,
Const,
Declare,
Default,
Export,
In,
Public,
Private,
Protected,
Readonly,
Static,
Out,
Override,
}

impl ModifierKind {
pub fn is_typescript_syntax(&self) -> bool {
!matches!(self, Self::Async | Self::Default | Self::Export | Self::Static)
}

pub fn as_str(self) -> &'static str {
match self {
Self::Abstract => "abstract",
Self::Accessor => "accessor",
Self::Async => "async",
Self::Const => "const",
Self::Declare => "declare",
Self::Default => "default",
Self::Export => "export",
Self::In => "in",
Self::Public => "public",
Self::Private => "private",
Self::Protected => "protected",
Self::Readonly => "readonly",
Self::Static => "static",
Self::Out => "out",
Self::Override => "override",
}
}
}
37 changes: 1 addition & 36 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;

use super::{inherit_variants, js::*, jsx::*, literal::*};
use super::{inherit_variants, js::*, jsx::*, literal::*, Modifiers};

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -1094,41 +1094,6 @@ pub struct Decorator<'a> {
pub expression: Expression<'a>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
pub enum ModifierKind {
Abstract,
Accessor,
Async,
Const,
Declare,
Default,
Export,
In,
Public,
Private,
Protected,
Readonly,
Static,
Out,
Override,
}

#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type", rename_all = "camelCase"))]
pub struct Modifier {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
pub kind: ModifierKind,
}

#[derive(Debug, Default, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(transparent))]
pub struct Modifiers<'a>(pub(crate) Option<Vec<'a, Modifier>>);

/// Export Assignment in non-module files
///
/// `export = foo`
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a> AstBuilder<'a> {
Span::default(),
VariableDeclarationKind::Var,
self.new_vec(),
Modifiers::empty(),
false,
);
let empty_decl = Declaration::VariableDeclaration(empty_decl);
mem::replace(decl, empty_decl)
Expand Down Expand Up @@ -1167,9 +1167,9 @@ impl<'a> AstBuilder<'a> {
span: Span,
kind: VariableDeclarationKind,
declarations: Vec<'a, VariableDeclarator<'a>>,
modifiers: Modifiers<'a>,
declare: bool,
) -> Box<'a, VariableDeclaration<'a>> {
self.alloc(VariableDeclaration { span, kind, declarations, modifiers })
self.alloc(VariableDeclaration { span, kind, declarations, declare })
}

#[inline]
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,23 +705,23 @@ impl<'a> Declaration<'a> {
}
}

pub fn modifiers(&self) -> Option<&Modifiers<'a>> {
pub fn declare(&self) -> bool {
match self {
Declaration::VariableDeclaration(decl) => Some(&decl.modifiers),
Declaration::FunctionDeclaration(decl) => Some(&decl.modifiers),
Declaration::ClassDeclaration(decl) => Some(&decl.modifiers),
Declaration::TSEnumDeclaration(decl) => Some(&decl.modifiers),
Declaration::TSTypeAliasDeclaration(decl) => Some(&decl.modifiers),
Declaration::TSModuleDeclaration(decl) => Some(&decl.modifiers),
Declaration::TSInterfaceDeclaration(decl) => Some(&decl.modifiers),
_ => None,
Declaration::VariableDeclaration(decl) => decl.declare,
Declaration::FunctionDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::ClassDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::TSEnumDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::TSTypeAliasDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::TSModuleDeclaration(decl) => decl.modifiers.is_contains_declare(),
Declaration::TSInterfaceDeclaration(decl) => decl.modifiers.is_contains_declare(),
_ => false,
}
}
}

impl<'a> VariableDeclaration<'a> {
pub fn is_typescript_syntax(&self) -> bool {
self.modifiers.contains(ModifierKind::Declare)
self.declare
}

pub fn has_init(&self) -> bool {
Expand Down
67 changes: 3 additions & 64 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
// 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.

use crate::ast::*;

use std::{cell::Cell, hash::Hash};

use oxc_allocator::Vec;
use oxc_span::{Atom, GetSpan, Span};

use crate::ast::Modifiers;
use crate::ast::*;

impl<'a> TSEnumDeclaration<'a> {
pub fn new(
span: Span,
Expand Down Expand Up @@ -198,68 +199,6 @@ impl<'a> Decorator<'a> {
}
}

impl ModifierKind {
pub fn is_typescript_syntax(&self) -> bool {
!matches!(self, Self::Async | Self::Default | Self::Export | Self::Static)
}
}

impl<'a> Modifiers<'a> {
pub fn new(modifiers: Vec<'a, Modifier>) -> Self {
Self(Some(modifiers))
}

pub fn empty() -> Self {
Self(None)
}

pub fn is_none(&self) -> bool {
self.0.is_none()
}

pub fn contains(&self, target: ModifierKind) -> bool {
self.0
.as_ref()
.map_or(false, |modifiers| modifiers.iter().any(|modifier| modifier.kind == target))
}

pub fn iter(&self) -> impl Iterator<Item = &Modifier> + '_ {
self.0.as_ref().into_iter().flat_map(|modifiers| modifiers.iter())
}

/// Find a modifier by kind
pub fn find(&self, kind: ModifierKind) -> Option<&Modifier> {
self.find_where(|modifier| modifier.kind == kind)
}

pub fn find_where<F>(&self, f: F) -> Option<&Modifier>
where
F: Fn(&Modifier) -> bool,
{
self.0.as_ref().and_then(|modifiers| modifiers.iter().find(|modifier| f(modifier)))
}

pub fn is_contains_declare(&self) -> bool {
self.contains(ModifierKind::Declare)
}

pub fn is_contains_abstract(&self) -> bool {
self.contains(ModifierKind::Abstract)
}

pub fn remove_type_modifiers(&mut self) {
if let Some(list) = &mut self.0 {
list.retain(|m| !m.kind.is_typescript_syntax());
}
}

pub fn add_modifier(&mut self, modifier: Modifier) {
if let Some(list) = self.0.as_mut() {
list.push(modifier);
}
}
}

impl ImportOrExportKind {
pub fn is_value(&self) -> bool {
matches!(self, Self::Value)
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for UsingDeclaration<'a> {
impl<'a, const MINIFY: bool> Gen<MINIFY> for VariableDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
p.add_source_mapping(self.span.start);
if self.modifiers.is_contains_declare() {
if self.declare {
p.print_str(b"declare ");
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a> IsolatedDeclarations<'a> {
decl: &VariableDeclaration<'a>,
check_binding: bool,
) -> Option<Box<'a, VariableDeclaration<'a>>> {
if decl.modifiers.is_contains_declare() {
if decl.declare {
None
} else {
let declarations =
Expand All @@ -39,7 +39,7 @@ impl<'a> IsolatedDeclarations<'a> {
decl.span,
decl.kind,
self.ast.new_vec_from_iter(declarations),
self.modifiers_declare(),
self.modifiers_declare().is_contains_declare(),
)
}

Expand Down Expand Up @@ -126,7 +126,7 @@ impl<'a> IsolatedDeclarations<'a> {
decl.span,
VariableDeclarationKind::Const,
declarations,
self.modifiers_declare(),
self.modifiers_declare().is_contains_declare(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a> IsolatedDeclarations<'a> {
span: SPAN,
kind,
declarations,
modifiers: self.modifiers_declare(),
declare: self.modifiers_declare().is_contains_declare(),
}),
ExportDefaultDeclarationKind::from(
self.ast.identifier_reference_expression(
Expand Down
Loading

0 comments on commit 8b4c9bf

Please sign in to comment.