skif-cli 0.4.0

Command-line interface for skif, a dependency and package-manager linter for JS/TS monorepos.
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! knip's run-time filters on the lint command: `--workspace` and the flags that narrow or bound what is reported.

#![allow(clippy::unwrap_used, clippy::expect_used)]

mod support;

use std::collections::BTreeSet;

use serde_json::Value;
use support::{fixture, redact, run, skif};

/// `(rule, path)` of every diagnostic of a JSON report.
fn findings(report: &Value) -> BTreeSet<(String, String)> {
    report["diagnostics"]
        .as_array()
        .unwrap()
        .iter()
        .map(|diagnostic| {
            (
                diagnostic["rule"].as_str().unwrap().to_owned(),
                diagnostic["location"]["path"].as_str().unwrap().to_owned(),
            )
        })
        .collect()
}

fn lint_json(args: &[&str]) -> (support::Run, Value) {
    let dir = fixture("filters");
    let result = run(skif().args(["--format", "json"]).args(args).arg(dir.path()));
    let report = result.json();
    (result, report)
}

#[test]
fn workspace_reports_only_the_selected_packages() {
    let dir = fixture("filters");
    let by_dir = run(skif()
        .args(["--color", "never", "--workspace", "./packages/a/"])
        .arg(dir.path()));
    assert_eq!(by_dir.code, 1, "{}{}", by_dir.stdout, by_dir.stderr);
    insta::assert_snapshot!("workspace_a_human", redact(&by_dir.stdout, dir.path()));

    let (_, by_name) = lint_json(&["-W", "@cli/a"]);
    let (_, by_dir) = lint_json(&["-W", "packages/a"]);
    assert_eq!(findings(&by_name), findings(&by_dir));
    assert!(
        findings(&by_name)
            .iter()
            .all(|(_, path)| path.starts_with("packages/a/")),
        "{by_name:#}"
    );
    assert_eq!(by_name["summary"]["packages"], 1);
}

#[test]
fn workspace_accepts_globs_and_exclusions() {
    let (_, all) = lint_json(&[]);
    let (_, globbed) = lint_json(&["--workspace", "packages/*"]);
    assert_eq!(findings(&all), findings(&globbed));
    let (_, not_a) = lint_json(&["--workspace", "!@cli/a"]);
    assert!(
        findings(&not_a)
            .iter()
            .all(|(_, path)| !path.starts_with("packages/a/")),
        "{not_a:#}"
    );
    assert!(!findings(&not_a).is_empty());
}

#[test]
fn workspace_that_matches_nothing_is_a_usage_error() {
    let (result, error) = lint_json(&["--workspace", "@cli/missing"]);
    assert_eq!(result.code, 2);
    assert_eq!(error["error"]["kind"], "usage", "{error:#}");
}

#[test]
fn workspace_limits_what_fix_changes() {
    let dir = fixture("filters");
    let result = run(skif()
        .args([
            "--fix-unsafe",
            "--no-install",
            "--no-prompt",
            "--workspace",
            "@cli/b",
            "--format",
            "json",
        ])
        .arg(dir.path()));
    assert_eq!(result.code, 0, "{}{}", result.stdout, result.stderr);
    assert!(dir.path().join("packages/a/orphan.ts").exists());
    let a = std::fs::read_to_string(dir.path().join("packages/a/package.json")).unwrap();
    assert!(a.contains("is-number"), "{a}");
    let b = std::fs::read_to_string(dir.path().join("packages/b/package.json")).unwrap();
    assert!(!b.contains("is-even"), "{b}");
}

#[test]
fn strict_uses_only_each_packages_own_dependencies() {
    let unlisted = ("unlisted-dependencies".to_owned(), "packages/a/index.ts".to_owned());
    let (_, production) = lint_json(&["--production"]);
    assert!(!findings(&production).contains(&unlisted), "{production:#}");
    let (result, strict) = lint_json(&["--strict"]);
    assert_eq!(result.code, 1);
    // `left-pad` is listed by the root only, which strict mode does not consult for `@cli/a`.
    assert!(findings(&strict).contains(&unlisted), "{strict:#}");
    assert!(findings(&strict).is_superset(&findings(&production)));
}

