Skip to content

Commit ae09a97

Browse files
authored
refactor(ast)!: remove Modifiers from ts nodes (#3846)
1 parent 1af5ed3 commit ae09a97

File tree

17 files changed

+197
-271
lines changed

17 files changed

+197
-271
lines changed

crates/oxc_ast/src/ast/ts.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use serde::Serialize;
2020
#[cfg(feature = "serialize")]
2121
use tsify::Tsify;
2222

23-
use super::{inherit_variants, js::*, jsx::*, literal::*, Modifiers};
23+
use super::{inherit_variants, js::*, jsx::*, literal::*};
2424

2525
#[cfg(feature = "serialize")]
2626
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
@@ -597,8 +597,7 @@ pub struct TSTypeAliasDeclaration<'a> {
597597
pub id: BindingIdentifier<'a>,
598598
pub type_annotation: TSType<'a>,
599599
pub type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
600-
/// Valid Modifiers: `declare`, `export`
601-
pub modifiers: Modifiers<'a>,
600+
pub declare: bool,
602601
}
603602

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

642640
#[visited_node]
@@ -803,8 +801,7 @@ pub struct TSModuleDeclaration<'a> {
803801
/// ^^^^^^
804802
/// ```
805803
pub kind: TSModuleDeclarationKind,
806-
/// Valid Modifiers: `declare`, `export`
807-
pub modifiers: Modifiers<'a>,
804+
pub declare: bool,
808805
pub scope_id: Cell<Option<ScopeId>>,
809806
}
810807

crates/oxc_ast/src/ast_builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,9 +1512,9 @@ impl<'a> AstBuilder<'a> {
15121512
id: TSModuleDeclarationName<'a>,
15131513
body: Option<TSModuleDeclarationBody<'a>>,
15141514
kind: TSModuleDeclarationKind,
1515-
modifiers: Modifiers<'a>,
1515+
declare: bool,
15161516
) -> Box<'a, TSModuleDeclaration<'a>> {
1517-
self.alloc(TSModuleDeclaration::new(span, id, body, kind, modifiers))
1517+
self.alloc(TSModuleDeclaration::new(span, id, body, kind, declare))
15181518
}
15191519

15201520
#[inline]
@@ -1847,15 +1847,15 @@ impl<'a> AstBuilder<'a> {
18471847
body: Box<'a, TSInterfaceBody<'a>>,
18481848
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
18491849
extends: Option<Vec<'a, TSInterfaceHeritage<'a>>>,
1850-
modifiers: Modifiers<'a>,
1850+
declare: bool,
18511851
) -> Declaration<'a> {
18521852
Declaration::TSInterfaceDeclaration(self.alloc(TSInterfaceDeclaration {
18531853
span,
18541854
id,
18551855
body,
18561856
type_parameters,
18571857
extends,
1858-
modifiers,
1858+
declare,
18591859
}))
18601860
}
18611861

@@ -1866,14 +1866,14 @@ impl<'a> AstBuilder<'a> {
18661866
id: BindingIdentifier<'a>,
18671867
type_annotation: TSType<'a>,
18681868
type_parameters: Option<Box<'a, TSTypeParameterDeclaration<'a>>>,
1869-
modifiers: Modifiers<'a>,
1869+
declare: bool,
18701870
) -> Declaration<'a> {
18711871
Declaration::TSTypeAliasDeclaration(self.alloc(TSTypeAliasDeclaration {
18721872
span,
18731873
id,
18741874
type_annotation,
18751875
type_parameters,
1876-
modifiers,
1876+
declare,
18771877
}))
18781878
}
18791879

crates/oxc_ast/src/ast_impl/js.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,9 @@ impl<'a> Declaration<'a> {
711711
Declaration::FunctionDeclaration(decl) => decl.declare,
712712
Declaration::ClassDeclaration(decl) => decl.declare,
713713
Declaration::TSEnumDeclaration(decl) => decl.declare,
714-
Declaration::TSTypeAliasDeclaration(decl) => decl.modifiers.is_contains_declare(),
715-
Declaration::TSModuleDeclaration(decl) => decl.modifiers.is_contains_declare(),
716-
Declaration::TSInterfaceDeclaration(decl) => decl.modifiers.is_contains_declare(),
714+
Declaration::TSTypeAliasDeclaration(decl) => decl.declare,
715+
Declaration::TSModuleDeclaration(decl) => decl.declare,
716+
Declaration::TSInterfaceDeclaration(decl) => decl.declare,
717717
_ => false,
718718
}
719719
}

crates/oxc_ast/src/ast_impl/ts.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::{cell::Cell, hash::Hash};
1111
use oxc_allocator::Vec;
1212
use oxc_span::{Atom, GetSpan, Span};
1313

14-
use crate::ast::Modifiers;
1514
use crate::ast::*;
1615

1716
impl<'a> TSEnumDeclaration<'a> {
@@ -150,9 +149,9 @@ impl<'a> TSModuleDeclaration<'a> {
150149
id: TSModuleDeclarationName<'a>,
151150
body: Option<TSModuleDeclarationBody<'a>>,
152151
kind: TSModuleDeclarationKind,
153-
modifiers: Modifiers<'a>,
152+
declare: bool,
154153
) -> Self {
155-
Self { span, id, body, kind, modifiers, scope_id: Cell::default() }
154+
Self { span, id, body, kind, declare, scope_id: Cell::default() }
156155
}
157156
}
158157

