alint 0.5.9

Language-agnostic linter for repository structure, file existence, filename conventions, and file content rules.
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
//! `alint init` — scaffold a starter `.alint.yml` based on a
//! lightweight detection of the repo's ecosystem and (optionally)
//! its workspace shape.
//!
//! Two distinct steps: [`detect`] reads a few root-level files to
//! decide which bundled rulesets apply; [`render`] turns that
//! detection into the YAML body that will be written to
//! `.alint.yml`. The split keeps detection unit-testable against
//! a tempdir without going through the CLI.

use std::fs;
use std::path::Path;

/// What `detect` figured out about the repo at the path it was
/// given. Pure data — `render` consumes it without re-reading
/// the disk.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Detection {
    pub languages: Vec<Language>,
    pub workspace: Option<WorkspaceFlavor>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Language {
    Rust,
    Node,
    Python,
    Go,
    Java,
}

impl Language {
    fn ruleset(self) -> &'static str {
        match self {
            Self::Rust => "alint://bundled/rust@v1",
            Self::Node => "alint://bundled/node@v1",
            Self::Python => "alint://bundled/python@v1",
            Self::Go => "alint://bundled/go@v1",
            Self::Java => "alint://bundled/java@v1",
        }
    }

    fn label(self) -> &'static str {
        match self {
            Self::Rust => "Rust",
            Self::Node => "Node",
            Self::Python => "Python",
            Self::Go => "Go",
            Self::Java => "Java",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkspaceFlavor {
    Cargo,
    Pnpm,
    Yarn,
}

impl WorkspaceFlavor {
    fn ruleset(self) -> &'static str {
        match self {
            Self::Cargo => "alint://bundled/monorepo/cargo-workspace@v1",
            Self::Pnpm => "alint://bundled/monorepo/pnpm-workspace@v1",
            Self::Yarn => "alint://bundled/monorepo/yarn-workspace@v1",
        }
    }

    fn label(self) -> &'static str {
        match self {
            Self::Cargo => "Cargo workspace",
            Self::Pnpm => "pnpm workspace",
            Self::Yarn => "Yarn / npm workspace",
        }
    }
}

/// Detect ecosystem + (optionally) workspace shape by reading a
/// fixed set of root files. `with_monorepo` switches workspace
/// detection on; without it, the result's `workspace` is always
/// `None` even if the repo would qualify.
pub fn detect(root: &Path, with_monorepo: bool) -> Detection {
    let mut languages = Vec::new();

    if root.join("Cargo.toml").is_file() {
        languages.push(Language::Rust);
    }
    if root.join("package.json").is_file() {
        languages.push(Language::Node);
    }
    if ["pyproject.toml", "setup.py", "setup.cfg"]
        .iter()
        .any(|f| root.join(f).is_file())
    {
        languages.push(Language::Python);
    }
    if root.join("go.mod").is_file() {
        languages.push(Language::Go);
    }
    if ["pom.xml", "build.gradle", "build.gradle.kts"]
        .iter()
        .any(|f| root.join(f).is_file())
    {
        languages.push(Language::Java);
    }

    let workspace = if with_monorepo {
        detect_workspace(root)
    } else {
        None
    };

    Detection {
        languages,
        workspace,
    }
}

fn detect_workspace(root: &Path) -> Option<WorkspaceFlavor> {
    // Cargo: root Cargo.toml has a `[workspace]` table. Read +
    // grep — full TOML parsing is overkill for a string-shape
    // check.
    if let Ok(content) = fs::read_to_string(root.join("Cargo.toml")) {
        if content
            .lines()
            .any(|l| l.trim_start().starts_with("[workspace]"))
        {
            return Some(WorkspaceFlavor::Cargo);
        }
    }
    // pnpm: root pnpm-workspace.yaml / .yml exists. The fact
    // gate inside the bundled ruleset re-checks this; here we
    // only need a presence test to decide what to scaffold.
    if root.join("pnpm-workspace.yaml").is_file() || root.join("pnpm-workspace.yml").is_file() {
        return Some(WorkspaceFlavor::Pnpm);
    }
    // Yarn / npm: root package.json contains a `"workspaces"`
    // field. Same string-shape check rather than full JSON
    // parsing.
    if let Ok(content) = fs::read_to_string(root.join("package.json")) {
        if content.contains("\"workspaces\"") {
            return Some(WorkspaceFlavor::Yarn);
        }
    }
    None
}