#[test]
fn strict_counts_required_peers_and_skips_type_only_imports() {
    let dir = fixture("strict");
    // Outside strict mode an import of a declaration file is not type-only, so `@types/typed-only` does not stand in
    // for `typed-only`.
    let expected = BTreeSet::from([("unlisted-dependencies".to_owned(), "packages/lib/types.d.ts".to_owned())]);
    for mode in [None, Some("--production")] {
        let result = run(skif().args(["--format", "json"]).args(mode).arg(dir.path()));
        assert_eq!(result.code, 1, "{mode:?}: {}{}", result.stdout, result.stderr);
        assert_eq!(findings(&result.json()), expected, "{mode:?}");
    }
    // `type-dep` is only imported for its types and `unused-peer` is a required peer nothing imports; `dev-runtime`
    // is imported at run time but listed only in `devDependencies`. The type-only import of `dev-types` and the
    // optional peer are not reported. The imports in `types.d.ts` are kept, as in knip: `declared-dep` is used,
    // `declared-types` may come from `devDependencies` and `@types/typed-only` stands in for `typed-only`.
    let strict = run(skif().args(["--color", "never", "--strict"]).arg(dir.path()));
    assert_eq!(strict.code, 1, "{}{}", strict.stdout, strict.stderr);
    insta::assert_snapshot!("strict_human", redact(&strict.stdout, dir.path()));
}

/// `--isolate-workspaces` is workspace isolation without production mode: `left-pad`, which only the root lists for
/// development, no longer satisfies `@cli/a`'s import, and the root's development dependencies are still checked, so
/// `left-pad` is unused there. `--strict`, which is production mode, checks no development dependency.
#[test]
fn isolate_workspaces_reports_a_members_use_of_an_ancestor_only_dev_dependency() {
    let unlisted = ("unlisted-dependencies".to_owned(), "packages/a/index.ts".to_owned());
    let unused_dev = ("unused-dev-dependencies".to_owned(), "package.json".to_owned());
    let (_, default) = lint_json(&[]);
    assert!(!findings(&default).contains(&unlisted), "{default:#}");
    assert!(!findings(&default).contains(&unused_dev), "{default:#}");
    let (result, isolated) = lint_json(&["--isolate-workspaces"]);
    assert_eq!(result.code, 1, "{}{}", result.stdout, result.stderr);
    assert!(findings(&isolated).contains(&unlisted), "{isolated:#}");
    assert!(findings(&isolated).contains(&unused_dev), "{isolated:#}");
    let (_, strict) = lint_json(&["--strict"]);
    assert!(!findings(&strict).contains(&unused_dev), "{strict:#}");
}

/// Isolation alone keeps the default mode's rules for what counts: a single package has nothing to be isolated from,
/// so `--isolate-workspaces` reports what the default run reports, without strict mode's type-only and
/// `devDependencies` findings.
#[test]
fn isolate_workspaces_is_not_strict_mode() {
    let dir = fixture("strict");
    let default = run(skif().args(["--format", "json"]).arg(dir.path()));
    let isolated = run(skif()
        .args(["--format", "json", "--isolate-workspaces"])
        .arg(dir.path()));
    assert_eq!(isolated.code, default.code, "{}{}", isolated.stdout, isolated.stderr);
    assert_eq!(findings(&isolated.json()), findings(&default.json()));
}

/// `[js] strict = true` in the configuration file is `--strict`: the same report and exit code, in a workspace and in
/// a single package; `[js] isolate-workspaces = true` is `--isolate-workspaces`.
#[test]
fn strict_and_isolate_workspaces_in_the_configuration_equal_their_flags() {
    for (name, key, flag) in [
        ("filters", "strict", "--strict"),
        ("strict", "strict", "--strict"),
        ("filters", "isolate-workspaces", "--isolate-workspaces"),
    ] {
        let by_flag = fixture(name);
        let flagged = run(skif().args(["--color", "never", flag]).arg(by_flag.path()));
        let by_file = fixture(name);
        std::fs::write(by_file.path().join("skif.toml"), format!("[js]\n{key} = true\n")).unwrap();
        let configured = run(skif().args(["--color", "never"]).arg(by_file.path()));
        assert_eq!(configured.code, flagged.code, "{name} {key}");
        assert_eq!(
            redact(&configured.stdout, by_file.path()),
            redact(&flagged.stdout, by_flag.path()),
            "{name} {key}"
        );
    }
}

