Skip to content

Commit 8814c53

Browse files
committed
refactor(ast): remove AstKind for PropertyKey (#12108)
- part of #11490 This removes the `PropertyKey` AstKind, in support of making the AST kinds more consistent (i.e., only `struct`s should be visited in the AST). Similar to past PRs, I've added a new `PropertyKeyKind` to narrow the `AstKind` enum (related: oxc-project/backlog#166). Comments on various changes: - `id_length` rule: I had to add new handling for nodes which were previously covered by visting `PropertyKey`. - `no_magic_numbers` rule: Similar to `id_length`, we need to be more careful when checking if a value is part of a class property initializer, since we can't fully rely on the parent kind anymore. - `prefer_dom_node_text_content` rule: the ancestry here has changed, fortunately for the better in this case. some of the previous juggling of parent/grandparent has been replaced with a simpler check. - `prefer_string_raw`: the logic for `PropertyKey` was already fully covered by `ObjectProperty`, so I've removed it. - Semantic binder changes: I added an additional check for binding the `SetAccessor` scope flags which ensures that we only set it when a function is part of a property definition's value, not its key. This evidenced as a test failure in the `no_setter_return` lint rule.
1 parent 228cff5 commit 8814c53

File tree

65 files changed

+383
-346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+383
-346
lines changed

crates/oxc_ast/src/ast_kind_impl.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ impl<'a> AstKind<'a> {
123123
}
124124
}
125125

126+
pub fn is_property_key(&self) -> bool {
127+
self.as_property_key_kind().is_some()
128+
}
129+
130+
pub fn as_property_key_kind(&self) -> Option<PropertyKeyKind<'a>> {
131+
match self {
132+
Self::IdentifierName(ident) => Some(PropertyKeyKind::Static(ident)),
133+
Self::PrivateIdentifier(ident) => Some(PropertyKeyKind::Private(ident)),
134+
_ => None,
135+
}
136+
}
137+
126138
pub fn from_expression(e: &'a Expression<'a>) -> Self {
127139
match e {
128140
Expression::BooleanLiteral(e) => Self::BooleanLiteral(e),
@@ -291,7 +303,6 @@ impl AstKind<'_> {
291303
Self::ObjectProperty(p) => {
292304
format!("ObjectProperty({})", p.key.name().unwrap_or(COMPUTED)).into()
293305
}
294-
Self::PropertyKey(p) => format!("PropertyKey({})", p.name().unwrap_or(COMPUTED)).into(),
295306
Self::Argument(_) => "Argument".into(),
296307
Self::AssignmentTarget(_) => "AssignmentTarget".into(),
297308
Self::SimpleAssignmentTarget(a) => {
@@ -559,3 +570,19 @@ impl GetSpan for ModuleDeclarationKind<'_> {
559570
}
560571
}
561572
}
573+
574+
pub enum PropertyKeyKind<'a> {
575+
/// A static identifier property key, like `a` in `{ a: 1 }`.
576+
Static(&'a IdentifierName<'a>),
577+
/// A private identifier property key, like `#a` in `class C { #a = 1 }`.
578+
Private(&'a PrivateIdentifier<'a>),
579+
}
580+
581+
impl GetSpan for PropertyKeyKind<'_> {
582+
fn span(&self) -> Span {
583+
match self {
584+
Self::Static(ident) => ident.span,
585+
Self::Private(ident) => ident.span,
586+
}
587+
}
588+
}

0 commit comments

Comments
 (0)