feat(semantic): add static property, ElementKind::Getter, ElementKind::Setter in ClassTable (#2445)

This commit is contained in:
Dunqing 2024-02-20 13:07:48 +08:00 committed by GitHub
parent 9cfc97c30d
commit 950298d960
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 12 deletions

View file

@ -1,7 +1,7 @@
use oxc_ast::{
ast::{
AccessorProperty, ClassBody, ClassElement, MethodDefinition, PrivateIdentifier,
PropertyDefinition,
AccessorProperty, ClassBody, ClassElement, MethodDefinition, MethodDefinitionKind,
PrivateIdentifier, PropertyDefinition,
},
AstKind,
};
@ -64,7 +64,13 @@ impl ClassTableBuilder {
if let Some(class_id) = self.current_class_id {
self.classes.add_element(
class_id,
Element::new(name, property.key.span(), is_private, ElementKind::Property),
Element::new(
name,
property.key.span(),
property.r#static,
is_private,
ElementKind::Accessor,
),
);
}
}
@ -79,7 +85,13 @@ impl ClassTableBuilder {
if let Some(class_id) = self.current_class_id {
self.classes.add_element(
class_id,
Element::new(name, property.key.span(), is_private, ElementKind::Property),
Element::new(
name,
property.key.span(),
property.r#static,
is_private,
ElementKind::Property,
),
);
}
}
@ -111,7 +123,7 @@ impl ClassTableBuilder {
}
pub fn declare_class_method(&mut self, method: &MethodDefinition) {
if method.kind.is_constructor() {
if method.kind.is_constructor() || method.value.is_typescript_syntax() {
return;
}
let is_private = method.key.is_private_identifier();
@ -121,7 +133,21 @@ impl ClassTableBuilder {
if let Some(class_id) = self.current_class_id {
self.classes.add_element(
class_id,
Element::new(name, method.key.span(), is_private, ElementKind::Method),
Element::new(
name,
method.key.span(),
method.r#static,
is_private,
match method.kind {
MethodDefinitionKind::Method => ElementKind::Method,
MethodDefinitionKind::Get => ElementKind::Method | ElementKind::Getter,
MethodDefinitionKind::Set => ElementKind::Method | ElementKind::Setter,
MethodDefinitionKind::Constructor => {
// Skip constructor
unreachable!()
}
},
),
);
}
}

View file

@ -10,12 +10,19 @@ pub struct Element {
pub name: Atom,
pub span: Span,
pub is_private: bool,
pub r#static: bool,
pub kind: ElementKind,
}
impl Element {
pub fn new(name: Atom, span: Span, is_private: bool, kind: ElementKind) -> Self {
Self { name, span, is_private, kind }
pub fn new(
name: Atom,
span: Span,
r#static: bool,
is_private: bool,
kind: ElementKind,
) -> Self {
Self { name, span, is_private, r#static, kind }
}
}

View file

@ -9,11 +9,13 @@ define_index_type! {
}
bitflags! {
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct ElementKind: u8 {
const Accessor = 0;
const Method = 1;
const Property = 2;
const Accessor = 1 << 0;
const Method = 1 << 1;
const Property = 1 << 2;
const Setter = 1 << 3;
const Getter = 1 << 4;
}
}
@ -29,4 +31,8 @@ impl ElementKind {
pub fn is_accessor(self) -> bool {
self.contains(Self::Accessor)
}
pub fn is_setter_or_getter(self) -> bool {
self.intersects(Self::Setter | Self::Getter)
}
}