fn rules(report: &Value) -> BTreeSet<String> {
    findings(report).into_iter().map(|(rule, _)| rule).collect()
}

fn set(items: &[&str]) -> BTreeSet<String> {
    items.iter().map(|item| (*item).to_owned()).collect()
}

#[test]
fn shortcuts_report_one_group_of_knip_issue_types() {
    let (_, files) = lint_json(&["--files"]);
    assert_eq!(rules(&files), set(&["unused-files"]));
    let (_, dependencies) = lint_json(&["--dependencies"]);
    assert_eq!(rules(&dependencies), set(&["unused-dependencies"]));
    let (_, exports) = lint_json(&["--exports"]);
    assert_eq!(rules(&exports), set(&["unused-exports"]));
    // `circular-imports` is off by default; including it turns it on, unless a flag turned it off.
    let (result, cycles) = lint_json(&["--cycles"]);
    assert_eq!(result.code, 1);
    assert_eq!(rules(&cycles), set(&["circular-imports"]));
    let (result, off) = lint_json(&["--cycles", "--ignore-rule", "circular-imports"]);
    assert_eq!(result.code, 0);
    assert_eq!(rules(&off), set(&[]));
}

#[test]
fn include_and_exclude_take_issue_types_and_rule_ids() {
    let (_, included) = lint_json(&["--include", "files,unused-exports"]);
    assert_eq!(rules(&included), set(&["unused-exports", "unused-files"]));
    let (_, excluded) = lint_json(&["--exclude", "dependencies", "--exclude", "unused-files"]);
    assert_eq!(rules(&excluded), set(&["unused-exports"]));
    let (result, error) = lint_json(&["--include", "nonsense"]);
    assert_eq!(result.code, 2);
    assert_eq!(error["error"]["kind"], "usage", "{error:#}");
}

#[test]
fn tags_leave_out_tagged_exports() {
    let (_, exports) = lint_json(&["--exports"]);
    assert_eq!(rules(&exports), set(&["unused-exports"]));
    let (result, tagged) = lint_json(&["--exports", "--tags=-custom"]);
    assert_eq!(result.code, 0);
    assert_eq!(rules(&tagged), set(&[]));
    let (result, error) = lint_json(&["--tags", "+custom"]);
    assert_eq!(result.code, 2);
    assert_eq!(error["error"]["kind"], "usage", "{error:#}");
}

#[test]
fn max_issues_and_no_exit_code_change_only_the_exit_code() {
    // The fixture has two errors.
    let (result, report) = lint_json(&["--max-issues", "2"]);
    assert_eq!(result.code, 0);
    assert_eq!(report["summary"]["errors"], 2);
    let (result, _) = lint_json(&["--max-issues", "1"]);
    assert_eq!(result.code, 1);
    let (result, report) = lint_json(&["--no-exit-code"]);
    assert_eq!(result.code, 0);
    assert_eq!(report["summary"]["errors"], 2);
    let (result, _) = lint_json(&["--no-exit-code", "--workspace", "@cli/missing"]);
    assert_eq!(result.code, 2);
}

#[test]
fn max_show_issues_shortens_the_list_but_not_the_summary() {
    let dir = fixture("filters");
    let human = run(skif()
        .args(["--color", "never", "--max-show-issues", "1"])
        .arg(dir.path()));
    assert_eq!(human.code, 1);
    assert!(
        human.stderr.contains("left 1 finding out of the list"),
        "{}",
        human.stderr
    );
    insta::assert_snapshot!("max_show_issues_human", redact(&human.stdout, dir.path()));

    let (_, all) = lint_json(&[]);
    let (_, shown) = lint_json(&["--max-show-issues", "1"]);
    assert_eq!(shown["summary"], all["summary"]);
    assert_eq!(shown["diagnostics"].as_array().unwrap().len(), 3);
}

