(
&self,
class_name: Token<'gc>,
properties: &[ObjectProperty<'gc>],
)
| 146 | } |
| 147 | |
| 148 | pub fn validate_object_literal( |
| 149 | &self, |
| 150 | class_name: Token<'gc>, |
| 151 | properties: &[ObjectProperty<'gc>], |
| 152 | ) -> Result<(), Vec<ValidationError<'gc>>> { |
| 153 | let class_info = self |
| 154 | .class_info |
| 155 | .get(class_name.lexeme) |
| 156 | .ok_or_else(|| vec![ValidationError::ClassNotFound(class_name)])?; |
| 157 | |
| 158 | let mut errors = Vec::new(); |
| 159 | let mut provided_fields = HashSet::new(); |
| 160 | |
| 161 | // Check each property |
| 162 | for prop in properties { |
| 163 | match prop { |
| 164 | ObjectProperty::Literal { key, value } => { |
| 165 | if let Some(field) = class_info |
| 166 | .fields |
| 167 | .iter() |
| 168 | .find(|f| f.name.lexeme == key.lexeme) |
| 169 | { |
| 170 | if !provided_fields.insert(key.lexeme) { |
| 171 | errors.push(ValidationError::DuplicateField(class_name, *key)); |
| 172 | continue; |
| 173 | } |
| 174 | // Type check |
| 175 | if self.check_type(value, field.ty).is_err() { |
| 176 | errors.push(ValidationError::TypeError { |
| 177 | class_token: class_name, |
| 178 | field_token: *key, |
| 179 | expected_type: field.ty, |
| 180 | }); |
| 181 | } |
| 182 | } else { |
| 183 | errors.push(ValidationError::InvalidField(class_name, *key)); |
| 184 | } |
| 185 | provided_fields.insert(key.lexeme); |
| 186 | } |
| 187 | ObjectProperty::Computed { .. } => { |
| 188 | errors.push(ValidationError::ComputedPropertyError(class_name)); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Check for missing required fields |
| 194 | let missing_fields: Vec<_> = class_info |
| 195 | .fields |
| 196 | .iter() |
| 197 | .filter(|field| field.required && !provided_fields.contains(field.name.lexeme)) |
| 198 | .map(|field| field.name.lexeme) |
| 199 | .collect(); |
| 200 | |
| 201 | if !missing_fields.is_empty() { |
| 202 | errors.push(ValidationError::MissingFields(class_name, missing_fields)); |
| 203 | } |
| 204 | |
| 205 | if errors.is_empty() { |
no test coverage detected