Skip to content
Merged
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
22 changes: 22 additions & 0 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`.
Expand Down
Loading