blue-lang-cli 0.0.216

The blue command line: run, fmt, ast, erase, check.
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
//! `blue census`: count, over a whole tree, every finding of every rule that
//! is being ratcheted in, and compare each count with its row's `ratchet`.
//!
//! A rule with `ratchet: Some(n)` for `n > 0` is computed on every program
//! and not enforced (`blue_lang_check::Rule::active`); this is where its
//! findings are counted. The census fails unless every count EQUALS its
//! ratchet: a count that rises is a new violation, and a count that falls is
//! progress the row has not recorded yet. Either way the fix is an edit of
//! the number, which a reviewer sees. `checks.namespace-census` runs it over
//! the repository.
//!
//! Each `.b` file is checked as an entry, as `blue check` would, against a
//! load path over every `bidamas/` directory in the tree (blue's own first).
//! A finding in a bidama is seen from every file that imports it, so findings
//! are counted once per (file, position, code).

use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

use blue_lang_check::{Code, RULES};
use blue_lang_runtime::pipeline::{check_entry, render, Checking};
use blue_lang_runtime::uses::Entry;
use blue_lang_runtime::word_shapes::Standing;

pub const DOCUMENTED: &[&str] = &["kakehashi", "toriyose", "umu"];

pub const DOC_CODES: &[Code] = &[Code::B0056, Code::B0057];

/// What the census counted.
pub struct Census {
    /// Files checked.
    pub files: usize,
    /// For each ratcheted code, each distinct finding, rendered.
    pub findings: BTreeMap<Code, BTreeSet<String>>,
    pub by_package: BTreeMap<(Code, String), BTreeSet<String>>,
    pub waived: BTreeMap<Code, BTreeSet<String>>,
}

impl Census {
    /// Every obligation whose waived debt is not the number its row holds:
    /// `(id, code, measured, held)`.
    #[must_use]
    pub fn held_mismatches(&self) -> Vec<(&'static str, Code, usize, u32)> {
        blue_lang_check::obligations::OBLIGATIONS
            .iter()
            .filter_map(|o| {
                let blue_lang_check::obligations::Refusal::Code(code) = o.refusal else {
                    return None;
                };
                let measured = self.waived.get(&code).map_or(0, BTreeSet::len);
                (measured != o.held as usize).then_some((o.id, code, measured, o.held))
            })
            .collect()
    }

    /// Every ratcheted rule whose measured count is not its ratchet:
    /// `(code, measured, ratchet)`.
    #[must_use]
    pub fn mismatches(&self) -> Vec<(Code, usize, u32)> {
        RULES
            .iter()
            .filter_map(|r| {
                let ratchet = r.ratchet?;
                let measured = self.findings.get(&r.code).map_or(0, BTreeSet::len);
                (measured != ratchet as usize).then_some((r.code, measured, ratchet))
            })
            .collect()
    }

    #[must_use]
    pub fn package_mismatches(&self, documented: &[&str]) -> Vec<(Code, String, usize)> {
        documented
            .iter()
            .flat_map(|p| {
                DOC_CODES.iter().filter_map(move |c| {
                    let n = self
                        .by_package
                        .get(&(*c, (*p).to_string()))
                        .map_or(0, BTreeSet::len);
                    (n > 0).then(|| (*c, (*p).to_string(), n))
                })
            })
            .collect()
    }
}

