Skip to main content

fallow_graph/graph/
namespace_escapes.rs

1//! Namespace bindings that get the full export credit of their target.
2//!
3//! A namespace binding (`import * as Ns`, or a named import of an
4//! `export * as Ns` re-export) that the code uses as a whole value can read
5//! any member through a computed key. The graph then credits every export of
6//! the target module instead of the members that the code reads by name. The
7//! credit sites record a [`NamespaceEscape`] here, so the analysis can report
8//! the bindings that hide exports from the unused-export check (issue #5054).
9//!
10//! The record is written by the same branches that give the full credit, so
11//! the reported set and the credited set cannot differ.
12
13use fallow_types::discover::FileId;
14
15/// Why a namespace binding gets the full credit of its target.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
17pub enum NamespaceEscapeReason {
18    /// The code uses the binding as a value: a cast, an assignment, a call
19    /// argument, a spread, a computed member read, or a reflective call such
20    /// as `Object.keys(Ns)`.
21    WholeObjectUse,
22    /// A non-entry module re-exports the binding with `export { Ns }`, so the
23    /// graph cannot see which members its consumers read.
24    ReExportedBinding,
25    /// The code reads no member of the binding by name and does not use it
26    /// as a value (for example only `typeof Ns`).
27    NoMemberRead,
28}
29
30/// One namespace binding that gets the full export credit of `target`.
31#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
32pub struct NamespaceEscape {
33    /// The file that declares the binding.
34    pub consumer: FileId,
35    /// The span of the import that declares the binding.
36    #[serde(with = "crate::cache::span_serde")]
37    pub import_span: oxc_span::Span,
38    /// The local name of the binding.
39    pub local_name: String,
40    /// The module whose exports get the full credit.
41    pub target: FileId,
42    /// Why the binding gets the full credit.
43    pub reason: NamespaceEscapeReason,
44    /// The members that the code also reads by name through the binding.
45    /// The full credit does not hide these members.
46    pub read_members: Vec<String>,
47}
48
49/// Sort the records of one graph build and keep one record per binding.
50///
51/// A binding can reach the credit code once per namespace lane (type and
52/// value), and both lanes give the same record.
53pub(in crate::graph) fn normalize(escapes: &mut Vec<NamespaceEscape>) {
54    escapes.sort_by(|a, b| {
55        (a.consumer.0, a.import_span.start, a.target.0, &a.local_name).cmp(&(
56            b.consumer.0,
57            b.import_span.start,
58            b.target.0,
59            &b.local_name,
60        ))
61    });
62    escapes.dedup_by(|a, b| {
63        a.consumer == b.consumer
64            && a.import_span == b.import_span
65            && a.target == b.target
66            && a.local_name == b.local_name
67    });
68}