fallow-cli 3.30.0

CLI for fallow, codebase intelligence for TypeScript and JavaScript
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Project grammar for the drift harness.
//!
//! A [`ProjectModel`] holds raw generated values. [`ProjectModel::materialize`]
//! interprets them into a base state and a head state, so shrinking works on
//! plain integers and booleans and never produces an invalid project.

use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::Path;
use std::process::Command;

use proptest::prelude::*;

/// Upper bound on generated source files, so one case stays near a second per surface.
const MAX_FILES: usize = 5;
const MAX_EXPORTS: usize = 3;
const MAX_IMPORTS: usize = 3;
const MAX_DEPS: usize = 3;
const MAX_CHANGES: usize = 3;
/// Length of the cyclic mask that selects which baseline entries a case keeps.
const BASELINE_MASK_LEN: usize = 6;

/// The workspace package that `--workspace` selects in a workspace layout.
pub const SELECTED_WORKSPACE: &str = "pkg-a";

#[derive(Debug, Clone)]
pub struct ExportSpec {
    pub is_type: bool,
    pub suppressed: bool,
}

#[derive(Debug, Clone)]
pub struct FileSpec {
    /// In a workspace layout: `false` puts the file in `pkg-a`, `true` in `pkg-b`.
    pub second_package: bool,
    pub entry_imported: bool,
    pub suppress_file: bool,
    pub exports: Vec<ExportSpec>,
    /// Raw `(file, export)` indices, reduced modulo the real counts.
    pub imports: Vec<(usize, usize)>,
}

#[derive(Debug, Clone)]
pub struct DepSpec {
    /// Raw index of the file that imports the dependency, if any.
    pub used_by: Option<usize>,
    pub dev: bool,
}

#[derive(Debug, Clone)]
pub struct MarkedBlock {
    pub file: usize,
    pub suppressed: bool,
}

#[derive(Debug, Clone)]
pub enum ChangeSpec {
    /// Append a new export to a file.
    Edit(usize),
    /// Add a new file that nothing imports.
    Add,
    /// `git mv` a file and update every import of it.
    Rename(usize),
    /// Delete a file and every import of it.
    Delete(usize),
    /// Add an unused dependency to the manifest of the first package.
    AddDependency,
}

#[derive(Debug, Clone)]
pub struct ProjectModel {
    pub workspaces: bool,
    pub files: Vec<FileSpec>,
    pub deps: Vec<DepSpec>,
    /// Two raw file indices that receive the same duplicated function.
    pub duplicate: Option<(usize, usize, bool)>,
    /// A file that receives a function above the default complexity thresholds.
    pub complex: Option<MarkedBlock>,
    pub changes: Vec<ChangeSpec>,
    /// Cyclic mask: entry `i` of a saved baseline stays when `mask[i % len]` is set.
    pub baseline_mask: Vec<bool>,
}

fn export_strategy() -> impl Strategy<Value = ExportSpec> {
    (any::<bool>(), prop::bool::weighted(0.3)).prop_map(|(is_type, suppressed)| ExportSpec {
        is_type,
        suppressed,
    })
}

fn file_strategy() -> impl Strategy<Value = FileSpec> {
    (
        any::<bool>(),
        prop::bool::weighted(0.6),
        prop::bool::weighted(0.15),
        prop::collection::vec(export_strategy(), 0..=MAX_EXPORTS),
        prop::collection::vec((0..MAX_FILES, 0..MAX_EXPORTS), 0..=MAX_IMPORTS),
    )
        .prop_map(
            |(second_package, entry_imported, suppress_file, exports, imports)| FileSpec {
                second_package,
                entry_imported,
                suppress_file,
                exports,
                imports,
            },
        )
}

fn dep_strategy() -> impl Strategy<Value = DepSpec> {
    (prop::option::of(0..MAX_FILES), prop::bool::weighted(0.3))
        .prop_map(|(used_by, dev)| DepSpec { used_by, dev })
}

