Skip to content

Commit

Permalink
refactor(ast)!: remove Modifiers from ts nodes (#3846)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jun 23, 2024
1 parent 1af5ed3 commit ae09a97
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 271 deletions.
11 changes: 4 additions & 7 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::*, Modifiers};
use super::{inherit_variants, js::*, jsx::*, literal::*};

#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -597,8 +597,7 @@ pub struct TSTypeAliasDeclaration<'a> {
pub id: BindingIdentifier<'a>,
pub type_annotation: TSType<'a>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
/// Valid Modifiers: `declare`, `export`
pub modifiers: Modifiers<'a>,
pub declare: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -635,8 +634,7 @@ pub struct TSInterfaceDeclaration<'a> {
pub body: Box<'a, TSInterfaceBody<'a>>,
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
pub extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
/// Valid Modifiers: `export`, `default`, `declare`
pub modifiers: Modifiers<'a>,
pub declare: bool,
}

#[visited_node]
Expand Down Expand Up @@ -803,8 +801,7 @@ pub struct TSModuleDeclaration<'a> {
/// ^^^^^^
/// ```
pub kind: TSModuleDeclarationKind,
/// Valid Modifiers: `declare`, `export`
pub modifiers: Modifiers<'a>,
pub declare: bool,
pub scope_id: Cell<Option<ScopeId>>,
}

Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,9 +1512,9 @@ impl<'a> AstBuilder<'a> {
id: TSModuleDeclarationName<'a>,
body: Option<TSModuleDeclarationBody<'a>>,
kind: TSModuleDeclarationKind,
modifiers: Modifiers<'a>,
declare: bool,
) -> Box<'a, TSModuleDeclaration<'a>> {
self.alloc(TSModuleDeclaration::new(span, id, body, kind, modifiers))
self.alloc(TSModuleDeclaration::new(span, id, body, kind, declare))
}

#[inline]
Expand Down Expand Up @@ -1847,15 +1847,15 @@ impl<'a> AstBuilder<'a> {
body: Box<'a, TSInterfaceBody<'a>>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
modifiers: Modifiers<'a>,
declare: bool,
) -> Declaration<'a> {
Declaration::TSInterfaceDeclaration(self.alloc(TSInterfaceDeclaration {
span,
id,
body,
type_parameters,
extends,
modifiers,
declare,
}))
}

Expand All @@ -1866,14 +1866,14 @@ impl<'a> AstBuilder<'a> {
id: BindingIdentifier<'a>,
type_annotation: TSType<'a>,
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
modifiers: Modifiers<'a>,
declare: bool,
) -> Declaration<'a> {
Declaration::TSTypeAliasDeclaration(self.alloc(TSTypeAliasDeclaration {
span,
id,
type_annotation,
type_parameters,
modifiers,
declare,
}))
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,9 @@ impl<'a> Declaration<'a> {
Declaration::FunctionDeclaration(decl) => decl.declare,
Declaration::ClassDeclaration(decl) => decl.declare,
Declaration::TSEnumDeclaration(decl) => decl.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(),
Declaration::TSTypeAliasDeclaration(decl) => decl.declare,
Declaration::TSModuleDeclaration(decl) => decl.declare,
Declaration::TSInterfaceDeclaration(decl) => decl.declare,
_ => false,
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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> {
Expand Down Expand Up @@ -150,9 +149,9 @@ impl<'a> TSModuleDeclaration<'a> {
id: TSModuleDeclarationName<'a>,
body: Option<TSModuleDeclarationBody<'a>>,
kind: TSModuleDeclarationKind,
modifiers: Modifiers<'a>,
declare: bool,
) -> Self {
Self { span, id, body, kind, modifiers, scope_id: Cell::default() }
Self { span, id, body, kind, declare, scope_id: Cell::default() }
}
}

Expand All @@ -161,7 +160,7 @@ impl<'a> Hash for TSModuleDeclaration<'a> {
self.id.hash(state);
self.body.hash(state);
self.kind.hash(state);
self.modifiers.hash(state);
self.declare.hash(state);
}
}

