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
//! Velite plugin.
//!
//! Detects Velite projects, keeps `velite.config.*` and generated `.velite`
//! collection output reachable, and models content roots declared via
//! `defineConfig` / `defineCollection` so Velite-managed markdown / MDX content
//! is not reported as unused.

use std::path::Path;

use fallow_graph::resolve::extract_package_name;
use oxc_allocator::Allocator;
use oxc_ast::ast::{Argument, CallExpression, Expression, ObjectExpression};
use oxc_ast_visit::{Visit, walk};
use oxc_parser::Parser;
use oxc_span::SourceType;

use super::{Plugin, PluginResult, config_parser};

const ENABLERS: &[&str] = &["velite"];
const CONFIG_PATTERNS: &[&str] = &["velite.config.{ts,mts,cts,js,mjs,cjs}"];
const ALWAYS_USED: &[&str] = &["velite.config.{ts,mts,cts,js,mjs,cjs}", ".velite/**"];
const DISCOVERY_HIDDEN_DIRS: &[&str] = &[".velite"];
const TOOLING_DEPENDENCIES: &[&str] = &["velite"];
const CONFIG_EXTENSIONS: &[&str] = &["ts", "mts", "cts", "js", "mjs", "cjs"];
const CONTENT_EXTENSIONS: &str = "{md,mdx,yml,yaml,json}";
/// Velite's default content root when `root` is omitted from the config.
const DEFAULT_ROOT: &str = "content";
/// Velite's default generated-output directory when `output.data` is omitted.
const DEFAULT_OUTPUT_DATA: &str = ".velite";

/// Built-in plugin for Velite content-pipeline projects.
pub struct VelitePlugin;

impl Plugin for VelitePlugin {
    fn name(&self) -> &'static str {
        "velite"
    }

    fn enablers(&self) -> &'static [&'static str] {
        ENABLERS
    }

    fn is_enabled_with_deps(&self, deps: &[String], root: &Path) -> bool {
        deps.iter()
            .any(|dep| ENABLERS.iter().any(|enabler| dep == enabler))
            || CONFIG_EXTENSIONS
                .iter()
                .any(|ext| root.join(format!("velite.config.{ext}")).is_file())
    }

    fn config_patterns(&self) -> &'static [&'static str] {
        CONFIG_PATTERNS
    }

    fn always_used(&self) -> &'static [&'static str] {
        ALWAYS_USED
    }

    fn discovery_hidden_dirs(&self) -> &'static [&'static str] {
        DISCOVERY_HIDDEN_DIRS
    }

    fn tooling_dependencies(&self) -> &'static [&'static str] {
        TOOLING_DEPENDENCIES
    }

    fn resolve_config(&self, config_path: &Path, source: &str, root: &Path) -> PluginResult {
        let mut result = PluginResult::default();

        for specifier in config_parser::extract_imports(source, config_path) {
            let package_name = extract_package_name(&specifier);
            if !package_name.is_empty()
                && !package_name.starts_with('.')
                && !package_name.starts_with('/')
            {
                result.referenced_dependencies.push(package_name);
            }
        }
        result.referenced_dependencies.sort();
        result.referenced_dependencies.dedup();

        let collected = collect_config(source, config_path);

        // Content root: top-level `root` (default `content`), normalized
        // config-relative. Collection `pattern` globs are relative to it.
        let root_dir = collected
            .root_dir
            .as_deref()
            .and_then(|raw| config_parser::normalize_config_path(raw, config_path, root))
            .or_else(|| config_parser::normalize_config_path(DEFAULT_ROOT, config_path, root));

        if let Some(root_dir) = root_dir {
            // Keep only positive globs. Fast-glob negations (`!posts/private/**`)
            // exclude files; they are not content entry roots.
            let positive: Vec<&str> = collected
                .patterns
                .iter()
                .filter(|pattern| !pattern.starts_with('!'))
                .map(|pattern| pattern.trim_start_matches("./"))
                .filter(|pattern| !pattern.is_empty())
                .collect();

            // Fall back to the whole content root when no positive collection
            // pattern survives (no `defineCollection`, or negation-only globs).
            if positive.is_empty() {
                result.push_entry_pattern(format!("{root_dir}/**/*.{CONTENT_EXTENSIONS}"));
            } else {
                for pattern in positive {
                    result.push_entry_pattern(format!("{root_dir}/{pattern}"));
                }
            }
        }

        // Generated output: the default `.velite` is covered by the static
        // `always_used` glob (matched anywhere, including workspace packages).
        // Only a non-default `output.data` needs an explicit, config-relative
        // always-used entry. Compare the raw value so a monorepo config that
        // spells out the default does not add a redundant entry.
        if let Some(output_dir) = collected
            .output_data
            .as_deref()
            .filter(|raw| raw.trim_start_matches("./") != DEFAULT_OUTPUT_DATA)
            .and_then(|raw| config_parser::normalize_config_path(raw, config_path, root))
        {
            result.always_used_files.push(format!("{output_dir}/**"));
        }

        result
    }
}

