glosslint 0.0.0 → 0.0.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/LICENSE +120 -0
- package/NOTICE +31 -0
- package/README.md +81 -2
- package/dist/cli.d.mts +67 -0
- package/dist/cli.mjs +484 -0
- package/dist/eslint.d.mts +26 -0
- package/dist/eslint.mjs +792 -0
- package/dist/index.d.mts +95 -0
- package/dist/index.mjs +459 -0
- package/dist/matcher-Xz4vspsE.mjs +715 -0
- package/dist/presets/inclusive.d.mts +3 -0
- package/dist/presets/inclusive.mjs +520 -0
- package/dist/presets/tech.d.mts +3 -0
- package/dist/presets/tech.mjs +2401 -0
- package/dist/tools-BrdtyK9t.mjs +275 -0
- package/dist/types-CBQGgzID.d.mts +190 -0
- package/dist/x18n.d.mts +23 -0
- package/dist/x18n.mjs +156 -0
- package/package.json +120 -2
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { c as toJson, i as testCorpus, n as explainMatches, r as printCorpus, s as formatFindings, t as explain } from "./tools-BrdtyK9t.mjs";
|
|
3
|
+
import { a as kindOf, n as createMatcher } from "./matcher-Xz4vspsE.mjs";
|
|
4
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
5
|
+
import { extname, relative, resolve, sep } from "node:path";
|
|
6
|
+
import process from "node:process";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
import { cac } from "cac";
|
|
9
|
+
import { escapePath, glob, isDynamicPattern } from "tinyglobby";
|
|
10
|
+
// package.json
|
|
11
|
+
var version = "0.0.1";
|
|
12
|
+
// src/cli.ts
|
|
13
|
+
/** Codes of the errors the CLI prints itself, next to the corpus's `GLOSSLINT_C00x`. */
|
|
14
|
+
const CLI_ERRORS = {
|
|
15
|
+
noConfig: "GLOSSLINT_C009",
|
|
16
|
+
loadConfig: "GLOSSLINT_C010",
|
|
17
|
+
notCorpus: "GLOSSLINT_C008",
|
|
18
|
+
usage: "GLOSSLINT_USAGE",
|
|
19
|
+
failed: "GLOSSLINT_ERROR"
|
|
20
|
+
};
|
|
21
|
+
const CONFIG_FILES = [
|
|
22
|
+
"glosslint.config.ts",
|
|
23
|
+
"glosslint.config.mts",
|
|
24
|
+
"glosslint.config.js",
|
|
25
|
+
"glosslint.config.mjs"
|
|
26
|
+
];
|
|
27
|
+
const DEFAULT_EXTENSIONS = [
|
|
28
|
+
"md",
|
|
29
|
+
"mdx",
|
|
30
|
+
"txt"
|
|
31
|
+
];
|
|
32
|
+
const MARKDOWN = /* @__PURE__ */ new Set([
|
|
33
|
+
".md",
|
|
34
|
+
".mdx",
|
|
35
|
+
".markdown"
|
|
36
|
+
]);
|
|
37
|
+
/** An error the CLI prints as a tree and exits 2 on: a bad config or a bad command line. */
|
|
38
|
+
var CliError = class extends Error {
|
|
39
|
+
code;
|
|
40
|
+
why;
|
|
41
|
+
fix;
|
|
42
|
+
config;
|
|
43
|
+
constructor(options) {
|
|
44
|
+
super(options.why);
|
|
45
|
+
this.code = options.code;
|
|
46
|
+
this.why = options.why;
|
|
47
|
+
this.fix = options.fix;
|
|
48
|
+
if (options.config !== void 0) this.config = options.config;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function isConfigError(error) {
|
|
52
|
+
return error instanceof Error && error.name === "GlosslintConfigError" && typeof error.code === "string" && typeof error.fix === "string";
|
|
53
|
+
}
|
|
54
|
+
function isCorpus(value) {
|
|
55
|
+
return typeof value === "object" && value !== null && value.kind === "glosslint.corpus" && Array.isArray(value.terms) && Array.isArray(value.allow);
|
|
56
|
+
}
|
|
57
|
+
const paint = (on, code, text) => on ? `\u001B[${code}m${text}\u001B[0m` : text;
|
|
58
|
+
const toPosix = (path) => path.split(sep).join("/");
|
|
59
|
+
function displayPath(cwd, file) {
|
|
60
|
+
return toPosix(relative(cwd, file)) || ".";
|
|
61
|
+
}
|
|
62
|
+
/** Finds and imports the config, and returns its default export, which must be a corpus. */
|
|
63
|
+
async function loadCorpus(cwd, config) {
|
|
64
|
+
let file;
|
|
65
|
+
if (config !== void 0) {
|
|
66
|
+
file = resolve(cwd, config);
|
|
67
|
+
if (!existsSync(file)) throw new CliError({
|
|
68
|
+
code: CLI_ERRORS.noConfig,
|
|
69
|
+
why: `The config "${config}" does not exist.`,
|
|
70
|
+
fix: "Check the path given to --config."
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
file = CONFIG_FILES.map((name) => resolve(cwd, name)).find((path) => existsSync(path));
|
|
74
|
+
if (!file) throw new CliError({
|
|
75
|
+
code: CLI_ERRORS.noConfig,
|
|
76
|
+
why: "No glosslint.config.ts, .mts, .js or .mjs in the current folder.",
|
|
77
|
+
fix: "Create glosslint.config.ts with `export default defineCorpus({ … })`, or pass --config <path>."
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const shown = displayPath(cwd, file);
|
|
81
|
+
let loaded;
|
|
82
|
+
try {
|
|
83
|
+
loaded = await import(pathToFileURL(file).href);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (isConfigError(error)) throw Object.assign(error, { config: shown });
|
|
86
|
+
throw new CliError({
|
|
87
|
+
code: CLI_ERRORS.loadConfig,
|
|
88
|
+
why: `Could not load ${shown}: ${error instanceof Error ? error.message : String(error)}`,
|
|
89
|
+
fix: "Fix the error in the config, then run glosslint again.",
|
|
90
|
+
config: shown
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (!isCorpus(loaded.default)) throw new CliError({
|
|
94
|
+
code: CLI_ERRORS.notCorpus,
|
|
95
|
+
why: `${shown} does not export a corpus as its default export.`,
|
|
96
|
+
fix: "End the config with `export default defineCorpus({ … })`.",
|
|
97
|
+
config: shown
|
|
98
|
+
});
|
|
99
|
+
return {
|
|
100
|
+
corpus: loaded.default,
|
|
101
|
+
file
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** Blanks a span with spaces, keeping line breaks, so every offset and line stays where it was. */
|
|
105
|
+
function blank(text) {
|
|
106
|
+
return text.replace(/[^\n\r]/g, " ");
|
|
107
|
+
}
|
|
108
|
+
const FRONTMATTER = /^(?:---|\+\+\+)\r?\n[\s\S]*?\r?\n(?:---|\+\+\+|\.\.\.)[ \t]*(?:\r?\n|$)/;
|
|
109
|
+
const FENCE = /^[ \t]*((?:>[ \t]?)*)[ \t]*((?:[-*+]|\d{1,9}[.)])[ \t]+)?(`{3,}|~{3,})([^\n]*)$/;
|
|
110
|
+
const HTML_COMMENT = /<!--[\s\S]*?(?:-->|$)/g;
|
|
111
|
+
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
|
+
const LINK_REFERENCE = /(?<=\])\[[^\]\n]*\]/g;
|
|
114
|
+
const REFERENCE_DEFINITION = /^ {0,3}\[[^\]\n]+\]:[ \t]*\S.*$/gm;
|
|
115
|
+
const AUTOLINK = /<(?:[a-z][a-z\d+.-]*:[^\s<>]*|[^\s<>@]+@[^\s<>]+)>/gi;
|
|
116
|
+
const TAG = /<\/?[A-Za-z][^<>]*>/g;
|
|
117
|
+
const LINE_PREFIX = /^( {0,3}(?:>[ \t]?)*)(#{1,6}(?=[ \t]|\r?$)|[-*+](?=[ \t])|\d{1,9}[.)](?=[ \t]))?([ \t]+\[[ xX]\](?=[ \t]))?/;
|
|
118
|
+
const HEADING = /^ {0,3}#{1,6}(?:[ \t]|\r?$)/;
|
|
119
|
+
const LIST_ITEM = /^ {0,3}(?:>[ \t]?)*(?:[-*+]|\d{1,9}[.)])[ \t]/;
|
|
120
|
+
/**
|
|
121
|
+
* Blanks what is not prose in Markdown, keeping every offset: frontmatter, fenced code, HTML
|
|
122
|
+
* 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.
|
|
124
|
+
*/
|
|
125
|
+
function maskMarkdown(text, options = {}) {
|
|
126
|
+
let out = text.replace(FRONTMATTER, blank);
|
|
127
|
+
const lines = out.split("\n");
|
|
128
|
+
let fence;
|
|
129
|
+
for (const [index, line] of lines.entries()) {
|
|
130
|
+
const match = FENCE.exec(line);
|
|
131
|
+
const marker = match?.[3];
|
|
132
|
+
const depth = match?.[1]?.split(">").length ?? 0;
|
|
133
|
+
if (fence !== void 0) {
|
|
134
|
+
if (marker && !match[2] && depth === fence.depth && marker[0] === fence.marker[0] && marker.length >= fence.marker.length && match[4].trim() === "") fence = void 0;
|
|
135
|
+
lines[index] = blank(line);
|
|
136
|
+
} else if (marker) {
|
|
137
|
+
fence = {
|
|
138
|
+
marker,
|
|
139
|
+
depth
|
|
140
|
+
};
|
|
141
|
+
lines[index] = blank(line);
|
|
142
|
+
} else if (options.mdx && /^(?:import|export)\s/.test(line)) lines[index] = blank(line);
|
|
143
|
+
}
|
|
144
|
+
out = lines.join("\n");
|
|
145
|
+
out = out.replace(HTML_COMMENT, blank);
|
|
146
|
+
out = out.replace(INLINE_CODE, blank);
|
|
147
|
+
out = out.replace(REFERENCE_DEFINITION, blank);
|
|
148
|
+
out = out.replace(LINK_TARGET, blank);
|
|
149
|
+
out = out.replace(LINK_REFERENCE, blank);
|
|
150
|
+
out = out.replace(AUTOLINK, blank);
|
|
151
|
+
out = out.replace(TAG, blank);
|
|
152
|
+
return out.split("\n").map((line) => {
|
|
153
|
+
let masked = line.replace(LINE_PREFIX, blank);
|
|
154
|
+
if (HEADING.test(line)) masked = masked.replace(/[ \t]#+[ \t]*(?=\r?$)/, blank);
|
|
155
|
+
return masked;
|
|
156
|
+
}).join("\n");
|
|
157
|
+
}
|
|
158
|
+
const TABLE_DELIMITER = /^\s*\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?\s*\r?$/;
|
|
159
|
+
/** Splits text into the pieces checked one at a time: headings, table cells, list items and paragraphs. */
|
|
160
|
+
function blocks(original, masked, markdown) {
|
|
161
|
+
const found = [];
|
|
162
|
+
let current;
|
|
163
|
+
const flush = () => {
|
|
164
|
+
if (current) found.push(current);
|
|
165
|
+
current = void 0;
|
|
166
|
+
};
|
|
167
|
+
let start = 0;
|
|
168
|
+
const lines = masked.split("\n");
|
|
169
|
+
for (const [index, line] of lines.entries()) {
|
|
170
|
+
const end = start + line.length;
|
|
171
|
+
const source = original.slice(start, end);
|
|
172
|
+
if (line.trim() === "") flush();
|
|
173
|
+
else if (markdown && HEADING.test(source)) {
|
|
174
|
+
flush();
|
|
175
|
+
found.push({
|
|
176
|
+
start,
|
|
177
|
+
end,
|
|
178
|
+
kind: "label"
|
|
179
|
+
});
|
|
180
|
+
} else if (markdown && source.trimStart().startsWith("|")) {
|
|
181
|
+
flush();
|
|
182
|
+
const header = TABLE_DELIMITER.test(lines[index + 1] ?? "");
|
|
183
|
+
let cellStart = start;
|
|
184
|
+
for (const cell of line.split("|")) {
|
|
185
|
+
if (cell.trim() !== "") found.push({
|
|
186
|
+
start: cellStart,
|
|
187
|
+
end: cellStart + cell.length,
|
|
188
|
+
kind: header ? "header" : kindOf({ text: cell })
|
|
189
|
+
});
|
|
190
|
+
cellStart += cell.length + 1;
|
|
191
|
+
}
|
|
192
|
+
} else if (markdown && LIST_ITEM.test(source)) {
|
|
193
|
+
flush();
|
|
194
|
+
current = {
|
|
195
|
+
start,
|
|
196
|
+
end,
|
|
197
|
+
kind: "sentence"
|
|
198
|
+
};
|
|
199
|
+
} else if (current) current.end = end;
|
|
200
|
+
else current = {
|
|
201
|
+
start,
|
|
202
|
+
end,
|
|
203
|
+
kind: "sentence"
|
|
204
|
+
};
|
|
205
|
+
start = end + 1;
|
|
206
|
+
}
|
|
207
|
+
flush();
|
|
208
|
+
return found;
|
|
209
|
+
}
|
|
210
|
+
function summarize(term) {
|
|
211
|
+
return {
|
|
212
|
+
term: term.term,
|
|
213
|
+
...term.description ? { description: term.description } : {},
|
|
214
|
+
...term.nuance ? { nuance: term.nuance } : {}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/** Checks one file's text. Markdown is masked first, so code and link targets are never reported. */
|
|
218
|
+
function checkFileText(matcher, text, file, options = {}) {
|
|
219
|
+
const markdown = options.markdown ?? false;
|
|
220
|
+
const masked = markdown ? maskMarkdown(text, { mdx: options.mdx ?? false }) : text;
|
|
221
|
+
const lineStarts = [0];
|
|
222
|
+
for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1)) lineStarts.push(index + 1);
|
|
223
|
+
const lineOf = (offset) => {
|
|
224
|
+
let low = 0;
|
|
225
|
+
let high = lineStarts.length - 1;
|
|
226
|
+
while (low < high) {
|
|
227
|
+
const mid = low + high + 1 >> 1;
|
|
228
|
+
if (lineStarts[mid] <= offset) low = mid;
|
|
229
|
+
else high = mid - 1;
|
|
230
|
+
}
|
|
231
|
+
return low;
|
|
232
|
+
};
|
|
233
|
+
const lineEnd = (line) => (lineStarts[line + 1] ?? text.length + 1) - 1;
|
|
234
|
+
const findings = [];
|
|
235
|
+
for (const block of blocks(text, masked, markdown)) for (const finding of matcher.check(masked.slice(block.start, block.end), block.kind)) {
|
|
236
|
+
const start = block.start + finding.start;
|
|
237
|
+
const end = block.start + finding.end;
|
|
238
|
+
const line = lineOf(start);
|
|
239
|
+
const lineStart = lineStarts[line];
|
|
240
|
+
findings.push(toCliFinding(finding, {
|
|
241
|
+
file,
|
|
242
|
+
text,
|
|
243
|
+
start,
|
|
244
|
+
end,
|
|
245
|
+
line,
|
|
246
|
+
lineStart,
|
|
247
|
+
lineEnd: lineEnd(lineOf(end))
|
|
248
|
+
}));
|
|
249
|
+
}
|
|
250
|
+
return findings;
|
|
251
|
+
}
|
|
252
|
+
function toCliFinding(finding, at) {
|
|
253
|
+
const { fix, term, start: _start, end: _end, ...rest } = finding;
|
|
254
|
+
const result = fix ? (at.text.slice(at.lineStart, at.start) + fix.replace + at.text.slice(at.end, at.lineEnd)).replace(/\s+/g, " ").trim() : void 0;
|
|
255
|
+
return {
|
|
256
|
+
code: rest.code,
|
|
257
|
+
file: at.file,
|
|
258
|
+
line: at.line + 1,
|
|
259
|
+
column: at.start - at.lineStart + 1,
|
|
260
|
+
start: at.start,
|
|
261
|
+
end: at.end,
|
|
262
|
+
text: at.text.slice(at.start, at.end),
|
|
263
|
+
message: rest.message,
|
|
264
|
+
...rest.why ? { why: rest.why } : {},
|
|
265
|
+
...fix && result !== void 0 ? { fix: {
|
|
266
|
+
...fix,
|
|
267
|
+
result
|
|
268
|
+
} } : {},
|
|
269
|
+
term: summarize(term),
|
|
270
|
+
...rest.see ? { see: rest.see } : {}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function usage(why, fix) {
|
|
274
|
+
return new CliError({
|
|
275
|
+
code: CLI_ERRORS.usage,
|
|
276
|
+
why,
|
|
277
|
+
fix
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/** The files `check` reads: named files as they are, folders by extension, and glob patterns. */
|
|
281
|
+
async function collectFiles(paths, cwd, extensions = DEFAULT_EXTENSIONS) {
|
|
282
|
+
const files = /* @__PURE__ */ new Set();
|
|
283
|
+
const patterns = [];
|
|
284
|
+
const ext = extensions.length === 1 ? extensions[0] : `{${extensions.join(",")}}`;
|
|
285
|
+
for (const path of paths.length > 0 ? paths : ["."]) {
|
|
286
|
+
if (isDynamicPattern(path)) {
|
|
287
|
+
patterns.push(path);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const absolute = resolve(cwd, path);
|
|
291
|
+
if (!existsSync(absolute)) throw usage(`"${path}" does not exist.`, "Pass files, folders or glob patterns that exist.");
|
|
292
|
+
if (statSync(absolute).isDirectory()) {
|
|
293
|
+
const folder = toPosix(relative(cwd, absolute));
|
|
294
|
+
patterns.push(folder ? `${escapePath(folder)}/**/*.${ext}` : `**/*.${ext}`);
|
|
295
|
+
} else files.add(absolute);
|
|
296
|
+
}
|
|
297
|
+
if (patterns.length > 0) for (const file of await glob(patterns, {
|
|
298
|
+
cwd,
|
|
299
|
+
absolute: true,
|
|
300
|
+
ignore: ["**/node_modules/**", "**/.git/**"]
|
|
301
|
+
})) files.add(resolve(file));
|
|
302
|
+
return [...files].sort((a, b) => displayPath(cwd, a).localeCompare(displayPath(cwd, b)));
|
|
303
|
+
}
|
|
304
|
+
function useColor(options, io) {
|
|
305
|
+
return options.color ?? io.color ?? false;
|
|
306
|
+
}
|
|
307
|
+
function parseExtensions(ext) {
|
|
308
|
+
if (ext === void 0) return DEFAULT_EXTENSIONS;
|
|
309
|
+
const list = String(ext).split(",").map((item) => item.trim().replace(/^\./, "")).filter(Boolean);
|
|
310
|
+
if (list.length === 0) throw usage(`--ext is empty.`, "Pass extensions such as --ext md,mdx.");
|
|
311
|
+
return list;
|
|
312
|
+
}
|
|
313
|
+
/** `glosslint check`: reports every finding in the files, as text or JSON. Exits 1 on findings. */
|
|
314
|
+
async function runCheck(paths, options, io) {
|
|
315
|
+
const format = options.format ?? "text";
|
|
316
|
+
if (format !== "text" && format !== "json") throw usage(`--format is "${format}".`, "Use --format text or --format json.");
|
|
317
|
+
const extensions = parseExtensions(options.ext);
|
|
318
|
+
const { corpus } = await loadCorpus(io.cwd, options.config);
|
|
319
|
+
const matcher = createMatcher(corpus);
|
|
320
|
+
const files = await collectFiles(paths, io.cwd, extensions);
|
|
321
|
+
const findings = [];
|
|
322
|
+
for (const file of files) {
|
|
323
|
+
const extension = extname(file).toLowerCase();
|
|
324
|
+
findings.push(...checkFileText(matcher, readFileSync(file, "utf8"), displayPath(io.cwd, file), {
|
|
325
|
+
markdown: MARKDOWN.has(extension),
|
|
326
|
+
mdx: extension === ".mdx"
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
if (format === "json") io.stdout(`${JSON.stringify(toJson(findings, files.length), null, 2)}\n`);
|
|
330
|
+
else io.stdout(`${formatFindings(findings, {
|
|
331
|
+
color: useColor(options, io),
|
|
332
|
+
files: files.length
|
|
333
|
+
})}\n`);
|
|
334
|
+
return findings.length > 0 ? 1 : 0;
|
|
335
|
+
}
|
|
336
|
+
function describe(finding) {
|
|
337
|
+
if (finding.code === "GLOSSLINT_AVOID") return `"${finding.text}" is an avoid word for ${finding.term.term}.`;
|
|
338
|
+
const replace = finding.fix?.replace ?? finding.term.term;
|
|
339
|
+
return finding.code === "GLOSSLINT_CASE" ? `"${finding.text}" is written "${replace}".` : `"${finding.text}" is spelled "${replace}".`;
|
|
340
|
+
}
|
|
341
|
+
function failureLines(failure) {
|
|
342
|
+
const key = failure.term.term;
|
|
343
|
+
if (failure.expected === "none") {
|
|
344
|
+
const about = failure.findings.filter((finding) => finding.term.term === key);
|
|
345
|
+
const avoid = about.some((finding) => finding.code === "GLOSSLINT_AVOID");
|
|
346
|
+
return [
|
|
347
|
+
`good example "${failure.example}" was flagged:`,
|
|
348
|
+
...about.map(describe),
|
|
349
|
+
avoid ? "└ narrow the avoid word or move the example" : "└ fix the example or the term's spelling"
|
|
350
|
+
];
|
|
351
|
+
}
|
|
352
|
+
if (failure.findings.length === 0) return [`bad example "${failure.example}" was not flagged.`, "└ add the wording to \"avoid\" or fix the example"];
|
|
353
|
+
return [
|
|
354
|
+
`bad example "${failure.example}" was flagged only for other terms:`,
|
|
355
|
+
...failure.findings.map(describe),
|
|
356
|
+
"└ add the wording to \"avoid\" or fix the example"
|
|
357
|
+
];
|
|
358
|
+
}
|
|
359
|
+
const count = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
360
|
+
/** `glosslint test`: checks every term's good and bad examples. Exits 1 when one fails. */
|
|
361
|
+
async function runTest(options, io) {
|
|
362
|
+
const { corpus } = await loadCorpus(io.cwd, options.config);
|
|
363
|
+
const color = useColor(options, io);
|
|
364
|
+
const tested = corpus.terms.filter((term) => (term.examples?.good?.length ?? 0) + (term.examples?.bad?.length ?? 0) > 0);
|
|
365
|
+
if (tested.length === 0) {
|
|
366
|
+
io.stdout("No term has examples.\n");
|
|
367
|
+
return 0;
|
|
368
|
+
}
|
|
369
|
+
const failures = testCorpus(corpus);
|
|
370
|
+
const width = Math.max(...tested.map((term) => term.term.length)) + 2;
|
|
371
|
+
const indent = " ".repeat(width + 2);
|
|
372
|
+
const lines = [];
|
|
373
|
+
let failing = 0;
|
|
374
|
+
for (const term of tested) {
|
|
375
|
+
const own = failures.filter((failure) => failure.term === term);
|
|
376
|
+
const name = term.term.padEnd(width);
|
|
377
|
+
if (own.length === 0) {
|
|
378
|
+
const good = term.examples?.good?.length ?? 0;
|
|
379
|
+
const bad = term.examples?.bad?.length ?? 0;
|
|
380
|
+
lines.push(`${paint(color, "32", "✓")} ${name}${good} good, ${bad} bad`);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
failing++;
|
|
384
|
+
const details = own.flatMap(failureLines);
|
|
385
|
+
lines.push(`${paint(color, "31", "✗")} ${name}${details[0]}`);
|
|
386
|
+
for (const detail of details.slice(1)) lines.push(`${indent}${detail}`);
|
|
387
|
+
}
|
|
388
|
+
io.stdout(`${lines.join("\n")}\n\n${count(tested.length, "term")}, ${failing} failing.\n`);
|
|
389
|
+
return failing > 0 ? 1 : 0;
|
|
390
|
+
}
|
|
391
|
+
/** `glosslint print`: writes the corpus as a Markdown glossary or as JSON. */
|
|
392
|
+
async function runPrint(options, io) {
|
|
393
|
+
const format = options.format ?? "md";
|
|
394
|
+
if (format !== "md" && format !== "json") throw usage(`--format is "${format}".`, "Use --format md or --format json.");
|
|
395
|
+
const { corpus } = await loadCorpus(io.cwd, options.config);
|
|
396
|
+
io.stdout(printCorpus(corpus, format));
|
|
397
|
+
return 0;
|
|
398
|
+
}
|
|
399
|
+
/** `glosslint explain`: what the corpus says about a word or phrase. Exits 1 when no term matches. */
|
|
400
|
+
async function runExplain(query, options, io) {
|
|
401
|
+
const { corpus } = await loadCorpus(io.cwd, options.config);
|
|
402
|
+
io.stdout(`${explain(corpus, query)}\n`);
|
|
403
|
+
return explainMatches(corpus, query).length > 0 ? 0 : 1;
|
|
404
|
+
}
|
|
405
|
+
/** Prints a config or usage error as a tree on stderr. */
|
|
406
|
+
function printError(error, io, color) {
|
|
407
|
+
const branches = [];
|
|
408
|
+
if (error.term && !error.why.includes(`"${error.term}"`)) branches.push(["term", error.term]);
|
|
409
|
+
branches.push(["fix", error.fix]);
|
|
410
|
+
if (error.config) branches.push(["config", error.config]);
|
|
411
|
+
const lines = branches.map(([label, text], index) => {
|
|
412
|
+
const branch = index === branches.length - 1 ? "╰▶" : "├▶";
|
|
413
|
+
return `${paint(color, "2", branch)} ${paint(color, "1", `${label}:`)} ${text}`;
|
|
414
|
+
});
|
|
415
|
+
io.stderr(`${[`${paint(color, "1;31", `[${error.code}]`)} ${error.why}`, ...lines].join("\n")}\n`);
|
|
416
|
+
}
|
|
417
|
+
function defaultIo() {
|
|
418
|
+
return {
|
|
419
|
+
cwd: process.cwd(),
|
|
420
|
+
stdout: (text) => process.stdout.write(text),
|
|
421
|
+
stderr: (text) => process.stderr.write(text),
|
|
422
|
+
color: Boolean(process.stdout.isTTY) && !process.env.NO_COLOR
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
/** Runs the CLI on `argv` (the arguments after the program name) and resolves to the exit code. */
|
|
426
|
+
async function main(argv, io = defaultIo()) {
|
|
427
|
+
const cli = cac("glosslint");
|
|
428
|
+
let run;
|
|
429
|
+
let color = io.color ?? false;
|
|
430
|
+
const configOption = "Config file (default: glosslint.config.{ts,mts,js,mjs})";
|
|
431
|
+
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) => {
|
|
433
|
+
run = () => runCheck(paths, options, io);
|
|
434
|
+
});
|
|
435
|
+
cli.command("test", "Check every term's good and bad examples").option("--config <path>", configOption).option("--color", colorOption).action((options) => {
|
|
436
|
+
run = () => runTest(options, io);
|
|
437
|
+
});
|
|
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) => {
|
|
439
|
+
run = () => runPrint(options, io);
|
|
440
|
+
});
|
|
441
|
+
cli.command("explain <query>", "Show what the corpus says about a word or phrase").option("--config <path>", configOption).action((query, options) => {
|
|
442
|
+
run = () => runExplain(String(query), options, io);
|
|
443
|
+
});
|
|
444
|
+
cli.help();
|
|
445
|
+
cli.version(version);
|
|
446
|
+
try {
|
|
447
|
+
const parsed = cli.parse([
|
|
448
|
+
"node",
|
|
449
|
+
"glosslint",
|
|
450
|
+
...argv
|
|
451
|
+
], { run: false });
|
|
452
|
+
if (typeof parsed.options.color === "boolean") color = parsed.options.color;
|
|
453
|
+
if (!cli.matchedCommand) return 0;
|
|
454
|
+
cli.runMatchedCommand();
|
|
455
|
+
return run ? await run() : 0;
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (error instanceof CliError || isConfigError(error)) {
|
|
458
|
+
printError(error, io, color);
|
|
459
|
+
return 2;
|
|
460
|
+
}
|
|
461
|
+
if (error instanceof Error && error.name === "CACError") {
|
|
462
|
+
printError({
|
|
463
|
+
code: CLI_ERRORS.usage,
|
|
464
|
+
why: `${error.message}.`,
|
|
465
|
+
fix: "Run glosslint --help for the commands and options."
|
|
466
|
+
}, io, color);
|
|
467
|
+
return 2;
|
|
468
|
+
}
|
|
469
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
470
|
+
io.stderr(`${paint(color, "1;31", `[${CLI_ERRORS.failed}]`)} ${message.split("\n")[0]}\n`);
|
|
471
|
+
return 2;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
function isBin() {
|
|
475
|
+
const entry = process.argv[1];
|
|
476
|
+
if (!entry) return false;
|
|
477
|
+
try {
|
|
478
|
+
return import.meta.url === pathToFileURL(realpathSync(entry)).href;
|
|
479
|
+
} catch {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (isBin()) process.exitCode = await main(process.argv.slice(2));
|
|
484
|
+
export { CLI_ERRORS, checkFileText, collectFiles, loadCorpus, main, maskMarkdown, runCheck, runExplain, runPrint, runTest };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { c as Corpus, h as KindOf } from "./types-CBQGgzID.mjs";
|
|
2
|
+
import { Linter } from "eslint";
|
|
3
|
+
/** Options of the ESLint config. */
|
|
4
|
+
export interface GlosslintEslintOptions {
|
|
5
|
+
/** The corpus, usually `import corpus from './glosslint.config.ts'`. */
|
|
6
|
+
corpus: Corpus;
|
|
7
|
+
/** Files the rules run on. */
|
|
8
|
+
files?: string[];
|
|
9
|
+
/**
|
|
10
|
+
* Callees whose first argument is never checked, added to `t`, `$t`, `te` and `tm`. Defaults to
|
|
11
|
+
* `endsWith`, `startsWith`, `includes` and `querySelector*`; a name ending in `*` matches as a prefix.
|
|
12
|
+
*/
|
|
13
|
+
ignoreCallees?: string[];
|
|
14
|
+
/** Report avoid words in declared names too. Off by default, since a rename touches every caller. */
|
|
15
|
+
identifierAvoid?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Decides whether a string is a label, header or sentence before glosslint's own `kindOf`, from its
|
|
18
|
+
* text, name and file. Return `undefined` to leave a string to glosslint.
|
|
19
|
+
*/
|
|
20
|
+
kindOf?: KindOf;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* ESLint flat configs that run glosslint's rules: `...glosslint({ corpus, files })`. They set no
|
|
24
|
+
* parser: your config supplies typescript-eslint, vue-eslint-parser or yaml-eslint-parser.
|
|
25
|
+
*/
|
|
26
|
+
export default function glosslint(options: GlosslintEslintOptions): Linter.Config[];
|