Expand Down
10 changes: 2 additions & 8 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3245,10 +3245,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSNamedTupleMember<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if self.modifiers.contains(ModifierKind::Export) {
p.print_str(b"export ");
}
if self.modifiers.contains(ModifierKind::Declare) {
if self.declare {
p.print_str(b"declare ");
}
p.print_str(b"module");
Expand Down Expand Up @@ -3300,10 +3297,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleBlock<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTypeAliasDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if self.modifiers.contains(ModifierKind::Export) {
p.print_str(b"export ");
}
if self.modifiers.contains(ModifierKind::Declare) {
if self.declare {
p.print_str(b"declare ");
}
p.print_str(b"type");
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<'a> IsolatedDeclarations<'a> {
&mut self,
decl: &Box<'a, TSModuleDeclaration<'a>>,
) -> Box<'a, TSModuleDeclaration<'a>> {
if decl.modifiers.is_contains_declare() {
if decl.declare {
return self.ast.copy(decl);
}

Expand All @@ -156,23 +156,23 @@ impl<'a> IsolatedDeclarations<'a> {
match body {
TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
let inner = self.transform_ts_module_declaration(decl);
return self.ast.ts_module_declaration(
self.ast.ts_module_declaration(
decl.span,
self.ast.copy(&decl.id),
Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)),
decl.kind,
self.modifiers_declare(),
);
self.modifiers_declare().is_contains_declare(),
)
}
TSModuleDeclarationBody::TSModuleBlock(block) => {
let body = self.transform_ts_module_block(block);
return self.ast.ts_module_declaration(
self.ast.ts_module_declaration(
decl.span,
self.ast.copy(&decl.id),
Some(TSModuleDeclarationBody::TSModuleBlock(body)),
decl.kind,
self.modifiers_declare(),
);
self.modifiers_declare().is_contains_declare(),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ impl Rule for ConsistentTypeDefinitions {
TSType::TSTypeLiteral(_)
if self.config == ConsistentTypeDefinitionsConfig::Interface =>
{
let start = if decl.modifiers.is_contains_declare() {
decl.span.start + 8
} else {
decl.span.start
};
let start = if decl.declare { decl.span.start + 8 } else { decl.span.start };

let name_span_start = &decl.id.span.start;
let mut name_span_end = &decl.id.span.end;
Expand Down Expand Up @@ -170,11 +166,7 @@ impl Rule for ConsistentTypeDefinitions {
AstKind::TSInterfaceDeclaration(decl)
if self.config == ConsistentTypeDefinitionsConfig::Type =>
{
let start = if decl.modifiers.is_contains_declare() {
decl.span.start + 8
} else {
decl.span.start
};
let start = if decl.declare { decl.span.start + 8 } else { decl.span.start };

let name_span_start = &decl.id.span.start;
let mut name_span_end = &decl.id.span.end;
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/typescript/no_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::{
ast::{ModifierKind, TSModuleDeclarationKind, TSModuleDeclarationName},
ast::{TSModuleDeclarationKind, TSModuleDeclarationName},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
Expand Down Expand Up @@ -107,7 +107,7 @@ fn is_declaration(node: &AstNode, ctx: &LintContext) -> bool {
let AstKind::TSModuleDeclaration(declaration) = node.kind() else {
return false;
};
declaration.modifiers.contains(ModifierKind::Declare)
declaration.declare
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'a> ParserImpl<'a> {
Modifiers::empty()
};

let declaration = self.parse_declaration(decl_span, modifiers)?;
let declaration = self.parse_declaration(decl_span, &modifiers)?;
let span = self.end_span(span);
Ok(self.ast.export_named_declaration(
span,
Expand Down Expand Up @@ -330,7 +330,7 @@ impl<'a> ParserImpl<'a> {
&& !self.peek_token().is_on_new_line
&& self.ts_enabled() =>
{
self.parse_ts_interface_declaration(decl_span, Modifiers::empty()).map(|decl| {
self.parse_ts_interface_declaration(decl_span, &Modifiers::empty()).map(|decl| {
match decl {
Declaration::TSInterfaceDeclaration(decl) => {
ExportDefaultDeclarationKind::TSInterfaceDeclaration(decl)
Expand Down
Loading

0 comments on commit ae09a97

Please sign in to comment.