Skip to main content

fallow_types/
results_namespace_escape.rs

1//! Namespace bindings that hide exports from the unused-export check.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7use crate::serde_path;
8
9/// A namespace binding that the code uses as a whole value, so fallow
10/// credits every export of the target module and does not check them.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
13pub struct NamespaceEscape {
14    /// The file that declares the binding.
15    #[serde(serialize_with = "serde_path::serialize")]
16    pub path: PathBuf,
17    /// 1-based line of the import that declares the binding.
18    pub line: u32,
19    /// 0-based byte column of the import specifier that declares the
20    /// binding: the `*` of `import * as Ns`, or the specifier of a named
21    /// import.
22    pub col: u32,
23    /// The local name of the binding.
24    pub local_name: String,
25    /// The module whose exports get the full credit.
26    #[serde(serialize_with = "serde_path::serialize")]
27    pub module: PathBuf,
28    /// The count of exports in `module` and the modules it re-exports from
29    /// that would be unused-export or unused-type findings without this credit.
30    pub hidden_exports: u32,
31    /// Why the binding gets the full credit.
32    pub reason: NamespaceEscapeReason,
33}
34
35/// Why a namespace binding gets the full export credit of its target.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
37#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
38#[serde(rename_all = "kebab-case")]
39pub enum NamespaceEscapeReason {
40    /// The code uses the binding as a value: a cast, an assignment, a call
41    /// argument, a spread, a computed member read, or a reflective call such
42    /// as `Object.keys(Ns)`.
43    WholeObjectUse,
44    /// A module that is not an entry point re-exports the binding with
45    /// `export { Ns }`, so fallow cannot see which members its consumers read.
46    ReExportedBinding,
47    /// The code reads no member of the binding by name and does not use it
48    /// as a value, for example only in `typeof Ns`.
49    NoMemberRead,
50}
51
52impl NamespaceEscapeReason {
53    /// The kebab-case name of the reason, as in JSON output.
54    #[must_use]
55    pub const fn as_str(self) -> &'static str {
56        match self {
57            Self::WholeObjectUse => "whole-object-use",
58            Self::ReExportedBinding => "re-exported-binding",
59            Self::NoMemberRead => "no-member-read",
60        }
61    }
62
63    /// The clause that tells why the binding gets the full credit, as the
64    /// finding message uses it after `namespace {name}`.
65    #[must_use]
66    pub const fn description(self) -> &'static str {
67        match self {
68            Self::WholeObjectUse => "is used as a whole value",
69            Self::ReExportedBinding => "is re-exported as a whole object",
70            Self::NoMemberRead => "has no member read by name",
71        }
72    }
73}
74
75impl NamespaceEscape {
76    /// The message of the finding, with `module` as the output shows the
77    /// target path. The first clause depends on the reason, for example
78    /// `namespace Ns is used as a whole value; 2 exports of src/a.ts are not
79    /// checked`.
80    #[must_use]
81    pub fn message(&self, module: &str) -> String {
82        let (noun, verb) = if self.hidden_exports == 1 {
83            ("export", "is")
84        } else {
85            ("exports", "are")
86        };
87        format!(
88            "namespace {} {}; {} {noun} of {module} {verb} not checked",
89            self.local_name,
90            self.reason.description(),
91            self.hidden_exports
92        )
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    fn escape(reason: NamespaceEscapeReason, hidden_exports: u32) -> NamespaceEscape {
101        NamespaceEscape {
102            path: PathBuf::from("src/app.ts"),
103            line: 1,
104            col: 7,
105            local_name: "Ns".to_string(),
106            module: PathBuf::from("src/lib.ts"),
107            hidden_exports,
108            reason,
109        }
110    }
111
112    #[test]
113    fn message_names_the_reason() {
114        assert_eq!(
115            escape(NamespaceEscapeReason::WholeObjectUse, 2).message("src/lib.ts"),
116            "namespace Ns is used as a whole value; 2 exports of src/lib.ts are not checked"
117        );
118        assert_eq!(
119            escape(NamespaceEscapeReason::ReExportedBinding, 2).message("src/lib.ts"),
120            "namespace Ns is re-exported as a whole object; 2 exports of src/lib.ts are not checked"
121        );
122        assert_eq!(
123            escape(NamespaceEscapeReason::NoMemberRead, 2).message("src/lib.ts"),
124            "namespace Ns has no member read by name; 2 exports of src/lib.ts are not checked"
125        );
126    }
127
128    #[test]
129    fn message_uses_singular_for_one_export() {
130        assert_eq!(
131            escape(NamespaceEscapeReason::WholeObjectUse, 1).message("src/lib.ts"),
132            "namespace Ns is used as a whole value; 1 export of src/lib.ts is not checked"
133        );
134    }
135}