fn marked_block_strategy() -> impl Strategy<Value = MarkedBlock> {
    (0..MAX_FILES, prop::bool::weighted(0.4))
        .prop_map(|(file, suppressed)| MarkedBlock { file, suppressed })
}

fn change_strategy() -> impl Strategy<Value = ChangeSpec> {
    prop_oneof![
        (0..MAX_FILES).prop_map(ChangeSpec::Edit),
        Just(ChangeSpec::Add),
        (0..MAX_FILES).prop_map(ChangeSpec::Rename),
        (0..MAX_FILES).prop_map(ChangeSpec::Delete),
        Just(ChangeSpec::AddDependency),
    ]
}

/// The proptest strategy for one generated project.
pub fn project_strategy() -> impl Strategy<Value = ProjectModel> {
    (
        prop::bool::weighted(0.35),
        prop::collection::vec(file_strategy(), 1..=MAX_FILES),
        prop::collection::vec(dep_strategy(), 0..=MAX_DEPS),
        prop::option::weighted(0.5, (0..MAX_FILES, 0..MAX_FILES, prop::bool::weighted(0.4))),
        prop::option::weighted(0.5, marked_block_strategy()),
        prop::collection::vec(change_strategy(), 0..=MAX_CHANGES),
        prop::collection::vec(any::<bool>(), BASELINE_MASK_LEN),
    )
        .prop_map(
            |(workspaces, files, deps, duplicate, complex, changes, baseline_mask)| ProjectModel {
                workspaces,
                files,
                deps,
                duplicate,
                complex,
                changes,
                baseline_mask,
            },
        )
}

/// One resolved source file of a project state.
#[derive(Debug, Clone)]
struct FileDef {
    id: usize,
    stem: String,
    package: usize,
    alive: bool,
    entry_imported: bool,
    suppress_file: bool,
    exports: Vec<ExportSpec>,
    /// Resolved `(file id, export index)` imports.
    imports: Vec<(usize, usize)>,
    deps: Vec<usize>,
    duplicate: Option<bool>,
    complex: Option<bool>,
    appended: usize,
}

/// A resolved project state: every source file plus the dependency list.
#[derive(Debug, Clone)]
struct State {
    workspaces: bool,
    files: Vec<FileDef>,
    deps: Vec<DepSpec>,
    added: usize,
    /// The first package manifest declares the unused `dep-added`.
    added_dependency: bool,
}

/// Rendered files of both commits and the git operations between them.
#[derive(Debug, Clone)]
pub struct Materialized {
    pub base: BTreeMap<String, String>,
    pub head: BTreeMap<String, String>,
    pub renames: Vec<(String, String)>,
}

impl ProjectModel {
    /// Render the base and head states. `suppressions` selects whether marked
    /// lines carry a suppression comment or a plain comment of the same length
    /// in lines, so both variants keep every finding on the same line.
    pub fn materialize(&self, suppressions: bool) -> Materialized {
        let base = self.base_state();
        let (head, renames) = apply_changes(&base, &self.changes);
        Materialized {
            base: render_state(&base, suppressions),
            head: render_state(&head, suppressions),
            renames,
        }
    }

    /// Whether the project uses the npm workspaces layout with two packages.
    pub const fn has_workspaces(&self) -> bool {
        self.workspaces
    }

