diff --git a/crates/oxc_ast/src/ast_kind_impl.rs b/crates/oxc_ast/src/ast_kind_impl.rs index 91c1f7140c4d6..3c36f95ba0769 100644 --- a/crates/oxc_ast/src/ast_kind_impl.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -437,6 +437,28 @@ impl<'a> MemberExpressionKind<'a> { } } + /// Returns the static property name of this member expression, if it has one, along with the source code [`Span`], + /// or `None` otherwise. + /// + /// If you don't need the [`Span`], use [`MemberExpressionKind::static_property_name`] instead. + pub fn static_property_info(&self) -> Option<(Span, &'a str)> { + match self { + Self::Computed(expr) => match &expr.expression { + Expression::StringLiteral(lit) => Some((lit.span, lit.value.as_str())), + Expression::TemplateLiteral(lit) => { + if lit.quasis.len() == 1 { + lit.quasis[0].value.cooked.map(|cooked| (lit.span, cooked.as_str())) + } else { + None + } + } + _ => None, + }, + Self::Static(expr) => Some((expr.property.span, expr.property.name.as_str())), + Self::PrivateField(_) => None, + } + } + /// Returns the object of the member expression, otherwise `None`. /// /// Example: returns the `obj` in `obj.prop` or `obj["prop"]`.