diff --git a/crates/oxc_ast/src/ast_impl/ts.rs b/crates/oxc_ast/src/ast_impl/ts.rs index f7dcf2efba71f..2a0f8e646e433 100644 --- a/crates/oxc_ast/src/ast_impl/ts.rs +++ b/crates/oxc_ast/src/ast_impl/ts.rs @@ -126,8 +126,29 @@ impl<'a> TSType<'a> { } impl TSAccessibility { - pub fn is_private(&self) -> bool { - matches!(self, TSAccessibility::Private) + #[inline] + pub fn is_private(self) -> bool { + matches!(self, Self::Private) + } + + pub fn as_str(self) -> &'static str { + match self { + Self::Public => "public", + Self::Private => "private", + Self::Protected => "protected", + } + } +} + +impl From for &'static str { + fn from(accessibility: TSAccessibility) -> Self { + accessibility.as_str() + } +} + +impl fmt::Display for TSAccessibility { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.as_str()) } } diff --git a/crates/oxc_isolated_declarations/src/class.rs b/crates/oxc_isolated_declarations/src/class.rs index d7f6862c6c4d8..517dfed89b14a 100644 --- a/crates/oxc_isolated_declarations/src/class.rs +++ b/crates/oxc_isolated_declarations/src/class.rs @@ -242,12 +242,13 @@ impl<'a> IsolatedDeclarations<'a> { let mut elements = self.ast.vec(); for (index, param) in function.params.items.iter().enumerate() { if param.accessibility.is_some() || param.readonly { - let type_annotation = if param.accessibility.is_some_and(|a| a.is_private()) { - None - } else { - // transformed params will definitely have type annotation - self.ast.copy(¶ms.items[index].pattern.type_annotation) - }; + let type_annotation = + if param.accessibility.is_some_and(oxc_ast::ast::TSAccessibility::is_private) { + None + } else { + // transformed params will definitely have type annotation + self.ast.copy(¶ms.items[index].pattern.type_annotation) + }; if let Some(new_element) = self.transform_formal_parameter_to_class_property(param, type_annotation) { @@ -270,7 +271,7 @@ impl<'a> IsolatedDeclarations<'a> { for element in &decl.body.body { if let ClassElement::MethodDefinition(method) = element { if method.key.is_private_identifier() - || method.accessibility.is_some_and(|a| a.is_private()) + || method.accessibility.is_some_and(oxc_ast::ast::TSAccessibility::is_private) || (method.computed && !self.is_literal_key(&method.key)) { continue; @@ -359,7 +360,7 @@ impl<'a> IsolatedDeclarations<'a> { if self.report_property_key(&method.key, method.computed) { continue; } - if method.accessibility.is_some_and(|a| a.is_private()) { + if method.accessibility.is_some_and(oxc_ast::ast::TSAccessibility::is_private) { elements.push(self.transform_private_modifier_method(method)); continue; }