Skip to main content

fallow_types/
extract_factory.rs

1//! Persisted facts of free-function factories: the class that an exported
2//! factory returns, and the member reads on the result of an imported one.
3
4/// An exported free-function factory proven to return one class instance, or
5/// one that declares an array of a class as its return type.
6///
7/// `export function useApi() { return new RESTApi() }` records
8/// `FactoryReturnExport { export_name: "useApi", class_local_name: "RESTApi" }`.
9/// The `class_local_name` is the factory module's own LOCAL name, resolved at
10/// analyze time through that module's imports/exports to the real class export,
11/// so a cross-module `const x = useApi(); x.member` consumer credits the class
12/// across the boundary. See issue #1441 (Part A).
13#[derive(
14    Debug,
15    Clone,
16    serde::Serialize,
17    serde::Deserialize,
18    bitcode::Encode,
19    bitcode::Decode,
20    PartialEq,
21    Eq,
22)]
23pub struct FactoryReturnExport {
24    /// Public export name (honors `export { useApi as useRestApi }`).
25    pub export_name: String,
26    /// The returned class's local name within the factory module.
27    pub class_local_name: String,
28    /// The factory returns a promise of the class (an `async` function, or a
29    /// `Promise<Class>` return type). Only an awaited call result is the class.
30    #[serde(default)]
31    pub is_async: bool,
32    /// The factory declares an array return type (`make(): Sheet[]`), so an
33    /// element of the call result is the class, not the result itself.
34    #[serde(default)]
35    pub returns_array: bool,
36}
37
38/// A member access on a value returned by an imported free-function factory.
39///
40/// `const x = importedFactory(); x.member` emits one fact per first-level read
41/// on `x`. The analyze layer resolves `callee_name` through the consumer's
42/// imports to the factory's origin module, reads that module's
43/// `exported_factory_returns` to learn the returned class's local name, resolves
44/// THAT through the factory module's own imports to the class export, and
45/// credits `member` on the class. See issue #1441 (Part A).
46#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, bitcode::Encode, bitcode::Decode)]
47#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
48pub struct FactoryFnMemberAccessFact {
49    /// Local imported function used as the factory callee.
50    pub callee_name: String,
51    /// Member accessed on the returned instance-like object.
52    pub member: String,
53    /// The local holds the awaited call result (`const x = await factory()`).
54    pub awaited: bool,
55    /// The member is read on an element of the call result: an iteration
56    /// callback parameter, a `for...of` variable, or an index
57    /// (`make().forEach((sheet) => sheet.member)`).
58    #[serde(default)]
59    pub element: bool,
60}