Skip to content

Commit

Permalink
refactor(isolated-declarations): remove Modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jun 23, 2024
1 parent ae09a97 commit ce62e74
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 42 deletions.
15 changes: 3 additions & 12 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,7 @@ impl<'a> IsolatedDeclarations<'a> {
inferred_accessor_types
}

pub fn transform_class(
&self,
decl: &Class<'a>,
modifiers: Option<Modifiers<'a>>,
) -> Option<Box<'a, Class<'a>>> {
pub fn transform_class(&self, decl: &Class<'a>) -> Option<Box<'a, Class<'a>>> {
if decl.declare {
return None;
}
Expand Down Expand Up @@ -462,11 +458,6 @@ impl<'a> IsolatedDeclarations<'a> {

let body = self.ast.class_body(decl.body.span, elements);

let mut modifiers = modifiers.unwrap_or_else(|| self.modifiers_declare());
if decl.r#abstract {
modifiers.add_modifier(Modifier { span: SPAN, kind: ModifierKind::Abstract });
};

Some(self.ast.class(
decl.r#type,
decl.span,
Expand All @@ -477,8 +468,8 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&decl.super_type_parameters),
self.ast.copy(&decl.implements),
self.ast.new_vec(),
modifiers.is_contains_abstract(),
modifiers.is_contains_declare(),
decl.r#abstract,
self.is_declare(),
))
}

Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> IsolatedDeclarations<'a> {
decl.span,
decl.kind,
self.ast.new_vec_from_iter(declarations),
self.modifiers_declare().is_contains_declare(),
self.is_declare(),
)
}

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

Expand Down Expand Up @@ -161,7 +161,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&decl.id),
Some(TSModuleDeclarationBody::TSModuleDeclaration(inner)),
decl.kind,
self.modifiers_declare().is_contains_declare(),
self.is_declare(),
)
}
TSModuleDeclarationBody::TSModuleBlock(block) => {
Expand All @@ -171,7 +171,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&decl.id),
Some(TSModuleDeclarationBody::TSModuleBlock(body)),
decl.kind,
self.modifiers_declare().is_contains_declare(),
self.is_declare(),
)
}
}
Expand All @@ -187,7 +187,7 @@ impl<'a> IsolatedDeclarations<'a> {
if !check_binding
|| func.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
{
self.transform_function(func, None).map(Declaration::FunctionDeclaration)
self.transform_function(func).map(Declaration::FunctionDeclaration)
} else {
None
}
Expand All @@ -202,7 +202,7 @@ impl<'a> IsolatedDeclarations<'a> {
if !check_binding
|| decl.id.as_ref().is_some_and(|id| self.scope.has_reference(&id.name))
{
self.transform_class(decl, None).map(Declaration::ClassDeclaration)
self.transform_class(decl).map(Declaration::ClassDeclaration)
} else {
None
}
Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,13 @@ impl<'a> IsolatedDeclarations<'a> {

members.push(member);
}
let mut modifiers = self.modifiers_declare();
if decl.r#const {
modifiers.add_modifier(Modifier { span: SPAN, kind: ModifierKind::Const });
}

Some(self.ast.ts_enum_declaration(
decl.span,
self.ast.copy(&decl.id),
members,
modifiers.is_contains_const(),
modifiers.is_contains_declare(),
decl.r#const,
self.is_declare(),
))
}

Expand Down
8 changes: 2 additions & 6 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ use crate::{
};

impl<'a> IsolatedDeclarations<'a> {
pub fn transform_function(
&mut self,
func: &Function<'a>,
modifiers: Option<Modifiers<'a>>,
) -> Option<Box<'a, Function<'a>>> {
pub fn transform_function(&mut self, func: &Function<'a>) -> Option<Box<'a, Function<'a>>> {
if func.declare {
None
} else {
Expand All @@ -32,7 +28,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.copy(&func.id),
false,
false,
modifiers.unwrap_or_else(|| self.modifiers_declare()).is_contains_declare(),
self.is_declare(),
self.ast.copy(&func.this_param),
params,
None,
Expand Down
12 changes: 3 additions & 9 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,8 @@ impl<'a> IsolatedDeclarations<'a> {
})
}

pub fn modifiers_declare(&self) -> Modifiers<'a> {
if self.scope.is_ts_module_block_flag() {
// If we are in a module block, we don't need to add declare
Modifiers::empty()
} else {
Modifiers::new(
self.ast.new_vec_single(Modifier { span: SPAN, kind: ModifierKind::Declare }),
)
}
pub fn is_declare(&self) -> bool {
// If we are in a module block, we don't need to add declare
!self.scope.is_ts_module_block_flag()
}
}
6 changes: 3 additions & 3 deletions crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ impl<'a> IsolatedDeclarations<'a> {
) -> Option<(Option<VariableDeclaration<'a>>, ExportDefaultDeclaration<'a>)> {
let declaration = match &decl.declaration {
ExportDefaultDeclarationKind::FunctionDeclaration(decl) => self
.transform_function(decl, Some(Modifiers::empty()))
.transform_function(decl)
.map(|d| (None, ExportDefaultDeclarationKind::FunctionDeclaration(d))),
ExportDefaultDeclarationKind::ClassDeclaration(decl) => self
.transform_class(decl, Some(Modifiers::empty()))
.transform_class(decl)
.map(|d| (None, ExportDefaultDeclarationKind::ClassDeclaration(d))),
ExportDefaultDeclarationKind::TSInterfaceDeclaration(_) => {
Some((None, self.ast.copy(&decl.declaration)))
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<'a> IsolatedDeclarations<'a> {
span: SPAN,
kind,
declarations,
declare: self.modifiers_declare().is_contains_declare(),
declare: self.is_declare(),
}),
ExportDefaultDeclarationKind::from(
self.ast.identifier_reference_expression(
Expand Down

0 comments on commit ce62e74

Please sign in to comment.