diff --git a/rbx_reflection/CHANGELOG.md b/rbx_reflection/CHANGELOG.md index acc3c6c34..7221cf1d9 100644 --- a/rbx_reflection/CHANGELOG.md +++ b/rbx_reflection/CHANGELOG.md @@ -1,6 +1,9 @@ # rbx_reflection Changelog ## Unreleased Changes +* Add `superclasses` method to `ReflectionDatabase` to get a set of superclasses for a given class. ([#402]) + +[#402]: https://github.com/rojo-rbx/rbx-dom/pull/402 ## 4.5.0 (2024-01-16) * Update to rbx_types 1.8. diff --git a/rbx_reflection/src/database.rs b/rbx_reflection/src/database.rs index 0390ff4c6..7b5959446 100644 --- a/rbx_reflection/src/database.rs +++ b/rbx_reflection/src/database.rs @@ -39,6 +39,24 @@ impl<'a> ReflectionDatabase<'a> { enums: HashMap::new(), } } + + /// Returns a list of superclasses for the provided class name. This list + /// will start with the provided class and end with `Instance` if the class + /// exists. + pub fn superclasses(&self, class_name: &str) -> Option> { + // As of the time of writing (14 March 2024), the class with the most + // superclasses has 6 of them. + let mut list = Vec::with_capacity(6); + let mut current_class = self.classes.get(class_name); + current_class?; + + while let Some(class) = current_class { + list.push(class); + current_class = class.superclass.as_ref().and_then(|s| self.classes.get(s)); + } + + Some(list) + } } /// Describes a class of Instance, its properties, and its relation to other