skif-js 0.4.0

JavaScript and TypeScript module graph and dependency rules for skif, built on oxc.
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
//! Entries of the ignore lists: exact names, globs and regular expressions, with the production marker and a record
//! of which entries matched something.

use std::sync::atomic::{AtomicBool, Ordering};

use globset::{GlobBuilder, GlobMatcher};
use regex::Regex;

/// Characters that make a plain string a regular expression instead of a glob. They are the ones knip treats as
/// regular expression syntax (`+ \ ( | ^ $`) that no glob uses, plus `)`. This is narrower than knip's test
/// (`/[*+\\(|{^$]/`): `*` and `{` stay glob syntax here, so `eslint-*` is a prefix glob and not a regular expression.
const REGEX_ONLY: [char; 6] = ['+', '\\', '(', ')', '|', '^'];

/// The regular expression an entry stands for, or `None` when it is a glob or an exact name.
///
/// An entry is a regular expression when it is delimited by slashes with optional flags (`/^@org\/.+$/`, `/x/i`; the
/// flags `i`, `m` and `s` apply, `g`, `u`, `y` and `d` are accepted and ignored) or, as in knip, when it contains one
/// of `+ \ ( ) | ^`, or `.*` / `.+`, or ends in `$`. The expression is not anchored: it matches
/// when it matches anywhere in the name, as a JavaScript `RegExp.test` does. Globs (`@types/*`, `eslint-*`) are
/// everything else.
///
/// The expression is compiled by the Rust `regex` crate, so JavaScript-only syntax (look-ahead, look-behind,
/// back-references) is an error and `\d` and `\w` are Unicode aware.
///
/// # Errors
/// The message of the regex compiler.
pub(crate) fn regex_of(entry: &str) -> Result<Option<Regex>, String> {
    let Some(source) = regex_source(entry) else {
        return Ok(None);
    };
    Regex::new(&source).map(Some).map_err(|err| {
        err.to_string()
            .lines()
            .last()
            .unwrap_or("invalid regular expression")
            .trim()
            .to_owned()
    })
}

fn regex_source(entry: &str) -> Option<String> {
    if let Some(rest) = entry.strip_prefix('/')
        && let Some(close) = rest.rfind('/')
    {
        let (body, flags) = (&rest[..close], &rest[close + 1..]);
        if !body.is_empty() && flags.chars().all(|flag| "gimsuyd".contains(flag)) {
            let inline: String = ['i', 'm', 's']
                .into_iter()
                .filter(|flag| flags.contains(*flag))
                .collect();
            return Some(if inline.is_empty() {
                body.to_owned()
            } else {
                format!("(?{inline}){body}")
            });
        }
    }
    let regex_like = entry.contains(REGEX_ONLY) || entry.contains(".*") || entry.contains(".+") || entry.ends_with('$');
    regex_like.then(|| entry.to_owned())
}

/// How the globs of a list treat `/`.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Separator {
    /// `*` matches `/` too: names such as `@types/*`.
    Any,
    /// `*` stops at `/`, as in every other path glob: directories such as `packages/*`.
    Literal,
}

/// The glob variants of an entry (`{,a}` expands to two), or an error when one does not compile.
fn glob_variants(entry: &str, separator: Separator) -> Result<Vec<GlobMatcher>, String> {
    skif_js_plugins::glob::expand_empty_alternatives(entry)
        .iter()
        .map(|variant| {
            GlobBuilder::new(variant)
                .literal_separator(separator == Separator::Literal)
                .build()
                .map(|glob| glob.compile_matcher())
                .map_err(|err| err.kind().to_string())
        })
        .collect()
}

/// Whether `entry` is a usable ignore-list entry: a regular expression that compiles or a glob that compiles.
///
/// # Errors
/// What is wrong with the entry.
pub(crate) fn check(entry: &str) -> Result<(), String> {
    match regex_of(entry)? {
        Some(_) => Ok(()),
        None => glob_variants(entry, Separator::Any).map(drop),
    }
}

/// `entry` for the run mode: an entry ending in `!` counts in production mode only (the marker is removed), the others
/// always count. `None` when the entry does not apply.
pub(crate) fn for_mode(entry: &str, production: bool) -> Option<&str> {
    match entry.strip_suffix('!') {
        Some(body) => production.then_some(body),
        None => Some(entry),
    }
}

/// Patterns of an `entry` or `project` list as the production run sees them: an entry ending in `!` is kept (without
/// the marker), any other becomes an exclusion.
pub(crate) fn production_view(patterns: &[String]) -> Vec<String> {
    patterns
        .iter()
        .filter(|pattern| !pattern.is_empty())
        .map(|pattern| match pattern.strip_suffix('!') {
            Some(body) => body.to_owned(),
            None if pattern.starts_with('!') => pattern.clone(),
            None => format!("!{pattern}"),
        })
        .collect()
}

