fallow_types/
results_namespace_escape.rs1use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7use crate::serde_path;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
12#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
13pub struct NamespaceEscape {
14 #[serde(serialize_with = "serde_path::serialize")]
16 pub path: PathBuf,
17 pub line: u32,
19 pub col: u32,
23 pub local_name: String,
25 #[serde(serialize_with = "serde_path::serialize")]
27 pub module: PathBuf,
28 pub hidden_exports: u32,
31 pub reason: NamespaceEscapeReason,
33}
34
35#[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 WholeObjectUse,
44 ReExportedBinding,
47 NoMemberRead,
50}
51
52impl NamespaceEscapeReason {
53 #[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 #[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 #[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}