fallow-core 2.85.0

Analysis orchestration for fallow codebase intelligence (dead code, duplication, plugins, cross-reference)
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
//! Shell tokenization: splitting on operators, skipping env wrappers and package managers.

use super::ENV_WRAPPERS;

/// Bun runtime boolean flags that may precede an executed file/binary
/// (`bun --bun <bin>`, `bun --watch <file>`, `bun --hot run dev`). Bun documents
/// these as flags that go immediately after `bun`, before the `run`/file/binary
/// target. None consume a value, so they can be skipped to reach the target.
/// Value-taking flags (`--filter <glob>`) are deliberately absent: an unrecognized
/// leading flag makes the parser treat the command as a script delegation rather
/// than guess where the binary starts. Source: Bun runtime docs (oven-sh/bun
/// docs/runtime/index.mdx, watch-mode.mdx).
const BUN_RUNTIME_FLAGS: &[&str] = &["--bun", "--watch", "--hot", "--smol", "--no-clear-screen"];

/// Split a script string on shell operators (`&&`, `||`, `;`, `|`, `&`).
/// Respects single and double quotes.
pub fn split_shell_operators(script: &str) -> Vec<&str> {
    let mut segments = Vec::new();
    let mut start = 0;
    let bytes = script.as_bytes();
    let len = bytes.len();
    let mut i = 0;
    let mut in_single_quote = false;
    let mut in_double_quote = false;

    while i < len {
        let b = bytes[i];

        // Toggle quote state
        if b == b'\'' && !in_double_quote {
            in_single_quote = !in_single_quote;
            i += 1;
            continue;
        }
        if b == b'"' && !in_single_quote {
            in_double_quote = !in_double_quote;
            i += 1;
            continue;
        }

        // Inside quotes — skip everything
        if in_single_quote || in_double_quote {
            i += 1;
            continue;
        }

        // Try to match a shell operator and split on it
        if let Some(op_len) = shell_operator_len(bytes, i) {
            segments.push(&script[start..i]);
            i += op_len;
            start = i;
            continue;
        }

        i += 1;
    }

    if start < len {
        segments.push(&script[start..]);
    }

    segments
}

/// Return the byte length of a shell operator at position `i`, or `None`.
///
/// Checks two-char operators (`&&`, `||`) before single-char ones (`&`, `|`, `;`)
/// to avoid splitting `&&` as two `&` operators.
fn shell_operator_len(bytes: &[u8], i: usize) -> Option<usize> {
    let b = bytes[i];
    let next = bytes.get(i + 1).copied();

    // Two-character operators: && ||
    if matches!((b, next), (b'&', Some(b'&')) | (b'|', Some(b'|'))) {
        return Some(2);
    }

    // Single-character operators: ; | &
    if b == b';' {
        return Some(1);
    }
    if b == b'|' && next != Some(b'|') {
        return Some(1);
    }
    if b == b'&' && next != Some(b'&') {
        return Some(1);
    }

    None
}

/// Skip env var assignments (`KEY=value`) and env wrapper commands (`cross-env`, `dotenv`, `env`)
/// at the start of a token list. Returns the index of the first real command token, or `None`
/// if all tokens were consumed.
pub fn skip_initial_wrappers(tokens: &[&str], mut idx: usize) -> Option<usize> {
    // Skip env var assignments (KEY=value pairs)
    while idx < tokens.len() && super::is_env_assignment(tokens[idx]) {
        idx += 1;
    }
    if idx >= tokens.len() {
        return None;
    }

    // Skip env wrapper commands (cross-env, dotenv, env)
    while idx < tokens.len() && ENV_WRAPPERS.contains(&tokens[idx]) {
        idx += 1;
        // Skip env var assignments after the wrapper
        while idx < tokens.len() && super::is_env_assignment(tokens[idx]) {
            idx += 1;
        }
        // dotenv uses -- as separator
        if idx < tokens.len() && tokens[idx] == "--" {
            idx += 1;
        }
    }
    if idx >= tokens.len() {
        return None;
    }

    Some(idx)
}

