Skip to main content

fallow_graph/resolve/
path_info.rs

1//! Specifier classification: bare specifiers, path aliases, and package name extraction.
2
3/// Check if a bare specifier looks like a path alias rather than an npm package.
4///
5/// Path aliases (e.g., `@/components`, `~/lib`, `#internal`, `~~/utils`) are resolved
6/// via tsconfig.json `paths` or package.json `imports`. They should not be cached
7/// (resolution depends on the importing file's tsconfig context) and should return
8/// `Unresolvable` (not `NpmPackage`) when resolution fails.
9#[must_use]
10pub fn is_path_alias(specifier: &str) -> bool {
11    if specifier.starts_with('#') {
12        return true;
13    }
14    if specifier.starts_with("~/") || specifier.starts_with("~~/") || specifier.starts_with("@@/") {
15        return true;
16    }
17    if specifier.starts_with("@/") {
18        return true;
19    }
20    if specifier.starts_with('@') {
21        let scope = specifier.split('/').next().unwrap_or(specifier);
22        if scope.len() > 1 && scope.chars().nth(1).is_some_and(|c| c.is_ascii_uppercase()) {
23            return true;
24        }
25    }
26
27    false
28}
29
30/// Check if a specifier is a bare specifier (npm package or Node.js imports map entry).
31#[must_use]
32pub fn is_bare_specifier(specifier: &str) -> bool {
33    !specifier.starts_with('.')
34        && !specifier.starts_with('/')
35        && !specifier.contains("://")
36        && !specifier.starts_with("data:")
37}
38
39/// Check if a string looks like a valid npm package name.
40///
41/// Rejects strings that are clearly not packages: shell variables (`$X`),
42/// pure numbers, strings starting with `!`, empty strings, etc.
43/// This prevents false "unlisted dependency" reports for test fixture
44/// artifacts like `$DIR` or `1`.
45#[must_use]
46pub fn is_valid_package_name(name: &str) -> bool {
47    if name.is_empty() {
48        return false;
49    }
50    let first = name.as_bytes()[0];
51    if first == b'$' || first == b'!' || first == b'#' {
52        return false;
53    }
54    if name.contains('?') || name.contains('!') || name.starts_with("__") {
55        return false;
56    }
57    if name.bytes().all(|b| b.is_ascii_digit()) {
58        return false;
59    }
60    if !name.bytes().any(|b| b.is_ascii_alphabetic() || b == b'@') {
61        return false;
62    }
63    !name.contains(' ') && !name.contains('\\')
64}
65
66/// Extract the npm package name from a specifier.
67/// `@scope/pkg/foo/bar` -> `@scope/pkg`
68/// `lodash/merge` -> `lodash`
69#[must_use]
70pub fn extract_package_name(specifier: &str) -> String {
71    if specifier.starts_with('@') {
72        let parts: Vec<&str> = specifier.splitn(3, '/').collect();
73        if parts.len() >= 2 {
74            format!("{}/{}", parts[0], parts[1])
75        } else {
76            specifier.to_string()
77        }
78    } else {
79        specifier.split('/').next().unwrap_or(specifier).to_string()
80    }
81}
82
83/// Normalize the body of a Deno `npm:` specifier into a plain npm package
84/// reference by dropping the `@<version>` selector while preserving the package
85/// name (including scope) and any subpath.
86///
87/// `rest` is the substring after the `npm:` scheme. Deno `npm:` specifiers are
88/// `<package>[@<version>][/<subpath>]`, so the version selector is the first
89/// `@` that follows the package name (after the scope's own `@scope/` for
90/// scoped packages). Examples:
91/// `@supabase/supabase-js@2` -> `@supabase/supabase-js`,
92/// `preact@10/hooks` -> `preact/hooks`, `express@^4.18.0` -> `express`,
93/// `foo` -> `foo`.
94#[must_use]
95pub fn normalize_npm_specifier(rest: &str) -> String {
96    let search_from = if rest.starts_with('@') {
97        match rest.find('/') {
98            Some(slash) => slash + 1,
99            None => return rest.to_string(),
100        }
101    } else {
102        0
103    };
104
105    let Some(at_rel) = rest[search_from..].find('@') else {
106        return rest.to_string();
107    };
108    let at = search_from + at_rel;
109    let end = rest[at..].find('/').map_or(rest.len(), |slash| at + slash);
110    let mut out = String::with_capacity(rest.len() - (end - at));
111    out.push_str(&rest[..at]);
112    out.push_str(&rest[end..]);
113    out
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn test_extract_package_name() {
122        assert_eq!(extract_package_name("react"), "react");
123        assert_eq!(extract_package_name("lodash/merge"), "lodash");
124        assert_eq!(extract_package_name("@scope/pkg"), "@scope/pkg");
125        assert_eq!(extract_package_name("@scope/pkg/foo"), "@scope/pkg");
126    }
127
128    #[test]
129    fn normalize_npm_specifier_scoped_with_version() {
130        assert_eq!(
131            normalize_npm_specifier("@supabase/supabase-js@2"),
132            "@supabase/supabase-js"
133        );
134    }
135
136    #[test]
137    fn normalize_npm_specifier_unscoped_with_version() {
138        assert_eq!(normalize_npm_specifier("express@^4.18.0"), "express");
139    }
140
141    #[test]
142    fn normalize_npm_specifier_version_then_subpath() {
143        assert_eq!(normalize_npm_specifier("preact@10/hooks"), "preact/hooks");
144        assert_eq!(
145            normalize_npm_specifier("@scope/name@1.2.3/sub"),
146            "@scope/name/sub"
147        );
148    }
149
150    #[test]
151    fn normalize_npm_specifier_no_version() {
152        assert_eq!(normalize_npm_specifier("foo"), "foo");
153        assert_eq!(normalize_npm_specifier("lodash/merge"), "lodash/merge");
154        assert_eq!(normalize_npm_specifier("@scope/pkg/sub"), "@scope/pkg/sub");
155    }
156
157    #[test]
158    fn normalize_npm_specifier_scope_only() {
159        assert_eq!(normalize_npm_specifier("@scope"), "@scope");
160    }
161
162    #[test]
163    fn normalize_npm_specifier_empty() {
164        assert_eq!(normalize_npm_specifier(""), "");
165    }
166
167    #[test]
168    fn test_is_bare_specifier() {
169        assert!(is_bare_specifier("react"));
170        assert!(is_bare_specifier("@scope/pkg"));
171        assert!(is_bare_specifier("#internal/module"));
172        assert!(!is_bare_specifier("./utils"));
173        assert!(!is_bare_specifier("../lib"));
174        assert!(!is_bare_specifier("/absolute"));
175    }
176
177    #[test]
178    fn test_is_bare_specifier_url_specifiers() {
179        assert!(!is_bare_specifier("https://cdn.example.com/lib.js"));
180        assert!(!is_bare_specifier("http://example.com/module"));
181        assert!(!is_bare_specifier("data:text/javascript,export default 42"));
182    }
183
184    #[test]
185    fn path_alias_hash_prefix() {
186        assert!(is_path_alias("#internal/module"));
187        assert!(is_path_alias("#shared"));
188    }
189
190    #[test]
191    fn path_alias_tilde_prefix() {
192        assert!(is_path_alias("~/components/Button"));
193        assert!(is_path_alias("~~/utils/helpers"));
194        assert!(is_path_alias("@@/shared/utils"));
195    }
196
197    #[test]
198    fn path_alias_at_slash_prefix() {
199        assert!(is_path_alias("@/components/Button"));
200        assert!(is_path_alias("@/lib"));
201    }
202
203    #[test]
204    fn path_alias_pascal_case_scope() {
205        assert!(is_path_alias("@Components/Button"));
206        assert!(is_path_alias("@Hooks/useApi"));
207        assert!(is_path_alias("@Services/auth"));
208    }
209
210    #[test]
211    fn path_alias_lowercase_scope_is_not_alias() {
212        assert!(!is_path_alias("@babel/core"));
213        assert!(!is_path_alias("@types/react"));
214        assert!(!is_path_alias("@scope/pkg"));
215    }
216
217    #[test]
218    fn path_alias_plain_specifier_is_not_alias() {
219        assert!(!is_path_alias("react"));
220        assert!(!is_path_alias("lodash/merge"));
221        assert!(!is_path_alias("my-utils"));
222    }
223
224    #[test]
225    fn path_alias_tilde_without_slash_is_not_alias() {
226        assert!(!is_path_alias("~something"));
227    }
228
229    #[test]
230    fn valid_package_names() {
231        assert!(is_valid_package_name("react"));
232        assert!(is_valid_package_name("@scope/pkg"));
233        assert!(is_valid_package_name("lodash.get"));
234        assert!(is_valid_package_name("my-pkg"));
235        assert!(is_valid_package_name("@babel/core"));
236        assert!(is_valid_package_name("3d-view")); // starts with digit but has letters
237    }
238
239    #[test]
240    fn invalid_package_names() {
241        assert!(!is_valid_package_name("$DIR"));
242        assert!(!is_valid_package_name("$ENV_VAR"));
243        assert!(!is_valid_package_name("1"));
244        assert!(!is_valid_package_name("123"));
245        assert!(!is_valid_package_name(""));
246        assert!(!is_valid_package_name("!important"));
247        assert!(!is_valid_package_name("has spaces"));
248        assert!(!is_valid_package_name("back\\slash"));
249    }
250
251    #[test]
252    fn extract_package_name_bare_scope_only() {
253        assert_eq!(extract_package_name("@scope"), "@scope");
254    }
255
256    #[test]
257    fn extract_package_name_deep_subpath() {
258        assert_eq!(
259            extract_package_name("@scope/pkg/deep/nested/path"),
260            "@scope/pkg"
261        );
262    }
263
264    #[test]
265    fn extract_package_name_single_name() {
266        assert_eq!(extract_package_name("react"), "react");
267    }
268
269    mod proptests {
270        use super::*;
271        use proptest::prelude::*;
272
273        proptest! {
274            /// Any specifier starting with `.` or `/` must NOT be classified as a bare specifier.
275            #[test]
276            fn relative_paths_are_not_bare(suffix in "[a-zA-Z0-9_/.-]{0,80}") {
277                let dot = format!(".{suffix}");
278                let slash = format!("/{suffix}");
279                prop_assert!(!is_bare_specifier(&dot), "'.{suffix}' was classified as bare");
280                prop_assert!(!is_bare_specifier(&slash), "'/{suffix}' was classified as bare");
281            }
282
283            /// Scoped packages (@scope/pkg) should extract exactly `@scope/pkg` — two segments.
284            #[test]
285            fn scoped_package_name_has_two_segments(
286                scope in "[a-z][a-z0-9-]{0,20}",
287                pkg in "[a-z][a-z0-9-]{0,20}",
288                subpath in "(/[a-z0-9-]{1,20}){0,3}",
289            ) {
290                let specifier = format!("@{scope}/{pkg}{subpath}");
291                let extracted = extract_package_name(&specifier);
292                let expected = format!("@{scope}/{pkg}");
293                prop_assert_eq!(extracted, expected);
294            }
295
296            /// Unscoped packages should extract exactly the first path segment.
297            #[test]
298            fn unscoped_package_name_is_first_segment(
299                pkg in "[a-z][a-z0-9-]{0,30}",
300                subpath in "(/[a-z0-9-]{1,20}){0,3}",
301            ) {
302                let specifier = format!("{pkg}{subpath}");
303                let extracted = extract_package_name(&specifier);
304                prop_assert_eq!(extracted, pkg);
305            }
306
307            /// is_bare_specifier, is_path_alias, and is_valid_package_name should never panic on arbitrary strings.
308            #[test]
309            fn classification_functions_no_panic(s in "[a-zA-Z0-9@#~/._$!\\-]{1,100}") {
310                let _ = is_bare_specifier(&s);
311                let _ = is_path_alias(&s);
312                let _ = is_valid_package_name(&s);
313            }
314
315            /// Valid npm package names (lowercase letters, digits, hyphens, dots) must be accepted.
316            #[test]
317            fn valid_npm_names_accepted(name in "[a-z][a-z0-9._-]{0,30}") {
318                prop_assert!(is_valid_package_name(&name));
319            }
320
321            /// Shell variable specifiers ($...) must be rejected.
322            #[test]
323            fn shell_variables_rejected(suffix in "[A-Z_]{1,20}") {
324                let specifier = format!("${suffix}");
325                prop_assert!(!is_valid_package_name(&specifier));
326            }
327
328            /// Pure numeric specifiers must be rejected.
329            #[test]
330            fn pure_numbers_rejected(n in "[0-9]{1,10}") {
331                prop_assert!(!is_valid_package_name(&n));
332            }
333
334            /// `@/` prefix should always be detected as a path alias.
335            #[test]
336            fn at_slash_is_path_alias(suffix in "[a-zA-Z0-9_/.-]{0,80}") {
337                let specifier = format!("@/{suffix}");
338                prop_assert!(is_path_alias(&specifier));
339            }
340
341            /// `~/` prefix should always be detected as a path alias.
342            #[test]
343            fn tilde_slash_is_path_alias(suffix in "[a-zA-Z0-9_/.-]{0,80}") {
344                let specifier = format!("~/{suffix}");
345                prop_assert!(is_path_alias(&specifier));
346            }
347
348            /// `#` prefix should always be detected as a path alias (Node.js imports map).
349            #[test]
350            fn hash_prefix_is_path_alias(suffix in "[a-zA-Z0-9_/.-]{0,80}") {
351                let specifier = format!("#{suffix}");
352                prop_assert!(is_path_alias(&specifier));
353            }
354
355            /// Extracted package name from node_modules path should never be empty.
356            #[test]
357            fn node_modules_package_name_never_empty(
358                pkg in "[a-z][a-z0-9-]{0,20}",
359                file in "[a-z]{1,10}\\.(js|ts|mjs)",
360            ) {
361                let path = std::path::PathBuf::from(format!("/project/node_modules/{pkg}/{file}"));
362                if let Some(name) = crate::resolve::fallbacks::extract_package_name_from_node_modules_path(&path) {
363                    prop_assert!(!name.is_empty());
364                }
365            }
366        }
367    }
368}