    fn base_state(&self) -> State {
        let count = self.files.len();
        let package_of =
            |index: usize| usize::from(self.workspaces && self.files[index].second_package);
        let mut files: Vec<FileDef> = self
            .files
            .iter()
            .enumerate()
            .map(|(id, spec)| FileDef {
                id,
                stem: format!("f{id}"),
                package: package_of(id),
                alive: true,
                entry_imported: spec.entry_imported,
                suppress_file: spec.suppress_file,
                exports: spec.exports.clone(),
                imports: Vec::new(),
                deps: Vec::new(),
                duplicate: None,
                complex: None,
                appended: 0,
            })
            .collect();
        for (id, spec) in self.files.iter().enumerate() {
            let mut imports: Vec<(usize, usize)> = spec
                .imports
                .iter()
                .filter_map(|&(raw_file, raw_export)| {
                    let target = raw_file % count;
                    let exports = self.files[target].exports.len();
                    (target != id && exports > 0 && package_of(target) == package_of(id))
                        .then_some((target, raw_export % exports.max(1)))
                })
                .collect();
            imports.sort_unstable();
            imports.dedup();
            files[id].imports = imports;
        }
        for (index, dep) in self.deps.iter().enumerate() {
            if let Some(raw) = dep.used_by {
                files[raw % count].deps.push(index);
            }
        }
        if let Some((first, second, suppressed)) = self.duplicate {
            let (first, second) = (first % count, second % count);
            if first != second {
                files[first].duplicate = Some(suppressed);
                files[second].duplicate = Some(suppressed);
            }
        }
        if let Some(block) = &self.complex {
            files[block.file % count].complex = Some(block.suppressed);
        }
        State {
            workspaces: self.workspaces,
            files,
            deps: self.deps.clone(),
            added: 0,
            added_dependency: false,
        }
    }
}

fn apply_changes(base: &State, changes: &[ChangeSpec]) -> (State, Vec<(String, String)>) {
    let mut head = base.clone();
    let mut renames = Vec::new();
    let count = base.files.len();
    for change in changes {
        match *change {
            ChangeSpec::Edit(raw) => {
                let file = &mut head.files[raw % count];
                if file.alive {
                    file.appended += 1;
                }
            }
            ChangeSpec::Add => head.added += 1,
            ChangeSpec::AddDependency => head.added_dependency = true,
            ChangeSpec::Rename(raw) => {
                let index = raw % count;
                if head.files[index].alive && !head.files[index].stem.starts_with('r') {
                    let old = file_path(&head, &head.files[index]);
                    head.files[index].stem = format!("r{index}");
                    renames.push((old, file_path(&head, &head.files[index])));
                }
            }
            ChangeSpec::Delete(raw) => {
                let index = raw % count;
                if head.files[index].alive {
                    head.files[index].alive = false;
                    renames.retain(|(_, new)| *new != file_path(&head, &head.files[index]));
                    for file in &mut head.files {
                        file.imports.retain(|&(target, _)| target != index);
                    }
                }
            }
        }
    }
    (head, renames)
}

fn package_dir(workspaces: bool, package: usize) -> &'static str {
    match (workspaces, package) {
        (false, _) => "",
        (true, 0) => "packages/a/",
        (true, _) => "packages/b/",
    }
}

fn file_path(state: &State, file: &FileDef) -> String {
    format!(
        "{}src/{}.ts",
        package_dir(state.workspaces, file.package),
        file.stem
    )
}

fn export_name(file: usize, export: usize, spec: &ExportSpec) -> String {
    if spec.is_type {
        format!("T{file}x{export}")
    } else {
        format!("e{file}x{export}")
    }
}

fn marker(suppressions: bool, suppressed: bool, directive: &str) -> String {
    if suppressions && suppressed {
        format!("// {directive}\n")
    } else {
        "// drift fixture line\n".to_string()
    }
}

fn render_state(state: &State, suppressions: bool) -> BTreeMap<String, String> {
    let mut out = BTreeMap::new();
    let packages = if state.workspaces { 2 } else { 1 };
    if state.workspaces {
        out.insert(
            "package.json".to_string(),
            "{\n  \"name\": \"drift-root\",\n  \"private\": true,\n  \"workspaces\": [\"packages/*\"]\n}\n"
                .to_string(),
        );
    }
    for package in 0..packages {
        let dir = package_dir(state.workspaces, package);
        out.insert(
            format!("{dir}package.json"),
            render_manifest(state, package),
        );
        out.insert(format!("{dir}src/index.ts"), render_entry(state, package));
    }
    for file in state.files.iter().filter(|file| file.alive) {
        out.insert(
            file_path(state, file),
            render_file(state, file, suppressions),
        );
    }
    for added in 0..state.added {
        out.insert(
            format!("{}src/n{added}.ts", package_dir(state.workspaces, 0)),
            format!("export const fresh{added} = {added};\n"),
        );
    }
    out
}

