Skip to content

Commit be5e5e8

Browse files
committed
feat(ast): add MemberExpressionKind::static_property_info (#11900)
1 parent 6c9c580 commit be5e5e8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,28 @@ impl<'a> MemberExpressionKind<'a> {
437437
}
438438
}
439439

440+
/// Returns the static property name of this member expression, if it has one, along with the source code [`Span`],
441+
/// or `None` otherwise.
442+
///
443+
/// If you don't need the [`Span`], use [`MemberExpressionKind::static_property_name`] instead.
444+
pub fn static_property_info(&self) -> Option<(Span, &'a str)> {
445+
match self {
446+
Self::Computed(expr) => match &expr.expression {
447+
Expression::StringLiteral(lit) => Some((lit.span, lit.value.as_str())),
448+
Expression::TemplateLiteral(lit) => {
449+
if lit.quasis.len() == 1 {
450+
lit.quasis[0].value.cooked.map(|cooked| (lit.span, cooked.as_str()))
451+
} else {
452+
None
453+
}
454+
}
455+
_ => None,
456+
},
457+
Self::Static(expr) => Some((expr.property.span, expr.property.name.as_str())),
458+
Self::PrivateField(_) => None,
459+
}
460+
}
461+
440462
/// Returns the object of the member expression, otherwise `None`.
441463
///
442464
/// Example: returns the `obj` in `obj.prop` or `obj["prop"]`.

0 commit comments

Comments
 (0)