sixtyfps_compilerlib/passes/
remove_unused_properties.rs

1// Copyright © SixtyFPS GmbH <info@sixtyfps.io>
2// SPDX-License-Identifier: (GPL-3.0-only OR LicenseRef-SixtyFPS-commercial)
3
4//! Remove the properties which are not used
5
6use crate::object_tree::Component;
7use std::collections::HashSet;
8
9pub fn remove_unused_properties(component: &Component) {
10    fn recurse_remove_unused_properties(component: &Component) {
11        crate::object_tree::recurse_elem_including_sub_components_no_borrow(
12            component,
13            &(),
14            &mut |elem, _| {
15                let mut to_remove = HashSet::new();
16                for (prop, decl) in &elem.borrow().property_declarations {
17                    if !decl.expose_in_public_api
18                        && !elem.borrow().named_references.is_referenced(prop)
19                        && !elem
20                            .borrow()
21                            .property_analysis
22                            .borrow()
23                            .get(prop)
24                            .map_or(false, |v| v.is_used())
25                    {
26                        to_remove.insert(prop.to_owned());
27                    }
28                }
29                let mut elem = elem.borrow_mut();
30                for x in &to_remove {
31                    elem.property_declarations.remove(x);
32                    elem.property_analysis.borrow_mut().remove(x);
33                    elem.bindings.remove(x);
34                }
35            },
36        );
37
38        for global in &component.used_types.borrow().globals {
39            recurse_remove_unused_properties(global);
40        }
41    }
42    recurse_remove_unused_properties(component)
43}