glosslint 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ import { _ as capitalise } from "./glossary-lk7EwGey.mjs";
2
+ // src/format.ts
3
+ const CODES = [
4
+ "GLOSSLINT_TERM",
5
+ "GLOSSLINT_AVOID",
6
+ "GLOSSLINT_CASE"
7
+ ];
8
+ const FIX_VERBS = {
9
+ autofix: "Change to",
10
+ suggestion: "Use",
11
+ rename: "Rename to"
12
+ };
13
+ const paint = (on, code, text) => on ? `\u001B[${code}m${text}\u001B[0m` : text;
14
+ function isCliFinding(finding) {
15
+ return "file" in finding;
16
+ }
17
+ function termLine(term) {
18
+ const description = term.description?.trim().replace(/\s+/g, " ");
19
+ if (!description) return term.term;
20
+ const lowered = /^\p{Lu}(?![\p{Lu}\p{N}])/u.test(description) ? description.charAt(0).toLowerCase() + description.slice(1) : description;
21
+ return `${term.term}, ${lowered}`;
22
+ }
23
+ /** One finding in the nostics tree format: the headline, then why, fix, term and see. */
24
+ function formatFinding(finding, options = {}) {
25
+ const color = options.color ?? false;
26
+ const where = isCliFinding(finding) ? `${finding.file}:${finding.line}:${finding.column}` : `offset ${finding.start}`;
27
+ const headline = `${paint(color, "1;31", `[${finding.code}]`)} "${finding.text}" ${isCliFinding(finding) ? "in" : "at"} ${paint(color, "36", where)}`;
28
+ const lines = [];
29
+ if (finding.why) lines.push(["why", capitalise(finding.why.trim().replace(/\s+/g, " "))]);
30
+ if (finding.fix) {
31
+ const result = "result" in finding.fix ? `: "${String(finding.fix.result).trim()}"` : "";
32
+ lines.push(["fix", `${FIX_VERBS[finding.fix.kind]} "${finding.fix.replace}"${result}`]);
33
+ }
34
+ lines.push(["term", termLine(finding.term)]);
35
+ if (finding.see) lines.push(["see", finding.see]);
36
+ return [headline, ...lines.map(([label, text], index) => {
37
+ const branch = index === lines.length - 1 ? "╰▶" : "├▶";
38
+ return `${paint(color, "2", branch)} ${paint(color, "1", `${label}:`)} ${text}`;
39
+ })].join("\n");
40
+ }
41
+ function plural(count, word) {
42
+ return `${count} ${word}${count === 1 ? "" : "s"}`;
43
+ }
44
+ /** Every finding in the tree format, followed by a summary line. */
45
+ function formatFindings(findings, options = {}) {
46
+ const blocks = findings.map((finding) => formatFinding(finding, options));
47
+ const withFiles = new Set(findings.filter(isCliFinding).map((finding) => finding.file)).size;
48
+ let summary;
49
+ if (options.files !== void 0) summary = findings.length === 0 ? `No findings in ${plural(options.files, "file")}.` : `${plural(findings.length, "finding")} in ${withFiles} of ${plural(options.files, "file")}.`;
50
+ else summary = findings.length === 0 ? "No findings." : `${plural(findings.length, "finding")}.`;
51
+ return [...blocks, summary].join("\n\n");
52
+ }
53
+ /** The JSON the CLI prints: `{ findings, summary }`. */
54
+ function toJson(findings, files) {
55
+ const counts = Object.fromEntries(CODES.map((code) => [code, 0]));
56
+ for (const finding of findings) counts[finding.code]++;
57
+ return {
58
+ findings,
59
+ summary: {
60
+ files,
61
+ findings: findings.length,
62
+ ...counts
63
+ }
64
+ };
65
+ }
66
+ export { formatFindings as n, toJson as r, formatFinding as t };
@@ -0,0 +1,32 @@
1
+ import { c as Corpus } from "./types-Bbp_cYfZ.mjs";
2
+ /** One problem `readGlossary` found in a glossary file. */
3
+ interface GlossaryProblem {
4
+ /** A stable code, `GLOSSLINT_G001` to `GLOSSLINT_G009`. */
5
+ code: string;
6
+ /** The 1-based line the problem is on. */
7
+ line: number;
8
+ /** What is wrong. */
9
+ why: string;
10
+ /** What to change, or what `glosslint doctor --fix` changes. */
11
+ fix: string;
12
+ /** Whether `glosslint doctor --fix` rewrites it (`G001` to `G003`). */
13
+ fixable: boolean;
14
+ /** The term the problem is in, when it is in one. */
15
+ term?: string;
16
+ }
17
+ /**
18
+ * Reads the text of a glossary file. `corpus` is set when no problem from `G004` to `G008` exists, and
19
+ * `fixed`, the canonical text, when the file can be rewritten and differs from it.
20
+ */
21
+ declare function readGlossary(text: string): {
22
+ problems: GlossaryProblem[];
23
+ corpus?: Corpus;
24
+ fixed?: string;
25
+ };
26
+ /**
27
+ * Reads a glossary file, `.agents/glossary.md` by default, as a corpus. Throws a `GlosslintConfigError`
28
+ * when the file is missing (`GLOSSLINT_C009`) or has a problem only a person can fix (`G004` to `G008`).
29
+ * A file with only fixable problems loads as its fixed form would.
30
+ */
31
+ declare function fromGlossary(path?: string | URL): Corpus;
32
+ export { fromGlossary as n, readGlossary as r, GlossaryProblem as t };