/// Take the census of `root`.
///
/// # Errors
///
/// A file that does not parse or whose imports do not resolve: the census
/// has nothing to count there, and a skipped file would under-count.
pub fn take(root: &Path) -> Result<Census, String> {
    let files = blue_lang_pkg::corpus::files(root);
    let loader = blue_lang_pkg::corpus::load_path(root, &files);
    let mut findings: BTreeMap<Code, BTreeSet<String>> = BTreeMap::new();
    let mut by_package: BTreeMap<(Code, String), BTreeSet<String>> = BTreeMap::new();
    let mut waived: BTreeMap<Code, BTreeSet<String>> = BTreeMap::new();
    for path in &files {
        let text = std::fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?;
        let checked = check_entry(
            Entry {
                path: Some(path),
                text: &text,
            },
            &loader,
            None,
            Checking::WithTests,
        )
        .map_err(|e| format!("{}: {e}", path.display()))?;
        // Enforced findings of a ratcheted rule count too: a rule at
        // `Some(0)` that fires is a count of one, not a pass.
        let ratcheted = |d: &&blue_lang_check::Diagnostic| d.code.rule().ratchet.is_some();
        let package = checked
            .program
            .file(blue_lang_runtime::uses::ResolvedProgram::ENTRY)
            .and_then(|f| f.package.as_deref())
            .map(|p| blue_lang_syntax::root_of(p).to_string());
        for d in checked
            .outcome
            .census
            .iter()
            .chain(checked.outcome.diagnostics.iter().filter(ratcheted))
        {
            let shown = render(&checked.program, d);
            if let Some(p) = &package {
                by_package
                    .entry((d.code, p.clone()))
                    .or_default()
                    .insert(shown.clone());
            }
            findings.entry(d.code).or_default().insert(shown);
        }
        for w in &checked.outcome.waived {
            if blue_lang_check::obligations::of_code(w.waiver.code).is_none() {
                continue;
            }
            let shown = format!(
                "{}: {} ({})",
                path.strip_prefix(root).unwrap_or(path).display(),
                render(&checked.program, &w.diagnostic)
                    .lines()
                    .next()
                    .unwrap_or_default(),
                w.waiver.reason
            );
            waived.entry(w.waiver.code).or_default().insert(shown);
        }
    }
    Ok(Census {
        files: files.len(),
        findings,
        by_package,
        waived,
    })
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReuseFact {
    pub scope: String,
    pub file: String,
    pub line: usize,
    pub name: String,
    pub shape: String,
    pub size: usize,
    pub home: Option<String>,
    pub fix: Option<&'static str>,
    pub word: bool,
    pub waived: bool,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ReuseScan {
    pub files: usize,
    pub definitions: usize,
    pub facts: Vec<ReuseFact>,
    pub blind: Vec<(String, String)>,
}

impl ReuseScan {
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "files": self.files,
            "definitions": self.definitions,
            "blind": self.blind.iter().map(|(file, why)| serde_json::json!({
                "file": file,
                "why": why,
            })).collect::<Vec<_>>(),
            "facts": self.facts.iter().map(ReuseFact::to_json).collect::<Vec<_>>(),
        })
    }
}

impl ReuseFact {
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "scope": self.scope,
            "file": self.file,
            "line": self.line,
            "name": self.name,
            "shape": self.shape,
            "size": self.size,
            "home": self.home,
            "fix": self.fix,
            "word": self.word,
            "waived": self.waived,
        })
    }
}

fn package_roots(files: &[std::path::PathBuf]) -> Vec<std::path::PathBuf> {
    let mut roots: Vec<std::path::PathBuf> = files
        .iter()
        .filter_map(|f| {
            let dir = f.parent()?;
            (dir.file_name()? == f.file_stem()?).then(|| dir.parent().map(Path::to_path_buf))?
        })
        .collect();
    roots.sort();
    roots.dedup();
    roots
}

pub(crate) fn environment_over(
    files: &[std::path::PathBuf],
) -> blue_lang_pkg::load_path::Environment {
    let mut roots = package_roots(files);
    roots.extend(
        std::env::var_os(blue_lang_pkg::load_path::BLUE_PATH)
            .as_deref()
            .map_or_else(Vec::new, |v| std::env::split_paths(v).collect()),
    );
    blue_lang_pkg::load_path::Environment::from_vars(
        std::env::join_paths(roots).unwrap_or_default().as_os_str(),
        std::env::var_os(blue_lang_pkg::load_path::BLUE_DIST)
            .unwrap_or_default()
            .as_os_str(),
    )
}