#[derive(Default)]
struct CollectedConfig {
    /// Raw `root` value from `defineConfig`, config-relative.
    root_dir: Option<String>,
    /// Raw `output.data` value from `defineConfig`, config-relative.
    output_data: Option<String>,
    /// Collection `pattern` globs, relative to `root_dir`.
    patterns: Vec<String>,
}

fn collect_config(source: &str, config_path: &Path) -> CollectedConfig {
    let source_type = SourceType::from_path(config_path).unwrap_or_default();
    let allocator = Allocator::default();
    let parsed = Parser::new(&allocator, source, source_type).parse();

    let mut collector = ConfigCollector::default();

    // Top-level `root` / `output.data` from the default-export config object
    // (handles `defineConfig({...})`, bare object, `satisfies`/`as`, const ref).
    if let Some(config) = config_parser::find_config_object_pub(&parsed.program) {
        collector.root_dir = config_parser::find_property(config, "root")
            .and_then(|prop| config_parser::expression_to_path_string(&prop.value));
        collector.output_data = config_parser::find_property(config, "output")
            .and_then(|prop| config_parser::object_expression(&prop.value))
            .and_then(|output| config_parser::find_property(output, "data"))
            .and_then(|prop| config_parser::expression_to_path_string(&prop.value));
    }

    // Collection patterns from every `defineCollection(...)` call, wherever it
    // appears (inline in `collections`, or extracted to a `const`).
    collector.visit_program(&parsed.program);
    collector.patterns.sort();
    collector.patterns.dedup();

    CollectedConfig {
        root_dir: collector.root_dir,
        output_data: collector.output_data,
        patterns: collector.patterns,
    }
}

#[derive(Default)]
struct ConfigCollector {
    root_dir: Option<String>,
    output_data: Option<String>,
    patterns: Vec<String>,
}

impl<'a> Visit<'a> for ConfigCollector {
    fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
        if call_name(call) == Some("defineCollection")
            && let Some(Expression::ObjectExpression(options)) =
                call.arguments.first().and_then(Argument::as_expression)
        {
            self.collect_pattern(options);
        }

        walk::walk_call_expression(self, call);
    }
}

impl ConfigCollector {
    fn collect_pattern(&mut self, options: &ObjectExpression<'_>) {
        let Some(prop) = config_parser::find_property(options, "pattern") else {
            return;
        };
        push_string_or_array(&prop.value, &mut self.patterns);
    }
}

/// Collect string-literal values from a `string | string[]` expression.
fn push_string_or_array(expr: &Expression<'_>, out: &mut Vec<String>) {
    match expr {
        Expression::ArrayExpression(array) => {
            for element in array.elements.iter().filter_map(|el| el.as_expression()) {
                if let Some(value) = config_parser::expression_to_string(element) {
                    out.push(value);
                }
            }
        }
        _ => {
            if let Some(value) = config_parser::expression_to_string(expr) {
                out.push(value);
            }
        }
    }
}

