Skip to content

Commit

Permalink
refactor(ast): move all ts ast related impl methods to ast_impl (#6015
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Dunqing committed Sep 24, 2024
1 parent 7f0e27a commit 1fc80d1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
42 changes: 0 additions & 42 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,6 @@ pub enum TSTypeOperatorOperator {
Readonly = 2,
}

impl TSTypeOperatorOperator {
pub fn to_str(self) -> &'static str {
match self {
TSTypeOperatorOperator::Keyof => "keyof",
TSTypeOperatorOperator::Readonly => "readonly",
TSTypeOperatorOperator::Unique => "unique",
}
}
}

/// TypeScript Array Type
///
/// Does not include tuple types, which are stored as [`TSTupleType`].
Expand Down Expand Up @@ -1372,20 +1362,6 @@ pub enum TSModuleDeclarationKind {
Namespace = 2,
}

impl TSModuleDeclarationKind {
pub fn is_global(self) -> bool {
matches!(self, TSModuleDeclarationKind::Global)
}

pub fn to_str(self) -> &'static str {
match self {
TSModuleDeclarationKind::Global => "global",
TSModuleDeclarationKind::Namespace => "namespace",
TSModuleDeclarationKind::Module => "module",
}
}
}

/// The name of a TypeScript [namespace or module declaration](TSModuleDeclaration).
///
/// Note that it is a syntax error for namespace declarations to have a string literal name.
Expand Down Expand Up @@ -1426,24 +1402,6 @@ pub enum TSModuleDeclarationBody<'a> {
TSModuleBlock(Box<'a, TSModuleBlock<'a>>) = 1,
}

impl<'a> TSModuleDeclarationBody<'a> {
pub fn is_empty(&self) -> bool {
match self {
TSModuleDeclarationBody::TSModuleDeclaration(declaration) => declaration.body.is_none(),
TSModuleDeclarationBody::TSModuleBlock(block) => block.body.len() == 0,
}
}

pub fn as_module_block_mut(&mut self) -> Option<&mut TSModuleBlock<'a>> {
match self {
TSModuleDeclarationBody::TSModuleBlock(block) => Some(block.as_mut()),
TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
decl.body.as_mut().and_then(|body| body.as_module_block_mut())
}
}
}
}

// See serializer in serialize.rs
#[ast(visit)]
#[derive(Debug)]
Expand Down
32 changes: 31 additions & 1 deletion crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ impl<'a> TSModuleDeclaration<'a> {
}

impl TSModuleDeclarationKind {
pub fn as_str(&self) -> &str {
pub fn is_global(self) -> bool {
matches!(self, TSModuleDeclarationKind::Global)
}

pub fn as_str(&self) -> &'static str {
match self {
Self::Global => "global",
Self::Module => "module",
Expand Down Expand Up @@ -208,6 +212,22 @@ impl<'a> TSModuleDeclarationBody<'a> {
pub fn is_strict(&self) -> bool {
matches!(self, Self::TSModuleBlock(block) if block.is_strict())
}

pub fn is_empty(&self) -> bool {
match self {
TSModuleDeclarationBody::TSModuleDeclaration(declaration) => declaration.body.is_none(),
TSModuleDeclarationBody::TSModuleBlock(block) => block.body.len() == 0,
}
}

pub fn as_module_block_mut(&mut self) -> Option<&mut TSModuleBlock<'a>> {
match self {
TSModuleDeclarationBody::TSModuleBlock(block) => Some(block.as_mut()),
TSModuleDeclarationBody::TSModuleDeclaration(decl) => {
decl.body.as_mut().and_then(|body| body.as_module_block_mut())
}
}
}
}

impl<'a> TSModuleBlock<'a> {
Expand Down Expand Up @@ -255,3 +275,13 @@ impl ImportOrExportKind {
matches!(self, Self::Type)
}
}

impl TSTypeOperatorOperator {
pub fn to_str(self) -> &'static str {
match self {
TSTypeOperatorOperator::Keyof => "keyof",
TSTypeOperatorOperator::Readonly => "readonly",
TSTypeOperatorOperator::Unique => "unique",
}
}
}
2 changes: 1 addition & 1 deletion crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ impl<'a> Format<'a> for TSModuleDeclaration<'a> {
parts.push(ss!("declare "));
}

parts.push(ss!(self.kind.to_str()));
parts.push(ss!(self.kind.as_str()));
parts.push(space!());
parts.push(self.id.format(p));
parts.push(ss!(" {"));
Expand Down

0 comments on commit 1fc80d1

Please sign in to comment.