fn render_manifest(state: &State, package: usize) -> String {
    let name = match (state.workspaces, package) {
        (false, _) => "drift-fixture",
        (true, 0) => SELECTED_WORKSPACE,
        (true, _) => "pkg-b",
    };
    let mut deps = Vec::new();
    let mut dev_deps = Vec::new();
    for (index, dep) in state.deps.iter().enumerate() {
        let owner = dep
            .used_by
            .map_or(0, |raw| state.files[raw % state.files.len()].package);
        if owner != package {
            continue;
        }
        let entry = format!("\"dep-{index}\": \"1.0.0\"");
        if dep.dev {
            dev_deps.push(entry);
        } else {
            deps.push(entry);
        }
    }
    if state.added_dependency && package == 0 {
        deps.push("\"dep-added\": \"1.0.0\"".to_string());
    }
    format!(
        "{{\n  \"name\": \"{name}\",\n  \"private\": true,\n  \"type\": \"module\",\n  \"main\": \"src/index.ts\",\n  \"dependencies\": {{{}}},\n  \"devDependencies\": {{{}}}\n}}\n",
        deps.join(", "),
        dev_deps.join(", ")
    )
}

fn render_entry(state: &State, package: usize) -> String {
    let mut out = String::from("// entry\n");
    for file in state
        .files
        .iter()
        .filter(|file| file.alive && file.entry_imported && file.package == package)
    {
        let _ = writeln!(out, "import \"./{}\";", file.stem);
    }
    out.push_str("export {};\n");
    out
}

fn render_file(state: &State, file: &FileDef, suppressions: bool) -> String {
    let mut out = marker(
        suppressions,
        file.suppress_file,
        "fallow-ignore-file unused-file",
    );
    let mut values = Vec::new();
    for &(target, export) in &file.imports {
        let target_file = &state.files[target];
        let spec = &target_file.exports[export];
        let name = export_name(target, export, spec);
        if spec.is_type {
            let _ = writeln!(
                out,
                "import type {{ {name} }} from \"./{}\";",
                target_file.stem
            );
            let _ = writeln!(out, "const use{name}: {name} | null = null;");
            values.push(format!("use{name}"));
        } else {
            let _ = writeln!(out, "import {{ {name} }} from \"./{}\";", target_file.stem);
            values.push(name);
        }
    }
    for &dep in &file.deps {
        let _ = writeln!(out, "import dep{dep} from \"dep-{dep}\";");
        values.push(format!("dep{dep}"));
    }
    if !values.is_empty() {
        let _ = writeln!(out, "console.log({});", values.join(", "));
    }
    for (index, spec) in file.exports.iter().enumerate() {
        let name = export_name(file.id, index, spec);
        let directive = if spec.is_type {
            "fallow-ignore-next-line unused-type"
        } else {
            "fallow-ignore-next-line unused-export"
        };
        out.push_str(&marker(suppressions, spec.suppressed, directive));
        if spec.is_type {
            let _ = writeln!(out, "export type {name} = {{ value{index}: number }};");
        } else {
            let _ = writeln!(out, "export const {name} = {};", file.id * 10 + index);
        }
    }
    if let Some(suppressed) = file.duplicate {
        out.push_str(&marker(
            suppressions,
            suppressed,
            "fallow-ignore-next-line code-duplication",
        ));
        out.push_str(DUPLICATED_FUNCTION);
        out.push_str("console.log(compute);\n");
    }
    if let Some(suppressed) = file.complex {
        out.push_str(&marker(
            suppressions,
            suppressed,
            "fallow-ignore-next-line complexity",
        ));
        out.push_str(&complex_function(file.id));
        let _ = writeln!(out, "console.log(branchy{});", file.id);
    }
    for appended in 0..file.appended {
        let _ = writeln!(
            out,
            "export const added{}x{appended} = {appended};",
            file.id
        );
    }
    out
}

