fallow-core 3.23.0

Internal detector backend for fallow-engine and fallow-api
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use super::*;

const MAX_EXPORTED_OBJECT_INSTANCE_MEMBER_ASSOCIATIONS: usize = 8192;

/// Credit member accesses produced by static-factory call bindings on the
/// originating class export.
pub(super) fn propagate_factory_call_accesses(
    graph: &ModuleGraph,
    resolved_modules: &[ResolvedModule],
    indexes: &MemberPassIndexes<'_>,
    accessed_members: &mut FxHashMap<ExportKey, FxHashSet<String>>,
) {
    for resolved in resolved_modules {
        let local_to_export_keys = indexes.local_keys(resolved.file_id);
        for access in factory_call_member_accesses(resolved) {
            let Some(seed_keys) = local_to_export_keys.get(access.callee_object.as_str()) else {
                continue;
            };
            for seed_key in seed_keys {
                for origin in
                    walk_re_export_origins(graph, seed_key.file_id, seed_key.export_name.as_str())
                {
                    let Some(origin_module) = indexes.module_by_id.get(&origin.file_id) else {
                        continue;
                    };
                    let matches_factory = origin_module.exports.iter().any(|export| {
                        export.name.matches_str(origin.export_name.as_str())
                            && export.members.iter().any(|member| {
                                member.is_instance_returning_static
                                    && member.kind == MemberKind::ClassMethod
                                    && member.name == access.callee_method
                            })
                    });
                    if !matches_factory {
                        continue;
                    }
                    accessed_members
                        .entry(origin)
                        .or_default()
                        .insert(access.member.clone());
                }
            }
        }
    }
}

pub(super) struct FactoryReturnCreditContext<'a, 'ctx> {
    pub(super) graph: &'ctx ModuleGraph,
    pub(super) indexes: &'ctx MemberPassIndexes<'a>,
    pub(super) accessed_members: &'ctx mut FxHashMap<ExportKey, FxHashSet<String>>,
}

pub(super) fn credit_factory_return_class_member(
    context: &mut FactoryReturnCreditContext<'_, '_>,
    factory_origin_file_id: FileId,
    class_local_name: &str,
    member: &str,
) {
    for class_origin in factory_return_class_origins(
        context.graph,
        context.indexes,
        factory_origin_file_id,
        class_local_name,
    ) {
        context
            .accessed_members
            .entry(class_origin)
            .or_default()
            .insert(member.to_string());
    }
}

/// Credit member accesses produced by cross-module free-function factory
/// bindings (`const x = importedFactory(); x.member`) onto the class the factory
/// returns. Each link in the resolution chain is also an over-credit guard, and
/// a wrong credit is a silent false-negative, so every link must hold:
///
///   1. the fact's callee resolves through the consumer's imports/exports to an
///      export key (`local_to_export_keys`);
///   2. that key walks (re-export aware) to an origin module that actually
///      declares an `exported_factory_returns` entry for the export, i.e. an
///      internal exported factory proven to return a single class;
///   3. the entry's `class_local_name` resolves through THAT factory module's own
///      imports/exports to a class export;
///   4. the resolved export is a class with members.
///
/// See issue #1441 (Part A).
pub(super) fn propagate_factory_fn_accesses(
    graph: &ModuleGraph,
    resolved_modules: &[ResolvedModule],
    indexes: &MemberPassIndexes<'_>,
    accessed_members: &mut FxHashMap<ExportKey, FxHashSet<String>>,
) {
    for resolved in resolved_modules {
        let local_to_export_keys = indexes.local_keys(resolved.file_id);
        for access in factory_fn_member_accesses(resolved) {
            let Some(seed_keys) = local_to_export_keys.get(access.callee_name.as_str()) else {
                continue;
            };
            let classes = seed_keys
                .iter()
                .flat_map(|seed_key| factory_return_classes_for_callee(graph, indexes, seed_key));
            for class_origin in classes {
                accessed_members
                    .entry(class_origin)
                    .or_default()
                    .insert(access.member.clone());
            }
        }
    }
}

