glosslint 0.0.1 → 0.1.1
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.
- package/README.md +56 -36
- package/dist/cli.d.mts +49 -5
- package/dist/cli.mjs +469 -61
- package/dist/eslint.d.mts +10 -6
- package/dist/eslint.mjs +19 -771
- package/dist/format-E5eFHoQT.mjs +66 -0
- package/dist/glossary-DmTK_e1d.d.mts +32 -0
- package/dist/glossary-ztPpmozR.mjs +1905 -0
- package/dist/inclusive-CP6oU8NW.mjs +520 -0
- package/dist/index.d.mts +6 -27
- package/dist/index.mjs +3 -459
- package/dist/oxlint.d.mts +26 -0
- package/dist/oxlint.mjs +33 -0
- package/dist/presets/inclusive.d.mts +1 -1
- package/dist/presets/inclusive.mjs +1 -520
- package/dist/presets/tech.d.mts +1 -1
- package/dist/presets/tech.mjs +1 -2401
- package/dist/rules-D9Jd07u1.mjs +792 -0
- package/dist/tech-D0v3ss45.mjs +2401 -0
- package/dist/{types-CBQGgzID.d.mts → types-Bbp_cYfZ.d.mts} +3 -1
- package/dist/x18n.d.mts +10 -5
- package/dist/x18n.mjs +12 -4
- package/package.json +17 -2
- package/dist/matcher-Xz4vspsE.mjs +0 -715
- package/dist/tools-BrdtyK9t.mjs +0 -275
package/dist/cli.mjs
CHANGED
|
@@ -1,14 +1,289 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import { a as kindOf, n as
|
|
4
|
-
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { n as formatFindings, r as toJson } from "./format-E5eFHoQT.mjs";
|
|
3
|
+
import { a as explain, c as testCorpus, d as createMatcher, h as kindOf, i as readGlossary, m as isTextName, n as GLOSSARY_PROBLEMS, o as explainMatches, s as printCorpus, t as DEFAULT_GLOSSARY, y as isCodeShape } from "./glossary-ztPpmozR.mjs";
|
|
4
|
+
import { existsSync, readFileSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { extname, relative, resolve, sep } from "node:path";
|
|
6
|
-
import process from "node:process";
|
|
7
6
|
import { pathToFileURL } from "node:url";
|
|
7
|
+
import process from "node:process";
|
|
8
8
|
import { cac } from "cac";
|
|
9
9
|
import { escapePath, glob, isDynamicPattern } from "tinyglobby";
|
|
10
10
|
// package.json
|
|
11
|
-
var version = "0.
|
|
11
|
+
var version = "0.1.1";
|
|
12
|
+
// src/comark.ts
|
|
13
|
+
const OPENER = /^[ \t]*(:{2,})[A-Za-z][\w-]*/;
|
|
14
|
+
const BARE = /^[ \t]*:[A-Za-z][\w-]*[ \t]*\r?$/;
|
|
15
|
+
const CLOSER = /^[ \t]*:{2,}[ \t]*\r?$/;
|
|
16
|
+
const SLOT = /^[ \t]*#[A-Za-z][\w-]*/;
|
|
17
|
+
const RULE = /^[ \t]*---[ \t]*\r?$/;
|
|
18
|
+
const YAML_ENTRY = /^([ \t]*)([\w.-]+):[ \t]*(.*?)[ \t]*\r?$/;
|
|
19
|
+
/** Reads `{key="value" key='v' key=v bool .class #id :bind='json'}` from `at`, or `undefined` if it doesn't close. */
|
|
20
|
+
function scanAttributes(line, at) {
|
|
21
|
+
const attributes = [];
|
|
22
|
+
let index = at + 1;
|
|
23
|
+
while (index < line.length) {
|
|
24
|
+
const char = line[index];
|
|
25
|
+
if (char === "}") return {
|
|
26
|
+
end: index + 1,
|
|
27
|
+
attributes
|
|
28
|
+
};
|
|
29
|
+
if (/\s/.test(char)) {
|
|
30
|
+
index++;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const key = /^[^\s=}"']+/.exec(line.slice(index));
|
|
34
|
+
if (!key) return void 0;
|
|
35
|
+
const name = key[0];
|
|
36
|
+
index += name.length;
|
|
37
|
+
if (line[index] !== "=") {
|
|
38
|
+
attributes.push({ name });
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
index++;
|
|
42
|
+
const quote = line[index];
|
|
43
|
+
if (quote === "\"" || quote === "'") {
|
|
44
|
+
let end = index + 1;
|
|
45
|
+
while (end < line.length && line[end] !== quote) end += line[end] === "\\" ? 2 : 1;
|
|
46
|
+
if (end >= line.length) return void 0;
|
|
47
|
+
attributes.push({
|
|
48
|
+
name,
|
|
49
|
+
value: {
|
|
50
|
+
start: index + 1,
|
|
51
|
+
end
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
index = end + 1;
|
|
55
|
+
} else {
|
|
56
|
+
const value = /^[^\s}]*/.exec(line.slice(index))[0];
|
|
57
|
+
attributes.push({
|
|
58
|
+
name,
|
|
59
|
+
value: {
|
|
60
|
+
start: index,
|
|
61
|
+
end: index + value.length
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
index += value.length;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Reads `[text]` from `at`, allowing nested brackets, or `undefined` if it doesn't close. */
|
|
69
|
+
function scanBrackets(line, at) {
|
|
70
|
+
let depth = 0;
|
|
71
|
+
for (let index = at; index < line.length; index++) {
|
|
72
|
+
const char = line[index];
|
|
73
|
+
if (char === "\\") index++;
|
|
74
|
+
else if (char === "[") depth++;
|
|
75
|
+
else if (char === "]" && --depth === 0) return index + 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function isText(name, value) {
|
|
79
|
+
return !name.startsWith(":") && isTextName(name) && value.trim() !== "" && !isCodeShape(value);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Reads what follows a component's name: optional `[text]`, then optional `{props}`. Returns where it
|
|
83
|
+
* ends, the text props and the kept `[text]`, or `undefined` when a bracket or brace doesn't close.
|
|
84
|
+
*/
|
|
85
|
+
function readComponent(line, nameEnd) {
|
|
86
|
+
let index = nameEnd;
|
|
87
|
+
let content;
|
|
88
|
+
if (line[index] === "[") {
|
|
89
|
+
const end = scanBrackets(line, index);
|
|
90
|
+
if (end === void 0) return void 0;
|
|
91
|
+
content = [index + 1, end - 1];
|
|
92
|
+
index = end;
|
|
93
|
+
}
|
|
94
|
+
const props = [];
|
|
95
|
+
if (line[index] === "{") {
|
|
96
|
+
const scanned = readProps(line, index);
|
|
97
|
+
if (!scanned) return void 0;
|
|
98
|
+
props.push(...scanned.props);
|
|
99
|
+
index = scanned.end;
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
end: index,
|
|
103
|
+
props,
|
|
104
|
+
...content ? { content } : {}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** Reads `{props}` at `at`, keeping the values of text props. */
|
|
108
|
+
function readProps(line, at) {
|
|
109
|
+
const scanned = scanAttributes(line, at);
|
|
110
|
+
if (!scanned) return void 0;
|
|
111
|
+
const props = [];
|
|
112
|
+
for (const attribute of scanned.attributes) if (attribute.value && isText(attribute.name, line.slice(attribute.value.start, attribute.value.end))) props.push({
|
|
113
|
+
name: attribute.name,
|
|
114
|
+
...attribute.value
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
end: scanned.end,
|
|
118
|
+
props
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const INLINE_NAME = /(?<![\p{L}\p{N}_:\\/]):[A-Za-z][\w-]*(?=[[{])/uy;
|
|
122
|
+
/** A link or image target after its `]`, with an optional title: `(/map "Map")`. */
|
|
123
|
+
const LINK_TARGET = /(?<=\])\((?:[^()\s]|\([^()\s]*\))*(?:\s+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\)))?\s*\)/g;
|
|
124
|
+
const LINK_TARGET_AT = new RegExp(LINK_TARGET.source, "y");
|
|
125
|
+
/**
|
|
126
|
+
* Blanks inline markup in `line` between `from` and `to`, left to right: components such as
|
|
127
|
+
* `:badge[v4.4]{color="info"}`, whose `[text]` is kept and scanned in turn, and `{attrs}` right after a
|
|
128
|
+
* `]` or a link target, as in `[map](/map){target="_blank"}`. Link targets are skipped, as the CLI blanks
|
|
129
|
+
* them anyway. Returns the line and the text props it found.
|
|
130
|
+
*/
|
|
131
|
+
function maskInline(line, blank, from = 0, to = line.length) {
|
|
132
|
+
let out = line;
|
|
133
|
+
const props = [];
|
|
134
|
+
const scan = (start, end) => {
|
|
135
|
+
let attrsAt = -1;
|
|
136
|
+
for (let index = start; index < end;) {
|
|
137
|
+
INLINE_NAME.lastIndex = index;
|
|
138
|
+
const name = INLINE_NAME.exec(line);
|
|
139
|
+
const component = name && readComponent(line, index + name[0].length);
|
|
140
|
+
if (component && component.end <= end) {
|
|
141
|
+
out = out.slice(0, index) + blank(out.slice(index, component.end)) + out.slice(component.end);
|
|
142
|
+
props.push(...component.props);
|
|
143
|
+
if (component.content) {
|
|
144
|
+
const [open, close] = component.content;
|
|
145
|
+
out = out.slice(0, open) + line.slice(open, close) + out.slice(close);
|
|
146
|
+
scan(open, close);
|
|
147
|
+
}
|
|
148
|
+
index = component.end;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const char = line[index];
|
|
152
|
+
if (char === "{" && index === attrsAt) {
|
|
153
|
+
const attrs = readProps(line, index);
|
|
154
|
+
if (attrs && attrs.end <= end) {
|
|
155
|
+
out = out.slice(0, index) + blank(out.slice(index, attrs.end)) + out.slice(attrs.end);
|
|
156
|
+
props.push(...attrs.props);
|
|
157
|
+
index = attrs.end;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (char === "(" && line[index - 1] === "]") {
|
|
162
|
+
LINK_TARGET_AT.lastIndex = index;
|
|
163
|
+
const target = LINK_TARGET_AT.exec(line);
|
|
164
|
+
if (target && index + target[0].length <= end) {
|
|
165
|
+
index += target[0].length;
|
|
166
|
+
attrsAt = index;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (char === "\\") index++;
|
|
171
|
+
else if (char === "]") attrsAt = index + 1;
|
|
172
|
+
index++;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
scan(from, to);
|
|
176
|
+
return {
|
|
177
|
+
line: out,
|
|
178
|
+
props
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** Reads a YAML props block's top-level `key: value` lines, keeping single-line scalars of text props. */
|
|
182
|
+
function yamlProps(line, offset, indent) {
|
|
183
|
+
const entry = YAML_ENTRY.exec(line);
|
|
184
|
+
if (!entry) return void 0;
|
|
185
|
+
const [, space, name, raw] = entry;
|
|
186
|
+
indent.value ??= space.length;
|
|
187
|
+
if (space.length !== indent.value || raw === "" || /^[|>[{&*!]/.test(raw)) return void 0;
|
|
188
|
+
let start = line.indexOf(raw, space.length + name.length + 1);
|
|
189
|
+
let value = raw;
|
|
190
|
+
if (raw[0] === "\"" || raw[0] === "'") {
|
|
191
|
+
const close = raw.indexOf(raw[0], 1);
|
|
192
|
+
if (close === -1 || raw.slice(close + 1).replace(/[ \t]+#.*$/, "") !== "") return void 0;
|
|
193
|
+
start++;
|
|
194
|
+
value = raw.slice(1, close);
|
|
195
|
+
} else value = raw.replace(/[ \t]+#.*$/, "");
|
|
196
|
+
if (!isText(name, value)) return void 0;
|
|
197
|
+
return {
|
|
198
|
+
name,
|
|
199
|
+
start: offset + start,
|
|
200
|
+
end: offset + start + value.length,
|
|
201
|
+
text: value
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Blanks Comark component markup in Markdown whose fences and code spans are already blanked, keeping
|
|
206
|
+
* every offset.
|
|
207
|
+
* Returns the lines with markup blanked and the text props to check on their own.
|
|
208
|
+
*/
|
|
209
|
+
function maskComark(lines, blank) {
|
|
210
|
+
const out = [...lines];
|
|
211
|
+
const props = [];
|
|
212
|
+
const offsets = [];
|
|
213
|
+
let offset = 0;
|
|
214
|
+
for (const line of lines) {
|
|
215
|
+
offsets.push(offset);
|
|
216
|
+
offset += line.length + 1;
|
|
217
|
+
}
|
|
218
|
+
let depth = 0;
|
|
219
|
+
for (let index = 0; index < lines.length; index++) {
|
|
220
|
+
const line = lines[index];
|
|
221
|
+
if (CLOSER.test(line)) {
|
|
222
|
+
out[index] = blank(line);
|
|
223
|
+
depth = Math.max(0, depth - 1);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const at = (spans) => props.push(...spans.map((span) => ({
|
|
227
|
+
name: span.name,
|
|
228
|
+
start: offsets[index] + span.start,
|
|
229
|
+
end: offsets[index] + span.end,
|
|
230
|
+
text: line.slice(span.start, span.end)
|
|
231
|
+
})));
|
|
232
|
+
const slot = depth > 0 ? SLOT.exec(line) : null;
|
|
233
|
+
if (slot) {
|
|
234
|
+
const end = slot[0].length;
|
|
235
|
+
const attrs = line[end] === "{" ? readProps(line, end) : {
|
|
236
|
+
end,
|
|
237
|
+
props: []
|
|
238
|
+
};
|
|
239
|
+
if (attrs && line.slice(attrs.end).trim() === "") {
|
|
240
|
+
out[index] = blank(line);
|
|
241
|
+
at(attrs.props);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const opener = OPENER.exec(line);
|
|
246
|
+
const read = opener ? readComponent(line, opener[0].length) : void 0;
|
|
247
|
+
if (!opener || !read || line.slice(read.end).trim() !== "") {
|
|
248
|
+
const inline = BARE.test(line) ? {
|
|
249
|
+
line: blank(line),
|
|
250
|
+
props: []
|
|
251
|
+
} : maskInline(line, blank);
|
|
252
|
+
out[index] = inline.line;
|
|
253
|
+
at(inline.props);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
out[index] = blank(line);
|
|
257
|
+
at(read.props);
|
|
258
|
+
if (read.content) {
|
|
259
|
+
const [start, end] = read.content;
|
|
260
|
+
const inline = maskInline(line, blank, start, end);
|
|
261
|
+
at(inline.props);
|
|
262
|
+
props.push({
|
|
263
|
+
name: "",
|
|
264
|
+
start: offsets[index] + start,
|
|
265
|
+
end: offsets[index] + end,
|
|
266
|
+
text: inline.line.slice(start, end).replace(LINK_TARGET, blank)
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
depth++;
|
|
270
|
+
if (!RULE.test(lines[index + 1] ?? "")) continue;
|
|
271
|
+
let close = index + 2;
|
|
272
|
+
while (close < lines.length && !RULE.test(lines[close]) && !CLOSER.test(lines[close])) close++;
|
|
273
|
+
if (close >= lines.length || !RULE.test(lines[close])) continue;
|
|
274
|
+
const indent = {};
|
|
275
|
+
for (let row = index + 1; row <= close; row++) {
|
|
276
|
+
out[row] = blank(lines[row]);
|
|
277
|
+
const prop = yamlProps(lines[row], offsets[row], indent);
|
|
278
|
+
if (prop) props.push(prop);
|
|
279
|
+
}
|
|
280
|
+
index = close;
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
lines: out,
|
|
284
|
+
props
|
|
285
|
+
};
|
|
286
|
+
}
|
|
12
287
|
// src/cli.ts
|
|
13
288
|
/** Codes of the errors the CLI prints itself, next to the corpus's `GLOSSLINT_C00x`. */
|
|
14
289
|
const CLI_ERRORS = {
|
|
@@ -39,12 +314,14 @@ var CliError = class extends Error {
|
|
|
39
314
|
code;
|
|
40
315
|
why;
|
|
41
316
|
fix;
|
|
317
|
+
term;
|
|
42
318
|
config;
|
|
43
319
|
constructor(options) {
|
|
44
320
|
super(options.why);
|
|
45
321
|
this.code = options.code;
|
|
46
322
|
this.why = options.why;
|
|
47
323
|
this.fix = options.fix;
|
|
324
|
+
if (options.term !== void 0) this.term = options.term;
|
|
48
325
|
if (options.config !== void 0) this.config = options.config;
|
|
49
326
|
}
|
|
50
327
|
};
|
|
@@ -59,24 +336,64 @@ const toPosix = (path) => path.split(sep).join("/");
|
|
|
59
336
|
function displayPath(cwd, file) {
|
|
60
337
|
return toPosix(relative(cwd, file)) || ".";
|
|
61
338
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
339
|
+
/**
|
|
340
|
+
* Finds the corpus: `config`, else `glossary`, else `glosslint.config.{ts,mts,js,mjs}` in `cwd`, else
|
|
341
|
+
* `.agents/glossary.md` in `cwd`.
|
|
342
|
+
*/
|
|
343
|
+
async function loadCorpus(cwd, options = {}) {
|
|
344
|
+
const { config, glossary } = options;
|
|
345
|
+
if (config !== void 0 && glossary !== void 0) throw usage("--config and --glossary both name the corpus.", "Pass one of them: --glossary for a glossary file, --config for a config.");
|
|
346
|
+
if (config !== void 0) return importConfig(cwd, resolve(cwd, config), config);
|
|
347
|
+
if (glossary !== void 0) return loadGlossary(cwd, glossary, true);
|
|
348
|
+
const found = CONFIG_FILES.map((name) => resolve(cwd, name)).find((path) => existsSync(path));
|
|
349
|
+
if (found) return importConfig(cwd, found);
|
|
350
|
+
if (existsSync(resolve(cwd, ".agents/glossary.md"))) return loadGlossary(cwd, DEFAULT_GLOSSARY, false);
|
|
351
|
+
throw new CliError({
|
|
352
|
+
code: CLI_ERRORS.noConfig,
|
|
353
|
+
why: `No ${DEFAULT_GLOSSARY} and no glosslint.config.ts, .mts, .js or .mjs in the current folder.`,
|
|
354
|
+
fix: `Create ${DEFAULT_GLOSSARY} with \`# Glossary\` and a \`## Term\` section per term, or pass --glossary <path> or --config <path>.`
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
/** `G004` to `G008`: the problems only a person can fix, which leave no corpus. */
|
|
358
|
+
function leavesNoCorpus(problem) {
|
|
359
|
+
return !problem.fixable && problem.code !== GLOSSARY_PROBLEMS.failingExample;
|
|
360
|
+
}
|
|
361
|
+
function missingGlossary(cwd, file, given) {
|
|
362
|
+
const shown = displayPath(cwd, file);
|
|
363
|
+
return new CliError({
|
|
364
|
+
code: CLI_ERRORS.noConfig,
|
|
365
|
+
why: given ? `The glossary "${shown}" does not exist.` : `No glossary at ${shown}.`,
|
|
366
|
+
fix: given ? "Check the path given to --glossary." : `Create ${shown} with \`# Glossary\` and a \`## Term\` section per term, or pass --glossary <path>.`
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
/** Reads a glossary file as a corpus, or throws its first problem that leaves no corpus. */
|
|
370
|
+
function loadGlossary(cwd, path, given) {
|
|
371
|
+
const file = resolve(cwd, path);
|
|
372
|
+
if (!existsSync(file)) throw missingGlossary(cwd, file, given);
|
|
373
|
+
const { problems, corpus } = readGlossary(readFileSync(file, "utf8"));
|
|
374
|
+
if (corpus) return {
|
|
375
|
+
corpus,
|
|
376
|
+
file,
|
|
377
|
+
from: "glossary"
|
|
378
|
+
};
|
|
379
|
+
const blocking = problems.filter(leavesNoCorpus);
|
|
380
|
+
const first = blocking[0];
|
|
381
|
+
const doctor = given ? `glosslint doctor --glossary ${path}` : "glosslint doctor";
|
|
382
|
+
const more = blocking.length > 1 ? ` ${count(blocking.length - 1, "more problem")}: run \`${doctor}\` to list them.` : "";
|
|
383
|
+
throw new CliError({
|
|
384
|
+
code: first.code,
|
|
385
|
+
why: `${displayPath(cwd, file)}:${first.line}: ${first.why}`,
|
|
386
|
+
fix: `${first.fix}${more}`,
|
|
387
|
+
...first.term === void 0 ? {} : { term: first.term }
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
/** Imports a config and returns its default export, which must be a corpus. */
|
|
391
|
+
async function importConfig(cwd, file, given) {
|
|
392
|
+
if (given !== void 0 && !existsSync(file)) throw new CliError({
|
|
393
|
+
code: CLI_ERRORS.noConfig,
|
|
394
|
+
why: `The config "${given}" does not exist.`,
|
|
395
|
+
fix: "Check the path given to --config."
|
|
396
|
+
});
|
|
80
397
|
const shown = displayPath(cwd, file);
|
|
81
398
|
let loaded;
|
|
82
399
|
try {
|
|
@@ -98,7 +415,8 @@ async function loadCorpus(cwd, config) {
|
|
|
98
415
|
});
|
|
99
416
|
return {
|
|
100
417
|
corpus: loaded.default,
|
|
101
|
-
file
|
|
418
|
+
file,
|
|
419
|
+
from: "config"
|
|
102
420
|
};
|
|
103
421
|
}
|
|
104
422
|
/** Blanks a span with spaces, keeping line breaks, so every offset and line stays where it was. */
|
|
@@ -109,7 +427,6 @@ const FRONTMATTER = /^(?:---|\+\+\+)\r?\n[\s\S]*?\r?\n(?:---|\+\+\+|\.\.\.)[ \t]
|
|
|
109
427
|
const FENCE = /^[ \t]*((?:>[ \t]?)*)[ \t]*((?:[-*+]|\d{1,9}[.)])[ \t]+)?(`{3,}|~{3,})([^\n]*)$/;
|
|
110
428
|
const HTML_COMMENT = /<!--[\s\S]*?(?:-->|$)/g;
|
|
111
429
|
const INLINE_CODE = /(?<!`)(`+)(?!`)(?:(?!\r?\n[ \t]*\r?\n)[\s\S])*?[^`]\1(?!`)/g;
|
|
112
|
-
const LINK_TARGET = /(?<=\])\((?:[^()\s]|\([^()\s]*\))*(?:\s+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\)))?\s*\)/g;
|
|
113
430
|
const LINK_REFERENCE = /(?<=\])\[[^\]\n]*\]/g;
|
|
114
431
|
const REFERENCE_DEFINITION = /^ {0,3}\[[^\]\n]+\]:[ \t]*\S.*$/gm;
|
|
115
432
|
const AUTOLINK = /<(?:[a-z][a-z\d+.-]*:[^\s<>]*|[^\s<>@]+@[^\s<>]+)>/gi;
|
|
@@ -120,9 +437,13 @@ const LIST_ITEM = /^ {0,3}(?:>[ \t]?)*(?:[-*+]|\d{1,9}[.)])[ \t]/;
|
|
|
120
437
|
/**
|
|
121
438
|
* Blanks what is not prose in Markdown, keeping every offset: frontmatter, fenced code, HTML
|
|
122
439
|
* comments and tags, inline code, link targets and references, and the markers of headings, lists
|
|
123
|
-
* and quotes. In MDX, `import` and `export` lines too.
|
|
440
|
+
* and quotes. In MDX, `import` and `export` lines too; in other Markdown, Comark component markup.
|
|
124
441
|
*/
|
|
125
442
|
function maskMarkdown(text, options = {}) {
|
|
443
|
+
return maskMarkdownParts(text, options).masked;
|
|
444
|
+
}
|
|
445
|
+
/** {@link maskMarkdown}, plus the Comark text props it blanked, which are checked on their own. */
|
|
446
|
+
function maskMarkdownParts(text, options) {
|
|
126
447
|
let out = text.replace(FRONTMATTER, blank);
|
|
127
448
|
const lines = out.split("\n");
|
|
128
449
|
let fence;
|
|
@@ -141,19 +462,26 @@ function maskMarkdown(text, options = {}) {
|
|
|
141
462
|
lines[index] = blank(line);
|
|
142
463
|
} else if (options.mdx && /^(?:import|export)\s/.test(line)) lines[index] = blank(line);
|
|
143
464
|
}
|
|
144
|
-
out = lines.join("\n");
|
|
145
|
-
|
|
146
|
-
|
|
465
|
+
out = lines.join("\n").replace(HTML_COMMENT, blank).replace(INLINE_CODE, blank);
|
|
466
|
+
let props = [];
|
|
467
|
+
if (!options.mdx) {
|
|
468
|
+
const comark = maskComark(out.split("\n"), blank);
|
|
469
|
+
out = comark.lines.join("\n");
|
|
470
|
+
props = comark.props;
|
|
471
|
+
}
|
|
147
472
|
out = out.replace(REFERENCE_DEFINITION, blank);
|
|
148
473
|
out = out.replace(LINK_TARGET, blank);
|
|
149
474
|
out = out.replace(LINK_REFERENCE, blank);
|
|
150
475
|
out = out.replace(AUTOLINK, blank);
|
|
151
476
|
out = out.replace(TAG, blank);
|
|
152
|
-
return
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
477
|
+
return {
|
|
478
|
+
masked: out.split("\n").map((line) => {
|
|
479
|
+
let masked = line.replace(LINE_PREFIX, blank);
|
|
480
|
+
if (HEADING.test(line)) masked = masked.replace(/[ \t]#+[ \t]*(?=\r?$)/, blank);
|
|
481
|
+
return masked;
|
|
482
|
+
}).join("\n"),
|
|
483
|
+
props
|
|
484
|
+
};
|
|
157
485
|
}
|
|
158
486
|
const TABLE_DELIMITER = /^\s*\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?\s*\r?$/;
|
|
159
487
|
/** Splits text into the pieces checked one at a time: headings, table cells, list items and paragraphs. */
|
|
@@ -217,7 +545,10 @@ function summarize(term) {
|
|
|
217
545
|
/** Checks one file's text. Markdown is masked first, so code and link targets are never reported. */
|
|
218
546
|
function checkFileText(matcher, text, file, options = {}) {
|
|
219
547
|
const markdown = options.markdown ?? false;
|
|
220
|
-
const masked = markdown ?
|
|
548
|
+
const { masked, props } = markdown ? maskMarkdownParts(text, { mdx: options.mdx ?? false }) : {
|
|
549
|
+
masked: text,
|
|
550
|
+
props: []
|
|
551
|
+
};
|
|
221
552
|
const lineStarts = [0];
|
|
222
553
|
for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1)) lineStarts.push(index + 1);
|
|
223
554
|
const lineOf = (offset) => {
|
|
@@ -232,22 +563,35 @@ function checkFileText(matcher, text, file, options = {}) {
|
|
|
232
563
|
};
|
|
233
564
|
const lineEnd = (line) => (lineStarts[line + 1] ?? text.length + 1) - 1;
|
|
234
565
|
const findings = [];
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
566
|
+
const all = [...blocks(text, masked, markdown), ...props.map((prop) => ({
|
|
567
|
+
start: prop.start,
|
|
568
|
+
end: prop.end,
|
|
569
|
+
kind: kindOf({
|
|
570
|
+
text: prop.text,
|
|
571
|
+
name: prop.name,
|
|
572
|
+
shown: true
|
|
573
|
+
}),
|
|
574
|
+
text: prop.text
|
|
575
|
+
}))].sort((a, b) => a.start - b.start);
|
|
576
|
+
for (const block of all) {
|
|
577
|
+
const read = block.text ?? masked.slice(block.start, block.end);
|
|
578
|
+
for (const finding of matcher.check(read, block.kind)) {
|
|
579
|
+
const start = block.start + finding.start;
|
|
580
|
+
const end = block.start + finding.end;
|
|
581
|
+
const line = lineOf(start);
|
|
582
|
+
const lineStart = lineStarts[line];
|
|
583
|
+
findings.push(toCliFinding(finding, {
|
|
584
|
+
file,
|
|
585
|
+
text,
|
|
586
|
+
start,
|
|
587
|
+
end,
|
|
588
|
+
line,
|
|
589
|
+
lineStart,
|
|
590
|
+
lineEnd: lineEnd(lineOf(end))
|
|
591
|
+
}));
|
|
592
|
+
}
|
|
249
593
|
}
|
|
250
|
-
return findings;
|
|
594
|
+
return findings.sort((a, b) => a.start - b.start);
|
|
251
595
|
}
|
|
252
596
|
function toCliFinding(finding, at) {
|
|
253
597
|
const { fix, term, start: _start, end: _end, ...rest } = finding;
|
|
@@ -315,9 +659,9 @@ async function runCheck(paths, options, io) {
|
|
|
315
659
|
const format = options.format ?? "text";
|
|
316
660
|
if (format !== "text" && format !== "json") throw usage(`--format is "${format}".`, "Use --format text or --format json.");
|
|
317
661
|
const extensions = parseExtensions(options.ext);
|
|
318
|
-
const
|
|
319
|
-
const matcher = createMatcher(corpus);
|
|
320
|
-
const files = await collectFiles(paths, io.cwd, extensions);
|
|
662
|
+
const loaded = await loadCorpus(io.cwd, options);
|
|
663
|
+
const matcher = createMatcher(loaded.corpus);
|
|
664
|
+
const files = (await collectFiles(paths, io.cwd, extensions)).filter((file) => loaded.from !== "glossary" || file !== loaded.file);
|
|
321
665
|
const findings = [];
|
|
322
666
|
for (const file of files) {
|
|
323
667
|
const extension = extname(file).toLowerCase();
|
|
@@ -359,7 +703,7 @@ function failureLines(failure) {
|
|
|
359
703
|
const count = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
360
704
|
/** `glosslint test`: checks every term's good and bad examples. Exits 1 when one fails. */
|
|
361
705
|
async function runTest(options, io) {
|
|
362
|
-
const { corpus } = await loadCorpus(io.cwd, options
|
|
706
|
+
const { corpus } = await loadCorpus(io.cwd, options);
|
|
363
707
|
const color = useColor(options, io);
|
|
364
708
|
const tested = corpus.terms.filter((term) => (term.examples?.good?.length ?? 0) + (term.examples?.bad?.length ?? 0) > 0);
|
|
365
709
|
if (tested.length === 0) {
|
|
@@ -392,16 +736,76 @@ async function runTest(options, io) {
|
|
|
392
736
|
async function runPrint(options, io) {
|
|
393
737
|
const format = options.format ?? "md";
|
|
394
738
|
if (format !== "md" && format !== "json") throw usage(`--format is "${format}".`, "Use --format md or --format json.");
|
|
395
|
-
const { corpus } = await loadCorpus(io.cwd, options
|
|
739
|
+
const { corpus } = await loadCorpus(io.cwd, options);
|
|
396
740
|
io.stdout(printCorpus(corpus, format));
|
|
397
741
|
return 0;
|
|
398
742
|
}
|
|
399
743
|
/** `glosslint explain`: what the corpus says about a word or phrase. Exits 1 when no term matches. */
|
|
400
744
|
async function runExplain(query, options, io) {
|
|
401
|
-
const { corpus } = await loadCorpus(io.cwd, options
|
|
745
|
+
const { corpus } = await loadCorpus(io.cwd, options);
|
|
402
746
|
io.stdout(`${explain(corpus, query)}\n`);
|
|
403
747
|
return explainMatches(corpus, query).length > 0 ? 0 : 1;
|
|
404
748
|
}
|
|
749
|
+
function formatProblem(problem, color) {
|
|
750
|
+
const lines = [["fix", problem.fix]];
|
|
751
|
+
if (problem.term && !problem.why.includes(`"${problem.term}"`)) lines.unshift(["term", problem.term]);
|
|
752
|
+
const branches = lines.map(([label, text], index) => {
|
|
753
|
+
const branch = index === lines.length - 1 ? "╰▶" : "├▶";
|
|
754
|
+
return `${paint(color, "2", branch)} ${paint(color, "1", `${label}:`)} ${text}`;
|
|
755
|
+
});
|
|
756
|
+
const where = paint(color, "36", `${problem.file}:${problem.line}`);
|
|
757
|
+
return [`${paint(color, "1;31", `[${problem.code}]`)} ${where}: ${problem.why}`, ...branches].join("\n");
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* `glosslint doctor`: reports the problems of the glossary file, and with `fix` rewrites it in canonical
|
|
761
|
+
* form when no problem would drop text. Exits 0 with no problems left, 1 with problems, 2 with no file.
|
|
762
|
+
*/
|
|
763
|
+
async function runDoctor(options, io) {
|
|
764
|
+
const format = options.format ?? "text";
|
|
765
|
+
if (format !== "text" && format !== "json") throw usage(`--format is "${format}".`, "Use --format text or --format json.");
|
|
766
|
+
const file = resolve(io.cwd, options.glossary ?? ".agents/glossary.md");
|
|
767
|
+
if (!existsSync(file)) throw missingGlossary(io.cwd, file, options.glossary !== void 0);
|
|
768
|
+
const shown = displayPath(io.cwd, file);
|
|
769
|
+
let result = readGlossary(readFileSync(file, "utf8"));
|
|
770
|
+
let fixed = 0;
|
|
771
|
+
if (options.fix && result.fixed !== void 0) {
|
|
772
|
+
fixed = result.problems.filter((problem) => problem.fixable).length;
|
|
773
|
+
writeFileSync(file, result.fixed);
|
|
774
|
+
result = readGlossary(result.fixed);
|
|
775
|
+
}
|
|
776
|
+
const problems = result.problems.map((problem) => ({
|
|
777
|
+
file: shown,
|
|
778
|
+
...problem
|
|
779
|
+
}));
|
|
780
|
+
const fixable = problems.filter((problem) => problem.fixable).length;
|
|
781
|
+
if (format === "json") {
|
|
782
|
+
const report = {
|
|
783
|
+
problems,
|
|
784
|
+
summary: {
|
|
785
|
+
file: shown,
|
|
786
|
+
problems: problems.length,
|
|
787
|
+
fixable,
|
|
788
|
+
fixed
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
io.stdout(`${JSON.stringify(report, null, 2)}\n`);
|
|
792
|
+
return problems.length > 0 ? 1 : 0;
|
|
793
|
+
}
|
|
794
|
+
const color = useColor(options, io);
|
|
795
|
+
const summary = [];
|
|
796
|
+
if (fixed > 0) summary.push(`Fixed ${count(fixed, "problem")} in ${shown}.`);
|
|
797
|
+
if (problems.length === 0) {
|
|
798
|
+
if (fixed === 0) summary.push(`No problems in ${shown}.`);
|
|
799
|
+
} else if (fixed > 0) summary.push(`${count(problems.length, "problem")} left.`);
|
|
800
|
+
else {
|
|
801
|
+
const canFix = result.fixed !== void 0 ? `, ${fixable} fixable with --fix` : "";
|
|
802
|
+
summary.push(`${count(problems.length, "problem")} in ${shown}${canFix}.`);
|
|
803
|
+
if (options.fix && fixable > 0) summary.push("The file is unchanged: --fix would drop text, so fix the other problems first.");
|
|
804
|
+
}
|
|
805
|
+
const blocks = problems.map((problem) => formatProblem(problem, color));
|
|
806
|
+
io.stdout(`${[...blocks, summary.join(" ")].join("\n\n")}\n`);
|
|
807
|
+
return problems.length > 0 ? 1 : 0;
|
|
808
|
+
}
|
|
405
809
|
/** Prints a config or usage error as a tree on stderr. */
|
|
406
810
|
function printError(error, io, color) {
|
|
407
811
|
const branches = [];
|
|
@@ -428,19 +832,23 @@ async function main(argv, io = defaultIo()) {
|
|
|
428
832
|
let run;
|
|
429
833
|
let color = io.color ?? false;
|
|
430
834
|
const configOption = "Config file (default: glosslint.config.{ts,mts,js,mjs})";
|
|
835
|
+
const glossaryOption = `Glossary file, read when there is no config (default: ${DEFAULT_GLOSSARY})`;
|
|
431
836
|
const colorOption = "Color the output (default: when writing to a terminal)";
|
|
432
|
-
cli.command("[...paths]", "Check Markdown and text files against the corpus").alias("check").option("--config <path>", configOption).option("--format <format>", "Output format: text or json", { default: "text" }).option("--ext <list>", "Extensions read in folders, comma-separated (default: md,mdx,txt)").option("--color", colorOption).action((paths, options) => {
|
|
837
|
+
cli.command("[...paths]", "Check Markdown and text files against the corpus").alias("check").option("--glossary <path>", glossaryOption).option("--config <path>", configOption).option("--format <format>", "Output format: text or json", { default: "text" }).option("--ext <list>", "Extensions read in folders, comma-separated (default: md,mdx,txt)").option("--color", colorOption).action((paths, options) => {
|
|
433
838
|
run = () => runCheck(paths, options, io);
|
|
434
839
|
});
|
|
435
|
-
cli.command("test", "Check every term's good and bad examples").option("--config <path>", configOption).option("--color", colorOption).action((options) => {
|
|
840
|
+
cli.command("test", "Check every term's good and bad examples").option("--glossary <path>", glossaryOption).option("--config <path>", configOption).option("--color", colorOption).action((options) => {
|
|
436
841
|
run = () => runTest(options, io);
|
|
437
842
|
});
|
|
438
|
-
cli.command("print", "Print the corpus as a Markdown glossary or JSON").option("--config <path>", configOption).option("--format <format>", "Output format: md or json", { default: "md" }).action((options) => {
|
|
843
|
+
cli.command("print", "Print the corpus as a Markdown glossary or JSON").option("--glossary <path>", glossaryOption).option("--config <path>", configOption).option("--format <format>", "Output format: md or json", { default: "md" }).action((options) => {
|
|
439
844
|
run = () => runPrint(options, io);
|
|
440
845
|
});
|
|
441
|
-
cli.command("explain <query>", "Show what the corpus says about a word or phrase").option("--config <path>", configOption).action((query, options) => {
|
|
846
|
+
cli.command("explain <query>", "Show what the corpus says about a word or phrase").option("--glossary <path>", glossaryOption).option("--config <path>", configOption).action((query, options) => {
|
|
442
847
|
run = () => runExplain(String(query), options, io);
|
|
443
848
|
});
|
|
849
|
+
cli.command("doctor", "Report the glossary file's problems, and fix the ones it can with --fix").option("--glossary <path>", `Glossary file (default: ${DEFAULT_GLOSSARY})`).option("--fix", "Rewrite the file in canonical form, unless that would drop text").option("--format <format>", "Output format: text or json", { default: "text" }).option("--color", colorOption).action((options) => {
|
|
850
|
+
run = () => runDoctor(options, io);
|
|
851
|
+
});
|
|
444
852
|
cli.help();
|
|
445
853
|
cli.version(version);
|
|
446
854
|
try {
|
|
@@ -481,4 +889,4 @@ function isBin() {
|
|
|
481
889
|
}
|
|
482
890
|
}
|
|
483
891
|
if (isBin()) process.exitCode = await main(process.argv.slice(2));
|
|
484
|
-
export { CLI_ERRORS, checkFileText, collectFiles, loadCorpus, main, maskMarkdown, runCheck, runExplain, runPrint, runTest };
|
|
892
|
+
export { CLI_ERRORS, checkFileText, collectFiles, loadCorpus, main, maskMarkdown, runCheck, runDoctor, runExplain, runPrint, runTest };
|