const DUPLICATED_FUNCTION: &str = "function compute(values: number[]): number {
  let total = 0;
  for (const value of values) {
    if (value > 10) {
      total += value * 2;
    } else if (value > 5) {
      total += value + 3;
    } else {
      total -= value;
    }
  }
  const result = total / values.length;
  return Math.round(result * 100) / 100;
}
";

fn complex_function(id: usize) -> String {
    let mut out =
        format!("function branchy{id}(a: number, b: number, c: number): number {{\n  let r = 0;\n");
    for step in 0..12 {
        let _ = writeln!(
            out,
            "  if (a > {step} && b < {step}) {{ if (c === {step}) {{ r += {step}; }} else {{ r -= {step}; }} }}"
        );
    }
    out.push_str("  return r;\n}\n");
    out
}

/// Write both commits of `materialized` into `root` as a fresh git repository.
///
/// # Panics
///
/// Panics when a file write or a git command fails.
pub fn write_repository(root: &Path, materialized: &Materialized) {
    git(root, &["init", "-q", "-b", "main"]);
    write_files(root, &materialized.base);
    git(root, &["add", "-A"]);
    git(root, &["commit", "-q", "--allow-empty", "-m", "base"]);
    for (old, new) in &materialized.renames {
        if let Some(parent) = root.join(new).parent() {
            std::fs::create_dir_all(parent).expect("create rename target dir");
        }
        git(root, &["mv", old, new]);
    }
    for path in materialized.base.keys() {
        let renamed = materialized.renames.iter().any(|(old, _)| old == path);
        if !renamed && !materialized.head.contains_key(path) {
            git(root, &["rm", "-q", path]);
        }
    }
    write_files(root, &materialized.head);
    git(root, &["add", "-A"]);
    git(root, &["commit", "-q", "--allow-empty", "-m", "head"]);
}

fn write_files(root: &Path, files: &BTreeMap<String, String>) {
    for (path, content) in files {
        let target = root.join(path);
        if let Some(parent) = target.parent() {
            std::fs::create_dir_all(parent).expect("create fixture dir");
        }
        std::fs::write(&target, content).expect("write fixture file");
    }
}

/// Run git in `root` with an isolated identity and no signing, and without
/// the `GIT_*` variables a hook environment can leak into the test process.
///
/// # Panics
///
/// Panics when git cannot start or exits with a failure.
pub fn git(root: &Path, args: &[&str]) -> String {
    // An empty global config next to the project keeps user hooks, signing and
    // aliases out of the fixture on every platform.
    let global = root.with_extension("gitconfig");
    if !global.is_file() {
        std::fs::write(&global, "").expect("write empty git config");
    }
    let output = Command::new("git")
        .args([
            "-c",
            "commit.gpgsign=false",
            "-c",
            "user.name=drift",
            "-c",
            "user.email=drift@example.invalid",
        ])
        .args(args)
        .current_dir(root)
        .env_remove("GIT_DIR")
        .env_remove("GIT_WORK_TREE")
        .env_remove("GIT_INDEX_FILE")
        .env("GIT_CONFIG_GLOBAL", &global)
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .output()
        .expect("run git");
    assert!(
        output.status.success(),
        "git {args:?} failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).into_owned()
}

/// Describe the files of the head commit, for a failure report.
pub fn describe(materialized: &Materialized) -> String {
    let mut out = String::new();
    for (path, content) in &materialized.head {
        let _ = writeln!(out, "--- {path}\n{content}");
    }
    if !materialized.renames.is_empty() {
        let _ = writeln!(out, "renames: {:?}", materialized.renames);
    }
    out
}