/// Credit member accesses produced by cross-module OBJECT-LITERAL factory bindings
/// (`const ui = importedFactory(); ui.orders.member`) onto the class the factory's
/// returned object literal exposes at that property path. Mirrors
/// `propagate_factory_fn_accesses` with an extra property-path lookup dimension;
/// every link (callee resolves to an export, the export's factory module declares
/// an object shape with that path, the path's class resolves to a class-with-members
/// export) is an over-credit gate, so a wrong or unresolvable chain is a silent
/// false-negative, never a false positive. See issue #1858.
pub(super) fn propagate_factory_return_object_accesses(
    graph: &ModuleGraph,
    resolved_modules: &[ResolvedModule],
    indexes: &MemberPassIndexes<'_>,
    accessed_members: &mut FxHashMap<ExportKey, FxHashSet<String>>,
) {
    for resolved in resolved_modules {
        let local_to_export_keys = indexes.local_keys(resolved.file_id);
        for access in factory_return_object_property_accesses(resolved) {
            let Some(seed_keys) = local_to_export_keys.get(access.callee_name.as_str()) else {
                continue;
            };
            let classes = seed_keys.iter().flat_map(|seed_key| {
                factory_return_object_property_classes(
                    graph,
                    indexes,
                    seed_key,
                    access.property_path.as_str(),
                )
            });
            let mut context = FactoryReturnCreditContext {
                graph,
                indexes,
                accessed_members,
            };
            for (factory_origin_file_id, class_local_name) in classes {
                credit_factory_return_class_member(
                    &mut context,
                    factory_origin_file_id,
                    &class_local_name,
                    access.member.as_str(),
                );
            }
        }
    }
}

/// Credit a member reached through a class instance stored in an exported object.
pub(super) fn propagate_exported_object_instance_accesses(
    graph: &ModuleGraph,
    resolved_modules: &[ResolvedModule],
    indexes: &MemberPassIndexes<'_>,
    accessed_members: &mut FxHashMap<ExportKey, FxHashSet<String>>,
    whole_object_used_exports: &mut FxHashSet<ExportKey>,
) {
    let mut origin_cache: FxHashMap<ExportKey, Vec<ExportKey>> = FxHashMap::default();
    let mut class_origins_by_property: FxHashMap<(FileId, String, String), Vec<ExportKey>> =
        FxHashMap::default();
    let mut processed_accesses: FxHashSet<(FileId, String, String, String)> = FxHashSet::default();
    let mut abstained_properties: FxHashSet<(FileId, String, String)> = FxHashSet::default();
    let mut emitted_associations = 0usize;
    for consumer in resolved_modules {
        let local_to_export_keys = indexes.local_keys(consumer.file_id);
        for access in SemanticFactView::new(&consumer.semantic_facts, &consumer.member_accesses)
            .ordinary_member_accesses()
        {
            let Some((container_local, property_path)) = access.object.split_once('.') else {
                continue;
            };
            let Some(container_keys) = local_to_export_keys.get(container_local) else {
                continue;
            };
            for container_key in container_keys {
                let origins = origin_cache
                    .entry(container_key.clone())
                    .or_insert_with(|| {
                        walk_re_export_origins(
                            graph,
                            container_key.file_id,
                            container_key.export_name.as_str(),
                        )
                    });
                for container_origin in origins {
                    let property_key = (
                        container_origin.file_id,
                        container_origin.export_name.clone(),
                        property_path.to_string(),
                    );
                    if abstained_properties.contains(&property_key) {
                        continue;
                    }
                    let class_origins = class_origins_by_property
                        .entry(property_key.clone())
                        .or_insert_with(|| {
                            let mut origins = indexes
                                .exported_object_classes_by_property
                                .get(&(
                                    container_origin.file_id,
                                    container_origin.export_name.as_str(),
                                    property_path,
                                ))
                                .into_iter()
                                .flatten()
                                .flat_map(|class_local_name| {
                                    factory_return_class_origins(
                                        graph,
                                        indexes,
                                        container_origin.file_id,
                                        class_local_name,
                                    )
                                })
                                .collect::<FxHashSet<_>>()
                                .into_iter()
                                .collect::<Vec<_>>();
                            origins.sort_unstable_by(|left, right| {
                                left.file_id
                                    .0
                                    .cmp(&right.file_id.0)
                                    .then_with(|| left.export_name.cmp(&right.export_name))
                            });
                            origins
                        });
                    if class_origins.is_empty() {
                        continue;
                    }
                    let processed_key = (
                        container_origin.file_id,
                        container_origin.export_name.clone(),
                        property_path.to_string(),
                        access.member.clone(),
                    );
                    if processed_accesses.contains(&processed_key) {
                        continue;
                    }
                    if emitted_associations >= MAX_EXPORTED_OBJECT_INSTANCE_MEMBER_ASSOCIATIONS
                        || emitted_associations.saturating_add(class_origins.len())
                            > MAX_EXPORTED_OBJECT_INSTANCE_MEMBER_ASSOCIATIONS
                    {
                        whole_object_used_exports.extend(class_origins.iter().cloned());
                        abstained_properties.insert(property_key);
                        continue;
                    }
                    processed_accesses.insert(processed_key);
                    emitted_associations += class_origins.len();
                    for class_origin in class_origins {
                        accessed_members
                            .entry(class_origin.clone())
                            .or_default()
                            .insert(access.member.clone());
                    }
                }
            }
        }
    }
}