@@ -161,7 +160,7 @@ impl<'a> Hash for TSModuleDeclaration<'a> {
161160
self.id.hash(state);
162161
self.body.hash(state);
163162
self.kind.hash(state);
164-
self.modifiers.hash(state);
163+
self.declare.hash(state);
165164
}
166165
}
167166

crates/oxc_codegen/src/gen.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,10 +3245,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSNamedTupleMember<'a> {
32453245

32463246
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {
32473247
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
3248-
if self.modifiers.contains(ModifierKind::Export) {
3249-
p.print_str(b"export ");
3250-
}
3251-
if self.modifiers.contains(ModifierKind::Declare) {
3248+
if self.declare {
32523249
p.print_str(b"declare ");
32533250
}
32543251
p.print_str(b"module");
@@ -3300,10 +3297,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleBlock<'a> {
33003297

33013298
impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTypeAliasDeclaration<'a> {
33023299
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
3303-
if self.modifiers.contains(ModifierKind::Export) {
3304-
p.print_str(b"export ");
3305-
}
3306-
if self.modifiers.contains(ModifierKind::Declare) {
3300+
if self.declare {
33073301
p.print_str(b"declare ");
33083302
}
33093303
p.print_str(b"type");

crates/oxc_isolated_declarations/src/declaration.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a> IsolatedDeclarations<'a> {
145145
&mut self,
146146
decl: &Box<'a, TSModuleDeclaration<'a>>,
147147
) -> Box<'a, TSModuleDeclaration<'a>> {
148-
if decl.modifiers.is_contains_declare() {
148+
if decl.declare {
149149
return self.ast.copy(decl);
150150
}
151151

@@ -156,23 +156,23 @@ impl<'a> IsolatedDeclarations<'a> {
156156
match body {
157157
TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
158158
let inner = self.transform_ts_module_declaration(decl);
159-
return self.ast.ts_module_declaration(
159+
self.ast.ts_module_declaration(
160160
decl.span,
161161
self.ast.copy(&decl.id),
162162
Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)),
163163
decl.kind,
164-
self.modifiers_declare(),
165-
);
164+
self.modifiers_declare().is_contains_declare(),
165+
)
166166
}
167167
TSModuleDeclarationBody::TSModuleBlock(block) => {
168168
let body = self.transform_ts_module_block(block);
169-
return self.ast.ts_module_declaration(
169+
self.ast.ts_module_declaration(
170170
decl.span,
171171
self.ast.copy(&decl.id),
172172
Some(TSModuleDeclarationBody::TSModuleBlock(body)),
173173
decl.kind,
174-
self.modifiers_declare(),
175-
);
174+
self.modifiers_declare().is_contains_declare(),
175+
)
176176
}
177177
}
178178
}

crates/oxc_linter/src/rules/typescript/consistent_type_definitions.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ impl Rule for ConsistentTypeDefinitions {
7272
TSType::TSTypeLiteral(_)
7373
if self.config == ConsistentTypeDefinitionsConfig::Interface =>
7474
{
75-
let start = if decl.modifiers.is_contains_declare() {
76-
decl.span.start + 8
77-
} else {
78-
decl.span.start
79-
};
75+
let start = if decl.declare { decl.span.start + 8 } else { decl.span.start };
8076

8177
let name_span_start = &decl.id.span.start;
8278
let mut name_span_end = &decl.id.span.end;
@@ -170,11 +166,7 @@ impl Rule for ConsistentTypeDefinitions {
170166
AstKind::TSInterfaceDeclaration(decl)
171167
if self.config == ConsistentTypeDefinitionsConfig::Type =>
172168
{
173-
let start = if decl.modifiers.is_contains_declare() {
174-
decl.span.start + 8
175-
} else {
176-
decl.span.start
177-
};
169+
let start = if decl.declare { decl.span.start + 8 } else { decl.span.start };
178170

179171
let name_span_start = &decl.id.span.start;
180172
let mut name_span_end = &decl.id.span.end;

crates/oxc_linter/src/rules/typescript/no_namespace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use oxc_ast::{
2-
ast::{ModifierKind, TSModuleDeclarationKind, TSModuleDeclarationName},
2+
ast::{TSModuleDeclarationKind, TSModuleDeclarationName},
33
AstKind,
44
};
55
use oxc_diagnostics::OxcDiagnostic;
@@ -107,7 +107,7 @@ fn is_declaration(node: &AstNode, ctx: &LintContext) -> bool {
107107
let AstKind::TSModuleDeclaration(declaration) = node.kind() else {
108108
return false;
109109
};
110-
declaration.modifiers.contains(ModifierKind::Declare)
110+
declaration.declare
111111
})
112112
}
113113

crates/oxc_parser/src/js/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'a> ParserImpl<'a> {
292292
Modifiers::empty()
293293
};
294294

295-
let declaration = self.parse_declaration(decl_span, modifiers)?;
295+
let declaration = self.parse_declaration(decl_span, &modifiers)?;
296296
let span = self.end_span(span);
297297
Ok(self.ast.export_named_declaration(
298298
span,
@@ -330,7 +330,7 @@ impl<'a> ParserImpl<'a> {
330330
&& !self.peek_token().is_on_new_line
331331
&& self.ts_enabled() =>
332332
{
333-
self.parse_ts_interface_declaration(decl_span, Modifiers::empty()).map(|decl| {
333+
self.parse_ts_interface_declaration(decl_span, &Modifiers::empty()).map(|decl| {
334334
match decl {
335335
Declaration::TSInterfaceDeclaration(decl) => {
336336
ExportDefaultDeclarationKind::TSInterfaceDeclaration(decl)

0 commit comments

Comments
 (0)