/// Advance past package manager prefixes (`npx`, `pnpx`, `bunx`, `yarn exec`, `pnpm dlx`, etc.).
/// Returns the index of the actual binary token, or `None` if the command delegates to a named
/// script (e.g., `npm run build`, `yarn build`).
pub fn advance_past_package_manager(tokens: &[&str], mut idx: usize) -> Option<usize> {
    let token = tokens[idx];
    if matches!(token, "npx" | "pnpx" | "bunx") {
        idx += 1;
        // Skip npx flags (--yes, --no-install, -p, --package)
        while idx < tokens.len() && tokens[idx].starts_with('-') {
            let flag = tokens[idx];
            idx += 1;
            // --package <name> consumes the next argument
            if matches!(flag, "--package" | "-p") && idx < tokens.len() {
                idx += 1;
            }
        }
    } else if token == "bun" {
        // `bun` is both a script runner and a direct executor:
        //   bun <script> / bun run <script>   -> named script (skip)
        //   bun exec <bin> / bun x <pkg>      -> executes a binary
        //   bun --bun <bin> ...               -> runtime flags, then a binary to run
        // Skip known boolean runtime flags, then classify the target. An unknown
        // leading flag is treated as a script delegation (skip) rather than guessed
        // at, since we cannot tell whether it consumes the following token as a value.
        idx += 1;
        let mut saw_runtime_flag = false;
        while idx < tokens.len() && BUN_RUNTIME_FLAGS.contains(&tokens[idx]) {
            idx += 1;
            saw_runtime_flag = true;
        }
        if idx >= tokens.len() {
            return None;
        }
        let subcmd = tokens[idx];
        if subcmd == "exec" || subcmd == "x" {
            idx += 1;
        } else if matches!(subcmd, "run" | "run-script") {
            // Delegates to a named script, not a binary invocation
            return None;
        } else if !saw_runtime_flag {
            // Bare `bun <name>` (or `bun --unknown-flag ...`) runs a script; skip.
            return None;
        }
        // else: `bun --bun <bin>`, the post-flag target is a binary invocation.
    } else if matches!(token, "yarn" | "pnpm" | "npm") {
        if idx + 1 < tokens.len() {
            let subcmd = tokens[idx + 1];
            if subcmd == "exec" || subcmd == "dlx" {
                idx += 2;
            } else if matches!(subcmd, "run" | "run-script") {
                // Delegates to a named script, not a binary invocation
                return None;
            } else {
                // Bare `yarn <name>` runs a script — skip
                return None;
            }
        } else {
            return None;
        }
    }
    if idx >= tokens.len() {
        return None;
    }

    Some(idx)
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- shell_operator_len ---

    #[test]
    fn operator_len_double_ampersand() {
        assert_eq!(shell_operator_len(b"&&", 0), Some(2));
    }

    #[test]
    fn operator_len_double_pipe() {
        assert_eq!(shell_operator_len(b"||", 0), Some(2));
    }

    #[test]
    fn operator_len_semicolon() {
        assert_eq!(shell_operator_len(b";", 0), Some(1));
    }

    #[test]
    fn operator_len_single_pipe() {
        assert_eq!(shell_operator_len(b"|x", 0), Some(1));
    }

    #[test]
    fn operator_len_single_ampersand() {
        assert_eq!(shell_operator_len(b"&x", 0), Some(1));
    }

    #[test]
    fn operator_len_non_operator() {
        assert_eq!(shell_operator_len(b"abc", 0), None);
        assert_eq!(shell_operator_len(b"xyz", 1), None);
    }

    #[test]
    fn operator_len_ampersand_at_end_of_slice() {
        assert_eq!(shell_operator_len(b"&", 0), Some(1));
    }

    #[test]
    fn operator_len_pipe_at_end_of_slice() {
        assert_eq!(shell_operator_len(b"|", 0), Some(1));
    }

    #[test]
    fn operator_len_semicolon_at_end() {
        assert_eq!(shell_operator_len(b";", 0), Some(1));
    }

    // --- split_shell_operators ---

    #[test]
    fn split_empty_input() {
        let segments = split_shell_operators("");
        assert!(segments.is_empty());
    }

    #[test]
    fn split_only_operators() {
        let segments = split_shell_operators("&&||;");
        assert!(segments.iter().all(|s| s.is_empty()));
    }

    #[test]
    fn split_single_quoted_operators_preserved() {
        let segments = split_shell_operators("echo 'a && b || c'");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0], "echo 'a && b || c'");
    }

    #[test]
    fn split_double_quoted_operators_preserved() {
        let segments = split_shell_operators("echo \"a | b ; c\"");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0], "echo \"a | b ; c\"");
    }

    #[test]
    fn split_nested_single_in_double_quotes() {
        let segments = split_shell_operators("echo \"it's fine\" && jest");
        assert_eq!(segments.len(), 2);
        assert_eq!(segments[1].trim(), "jest");
    }

    #[test]
    fn split_nested_double_in_single_quotes() {
        let segments = split_shell_operators("echo 'say \"hello\"' && jest");
        assert_eq!(segments.len(), 2);
        assert_eq!(segments[1].trim(), "jest");
    }

    #[test]
    fn split_no_operators() {
        let segments = split_shell_operators("webpack --mode production");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0], "webpack --mode production");
    }

    #[test]
    fn split_trailing_operator() {
        let segments = split_shell_operators("server &");
        assert_eq!(segments.len(), 1);
        assert_eq!(segments[0], "server ");
    }

    #[test]
    fn split_mixed_operators() {
        let segments = split_shell_operators("a && b || c ; d | e & f");
        assert_eq!(segments.len(), 6);
        assert_eq!(segments[0].trim(), "a");
        assert_eq!(segments[1].trim(), "b");
        assert_eq!(segments[2].trim(), "c");
        assert_eq!(segments[3].trim(), "d");
        assert_eq!(segments[4].trim(), "e");
        assert_eq!(segments[5].trim(), "f");
    }

    // --- skip_initial_wrappers ---

    #[test]
    fn skip_wrappers_no_wrappers() {
        let tokens = vec!["webpack", "--mode", "production"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), Some(0));
    }

    #[test]
    fn skip_wrappers_env_prefix() {
        let tokens = vec!["env", "NODE_ENV=production", "webpack"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), Some(2));
    }

    #[test]
    fn skip_wrappers_cross_env_prefix() {
        let tokens = vec!["cross-env", "NODE_ENV=production", "webpack"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), Some(2));
    }

    #[test]
    fn skip_wrappers_dotenv_with_separator() {
        let tokens = vec!["dotenv", "--", "webpack"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), Some(2));
    }

    #[test]
    fn skip_wrappers_env_var_only() {
        let tokens = vec!["NODE_ENV=production", "CI=true"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), None);
    }

    #[test]
    fn skip_wrappers_cross_env_only() {
        let tokens = vec!["cross-env", "NODE_ENV=production"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), None);
    }

    #[test]
    fn skip_wrappers_multiple_env_vars_then_binary() {
        let tokens = vec!["NODE_ENV=test", "CI=true", "DEBUG=1", "jest"];
        assert_eq!(skip_initial_wrappers(&tokens, 0), Some(3));
    }

    #[test]
    fn skip_wrappers_starting_at_nonzero_index() {
        let tokens = vec!["ignored", "cross-env", "NODE_ENV=prod", "webpack"];
        assert_eq!(skip_initial_wrappers(&tokens, 1), Some(3));
    }

    // --- advance_past_package_manager ---

    #[test]
    fn advance_npm_run_returns_none() {
        let tokens = vec!["npm", "run", "build"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_npm_run_script_returns_none() {
        let tokens = vec!["npm", "run-script", "test"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_yarn_bare_returns_none() {
        let tokens = vec!["yarn", "build"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_yarn_exec() {
        let tokens = vec!["yarn", "exec", "jest", "--coverage"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(2));
    }

    #[test]
    fn advance_pnpm_exec() {
        let tokens = vec!["pnpm", "exec", "vitest", "run"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(2));
    }

    #[test]
    fn advance_pnpm_dlx() {
        let tokens = vec!["pnpm", "dlx", "create-react-app"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(2));
    }

    #[test]
    fn advance_npx_simple() {
        let tokens = vec!["npx", "eslint", "src"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(1));
    }

    #[test]
    fn advance_npx_with_flags() {
        let tokens = vec!["npx", "--yes", "--package", "@scope/tool", "eslint"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(4));
    }

    #[test]
    fn advance_pnpx_simple() {
        let tokens = vec!["pnpx", "vitest"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(1));
    }

    #[test]
    fn advance_bunx_simple() {
        let tokens = vec!["bunx", "esbuild", "src/index.ts"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(1));
    }

    #[test]
    fn advance_no_package_manager() {
        let tokens = vec!["webpack", "--mode", "production"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(0));
    }

    #[test]
    fn advance_bare_npm_returns_none() {
        let tokens = vec!["npm"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_bare_yarn_returns_none() {
        let tokens = vec!["yarn"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_npx_with_only_flags() {
        let tokens = vec!["npx", "--yes"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_bun_exec() {
        let tokens = vec!["bun", "exec", "jest"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(2));
    }

    #[test]
    fn advance_bun_run_returns_none() {
        let tokens = vec!["bun", "run", "dev"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_bun_runtime_flag_then_binary() {
        // `bun --bun prek install`: --bun forces the bun runtime for the
        // executed binary; `prek` is the binary, not a script.
        let tokens = vec!["bun", "--bun", "prek", "install"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(2));
    }

    #[test]
    fn advance_bun_multiple_runtime_flags_then_binary() {
        let tokens = vec!["bun", "--bun", "--watch", "prek"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(3));
    }

    #[test]
    fn advance_bun_runtime_flag_then_run_is_script() {
        // Bun documents `bun --watch run dev` (flag before `run`); the target
        // is still a named script, so nothing is credited.
        let tokens = vec!["bun", "--watch", "run", "dev"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_bun_x_executes_binary() {
        // `bun x <pkg>` is the bun-native alias of `bunx <pkg>`.
        let tokens = vec!["bun", "x", "cowsay"];
        assert_eq!(advance_past_package_manager(&tokens, 0), Some(2));
    }

    #[test]
    fn advance_bun_unknown_leading_flag_returns_none() {
        // `--filter` consumes a value; an unrecognized leading flag is treated
        // as a script delegation rather than guessed at (conservative: avoid
        // crediting the flag value as a package).
        let tokens = vec!["bun", "--filter", "foo", "run", "build"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_bun_bare_name_returns_none() {
        // Bare `bun <name>` runs a script, like `yarn <name>`.
        let tokens = vec!["bun", "scripts/build.ts"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }

    #[test]
    fn advance_bun_runtime_flag_only_returns_none() {
        let tokens = vec!["bun", "--watch"];
        assert_eq!(advance_past_package_manager(&tokens, 0), None);
    }
}