1use crate::expression::{Expression, PropertyKey};
4use crate::function::Function;
5use crate::identifier::Identifier;
6use crate::statement::Statement;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
14#[allow(clippy::struct_field_names)] pub struct Class {
16 id: Option<Identifier>,
17 super_class: Option<Box<Expression>>,
18 body: Vec<ClassMember>,
19}
20
21impl Class {
22 #[must_use]
24 pub fn new(
25 id: Option<Identifier>,
26 super_class: Option<Expression>,
27 body: Vec<ClassMember>,
28 ) -> Self {
29 Self {
30 id,
31 super_class: super_class.map(Box::new),
32 body,
33 }
34 }
35
36 #[must_use]
39 pub fn id(&self) -> Option<&Identifier> {
40 self.id.as_ref()
41 }
42
43 #[must_use]
45 pub fn super_class(&self) -> Option<&Expression> {
46 self.super_class.as_deref()
47 }
48
49 #[must_use]
51 pub fn body(&self) -> &[ClassMember] {
52 &self.body
53 }
54}
55
56impl std::fmt::Display for Class {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 f.write_str("class")?;
59 if let Some(name) = &self.id {
60 write!(f, " {name}")?;
61 }
62 if let Some(super_expr) = &self.super_class {
63 write!(f, " extends {super_expr}")?;
64 }
65 f.write_str(" { ... }")
66 }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq)]
71pub enum ClassMember {
72 Method {
74 key: PropertyKey,
76 value: Function,
78 kind: MethodKind,
80 is_static: bool,
82 computed: bool,
84 },
85 Property {
87 key: PropertyKey,
89 value: Option<Expression>,
91 is_static: bool,
93 computed: bool,
95 },
96 StaticBlock {
98 body: Vec<Statement>,
100 },
101}
102
103#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum MethodKind {
106 Constructor,
108 Method,
110 Get,
112 Set,
114}
115
116impl std::fmt::Display for MethodKind {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 f.write_str(match self {
119 Self::Constructor => "constructor",
120 Self::Method => "method",
121 Self::Get => "get",
122 Self::Set => "set",
123 })
124 }
125}