#[test]
fn include_entry_exports_reports_exports_of_entry_files() {
    let entry = ("unused-exports".to_owned(), "packages/a/index.ts".to_owned());
    let (_, default) = lint_json(&["--exports"]);
    assert!(!findings(&default).contains(&entry), "{default:#}");
    let (_, included) = lint_json(&["--exports", "--include-entry-exports"]);
    assert!(findings(&included).contains(&entry), "{included:#}");
}

#[test]
fn run_diagnostics_go_to_stderr() {
    let dir = fixture("filters");
    let plain = run(skif().args(["--format", "json"]).arg(dir.path()));
    let traced = run(skif()
        .args(["--format", "json", "--debug", "--duration", "--memory", "--performance"])
        .arg(dir.path()));
    // stdout keeps exactly the report.
    assert_eq!(traced.json(), plain.json());
    let debug: Vec<&str> = traced
        .stderr
        .lines()
        .filter(|line| line.starts_with("skif debug:"))
        .collect();
    insta::assert_snapshot!("debug_lines", redact(&debug.join("\n"), dir.path()));
    assert!(traced.stderr.contains("skif timing: lint roots: "), "{}", traced.stderr);
    assert!(traced.stderr.contains("skif: finished in "), "{}", traced.stderr);
    assert!(traced.stderr.contains("skif: peak memory"), "{}", traced.stderr);
    assert!(!plain.stderr.contains("skif"), "{}", plain.stderr);
}

#[test]
fn trace_dependency_lists_manifests_imports_and_users() {
    let dir = fixture("filters");
    let human = run(skif().args(["--trace-dependency", "left-pad"]).arg(dir.path()));
    assert_eq!(human.code, 0, "{}{}", human.stdout, human.stderr);
    insta::assert_snapshot!("trace_left_pad_human", redact(&human.stdout, dir.path()));

    let (result, unused) = lint_json(&["--trace-dependency", "is-number"]);
    assert_eq!(result.code, 0);
    let trace = &unused["traces"][0];
    assert_eq!(trace["root"], ".");
    assert_eq!(trace["listed-in"][0]["manifest"], "packages/a/package.json");
    assert_eq!(trace["imported-by"], Value::Array(Vec::new()));
    assert_eq!(trace["used-by"], Value::Array(Vec::new()));

    let (result, error) = lint_json(&["--trace-dependency", "left-pad", "--fix"]);
    assert_eq!(result.code, 2);
    assert_eq!(error["error"]["kind"], "usage", "{error:#}");
}

#[test]
fn no_gitignore_analyses_ignored_files() {
    let dir = fixture("filters");
    std::fs::write(dir.path().join(".gitignore"), "packages/a/orphan.ts\n").unwrap();
    let orphan = ("unused-files".to_owned(), "packages/a/orphan.ts".to_owned());
    let honoured = run(skif().args(["--format", "json"]).arg(dir.path())).json();
    assert!(!findings(&honoured).contains(&orphan), "{honoured:#}");
    let ignored = run(skif().args(["--format", "json", "--no-gitignore"]).arg(dir.path())).json();
    assert!(findings(&ignored).contains(&orphan), "{ignored:#}");
}

#[test]
fn findings_of_no_package_belong_to_the_root() {
    // `unmatched-workspace-patterns` reports `missing/*` in the root manifest, with no package, and fixes it there.
    let root = ("unmatched-workspace-patterns".to_owned(), "package.json".to_owned());
    for (workspace, selected) in [("@cli/b", false), (".", true)] {
        let dir = fixture("filters-root");
        let report = run(skif()
            .args(["--format", "json", "--workspace", workspace])
            .arg(dir.path()))
        .json();
        assert_eq!(
            findings(&report).contains(&root),
            selected,
            "-W {workspace}: {report:#}"
        );

        let fixed = run(skif()
            .args([
                "--fix-unsafe",
                "--no-install",
                "--no-prompt",
                "--format",
                "json",
                "--workspace",
                workspace,
            ])
            .arg(dir.path()));
        assert_eq!(fixed.code, 0, "{}{}", fixed.stdout, fixed.stderr);
        let manifest = std::fs::read_to_string(dir.path().join("package.json")).unwrap();
        assert_eq!(!manifest.contains("missing/*"), selected, "-W {workspace}: {manifest}");
    }
}