fn call_name<'a>(call: &'a CallExpression<'a>) -> Option<&'a str> {
    match &call.callee {
        Expression::Identifier(identifier) => Some(identifier.name.as_str()),
        Expression::StaticMemberExpression(member) => Some(member.property.name.as_str()),
        _ => None,
    }
}

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

    #[test]
    fn activates_from_packages_or_config_file() {
        let plugin = VelitePlugin;
        let tmp = tempfile::tempdir().expect("temp dir");

        assert!(plugin.is_enabled_with_deps(&["velite".to_string()], tmp.path()));
        assert!(!plugin.is_enabled_with_deps(&["next".to_string()], tmp.path()));

        for ext in CONFIG_EXTENSIONS {
            let cfg = tmp.path().join(format!("velite.config.{ext}"));
            std::fs::write(&cfg, "export default {};\n").expect("config");
            assert!(
                plugin.is_enabled_with_deps(&[], tmp.path()),
                "velite.config.{ext} should activate the plugin"
            );
            std::fs::remove_file(&cfg).expect("remove config");
        }
    }

    #[test]
    fn exposes_static_velite_conventions() {
        let plugin = VelitePlugin;

        assert_eq!(plugin.config_patterns(), CONFIG_PATTERNS);
        assert!(
            plugin
                .always_used()
                .contains(&"velite.config.{ts,mts,cts,js,mjs,cjs}")
        );
        assert!(plugin.always_used().contains(&".velite/**"));
        assert_eq!(plugin.discovery_hidden_dirs(), DISCOVERY_HIDDEN_DIRS);
        assert!(plugin.tooling_dependencies().contains(&"velite"));
    }

    fn patterns_of(result: &PluginResult) -> Vec<String> {
        result
            .entry_patterns
            .iter()
            .map(|rule| rule.pattern.clone())
            .collect()
    }

    #[test]
    fn extracts_content_roots_and_imported_config_packages() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection, s } from 'velite';
            import rehypeShiki from '@shikijs/rehype';

            const posts = defineCollection({
                name: 'Post',
                pattern: 'blog/**/*.mdx',
                schema: s.object({}),
            });

            export default defineConfig({
                root: 'content',
                output: { data: '.velite', assets: 'public/static' },
                collections: { posts },
            });
        ";

        let result = plugin.resolve_config(&config_path, source, root);
        let patterns = patterns_of(&result);

        assert!(patterns.contains(&"content/blog/**/*.mdx".to_string()));
        assert!(
            result
                .referenced_dependencies
                .contains(&"velite".to_string())
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"@shikijs/rehype".to_string())
        );
        // Default output dir is covered by static always_used; no extra entry.
        assert!(result.always_used_files.is_empty());
    }

    #[test]
    fn defaults_root_to_content_when_omitted() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                collections: {
                    docs: defineCollection({ pattern: 'docs/**/*.md', schema: {} }),
                },
            });
        ";

        let patterns = patterns_of(&plugin.resolve_config(&config_path, source, root));
        assert!(patterns.contains(&"content/docs/**/*.md".to_string()));
    }

    #[test]
    fn honors_explicit_root_and_array_patterns() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                root: './src/content',
                collections: {
                    mixed: defineCollection({ pattern: ['posts/*.md', 'pages/*.mdx'] }),
                },
            });
        ";

        let patterns = patterns_of(&plugin.resolve_config(&config_path, source, root));
        assert!(patterns.contains(&"src/content/posts/*.md".to_string()));
        assert!(patterns.contains(&"src/content/pages/*.mdx".to_string()));
    }

    #[test]
    fn custom_output_data_is_credited_as_always_used() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                output: { data: 'generated/velite' },
                collections: {
                    docs: defineCollection({ pattern: 'docs/**/*.md' }),
                },
            });
        ";

        let result = plugin.resolve_config(&config_path, source, root);
        assert!(
            result
                .always_used_files
                .contains(&"generated/velite/**".to_string())
        );
    }

    #[test]
    fn negation_only_pattern_falls_back_to_root_glob() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                collections: {
                    docs: defineCollection({ pattern: ['!private/**'] }),
                },
            });
        ";

        let patterns = patterns_of(&plugin.resolve_config(&config_path, source, root));
        assert!(patterns.contains(&format!("content/**/*.{CONTENT_EXTENSIONS}")));
        assert!(!patterns.iter().any(|p| p.contains('!')));
    }

    #[test]
    fn default_output_data_adds_no_redundant_always_used_entry() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("apps/web/velite.config.ts");
        // Explicitly spells out the default; static `.velite/**` already covers it.
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                output: { data: '.velite' },
                collections: { docs: defineCollection({ pattern: 'docs/**/*.md' }) },
            });
        ";

        let result = plugin.resolve_config(&config_path, source, root);
        assert!(
            result.always_used_files.is_empty(),
            "default output.data must not add a redundant entry: {:?}",
            result.always_used_files
        );
    }

    #[test]
    fn custom_output_data_in_workspace_is_scoped_to_package() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("apps/web/velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                output: { data: 'generated/velite' },
                collections: { docs: defineCollection({ pattern: 'docs/**/*.md' }) },
            });
        ";

        let result = plugin.resolve_config(&config_path, source, root);
        assert!(
            result
                .always_used_files
                .contains(&"apps/web/generated/velite/**".to_string()),
            "custom output.data must be credited config-relative: {:?}",
            result.always_used_files
        );
    }

    #[test]
    fn nested_workspace_config_scopes_patterns_to_package() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("apps/web/velite.config.ts");
        let source = r"
            import { defineConfig, defineCollection } from 'velite';
            export default defineConfig({
                root: 'content',
                collections: { posts: defineCollection({ pattern: 'posts/**/*.md' }) },
            });
        ";

        let patterns = patterns_of(&plugin.resolve_config(&config_path, source, root));
        assert!(patterns.contains(&"apps/web/content/posts/**/*.md".to_string()));
        assert!(
            !patterns.iter().any(|p| p.starts_with("content/")),
            "patterns must be scoped to the config's package: {patterns:?}"
        );
    }

    #[test]
    fn malformed_config_falls_back_to_default_root_glob() {
        let plugin = VelitePlugin;
        let root = Path::new("/repo");
        let config_path = root.join("velite.config.ts");
        // No collections / patterns recoverable.
        let source = "export default someFactory();\n";

        let patterns = patterns_of(&plugin.resolve_config(&config_path, source, root));
        assert!(patterns.contains(&format!("content/**/*.{CONTENT_EXTENSIONS}")));
    }
}