/// The `(factory-module, class-local-name)` pairs a callee's exported object-literal
/// factory exposes at `property_path`, resolved across re-export barrels. Empty for
/// any callee that is not an internal exported object-factory declaring that path.
fn factory_return_object_property_classes(
    graph: &ModuleGraph,
    indexes: &MemberPassIndexes<'_>,
    seed_key: &ExportKey,
    property_path: &str,
) -> Vec<(FileId, String)> {
    let mut out = Vec::new();
    for factory_origin in
        walk_re_export_origins(graph, seed_key.file_id, seed_key.export_name.as_str())
    {
        let Some(factory_module) = indexes.module_by_id.get(&factory_origin.file_id) else {
            continue;
        };
        let Some(shape) = factory_module
            .exported_factory_return_object_shapes
            .iter()
            .find(|shape| shape.export_name.as_str() == factory_origin.export_name.as_str())
        else {
            continue;
        };
        for property in &shape.properties {
            if property.property_path == property_path {
                out.push((factory_origin.file_id, property.class_local_name.clone()));
            }
        }
    }
    out
}

/// The class exports a proven exported factory returns, resolved from the factory's
/// own module. Shared by the member-credit and whole-object-suppress passes: each
/// link is an over-credit gate, so a callee that is not a proven factory yields
/// nothing.
fn factory_return_class_origins(
    graph: &ModuleGraph,
    indexes: &MemberPassIndexes<'_>,
    factory_origin_file_id: FileId,
    class_local_name: &str,
) -> Vec<ExportKey> {
    let factory_local_keys = indexes.local_keys(factory_origin_file_id);
    let mut class_seed_keys = factory_local_keys
        .get(class_local_name)
        .cloned()
        .unwrap_or_default();
    if class_seed_keys.is_empty()
        && let Some((namespace_local, export_name)) = class_local_name.split_once('.')
        && !export_name.contains('.')
        && let Some(factory_module) = indexes.module_by_id.get(&factory_origin_file_id)
    {
        class_seed_keys.extend(factory_module.all_resolved_imports().filter_map(|import| {
            if import.info.local_name != namespace_local
                || !matches!(
                    import.info.imported_name,
                    crate::extract::ImportedName::Namespace
                )
            {
                return None;
            }
            Some(ExportKey::new(
                import.target.internal_file_id()?,
                export_name,
            ))
        }));
    }
    class_seed_keys
        .iter()
        .flat_map(|class_seed| export_key_with_origins(graph, class_seed))
        .filter(|class_origin| {
            indexes
                .module_by_id
                .get(&class_origin.file_id)
                .is_some_and(|class_module| {
                    export_is_class_with_members(class_module, class_origin.export_name.as_str())
                })
        })
        .collect()
}

/// The classes a callee's proven exported factory returns, resolved across modules.
/// Empty for any callee that is not an internal exported factory with a strict,
/// value-proven return: each link of the chain is an over-credit gate.
fn factory_return_classes_for_callee(
    graph: &ModuleGraph,
    indexes: &MemberPassIndexes<'_>,
    seed_key: &ExportKey,
) -> Vec<ExportKey> {
    let mut origins = Vec::new();
    for factory_origin in
        walk_re_export_origins(graph, seed_key.file_id, seed_key.export_name.as_str())
    {
        let Some(factory_module) = indexes.module_by_id.get(&factory_origin.file_id) else {
            continue;
        };
        let Some(factory_return) =
            factory_module
                .exported_factory_returns
                .iter()
                .find(|factory_return| {
                    factory_origin.export_name.as_str() == factory_return.export_name.as_str()
                })
        else {
            continue;
        };
        origins.extend(factory_return_class_origins(
            graph,
            indexes,
            factory_origin.file_id,
            factory_return.class_local_name.as_str(),
        ));
    }
    origins
}

/// Suppress a class whose factory-returned instance is consumed opaquely
/// (`const { a, ...rest } = importedFactory()`, a computed destructure key).
///
/// The consumer can read any property, so no set of member accesses describes what
/// is used. Crediting only the keys the pattern names would leave every other live
/// member reported as dead. Mark the export wholly used instead; the member scan
/// then skips it, exactly as it does for a class whose instance escapes.
pub(super) fn propagate_factory_fn_whole_object_uses(
    graph: &ModuleGraph,
    resolved_modules: &[ResolvedModule],
    indexes: &MemberPassIndexes<'_>,
    whole_object_used_exports: &mut FxHashSet<ExportKey>,
) {
    for resolved in resolved_modules {
        let local_to_export_keys = indexes.local_keys(resolved.file_id);
        for fact in factory_fn_whole_objects(resolved) {
            let Some(seed_keys) = local_to_export_keys.get(fact.callee_name.as_str()) else {
                continue;
            };
            let classes = seed_keys
                .iter()
                .flat_map(|seed_key| factory_return_classes_for_callee(graph, indexes, seed_key));
            whole_object_used_exports.extend(classes);
        }
    }
}