Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): allow conversion from TSAccessibility into &'static str #4711

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TSAccessibility> 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())
}
}

Expand Down
17 changes: 9 additions & 8 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params.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(&params.items[index].pattern.type_annotation)
};
if let Some(new_element) =
self.transform_formal_parameter_to_class_property(param, type_annotation)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down