i_slint_compiler/passes/
check_public_api.rs1use std::rc::Rc;
7
8use crate::diagnostics::{BuildDiagnostics, DiagnosticLevel};
9use crate::langtype::ElementType;
10use crate::object_tree::{Component, Document, ExportedName, PropertyVisibility};
11use crate::{CompilerConfiguration, ComponentSelection};
12use itertools::Either;
13
14pub fn check_public_api(
15 doc: &mut Document,
16 config: &CompilerConfiguration,
17 diag: &mut BuildDiagnostics,
18) {
19 let last = doc.last_exported_component();
20
21 if last.is_none() && !matches!(&config.components_to_generate, ComponentSelection::Named(_)) {
22 let last_imported = doc
23 .node
24 .as_ref()
25 .and_then(|n| {
26 let import_node = n.ImportSpecifier().last()?;
27 let import = crate::typeloader::ImportedName::extract_imported_names(&import_node.ImportIdentifierList()?).last()?;
28 let ElementType::Component(c) = doc.local_registry.lookup_element(&import.internal_name).ok()? else { return None };
29 diag.push_warning(format!("No component is exported. The last imported component '{}' will be used. This is deprecated", import.internal_name), &import_node);
30 let exported_name = ExportedName{ name: import.internal_name, name_ident: import_node.into() };
31 Some((exported_name, Either::Left(c)))
32 });
33 doc.exports.add_reexports(last_imported, diag);
34 }
35
36 match &config.components_to_generate {
37 ComponentSelection::ExportedWindows => doc.exports.retain(|export| {
38 if let Either::Left(c) = &export.1
40 && !c.is_global() && !super::windows::inherits_window(c) {
41 let is_last = last.as_ref().is_some_and(|last| !Rc::ptr_eq(last, c));
42 if is_last {
43 diag.push_warning(format!("Exported component '{}' doesn't inherit Window. No code will be generated for it", export.0.name), &export.0.name_ident);
44 return false;
45 } else if config.library_name.is_none () {
46 diag.push_warning(format!("Exported component '{}' doesn't inherit Window. This is deprecated", export.0.name), &export.0.name_ident);
47 }
48 }
49 true
50 }),
51 ComponentSelection::LastExported => doc.exports.retain(|export| {
53 if let Either::Left(c) = &export.1 {
54 c.is_global() || last.as_ref().is_none_or(|last| Rc::ptr_eq(last, c))
55 } else {
56 true
57 }
58 }),
59 ComponentSelection::Named(name) => {
61 doc.exports.retain(|export| {
62 if let Either::Left(c) = &export.1 {
63 c.is_global() || c.id == name
64 } else {
65 true
66 }
67 });
68 if doc.last_exported_component().is_none() {
69 if let Ok(ElementType::Component(c)) = doc.local_registry.lookup_element(name)
71 && let Some(name_ident) = c.node.as_ref().map(|n| n.DeclaredIdentifier().into()) {
72 doc.exports.add_reexports(
73 [(ExportedName{ name: name.into(), name_ident }, Either::Left(c))],
74 diag,
75 );
76 }
77 }
78 },
79 }
80
81 for c in doc.exported_roots() {
82 check_public_api_component(&c, diag);
83 }
84 for (export_name, e) in &*doc.exports {
85 if let Some(c) = e.as_ref().left()
86 && c.is_global()
87 {
88 c.exported_global_names.borrow_mut().push(export_name.clone());
90 check_public_api_component(c, diag)
91 }
92 }
93}
94
95fn check_public_api_component(root_component: &Rc<Component>, diag: &mut BuildDiagnostics) {
96 let mut root_elem = root_component.root_element.borrow_mut();
97 let root_elem = &mut *root_elem;
98 let mut pa = root_elem.property_analysis.borrow_mut();
99 root_elem.property_declarations.iter_mut().for_each(|(n, d)| {
100 if d.property_type.ok_for_public_api() {
101 if d.visibility == PropertyVisibility::Private {
102 root_component.private_properties.borrow_mut().push((n.clone(), d.property_type.clone()));
103 } else {
104 d.expose_in_public_api = true;
105 if d.visibility != PropertyVisibility::Output {
106 pa.entry(n.clone()).or_default().is_set = true;
107 }
108 }
109 } else {
110 diag.push_diagnostic(
111 format!("Properties of type {} are not supported yet for public API. The property will not be exposed", d.property_type),
112 &d.type_node(),
113 DiagnosticLevel::Warning
114 );
115 }
116 });
117}