/// The exclusions a production run adds to the project for the entries of `patterns` that do not end in `!`: those
/// files run in development only, so they are not project files (`entry = ["src/index.ts!", "scripts/*.ts"]` keeps
/// the scripts out of `unused-files`).
pub(crate) fn non_production_entries(patterns: &[String]) -> Vec<String> {
    patterns
        .iter()
        .filter(|pattern| !pattern.is_empty() && !pattern.ends_with('!') && !pattern.starts_with('!'))
        .map(|pattern| format!("!{pattern}"))
        .collect()
}

/// `patterns` as exclusions: the files a tool contributes to the development project are not project files in a
/// production run.
pub(crate) fn production_exclusions(patterns: &[String]) -> Vec<String> {
    patterns
        .iter()
        .filter(|pattern| !pattern.is_empty())
        .map(|pattern| {
            if pattern.starts_with('!') {
                pattern.clone()
            } else {
                format!("!{pattern}")
            }
        })
        .collect()
}

/// Patterns of an `entry` or `project` list as a normal run sees them: the production marker is removed.
pub(crate) fn plain_view(patterns: &[String]) -> Vec<String> {
    patterns
        .iter()
        .map(|pattern| pattern.strip_suffix('!').unwrap_or(pattern).to_owned())
        .collect()
}

/// The globs to use for a user-configured `entry` or `project` list: `default` when nothing is configured; in a
/// production run the entries ending in `!` (with the others excluded), falling back to `default` when none ends in `!`.
pub(crate) fn user_globs(patterns: &[String], production: bool, default: &[String]) -> Vec<String> {
    if patterns.is_empty() {
        return default.to_vec();
    }
    if !production {
        return plain_view(patterns);
    }
    let view = production_view(patterns);
    if view.iter().any(|pattern| !pattern.starts_with('!')) {
        view
    } else {
        default.iter().cloned().chain(view).collect()
    }
}

enum Matcher {
    Globs(Vec<GlobMatcher>),
    Regex(Regex),
}

/// One configured entry and whether it ever matched.
struct Entry {
    text: String,
    matcher: Matcher,
    hit: AtomicBool,
}

impl Entry {
    fn matches(&self, name: &str) -> bool {
        match &self.matcher {
            Matcher::Globs(globs) => globs.iter().any(|glob| glob.is_match(name)),
            Matcher::Regex(regex) => regex.is_match(name),
        }
    }
}

/// A user-configured ignore list that remembers which entries matched, so entries that matched nothing can be
/// reported.
#[derive(Default)]
pub(crate) struct Listed {
    entries: Vec<Entry>,
}

impl std::fmt::Debug for Listed {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_list()
            .entries(self.entries.iter().map(|entry| entry.text.as_str()))
            .finish()
    }
}