#[must_use]
pub fn reuse_scan(root: &Path) -> ReuseScan {
    let files = blue_lang_pkg::corpus::files(root);
    let mut scan = ReuseScan {
        files: files.len(),
        ..ReuseScan::default()
    };
    let floor = blue_lang_check::shape::Near::DEFAULT.min_size;
    let environment = environment_over(&files);
    for path in &files {
        let rel = path
            .strip_prefix(root)
            .unwrap_or(path)
            .display()
            .to_string();
        let text = match std::fs::read_to_string(path) {
            Ok(t) => t,
            Err(e) => {
                scan.blind.push((rel, e.to_string()));
                continue;
            }
        };
        let loader = environment.load_path(Some(path));
        let checked = match check_entry(
            Entry {
                path: Some(path),
                text: &text,
            },
            &loader,
            None,
            Checking::WithTests,
        ) {
            Ok(c) => c,
            Err(e) => {
                scan.blind.push((rel, e.to_string()));
                continue;
            }
        };
        let package = checked
            .program
            .file(blue_lang_runtime::uses::ResolvedProgram::ENTRY)
            .and_then(|f| f.package.clone());
        let scope = package.unwrap_or_else(|| format!("%root:{rel}"));
        let entry = blue_lang_mondou::facts::of_entry(&checked);
        let shapes: Vec<(
            &blue_lang_mondou::facts::DefFact,
            &blue_lang_runtime::def_hash::Shape,
        )> = entry
            .facts
            .iter()
            .filter_map(|f| f.shape.as_ref().map(|s| (f, s)))
            .collect();
        scan.definitions += shapes.len();
        for (f, s) in shapes.into_iter().filter(|(_, s)| s.size >= floor) {
            let line = f.line;
            let standing = blue_lang_runtime::word_shapes::standing(&s.hash, &s.key);
            let (home, fix, word) = match standing {
                Standing::NoWord => (None, None, false),
                Standing::OwnWord => (None, None, true),
                Standing::CopyOf { word, foundation } => (
                    Some(word.to_string()),
                    Some(if foundation { "import" } else { "move_down" }),
                    false,
                ),
            };
            let waived = checked
                .outcome
                .waived
                .iter()
                .any(|w| w.waiver.code == Code::B0070 && w.waiver.top_level == s.top_level);
            scan.facts.push(ReuseFact {
                scope: scope.clone(),
                file: rel.clone(),
                line,
                name: s.name().to_string(),
                shape: s.hash.hex(),
                size: s.size,
                home,
                fix,
                word,
                waived,
            });
        }
    }
    scan
}

#[must_use]
pub fn word_copy_lines(file: &Path) -> Vec<String> {
    let Ok(text) = std::fs::read_to_string(file) else {
        return Vec::new();
    };
    let loader = blue_lang_pkg::load_path::LoadPath::for_entry(file);
    let found = blue_lang_runtime::pipeline::word_copies(
        Entry {
            path: Some(file),
            text: &text,
        },
        &loader,
    );
    found
        .unwrap_or_default()
        .iter()
        .map(|d| {
            let (line, column) = blue_lang_syntax::Span::line_col(&text, d.span.start);
            format!(
                "{}:{line}:{column}: {}: {}",
                file.display(),
                d.message,
                d.help.as_deref().unwrap_or_default()
            )
        })
        .collect()
}

fn hook_file(input: &str) -> Option<std::path::PathBuf> {
    let doc: serde_json::Value = serde_json::from_str(input).ok()?;
    let path = Path::new(doc.get("tool_input")?.get("file_path")?.as_str()?);
    (path.extension().is_some_and(|x| x == "b") && path.is_file()).then(|| path.to_path_buf())
}

