Skip to main content

fallow_types/
extract_type_members.rs

1//! Persisted property types of named types, for the typed property join of
2//! the member pass.
3
4/// A named-type property whose declared type is a named type reference.
5///
6/// `interface Opts { c: OptDep }` (or `type Opts = { c: OptDep }`) records
7/// `TypeMemberTypeEntry { type_name: "Opts", property: "c", property_type: "OptDep" }`.
8/// Both `type_name` and `property_type` are the DECLARING module's own local
9/// names; resolution through that module's imports/exports is deferred to
10/// analyze time, mirroring `FactoryReturnExport.class_local_name`. Consumed by
11/// the `unused-class-member` typed-property-hop join so a consumer's
12/// `this.opts.c.optM()` credits `OptDep.optM` across module boundaries.
13/// See issue #1785.
14#[derive(
15    Debug,
16    Clone,
17    serde::Serialize,
18    serde::Deserialize,
19    bitcode::Encode,
20    bitcode::Decode,
21    PartialEq,
22    Eq,
23)]
24pub struct TypeMemberTypeEntry {
25    /// Local interface or type-alias name declaring the property.
26    pub type_name: String,
27    /// Property name declared on the type.
28    pub property: String,
29    /// The property's declared type name (local to the declaring module).
30    /// A type import such as `import("./room").Room` uses the form of
31    /// [`import_type_reference`].
32    pub property_type: String,
33}
34
35const IMPORT_TYPE_PREFIX: &str = "import(\"";
36const IMPORT_TYPE_SEPARATOR: &str = "\").";
37
38/// The `property_type` of a [`TypeMemberTypeEntry`] for the type import
39/// `import("<source>").<name>`.
40#[must_use]
41pub fn import_type_reference(source: &str, name: &str) -> String {
42    format!("{IMPORT_TYPE_PREFIX}{source}{IMPORT_TYPE_SEPARATOR}{name}")
43}
44
45/// The source and the export name of a type import that
46/// [`import_type_reference`] makes. `None` for a plain type name.
47#[must_use]
48pub fn parse_import_type_reference(reference: &str) -> Option<(&str, &str)> {
49    reference
50        .strip_prefix(IMPORT_TYPE_PREFIX)?
51        .rsplit_once(IMPORT_TYPE_SEPARATOR)
52        .filter(|(source, name)| !source.is_empty() && !name.is_empty() && !name.contains('.'))
53}