impl Listed {
    /// Compiles `patterns` for the run mode; an entry that does not compile is skipped (configuration validation
    /// reports it).
    pub(crate) fn new<I, S>(patterns: I, production: bool) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self::compile(patterns, production, Separator::Any)
    }

    /// Like [`Listed::new`] for a list of directories, whose globs do not let `*` cross a `/`.
    pub(crate) fn directories<I, S>(patterns: I, production: bool) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        Self::compile(patterns, production, Separator::Literal)
    }

    fn compile<I, S>(patterns: I, production: bool, separator: Separator) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let entries = patterns
            .into_iter()
            .filter_map(|pattern| {
                let text = for_mode(pattern.as_ref(), production)?.to_owned();
                let matcher = match regex_of(&text) {
                    Ok(Some(regex)) => Matcher::Regex(regex),
                    Ok(None) => Matcher::Globs(glob_variants(&text, separator).ok()?),
                    Err(_) => return None,
                };
                Some(Entry {
                    text,
                    matcher,
                    hit: AtomicBool::new(false),
                })
            })
            .collect();
        Self { entries }
    }

    /// Whether the list has no entry for the run mode.
    pub(crate) fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Whether `name` matches an entry. Every entry that matches is recorded, not only the first.
    pub(crate) fn is_match(&self, name: &str) -> bool {
        let mut found = false;
        for entry in &self.entries {
            if entry.matches(name) {
                entry.hit.store(true, Ordering::Relaxed);
                found = true;
            }
        }
        found
    }

    /// Every entry, as written (without the production marker), and whether it is stale: it matched nothing so far.
    pub(crate) fn verdicts(&self) -> Vec<(&str, bool)> {
        self.verdicts_except(&[])
    }

    /// Like [`verdicts`](Self::verdicts), an entry that matches one of `names` not being stale either. Testing
    /// `names` records nothing.
    pub(crate) fn verdicts_except(&self, names: &[&str]) -> Vec<(&str, bool)> {
        self.entries
            .iter()
            .map(|entry| {
                let stale = !entry.hit.load(Ordering::Relaxed) && !names.iter().any(|name| entry.matches(name));
                (entry.text.as_str(), stale)
            })
            .collect()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn slash_delimited_and_regex_like_entries_are_regular_expressions() {
        for entry in ["/^@org\\/.+$/", "^@org/.+$", "@org/.*", "a|b", "/x/i", "(a)"] {
            assert!(regex_of(entry).unwrap().is_some(), "{entry}");
        }
        for entry in ["@types/*", "eslint", "eslint-*", "{a,b}", "/abs/*", "/abs/path"] {
            assert!(regex_of(entry).unwrap().is_none(), "{entry}");
        }
    }

    #[test]
    fn regular_expressions_are_unanchored_and_honour_flags() {
        let listed = Listed::new(["/^@org\\/.+$/", "/^VITE/i", "tool-.*"], false);
        assert!(listed.is_match("@org/a"));
        assert!(!listed.is_match("x@org/a"));
        assert!(listed.is_match("vite-env"));
        assert!(listed.is_match("my-tool-x"));
        assert!(!listed.is_match("other"));
    }

    #[test]
    fn directory_globs_do_not_cross_a_slash() {
        let names = Listed::new(["@types/*"], false);
        assert!(names.is_match("@types/node") && names.is_match("@types/a/b"));
        let directories = Listed::directories(["packages/*", "tools/**"], false);
        assert!(directories.is_match("packages/a"));
        assert!(!directories.is_match("packages/a/nested"));
        assert!(directories.is_match("tools/a/deep"));
    }

    #[test]
    fn a_star_is_a_glob_here_and_a_regular_expression_in_knip() {
        let listed = Listed::new(["eslint-*"], false);
        assert!(listed.is_match("eslint-plugin"));
        assert!(!listed.is_match("my-eslint-plugin"));
        assert!(check("/^(?!@types)/").is_err());
    }

    #[test]
    fn globs_and_exact_names_match() {
        let listed = Listed::new(["@types/*", "eslint"], false);
        assert!(listed.is_match("@types/node"));
        assert!(listed.is_match("eslint"));
        assert!(!listed.is_match("eslint-plugin-x"));
    }

    #[test]
    fn invalid_entries_are_reported_by_check() {
        assert!(check("/(/").is_err());
        assert!(check("/x/q").is_ok());
        assert!(check("[bad").is_err());
        assert!(check("@org/*").is_ok());
        assert!(check("{,src/}a").is_ok());
    }

    #[test]
    fn the_production_marker_limits_an_entry_to_production() {
        let normal = Listed::new(["a!", "b"], false);
        assert!(!normal.is_match("a"));
        assert!(normal.is_match("b"));
        let production = Listed::new(["a!", "b"], true);
        assert!(production.is_match("a"));
        assert!(production.is_match("b"));
    }

    #[test]
    fn verdicts_call_the_entries_that_never_matched_stale() {
        let listed = Listed::new(["a", "b", "c*", "d*"], false);
        assert!(listed.is_match("a"));
        assert!(listed.is_match("cx"));
        assert_eq!(
            listed.verdicts(),
            [("a", false), ("b", true), ("c*", false), ("d*", true)]
        );
        assert_eq!(
            listed.verdicts_except(&["dx"]),
            [("a", false), ("b", true), ("c*", false), ("d*", false)]
        );
        assert_eq!(listed.verdicts()[3], ("d*", true), "testing names records nothing");
    }

    #[test]
    fn production_exclusions_cover_entries_without_the_marker_and_tool_project_files() {
        let entries = vec![
            "src/index.ts!".to_owned(),
            "scripts/*.ts".to_owned(),
            "!skip".to_owned(),
        ];
        assert_eq!(non_production_entries(&entries), ["!scripts/*.ts"]);
        let tool = vec!["stories/**".to_owned(), "!keep/**".to_owned()];
        assert_eq!(production_exclusions(&tool), ["!stories/**", "!keep/**"]);
    }

    #[test]
    fn entry_and_project_lists_follow_the_mode() {
        let patterns = vec![
            "src/index.ts!".to_owned(),
            "src/cli.ts".to_owned(),
            "!skip/**".to_owned(),
        ];
        let default = vec!["index.ts".to_owned()];
        assert_eq!(
            user_globs(&patterns, false, &default),
            ["src/index.ts", "src/cli.ts", "!skip/**"]
        );
        assert_eq!(
            user_globs(&patterns, true, &default),
            ["src/index.ts", "!src/cli.ts", "!skip/**"]
        );
        let unmarked = vec!["src/cli.ts".to_owned()];
        assert_eq!(user_globs(&unmarked, true, &default), ["index.ts", "!src/cli.ts"]);
        assert_eq!(user_globs(&[], true, &default), ["index.ts"]);
    }
}