/// Render a complete `.alint.yml` body from a [`Detection`].
/// Always emits `oss-baseline@v1`; appends ecosystem rulesets
/// for each detected language and (when applicable) the
/// monorepo + workspace overlay. Output is hand-formatted YAML
/// (not serialized via serde) so the generated file can carry
/// header comments that survive subsequent edits.
pub fn render(detection: &Detection) -> String {
    let mut out = String::with_capacity(512);
    out.push_str("# Generated by `alint init`. Adjust as needed.\n");
    let summary = render_summary(detection);
    if !summary.is_empty() {
        out.push_str("# Detected: ");
        out.push_str(&summary);
        out.push_str(".\n");
    }
    out.push_str("# Run `alint check` to lint, `alint fix` to apply auto-fixable rules.\n");
    out.push_str("# See https://alint.org for the full ruleset reference.\n");
    out.push('\n');
    out.push_str("version: 1\n");
    if detection.workspace.is_some() {
        // `nested_configs: true` lets each subdirectory layer
        // its own `.alint.yml` on top of this one — the canonical
        // shape for workspace-tier monorepos where individual
        // packages may want to extend or override.
        out.push_str("nested_configs: true\n");
    }
    out.push('\n');
    out.push_str("extends:\n");
    out.push_str("  - alint://bundled/oss-baseline@v1\n");
    for lang in &detection.languages {
        out.push_str("  - ");
        out.push_str(lang.ruleset());
        out.push('\n');
    }
    if let Some(flavor) = detection.workspace {
        out.push_str("  - alint://bundled/monorepo@v1\n");
        out.push_str("  - ");
        out.push_str(flavor.ruleset());
        out.push('\n');
    }
    out
}