pub fn reuse_hook() {
    use std::io::{Read, Write};
    std::panic::set_hook(Box::new(|_| {}));
    let _ = std::panic::catch_unwind(|| {
        let mut input = String::new();
        let _ = std::io::stdin().read_to_string(&mut input);
        let Some(file) = hook_file(&input) else {
            return;
        };
        let mut out = std::io::stdout().lock();
        for line in word_copy_lines(&file) {
            let _ = writeln!(out, "{line}");
        }
    });
}

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

    fn project(name: &str, body: &str) -> std::path::PathBuf {
        let root = std::env::temp_dir().join(format!(
            "blue-census-documented-{}-{name}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&root);
        let dir = root.join("bidamas").join("kagi");
        std::fs::create_dir_all(&dir).expect("mkdir");
        std::fs::write(dir.join("Bluefile"), "package(\"kagi\", \"0.1.0\")\n").expect("write");
        std::fs::write(dir.join("kagi.b"), body).expect("write");
        root
    }

    const HEAD: &str = "@moduledoc \"\"\"\nkagi holds one value.\n\n    kagi #=> 1\n\"\"\"\n\n";

    #[test]
    fn a_documented_bidama_with_an_undocumented_definition_is_named() {
        let root = project("red", &format!("{HEAD}def kagi()\n  1\nend\n"));
        let c = take(&root).expect("census");
        let _ = std::fs::remove_dir_all(&root);
        assert_eq!(
            c.package_mismatches(&["kagi"]),
            vec![(Code::B0056, "kagi".to_string(), 1)]
        );
        assert!(c.package_mismatches(&["other"]).is_empty());
    }

    #[test]
    fn a_documented_bidama_whose_every_definition_has_a_doc_and_an_example_holds() {
        let root = project(
            "green",
            &format!(
                "{HEAD}@doc \"\"\"\nkagi is one.\n\n    kagi #=> 1\n\"\"\"\ndef kagi()\n  1\nend\n"
            ),
        );
        let c = take(&root).expect("census");
        let _ = std::fs::remove_dir_all(&root);
        assert!(c.package_mismatches(&["kagi"]).is_empty());
    }

    #[test]
    fn a_doc_without_an_example_is_named_too() {
        let root = project(
            "example",
            &format!("{HEAD}@doc \"\"\"\nkagi is one.\n\"\"\"\ndef kagi()\n  1\nend\n"),
        );
        let c = take(&root).expect("census");
        let _ = std::fs::remove_dir_all(&root);
        assert_eq!(
            c.package_mismatches(&["kagi"]),
            vec![(Code::B0057, "kagi".to_string(), 1)]
        );
    }
}

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

    const POINT: &str = "@moduledoc \"\"\"\nkagi asks the machine.\n\n    1 + 1 #=> 2\n\"\"\"\n\n@doc \"\"\"\nkagi runs `date`.\n\n    1 + 1 #=> 2\n\"\"\"\ndef kagi()\n  exec_capture(\"date\")\nend\n";

    fn project(name: &str, body: &str) -> std::path::PathBuf {
        let root =
            std::env::temp_dir().join(format!("blue-census-held-{}-{name}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        let dir = root.join("bidamas").join("kagi");
        std::fs::create_dir_all(&dir).expect("mkdir");
        std::fs::write(dir.join("Bluefile"), "package(\"kagi\", \"0.1.0\")\n").expect("write");
        std::fs::write(dir.join("kagi.b"), body).expect("write");
        root
    }

    /// **A waived integration point is debt the census counts, with its
    /// reason; a point with a test of its kind is no debt.** The count is
    /// what `held_mismatches` holds against the obligation row.
    #[test]
    fn a_waived_point_is_counted_and_a_tested_one_is_not() {
        let waived = POINT.replace(
            "@doc \"\"\"\nkagi runs",
            "# waive B0083: the fixture's debt\n@doc \"\"\"\nkagi runs",
        );
        let root = project("waived", &waived);
        let c = take(&root).expect("census");
        let _ = std::fs::remove_dir_all(&root);
        let counted: Vec<&String> = c.waived.get(&Code::B0083).into_iter().flatten().collect();
        assert_eq!(counted.len(), 1, "{counted:?}");
        assert!(counted[0].contains("the fixture's debt"), "{counted:?}");
        let tested = format!(
            "{POINT}\ntest \"kagi\", {{kind: :integration, allow: [:process]}}\n  assert kagi != nil\nend\n"
        );
        let root = project("tested", &tested);
        let c = take(&root).expect("census");
        let _ = std::fs::remove_dir_all(&root);
        assert!(c.waived.get(&Code::B0083).is_none());
    }
}