#[test]
fn include_turns_on_a_rule_the_configuration_turned_off() {
    let dir = fixture("filters");
    std::fs::write(dir.path().join("skif.toml"), "[rules]\nunused-files = \"off\"\n").unwrap();
    let lint = |args: &[&str]| run(skif().args(["--format", "json"]).args(args).arg(dir.path())).json();
    assert!(!rules(&lint(&[])).contains("unused-files"));
    let included = lint(&["--include", "files"]);
    assert_eq!(rules(&included), set(&["unused-files"]));
    // Back at its default severity, not raised to `error`.
    assert_eq!(included["diagnostics"][0]["severity"], "warning", "{included:#}");
    assert_eq!(
        rules(&lint(&["--include", "files", "--rule", "unused-files=off"])),
        set(&[])
    );
}

#[test]
fn tags_need_ignore_tags_to_be_a_list() {
    let dir = fixture("filters");
    std::fs::write(
        dir.path().join("skif.toml"),
        "[rules]\nunused-exports = { level = \"warn\", ignore-tags = \"custom\" }\n",
    )
    .unwrap();
    let result = run(skif().args(["--format", "json", "--tags=-other"]).arg(dir.path()));
    assert_eq!(result.code, 2, "{}{}", result.stdout, result.stderr);
    let error = result.json();
    assert!(
        error["error"]["message"].as_str().unwrap().contains("ignore-tags"),
        "{error:#}"
    );
}

#[test]
fn max_show_issues_is_for_reading_only() {
    for args in [
        &["--format", "sarif", "--max-show-issues", "1"][..],
        &["--format", "github", "--max-show-issues", "1"],
        &["--format", "compact", "--max-show-issues", "1"],
        &["--fix", "--max-show-issues", "1"],
    ] {
        let dir = fixture("filters");
        let result = run(skif().args(args).arg(dir.path()));
        assert_eq!(result.code, 2, "{args:?}: {}{}", result.stdout, result.stderr);
        assert!(
            result.stderr.contains("--max-show-issues"),
            "{args:?}: {}",
            result.stderr
        );
    }
}

#[test]
fn max_show_issues_rejects_any_other_format_among_several() {
    let dir = fixture("filters");
    let report = dir.path().join("report.txt");
    let result = run(skif()
        .args(["--format", "human", "--max-show-issues", "1", "--format"])
        .arg(format!("compact={}", report.display()))
        .arg(dir.path()));
    assert_eq!(result.code, 2, "{}{}", result.stdout, result.stderr);
    assert!(result.stderr.contains("--max-show-issues"), "{}", result.stderr);
    assert!(!report.exists());

    let json = dir.path().join("report.json");
    let result = run(skif()
        .args([
            "--color",
            "never",
            "--format",
            "human",
            "--max-show-issues",
            "1",
            "--format",
        ])
        .arg(format!("json={}", json.display()))
        .arg(dir.path()));
    assert_eq!(result.code, 1, "{}{}", result.stdout, result.stderr);
    let written: Value = serde_json::from_str(&std::fs::read_to_string(&json).unwrap()).unwrap();
    let (_, shown) = lint_json(&["--max-show-issues", "1"]);
    assert_eq!(written["diagnostics"], shown["diagnostics"]);
    assert_eq!(written["summary"], shown["summary"]);
}

#[test]
fn trace_dependency_conflicts_with_the_report_filters() {
    for filter in [
        "--workspace=@cli/a",
        "--include=files",
        "--exclude=files",
        "--files",
        "--dependencies",
        "--tags=-internal",
        "--max-show-issues=1",
        "--rule=unused-files=off",
        "--ignore-rule=unused-files",
    ] {
        let (result, error) = lint_json(&["--trace-dependency", "left-pad", filter]);
        assert_eq!(result.code, 2, "{filter}");
        assert_eq!(error["error"]["kind"], "usage", "{filter}: {error:#}");
    }
}