refactor(ast): move all ts ast related impl methods to ast_impl (#6015)

This commit is contained in:
Dunqing 2024-09-24 06:07:37 +00:00
parent 7f0e27afd9
commit 1fc80d1bba
3 changed files with 32 additions and 44 deletions

View file

@ -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`].
@ -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.
@ -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)]

View file

@ -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",
@ -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> {
@ -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",
}
}
}

View file

@ -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!(" {"));