/// Human-readable summary of what `detect` found, for the
/// generated header comment AND the CLI's stdout summary line.
pub fn render_summary(detection: &Detection) -> String {
    let mut parts: Vec<&str> = detection.languages.iter().map(|l| l.label()).collect();
    if let Some(flavor) = detection.workspace {
        parts.push(flavor.label());
    }
    parts.join(", ")
}

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

    fn td() -> tempfile::TempDir {
        tempfile::Builder::new()
            .prefix("alint-init-")
            .tempdir()
            .unwrap()
    }

    fn touch(root: &Path, rel: &str, content: &str) {
        let path = root.join(rel);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(path, content).unwrap();
    }

    #[test]
    fn detects_no_languages_in_empty_dir() {
        let tmp = td();
        let det = detect(tmp.path(), false);
        assert!(det.languages.is_empty());
        assert_eq!(det.workspace, None);
    }

    #[test]
    fn detects_rust_via_cargo_toml() {
        let tmp = td();
        touch(tmp.path(), "Cargo.toml", "[package]\nname = \"x\"\n");
        let det = detect(tmp.path(), false);
        assert_eq!(det.languages, vec![Language::Rust]);
    }

    #[test]
    fn detects_node_via_package_json() {
        let tmp = td();
        touch(tmp.path(), "package.json", "{\"name\":\"x\"}\n");
        let det = detect(tmp.path(), false);
        assert_eq!(det.languages, vec![Language::Node]);
    }

    #[test]
    fn detects_python_via_any_of_three_manifests() {
        for manifest in ["pyproject.toml", "setup.py", "setup.cfg"] {
            let tmp = td();
            touch(tmp.path(), manifest, "");
            let det = detect(tmp.path(), false);
            assert_eq!(
                det.languages,
                vec![Language::Python],
                "manifest: {manifest}"
            );
        }
    }

    #[test]
    fn detects_go_via_go_mod() {
        let tmp = td();
        touch(tmp.path(), "go.mod", "module example.com/x\n");
        let det = detect(tmp.path(), false);
        assert_eq!(det.languages, vec![Language::Go]);
    }

    #[test]
    fn detects_java_via_pom_or_gradle() {
        for manifest in ["pom.xml", "build.gradle", "build.gradle.kts"] {
            let tmp = td();
            touch(tmp.path(), manifest, "");
            let det = detect(tmp.path(), false);
            assert_eq!(det.languages, vec![Language::Java], "manifest: {manifest}");
        }
    }

    #[test]
    fn detects_polyglot_repos() {
        let tmp = td();
        touch(tmp.path(), "Cargo.toml", "[package]\nname = \"x\"\n");
        touch(tmp.path(), "package.json", "{}");
        let det = detect(tmp.path(), false);
        assert_eq!(det.languages, vec![Language::Rust, Language::Node]);
    }

    #[test]
    fn workspace_detection_off_by_default() {
        let tmp = td();
        touch(tmp.path(), "Cargo.toml", "[workspace]\nmembers = []\n");
        let det = detect(tmp.path(), false);
        assert_eq!(det.workspace, None);
    }

    #[test]
    fn detects_cargo_workspace_with_monorepo_flag() {
        let tmp = td();
        touch(
            tmp.path(),
            "Cargo.toml",
            "[workspace]\nmembers = [\"crates/*\"]\n",
        );
        let det = detect(tmp.path(), true);
        assert_eq!(det.workspace, Some(WorkspaceFlavor::Cargo));
        assert_eq!(det.languages, vec![Language::Rust]);
    }

    #[test]
    fn cargo_without_workspace_table_is_not_a_workspace() {
        let tmp = td();
        touch(tmp.path(), "Cargo.toml", "[package]\nname = \"x\"\n");
        let det = detect(tmp.path(), true);
        assert_eq!(det.workspace, None);
    }

    #[test]
    fn detects_pnpm_workspace() {
        for f in ["pnpm-workspace.yaml", "pnpm-workspace.yml"] {
            let tmp = td();
            touch(tmp.path(), "package.json", "{}");
            touch(tmp.path(), f, "packages:\n  - 'packages/*'\n");
            let det = detect(tmp.path(), true);
            assert_eq!(det.workspace, Some(WorkspaceFlavor::Pnpm), "file: {f}");
        }
    }

    #[test]
    fn detects_yarn_workspace_via_package_json_workspaces_field() {
        let tmp = td();
        touch(
            tmp.path(),
            "package.json",
            "{\"name\":\"root\",\"workspaces\":[\"packages/*\"]}\n",
        );
        let det = detect(tmp.path(), true);
        assert_eq!(det.workspace, Some(WorkspaceFlavor::Yarn));
    }

    #[test]
    fn cargo_workspace_takes_precedence_over_yarn_when_both_match() {
        // A repo with a Cargo workspace AND a package.json with
        // `"workspaces"` (rare but possible — e.g. a Rust + JS
        // monorepo) gets the Cargo flavour, since the Cargo
        // workspace check runs first. Heuristic — if a user has
        // both, they can override the generated config.
        let tmp = td();
        touch(
            tmp.path(),
            "Cargo.toml",
            "[workspace]\nmembers = [\"crates/*\"]\n",
        );
        touch(
            tmp.path(),
            "package.json",
            "{\"workspaces\":[\"packages/*\"]}\n",
        );
        let det = detect(tmp.path(), true);
        assert_eq!(det.workspace, Some(WorkspaceFlavor::Cargo));
    }

    #[test]
    fn render_minimal_repo_extends_only_oss_baseline() {
        let det = Detection::default();
        let out = render(&det);
        assert!(out.contains("alint://bundled/oss-baseline@v1"));
        assert!(!out.contains("rust@v1"));
        assert!(!out.contains("monorepo@v1"));
        assert!(!out.contains("nested_configs"));
    }

    #[test]
    fn render_polyglot_extends_each_language() {
        let det = Detection {
            languages: vec![Language::Rust, Language::Node],
            workspace: None,
        };
        let out = render(&det);
        assert!(out.contains("rust@v1"));
        assert!(out.contains("node@v1"));
        assert!(!out.contains("monorepo@v1"));
    }

    #[test]
    fn render_workspace_adds_monorepo_overlay_and_nested_configs() {
        let det = Detection {
            languages: vec![Language::Rust],
            workspace: Some(WorkspaceFlavor::Cargo),
        };
        let out = render(&det);
        assert!(out.contains("nested_configs: true"));
        assert!(out.contains("alint://bundled/monorepo@v1"));
        assert!(out.contains("alint://bundled/monorepo/cargo-workspace@v1"));
    }

    #[test]
    fn render_includes_detection_summary_in_header() {
        let det = Detection {
            languages: vec![Language::Rust],
            workspace: Some(WorkspaceFlavor::Cargo),
        };
        let out = render(&det);
        assert!(out.contains("# Detected: Rust, Cargo workspace."));
    }

    #[test]
    fn render_omits_detection_line_for_empty_detection() {
        let det = Detection::default();
        let out = render(&det);
        assert!(!out.contains("# Detected:"));
    }
}