Skip to content

Commit

Permalink
refactor(ast)!: remove Modifiers from ts nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 23, 2024
1 parent 1af5ed3 commit cf839d4
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 257 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
46 changes: 43 additions & 3 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,23 @@ impl<'a> ParserImpl<'a> {

self.asi()?;
let span = self.end_span(span);
Ok(self.ast.ts_type_alias_declaration(span, id, annotation, params, modifiers))

for modifier in modifiers.iter() {
if modifier.kind != ModifierKind::Declare {
self.error(diagnostics::modifiers_cannot_appear(
modifier.span,
modifier.kind.as_str(),
));
}
}

Ok(self.ast.ts_type_alias_declaration(
span,
id,
annotation,
params,
modifiers.is_contains_declare(),
))
}

/** --------------------- Interface ------------------------ */
Expand All @@ -134,13 +150,23 @@ impl<'a> ParserImpl<'a> {
let (extends, _) = self.parse_heritage_clause()?;
let body = self.parse_ts_interface_body()?;
let extends = extends.map(|e| self.ast.ts_interface_heritages(e));

for modifier in modifiers.iter() {
if modifier.kind != ModifierKind::Declare {
self.error(diagnostics::modifiers_cannot_appear(
modifier.span,
modifier.kind.as_str(),
));
}
}

Ok(self.ast.ts_interface_declaration(
self.end_span(span),
id,
body,
type_parameters,
extends,
modifiers,
modifiers.is_contains_declare(),
))
}

Expand Down Expand Up @@ -264,7 +290,21 @@ impl<'a> ParserImpl<'a> {
None
};

Ok(self.ast.ts_module_declaration(self.end_span(span), id, body, kind, modifiers))
for modifier in modifiers.iter() {
if modifier.kind != ModifierKind::Declare {
self.error(diagnostics::modifiers_cannot_appear(
modifier.span,
modifier.kind.as_str(),
));
}
}
Ok(self.ast.ts_module_declaration(
self.end_span(span),
id,
body,
kind,
modifiers.is_contains_declare(),
))
}

/** ----------------------- declare --------------------- */
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_semantic/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,7 @@ impl<'a> Binder for TSModuleDeclaration<'a> {
fn bind(&self, builder: &mut SemanticBuilder) {
// At declaration time a module has no value declaration it is only when a value declaration
// is made inside a the scope of a module that the symbol is modified
let ambient = if self.modifiers.contains(ModifierKind::Declare) {
SymbolFlags::Ambient
} else {
SymbolFlags::None
};
let ambient = if self.declare { SymbolFlags::Ambient } else { SymbolFlags::None };
builder.declare_symbol(
self.span,
self.id.name().as_str(),
Expand Down
8 changes: 1 addition & 7 deletions crates/oxc_semantic/src/checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,8 @@ pub fn check<'a>(node: &AstNode<'a>, ctx: &SemanticBuilder<'a>) {
AstKind::TSTypeParameterDeclaration(declaration) => {
ts::check_ts_type_parameter_declaration(declaration, ctx);
}
AstKind::TSModuleDeclaration(decl) => ts::check_ts_module_declaration(decl, node, ctx),
AstKind::TSModuleDeclaration(decl) => ts::check_ts_module_declaration(decl, ctx),
AstKind::TSEnumDeclaration(decl) => ts::check_ts_enum_declaration(decl, ctx),
AstKind::TSTypeAliasDeclaration(decl) => {
ts::check_ts_type_alias_declaration(decl, node, ctx);
}
AstKind::TSInterfaceDeclaration(decl) => {
ts::check_ts_interface_declaration(decl, node, ctx);
}
_ => {}
}
}
Loading

0 comments on commit cf839d4

Please sign in to comment.