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.
- package/README.md +53 -36
- package/dist/cli.d.mts +48 -4
- package/dist/cli.mjs +142 -35
- package/dist/eslint.d.mts +9 -6
- package/dist/eslint.mjs +18 -771
- package/dist/format-CQ9xdJYe.mjs +66 -0
- package/dist/glossary-DmTK_e1d.d.mts +32 -0
- package/dist/glossary-lk7EwGey.mjs +1872 -0
- package/dist/inclusive-CP6oU8NW.mjs +520 -0
- package/dist/index.d.mts +6 -26
- 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-D-PrDySk.mjs +768 -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 +16 -2
- package/dist/matcher-Xz4vspsE.mjs +0 -715
- package/dist/tools-BrdtyK9t.mjs +0 -275
|
@@ -1,715 +0,0 @@
|
|
|
1
|
-
import { splitByCase } from "scule";
|
|
2
|
-
// src/words.ts
|
|
3
|
-
const SEPARATORS = [
|
|
4
|
-
" ",
|
|
5
|
-
" ",
|
|
6
|
-
"\n",
|
|
7
|
-
"-",
|
|
8
|
-
"‐",
|
|
9
|
-
"‑",
|
|
10
|
-
"_",
|
|
11
|
-
"/",
|
|
12
|
-
"."
|
|
13
|
-
];
|
|
14
|
-
/** Splits text or an identifier into words: `dayPass`, `day-pass` and `day pass` all give day, pass. */
|
|
15
|
-
function splitWords(text) {
|
|
16
|
-
return splitByCase(text.trim(), SEPARATORS).filter((word) => word.length > 0);
|
|
17
|
-
}
|
|
18
|
-
/** A term's key: its words joined and lowercased, letters and digits only, so `Re-charge point` and `recharge point` share one. */
|
|
19
|
-
function termKey(text) {
|
|
20
|
-
return splitWords(text).join("").toLowerCase().replace(/[^\p{L}\p{N}]/gu, "");
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Starts a reason that opens a sentence with a capital, so "riders confuse it" reads "Riders confuse it". A
|
|
24
|
-
* first word with a capital of its own, such as `eBike`, stays as written.
|
|
25
|
-
*/
|
|
26
|
-
function capitalise(text) {
|
|
27
|
-
return text.replace(/^(\s*)(\p{Ll})(?=[\p{Ll}\p{N}'’-]*(?![\p{L}\p{N}]))/u, (_, space, letter) => space + letter.toUpperCase());
|
|
28
|
-
}
|
|
29
|
-
/** A word of shown text, for case styles: a letter, then letters, digits, periods and apostrophes. */
|
|
30
|
-
const WORD = /\p{L}[\p{L}\p{N}.'’]*/gu;
|
|
31
|
-
/** English words that Title Case leaves lowercase past a label's first word. */
|
|
32
|
-
const SMALL_WORDS = /* @__PURE__ */ new Set([
|
|
33
|
-
"a",
|
|
34
|
-
"an",
|
|
35
|
-
"and",
|
|
36
|
-
"as",
|
|
37
|
-
"at",
|
|
38
|
-
"by",
|
|
39
|
-
"for",
|
|
40
|
-
"from",
|
|
41
|
-
"in",
|
|
42
|
-
"of",
|
|
43
|
-
"on",
|
|
44
|
-
"or",
|
|
45
|
-
"per",
|
|
46
|
-
"the",
|
|
47
|
-
"to",
|
|
48
|
-
"via",
|
|
49
|
-
"vs",
|
|
50
|
-
"with"
|
|
51
|
-
]);
|
|
52
|
-
const isUpper = (char) => char !== char.toLowerCase() && char === char.toUpperCase();
|
|
53
|
-
/** Whether every word but a small one past the first starts with a capital, as "Buy a Day Pass" does. */
|
|
54
|
-
function isTitled(text) {
|
|
55
|
-
const words = text.match(WORD) ?? [];
|
|
56
|
-
return words.length > 1 && words.every((word, index) => isUpper(word.charAt(0)) || index > 0 && SMALL_WORDS.has(word));
|
|
57
|
-
}
|
|
58
|
-
/** Characters allowed between the words of one term: spaces, hyphens and underscores. */
|
|
59
|
-
const WORD_GAP = /^[\s\-‐‑_]+$/;
|
|
60
|
-
const TOKEN = /[\p{L}\p{N}](?:[\p{L}\p{N}.]*[\p{L}\p{N}])?/gu;
|
|
61
|
-
const APOSTROPHE = /['’]/;
|
|
62
|
-
const PLACEHOLDER = /\{\{[\s\S]*?\}\}|\{\s*[\p{L}\p{N}_$.-]+\s*(?:,\s*(?:number|date|time)[^{}]*)?\}/gu;
|
|
63
|
-
const ICU_HEAD = /\{\s*[\p{L}\p{N}_$.-]+\s*,\s*(?:plural|select|selectordinal)\s*,/gu;
|
|
64
|
-
const ICU_BRANCH = /(?:=\d+|[\p{L}\p{N}_]+)\s*\{|[{}#]/gu;
|
|
65
|
-
const URL = /\b[a-z][a-z\d+.-]*:\/\/[^\s<>"'`)\]]*|\bwww\.[^\s<>"'`)\]]+|\bmailto:[^\s<>"'`)\]]+|[\w.+-]+@[\w-]+(?:\.[\w-]+)+/giu;
|
|
66
|
-
const BACKTICKS = /(`+)[\s\S]*?\1/gu;
|
|
67
|
-
const PATH = /(?<![\p{L}\p{N}.~])(?:\.{1,2}|~)?\/[\p{L}\p{N}_.~/-]+/gu;
|
|
68
|
-
const CHUNK = /[\p{L}\p{N}_]+(?:[-.][\p{L}\p{N}_]+)*/gu;
|
|
69
|
-
const CAMEL = /\p{Ll}\p{Lu}/u;
|
|
70
|
-
const SLUG = /^[\p{Ll}\p{N}]+(?:-[\p{Ll}\p{N}]+)+$/u;
|
|
71
|
-
const PATH_EDGE = /[/#@=]/;
|
|
72
|
-
function blank(text, start, end) {
|
|
73
|
-
return text.slice(0, start) + " ".repeat(end - start) + text.slice(end);
|
|
74
|
-
}
|
|
75
|
-
function blankAll(text, pattern) {
|
|
76
|
-
return text.replace(pattern, (match) => " ".repeat(match.length));
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Blanks an ICU message's syntax and keeps the words of its branches, such as `docks` in
|
|
80
|
-
* `{n, plural, other {# docks}}`. Odd brace depth is an argument, even depth is branch text.
|
|
81
|
-
* Returns undefined when the braces don't balance.
|
|
82
|
-
*/
|
|
83
|
-
function maskIcu(text) {
|
|
84
|
-
let depth = 0;
|
|
85
|
-
let out = "";
|
|
86
|
-
for (const char of text) {
|
|
87
|
-
if (char === "{") depth++;
|
|
88
|
-
else if (char === "}" && --depth < 0) return void 0;
|
|
89
|
-
const keep = char !== "{" && char !== "}" && (depth === 0 || depth % 2 === 0 && char !== "#");
|
|
90
|
-
out += keep ? char : " ".repeat(char.length);
|
|
91
|
-
}
|
|
92
|
-
return depth === 0 ? out : void 0;
|
|
93
|
-
}
|
|
94
|
-
/** Whether a token with this key is spared from masking because it names an exact term, singular or plural. */
|
|
95
|
-
function isKept(key, keep) {
|
|
96
|
-
return keep.has(key) || key.endsWith("s") && keep.has(key.slice(0, -1)) || key.endsWith("es") && keep.has(key.slice(0, -2));
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Blanks out what is code rather than words, keeping every offset: placeholders, URLs, paths, backtick
|
|
100
|
-
* code, and camel, snake and slug tokens unless their key is in `keep`.
|
|
101
|
-
*/
|
|
102
|
-
function mask(text, keep = /* @__PURE__ */ new Set()) {
|
|
103
|
-
let out = blankAll(text, BACKTICKS);
|
|
104
|
-
out = blankAll(out, URL);
|
|
105
|
-
out = blankAll(out, PATH);
|
|
106
|
-
if (out.search(ICU_HEAD) !== -1) {
|
|
107
|
-
const icu = maskIcu(out);
|
|
108
|
-
if (icu !== void 0) out = icu;
|
|
109
|
-
else {
|
|
110
|
-
out = blankAll(out, ICU_HEAD);
|
|
111
|
-
out = blankAll(out, ICU_BRANCH);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
out = blankAll(out, PLACEHOLDER);
|
|
115
|
-
for (const match of out.matchAll(CHUNK)) {
|
|
116
|
-
const chunk = match[0];
|
|
117
|
-
const start = match.index;
|
|
118
|
-
const end = start + chunk.length;
|
|
119
|
-
if (chunk.includes("_")) {
|
|
120
|
-
if (!isKept(termKey(chunk), keep)) out = blank(out, start, end);
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
if (SLUG.test(chunk) && (chunk.split("-").length >= 3 || PATH_EDGE.test(text[start - 1] ?? "") || PATH_EDGE.test(text[end] ?? ""))) {
|
|
124
|
-
if (!isKept(termKey(chunk), keep)) out = blank(out, start, end);
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
let offset = start;
|
|
128
|
-
for (const part of chunk.split(/([-.])/)) {
|
|
129
|
-
if (CAMEL.test(part) && !isKept(termKey(part), keep)) out = blank(out, offset, offset + part.length);
|
|
130
|
-
offset += part.length;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return out;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* The words of `text` with their offsets, after `mask`. A word right after an apostrophe that follows
|
|
137
|
-
* a letter, such as the `s` of a possessive or the `t` of `don't`, never forms a token.
|
|
138
|
-
*/
|
|
139
|
-
function tokenize(text, keep = /* @__PURE__ */ new Set()) {
|
|
140
|
-
const masked = mask(text, keep);
|
|
141
|
-
const tokens = [];
|
|
142
|
-
for (const match of masked.matchAll(TOKEN)) {
|
|
143
|
-
const start = match.index;
|
|
144
|
-
if (APOSTROPHE.test(text[start - 1] ?? "") && /\p{L}/u.test(text[start - 2] ?? "")) continue;
|
|
145
|
-
const word = match[0];
|
|
146
|
-
tokens.push({
|
|
147
|
-
word,
|
|
148
|
-
key: word.toLowerCase().replace(/[^\p{L}\p{N}]/gu, ""),
|
|
149
|
-
start,
|
|
150
|
-
end: start + word.length
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
return tokens;
|
|
154
|
-
}
|
|
155
|
-
// src/kind.ts
|
|
156
|
-
const LABEL_WORD = /^(?:label|(?:sub)?title|button|tab|(?:sub)?heading)s?$/;
|
|
157
|
-
const HEADER_WORD = /^(?:header|column|col|th)s?$/;
|
|
158
|
-
const SENTENCE_WORD = /^(?:description|tooltip|text|message|intro|outro|hint|explanation|body|help)s?$/;
|
|
159
|
-
const HEADING_TAG = /^h[1-6]$/i;
|
|
160
|
-
/** First words that open a sentence rather than a label, as in "There are no rides". English only. */
|
|
161
|
-
const SENTENCE_OPENER = /^(?:there|this|that|these|those|it|you|we|they|i|is|are|was|were|do|does|did|can|could|will|would|should|has|have)$/i;
|
|
162
|
-
/** The kind a site's name gives it, with a closing stop always making a sentence. */
|
|
163
|
-
function byName(name, text) {
|
|
164
|
-
const trimmed = text.trim();
|
|
165
|
-
const endsSentence = /[.!?]$/.test(trimmed);
|
|
166
|
-
const words = splitWords(name ?? "").map((word) => HEADING_TAG.test(word) ? "heading" : word.toLowerCase().replace(/\d+$/, ""));
|
|
167
|
-
const last = words.at(-1) ?? "";
|
|
168
|
-
const header = words.some((word) => HEADER_WORD.test(word));
|
|
169
|
-
if ((header || words.some((word) => LABEL_WORD.test(word)) || last === "name") && !endsSentence && splitWords(trimmed).length <= 7) return header ? "header" : "label";
|
|
170
|
-
if (words.some((word) => SENTENCE_WORD.test(word)) || endsSentence) return "sentence";
|
|
171
|
-
return "unknown";
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* The kind of shown text from its shape alone: a label when it is a few words that don't open like a
|
|
175
|
-
* sentence, or when it is written in Title Case, else a sentence.
|
|
176
|
-
*/
|
|
177
|
-
function byShape(text) {
|
|
178
|
-
const trimmed = text.trim().replace(/:$/, "");
|
|
179
|
-
const words = trimmed.split(/\s+/);
|
|
180
|
-
return words.length <= 4 && !SENTENCE_OPENER.test(words[0]) || isTitled(trimmed.replace(/\([^)]*\)/g, "")) ? "label" : "sentence";
|
|
181
|
-
}
|
|
182
|
-
/** Decides a site's kind: `custom` first, then the site's name, then, for shown text, its shape. */
|
|
183
|
-
function classify(site, custom) {
|
|
184
|
-
const own = custom?.(site);
|
|
185
|
-
if (own !== void 0) return {
|
|
186
|
-
kind: own,
|
|
187
|
-
guessed: false
|
|
188
|
-
};
|
|
189
|
-
const named = byName(site.name, site.text);
|
|
190
|
-
if (named !== "unknown" || !site.shown) return {
|
|
191
|
-
kind: named,
|
|
192
|
-
guessed: false
|
|
193
|
-
};
|
|
194
|
-
return {
|
|
195
|
-
kind: byShape(site.text),
|
|
196
|
-
guessed: true
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* glosslint's default kind for a string: from its key, attribute or element name, else from a closing
|
|
201
|
-
* stop, else, when `shown` is set, from its length and capitals. Every adapter uses it unless its
|
|
202
|
-
* `kindOf` option decides first.
|
|
203
|
-
*/
|
|
204
|
-
function kindOf(site) {
|
|
205
|
-
return classify(site).kind;
|
|
206
|
-
}
|
|
207
|
-
/** How a term's capitals are checked: its `case`, else `context` when it sets `casing`, else `spelling`. */
|
|
208
|
-
function caseModeOf(term) {
|
|
209
|
-
return term.case ?? (term.casing ? "context" : "spelling");
|
|
210
|
-
}
|
|
211
|
-
// src/matcher.ts
|
|
212
|
-
const ALNUM = /[\p{L}\p{N}]/u;
|
|
213
|
-
const hasLower = (text) => /\p{Ll}/u.test(text);
|
|
214
|
-
const isAllCaps = (text) => /\p{Lu}.*\p{Lu}/u.test(text) && !hasLower(text);
|
|
215
|
-
const upperFirst = (text) => text.charAt(0).toUpperCase() + text.slice(1);
|
|
216
|
-
/** Keeps a word's capitals when it has one past its first letter, such as `DP` or `eBike`. */
|
|
217
|
-
const keepsCapitals = (word) => /^.+\p{Lu}/u.test(word);
|
|
218
|
-
/**
|
|
219
|
-
* Words in parentheses keep their case ("(from dock)"), and the first word after a dash or colon
|
|
220
|
-
* starts again, as in "Rentals - At Another Dock".
|
|
221
|
-
*/
|
|
222
|
-
function titleCase(text) {
|
|
223
|
-
return text.replace(WORD, (word, offset) => {
|
|
224
|
-
const before = text.slice(0, offset);
|
|
225
|
-
if (before.split("(").length > before.split(")").length) return word;
|
|
226
|
-
if (!(!/\p{L}/u.test(before) || /[-–—:]\s*$/.test(before)) && SMALL_WORDS.has(word.toLowerCase())) return word.toLowerCase();
|
|
227
|
-
return word === word.toLowerCase() ? upperFirst(word) : word;
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
function lowerCase(text) {
|
|
231
|
-
return text.replace(WORD, (word) => keepsCapitals(word) ? word : word.toLowerCase());
|
|
232
|
-
}
|
|
233
|
-
function sentenceCase(text, atStart) {
|
|
234
|
-
const lower = lowerCase(text);
|
|
235
|
-
return atStart ? upperFirst(lower) : lower;
|
|
236
|
-
}
|
|
237
|
-
function applyStyle(style, text, atStart) {
|
|
238
|
-
if (style === "title") return titleCase(text);
|
|
239
|
-
if (style === "sentence") return sentenceCase(text, atStart);
|
|
240
|
-
if (style === "lower") return lowerCase(text);
|
|
241
|
-
if (style === "upper") return text.toUpperCase();
|
|
242
|
-
return text;
|
|
243
|
-
}
|
|
244
|
-
function squash(text) {
|
|
245
|
-
return text.toLowerCase().replace(/\s+/g, " ");
|
|
246
|
-
}
|
|
247
|
-
/** Words joined by single spaces, without periods, so `Approx. time` and `Approx-time` compare equal. */
|
|
248
|
-
function spaced(text) {
|
|
249
|
-
return text.replace(/\./g, "").replace(/[\s\-‐‑_]+/g, " ").trim();
|
|
250
|
-
}
|
|
251
|
-
/** Whether `written` is `word` with some of its periods left out, ignoring case and word gaps. */
|
|
252
|
-
function periodsFit(written, word) {
|
|
253
|
-
const flat = (text) => text.toLowerCase().replace(/(?<=[\p{L}\p{N}])\.(?=[\p{L}\p{N}])/gu, "").replace(/[\s\-‐‑_]+/g, "");
|
|
254
|
-
const w = flat(written);
|
|
255
|
-
const a = flat(word);
|
|
256
|
-
let j = 0;
|
|
257
|
-
for (const char of a) if (char === w[j]) j++;
|
|
258
|
-
else if (char !== ".") return false;
|
|
259
|
-
return j === w.length;
|
|
260
|
-
}
|
|
261
|
-
/** Splits `C++` into "", "C" and "++": the symbols before the first letter or digit, the rest, and the symbols after the last. */
|
|
262
|
-
function symbols(text) {
|
|
263
|
-
const lead = /^[^\p{L}\p{N}]*/u.exec(text)[0];
|
|
264
|
-
const trail = /[^\p{L}\p{N}]*$/u.exec(text.slice(lead.length))[0];
|
|
265
|
-
return {
|
|
266
|
-
lead,
|
|
267
|
-
core: text.slice(lead.length, text.length - trail.length),
|
|
268
|
-
trail
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
/** Writes `canonical` with the writer's capitals: each word starts as the writer's letter at that place does. */
|
|
272
|
-
function keepCapitals(canonical, surface) {
|
|
273
|
-
if (isAllCaps(surface)) return canonical.toUpperCase();
|
|
274
|
-
const letters = Array.from(surface).filter((char) => ALNUM.test(char));
|
|
275
|
-
const chars = Array.from(canonical);
|
|
276
|
-
let index = 0;
|
|
277
|
-
let out = "";
|
|
278
|
-
for (const [position, char] of chars.entries()) {
|
|
279
|
-
if (!ALNUM.test(char)) {
|
|
280
|
-
out += char;
|
|
281
|
-
continue;
|
|
282
|
-
}
|
|
283
|
-
const wordStart = position === 0 || !ALNUM.test(chars[position - 1]);
|
|
284
|
-
const written = letters[index++];
|
|
285
|
-
out += wordStart && written !== void 0 ? isUpper(written) ? char.toUpperCase() : char.toLowerCase() : char;
|
|
286
|
-
}
|
|
287
|
-
return index === letters.length ? out : canonical;
|
|
288
|
-
}
|
|
289
|
-
function plural(word) {
|
|
290
|
-
const ending = /(?:s|x|z|ch|sh)$/i.test(word) ? "es" : "s";
|
|
291
|
-
return word + (isAllCaps(word) ? ending.toUpperCase() : ending);
|
|
292
|
-
}
|
|
293
|
-
function withSuffix(replace, suffix, abbr) {
|
|
294
|
-
if (suffix === "") return replace;
|
|
295
|
-
if (hasLower(replace) || abbr) return replace + suffix.toLowerCase();
|
|
296
|
-
return replace + suffix;
|
|
297
|
-
}
|
|
298
|
-
/** Words whose period ends the word, not the sentence, beside those the corpus writes with one, such as `hrs.`. */
|
|
299
|
-
const DOTTED_WORDS = [
|
|
300
|
-
"e.g.",
|
|
301
|
-
"i.e.",
|
|
302
|
-
"vs.",
|
|
303
|
-
"approx.",
|
|
304
|
-
"incl.",
|
|
305
|
-
"excl.",
|
|
306
|
-
"avg.",
|
|
307
|
-
"no."
|
|
308
|
-
];
|
|
309
|
-
/**
|
|
310
|
-
* Whether a term at `start` begins a sentence. After a colon, either case is right, so the writer's
|
|
311
|
-
* first letter decides. A period after a word in `dotted`, such as `hrs.`, ends no sentence.
|
|
312
|
-
*/
|
|
313
|
-
function atSentenceStart(text, start, dotted) {
|
|
314
|
-
const before = text.slice(0, start);
|
|
315
|
-
if (/^[\s\p{Ps}\p{Pi}"'*_•-]*$/u.test(before)) return true;
|
|
316
|
-
if (/\|\s*[\p{Ps}\p{Pi}"'*_]*$/u.test(before)) return true;
|
|
317
|
-
const end = /(\S*)([.!?:])["'\p{Pf}\p{Pe}]*\s+[\p{Ps}\p{Pi}"'*_]*$/u.exec(before);
|
|
318
|
-
if (!end) return false;
|
|
319
|
-
if (end[2] === ":") return isUpper(text.charAt(start));
|
|
320
|
-
return !(end[2] === "." && dotted.has(`${end[1].toLowerCase()}.`));
|
|
321
|
-
}
|
|
322
|
-
function endSentence(text) {
|
|
323
|
-
const trimmed = text.trim().replace(/\s+/g, " ");
|
|
324
|
-
return /[.!?:]$/.test(trimmed) ? trimmed : `${trimmed}.`;
|
|
325
|
-
}
|
|
326
|
-
function makeFinding(code, start, end, text, term, headline, fix, reason) {
|
|
327
|
-
const see = term.links?.[0]?.url;
|
|
328
|
-
const why = reason ?? term.nuance;
|
|
329
|
-
return {
|
|
330
|
-
code,
|
|
331
|
-
start,
|
|
332
|
-
end,
|
|
333
|
-
text,
|
|
334
|
-
message: [
|
|
335
|
-
headline,
|
|
336
|
-
reason && endSentence(capitalise(reason)),
|
|
337
|
-
term.description && `${term.term}: ${endSentence(term.description)}`,
|
|
338
|
-
term.nuance && term.nuance !== reason ? endSentence(term.nuance) : void 0,
|
|
339
|
-
see && `See ${see}`
|
|
340
|
-
].filter(Boolean).join(" "),
|
|
341
|
-
...why ? { why } : {},
|
|
342
|
-
...fix ? { fix } : {},
|
|
343
|
-
term,
|
|
344
|
-
...see ? { see } : {}
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
/** Finds a key in `map`, as written or as a plural ending in `s` or `es`. */
|
|
348
|
-
function lookup(map, key) {
|
|
349
|
-
const exact = map.get(key);
|
|
350
|
-
if (exact) return {
|
|
351
|
-
value: exact,
|
|
352
|
-
suffix: 0
|
|
353
|
-
};
|
|
354
|
-
if (key.length > 2 && key.endsWith("s")) {
|
|
355
|
-
const single = map.get(key.slice(0, -1));
|
|
356
|
-
if (single) return {
|
|
357
|
-
value: single,
|
|
358
|
-
suffix: 1
|
|
359
|
-
};
|
|
360
|
-
const es = key.endsWith("es") ? map.get(key.slice(0, -2)) : void 0;
|
|
361
|
-
if (es) return {
|
|
362
|
-
value: es,
|
|
363
|
-
suffix: 2
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
/** The longest run of words from `index` whose joined key `find` knows, up to `max` words. */
|
|
368
|
-
function longest(text, words, index, max, gap, find, accept = (hit) => hit) {
|
|
369
|
-
let count = 1;
|
|
370
|
-
while (count < max && index + count < words.length && gap(text.slice(words[index + count - 1].end, words[index + count].start))) count++;
|
|
371
|
-
for (let n = count; n >= 1; n--) {
|
|
372
|
-
const run = words.slice(index, index + n);
|
|
373
|
-
const found = find(run.map((word) => word.key).join(""));
|
|
374
|
-
const start = run[0].start;
|
|
375
|
-
const end = run.at(-1).end;
|
|
376
|
-
for (const candidate of [found].flat()) {
|
|
377
|
-
if (!candidate) continue;
|
|
378
|
-
const hit = accept({
|
|
379
|
-
value: candidate.value,
|
|
380
|
-
start,
|
|
381
|
-
end,
|
|
382
|
-
next: index + n,
|
|
383
|
-
suffix: text.slice(end - candidate.suffix, end)
|
|
384
|
-
});
|
|
385
|
-
if (hit) return hit;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
function identifierWords(text) {
|
|
390
|
-
const words = [];
|
|
391
|
-
let cursor = 0;
|
|
392
|
-
for (const word of splitWords(text)) {
|
|
393
|
-
const found = text.indexOf(word, cursor);
|
|
394
|
-
if (found === -1) continue;
|
|
395
|
-
cursor = found + word.length;
|
|
396
|
-
const lead = /^[^\p{L}\p{N}]*/u.exec(word)?.[0].length ?? 0;
|
|
397
|
-
const trail = /[^\p{L}\p{N}]*$/u.exec(word)?.[0].length ?? 0;
|
|
398
|
-
const start = found + lead;
|
|
399
|
-
const end = Math.max(start, cursor - trail);
|
|
400
|
-
const key = word.toLowerCase().replace(/[^\p{L}\p{N}]/gu, "");
|
|
401
|
-
if (key) words.push({
|
|
402
|
-
key,
|
|
403
|
-
start,
|
|
404
|
-
end
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
return words;
|
|
408
|
-
}
|
|
409
|
-
/**
|
|
410
|
-
* The separator `part` uses: `_` or `-` between its words, none between camel humps, and for a
|
|
411
|
-
* single word the one joining it to a neighbouring word of `identifier`. A leading `_`, as in
|
|
412
|
-
* `_dockCount`, joins no words, so it is not a separator.
|
|
413
|
-
*/
|
|
414
|
-
function separatorOf(part, identifier, start, end) {
|
|
415
|
-
const inner = /[\p{L}\p{N}]([_-])[\p{L}\p{N}]/u.exec(part)?.[1];
|
|
416
|
-
if (inner) return inner;
|
|
417
|
-
if (splitWords(part).length > 1) return "";
|
|
418
|
-
const before = identifier[start - 1] ?? "";
|
|
419
|
-
if (/[_-]/.test(before) && ALNUM.test(identifier[start - 2] ?? "")) return before;
|
|
420
|
-
const after = identifier[end] ?? "";
|
|
421
|
-
if (/[_-]/.test(after) && ALNUM.test(identifier[end + 1] ?? "")) return after;
|
|
422
|
-
return "";
|
|
423
|
-
}
|
|
424
|
-
/** Writes words in the casing style of `part`, the matched words of an identifier: snake, kebab or camel. */
|
|
425
|
-
function identifierStyle(words, part, separator) {
|
|
426
|
-
if (isAllCaps(part)) return words.join(separator || "_").toUpperCase();
|
|
427
|
-
if (separator) return words.join(separator);
|
|
428
|
-
const camel = words.map((word, index) => index === 0 ? word : upperFirst(word)).join("");
|
|
429
|
-
return isUpper(part.charAt(0)) ? upperFirst(camel) : camel;
|
|
430
|
-
}
|
|
431
|
-
function allowPattern(phrase) {
|
|
432
|
-
const body = phrase.trim().split(/\s+/).map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("[\\s\\-‐‑_]+");
|
|
433
|
-
return new RegExp(`(?<![\\p{L}\\p{N}])${body}(?:e?s)?(?![\\p{L}\\p{N}])`, "giu");
|
|
434
|
-
}
|
|
435
|
-
/** Compiles a corpus for matching. Build it once and reuse it. */
|
|
436
|
-
function createMatcher(corpus, options = {}) {
|
|
437
|
-
const terms = /* @__PURE__ */ new Map();
|
|
438
|
-
const abbrs = /* @__PURE__ */ new Map();
|
|
439
|
-
const keep = /* @__PURE__ */ new Set();
|
|
440
|
-
let maxWords = 1;
|
|
441
|
-
for (const term of corpus.terms) {
|
|
442
|
-
const key = termKey(term.term);
|
|
443
|
-
const { lead, core, trail } = symbols(term.term);
|
|
444
|
-
const entry = {
|
|
445
|
-
term,
|
|
446
|
-
canonical: term.term,
|
|
447
|
-
mode: caseModeOf(term),
|
|
448
|
-
skip: new Set(term.skip),
|
|
449
|
-
words: splitWords(core || term.term).map((word) => word.toLowerCase()),
|
|
450
|
-
lead,
|
|
451
|
-
trail
|
|
452
|
-
};
|
|
453
|
-
terms.set(key, entry);
|
|
454
|
-
if (entry.mode === "exact") keep.add(key);
|
|
455
|
-
maxWords = Math.max(maxWords, entry.words.length);
|
|
456
|
-
for (const abbr of term.abbr ?? []) {
|
|
457
|
-
const abbrKey = termKey(abbr);
|
|
458
|
-
const parts = symbols(abbr);
|
|
459
|
-
const words = splitWords(parts.core || abbr).map((w) => w.toLowerCase());
|
|
460
|
-
abbrs.set(abbrKey, {
|
|
461
|
-
...entry,
|
|
462
|
-
canonical: abbr,
|
|
463
|
-
mode: "abbr",
|
|
464
|
-
words,
|
|
465
|
-
lead: parts.lead,
|
|
466
|
-
trail: parts.trail
|
|
467
|
-
});
|
|
468
|
-
keep.add(abbrKey);
|
|
469
|
-
maxWords = Math.max(maxWords, words.length);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
const avoids = /* @__PURE__ */ new Map();
|
|
473
|
-
let maxAvoidWords = 1;
|
|
474
|
-
for (const entry of terms.values()) for (const avoid of entry.term.avoid ?? []) {
|
|
475
|
-
const { word, why } = typeof avoid === "string" ? {
|
|
476
|
-
word: avoid,
|
|
477
|
-
why: void 0
|
|
478
|
-
} : avoid;
|
|
479
|
-
const key = termKey(word);
|
|
480
|
-
if (terms.has(key)) continue;
|
|
481
|
-
avoids.delete(key);
|
|
482
|
-
avoids.set(key, {
|
|
483
|
-
term: entry.term,
|
|
484
|
-
entry,
|
|
485
|
-
word,
|
|
486
|
-
...why ? { why } : {},
|
|
487
|
-
caseSensitive: /\p{Lu}{2}/u.test(word)
|
|
488
|
-
});
|
|
489
|
-
maxAvoidWords = Math.max(maxAvoidWords, splitWords(word).length);
|
|
490
|
-
}
|
|
491
|
-
const allow = corpus.allow.map(allowPattern);
|
|
492
|
-
const dotted = new Set(DOTTED_WORDS);
|
|
493
|
-
for (const written of [...[...terms.values(), ...abbrs.values()].map((entry) => entry.canonical), ...[...avoids.values()].map((avoid) => avoid.word)]) {
|
|
494
|
-
const last = written.split(/\s+/).at(-1);
|
|
495
|
-
if (last.endsWith(".")) dotted.add(last.toLowerCase());
|
|
496
|
-
}
|
|
497
|
-
const startsSentence = (text, start) => atSentenceStart(text, start, dotted);
|
|
498
|
-
const casing = corpus.casing ?? {};
|
|
499
|
-
/** The case style of `term` in `kind` of text, or undefined where casing isn't checked. */
|
|
500
|
-
const styleOf = (term, kind) => {
|
|
501
|
-
const own = term.casing ?? {};
|
|
502
|
-
if (kind === "header") return own.header ?? casing.header ?? own.label ?? casing.label ?? "title";
|
|
503
|
-
if (kind === "label") return own.label ?? casing.label ?? "title";
|
|
504
|
-
if (kind === "sentence") return own.sentence ?? casing.sentence ?? "sentence";
|
|
505
|
-
};
|
|
506
|
-
const termLimit = maxWords + 1;
|
|
507
|
-
const avoidLimit = maxAvoidWords + 1;
|
|
508
|
-
const textGap = (between) => WORD_GAP.test(between);
|
|
509
|
-
const avoidGap = (between) => WORD_GAP.test(between.replace(/^\./, ""));
|
|
510
|
-
const findTerm = (key) => [lookup(terms, key), lookup(abbrs, key)];
|
|
511
|
-
const withSymbols = (text) => (hit) => {
|
|
512
|
-
const { lead, trail } = hit.value;
|
|
513
|
-
if (lead === "" && trail === "") return hit;
|
|
514
|
-
if (trail !== "" && hit.suffix !== "") return void 0;
|
|
515
|
-
if (text.slice(hit.start - lead.length, hit.start) !== lead) return void 0;
|
|
516
|
-
if (text.slice(hit.end, hit.end + trail.length) !== trail) return void 0;
|
|
517
|
-
return {
|
|
518
|
-
...hit,
|
|
519
|
-
start: hit.start - lead.length,
|
|
520
|
-
end: hit.end + trail.length
|
|
521
|
-
};
|
|
522
|
-
};
|
|
523
|
-
function termFinding(text, hit, kind) {
|
|
524
|
-
const entry = hit.value;
|
|
525
|
-
const written = text.slice(hit.start, hit.end);
|
|
526
|
-
const surface = written.slice(0, written.length - hit.suffix.length);
|
|
527
|
-
const exact = entry.mode === "exact" || entry.mode === "abbr";
|
|
528
|
-
if (entry.mode === "abbr" && !/\p{Lu}/u.test(surface)) return void 0;
|
|
529
|
-
if (entry.skip.has("lower") && !/\p{Lu}/u.test(surface)) return void 0;
|
|
530
|
-
if (entry.skip.has("upper") && !hasLower(surface)) return void 0;
|
|
531
|
-
const fixable = !/[\r\n]/.test(written);
|
|
532
|
-
const style = styleOf(entry.term, kind);
|
|
533
|
-
const contextWant = entry.mode === "context" && style ? applyStyle(style, entry.canonical, startsSentence(text, hit.start)) : void 0;
|
|
534
|
-
const shouted = style === "upper" && !hasLower(surface);
|
|
535
|
-
if (squash(surface) !== squash(entry.canonical)) {
|
|
536
|
-
const replace = withSuffix(shouted ? entry.canonical.toUpperCase() : exact ? entry.canonical : contextWant ?? keepCapitals(entry.canonical, surface), hit.suffix, entry.mode === "abbr");
|
|
537
|
-
return makeFinding("GLOSSLINT_TERM", hit.start, hit.end, written, entry.term, `"${written}" is spelled "${replace}".`, fixable ? {
|
|
538
|
-
kind: "autofix",
|
|
539
|
-
replace
|
|
540
|
-
} : void 0);
|
|
541
|
-
}
|
|
542
|
-
if (shouted) return void 0;
|
|
543
|
-
const want = exact ? entry.canonical : contextWant;
|
|
544
|
-
const where = exact || contextWant === void 0 ? "" : ` in ${kind}s`;
|
|
545
|
-
if (want === void 0 || surface.replace(/\s+/g, " ") === want) return void 0;
|
|
546
|
-
const replace = withSuffix(want, hit.suffix, entry.mode === "abbr");
|
|
547
|
-
return makeFinding("GLOSSLINT_CASE", hit.start, hit.end, written, entry.term, `"${written}" is written "${replace}"${where}.`, fixable ? {
|
|
548
|
-
kind: "autofix",
|
|
549
|
-
replace
|
|
550
|
-
} : void 0);
|
|
551
|
-
}
|
|
552
|
-
function avoidFinding(text, hit, kind) {
|
|
553
|
-
const avoid = hit.value;
|
|
554
|
-
const end = hit.suffix === "" && avoid.word.endsWith(".") && text[hit.end] === "." ? hit.end + 1 : hit.end;
|
|
555
|
-
const written = text.slice(hit.start, end);
|
|
556
|
-
const surface = hit.suffix ? written.slice(0, -hit.suffix.length) : written;
|
|
557
|
-
if (!periodsFit(surface, avoid.word)) return void 0;
|
|
558
|
-
if (avoid.caseSensitive && spaced(surface) !== spaced(avoid.word)) return void 0;
|
|
559
|
-
const { entry } = avoid;
|
|
560
|
-
let base;
|
|
561
|
-
const style = styleOf(entry.term, kind);
|
|
562
|
-
if (style === "upper" && (entry.mode === "context" || !hasLower(surface))) base = entry.canonical.toUpperCase();
|
|
563
|
-
else if (entry.mode === "exact" || avoid.caseSensitive) base = entry.canonical;
|
|
564
|
-
else if (entry.mode === "context" && style && kind !== "sentence") base = applyStyle(style, entry.canonical, startsSentence(text, hit.start));
|
|
565
|
-
else if (entry.mode === "context" && style && style !== "sentence") base = applyStyle(style, entry.canonical, false);
|
|
566
|
-
else if (isAllCaps(surface) && hasLower(avoid.word)) base = entry.canonical.toUpperCase();
|
|
567
|
-
else if (isUpper(surface.charAt(0))) base = upperFirst(entry.canonical);
|
|
568
|
-
else base = lowerCase(entry.canonical);
|
|
569
|
-
const replace = hit.suffix ? plural(base) : base;
|
|
570
|
-
const words = splitWords(replace).map((word) => word.toLowerCase());
|
|
571
|
-
const before = /([\p{L}\p{N}]+)\s+$/u.exec(text.slice(0, hit.start))?.[1]?.toLowerCase();
|
|
572
|
-
const after = /^\s+([\p{L}\p{N}]+)/u.exec(text.slice(end))?.[1]?.toLowerCase();
|
|
573
|
-
if (before === words[0] || after === words.at(-1)) return void 0;
|
|
574
|
-
const stop = replace.endsWith(".") && text[end] === "." ? end + 1 : end;
|
|
575
|
-
return makeFinding("GLOSSLINT_AVOID", hit.start, stop, text.slice(hit.start, stop), avoid.term, `Use "${replace}" instead of "${written}".`, {
|
|
576
|
-
kind: "suggestion",
|
|
577
|
-
replace
|
|
578
|
-
}, avoid.why);
|
|
579
|
-
}
|
|
580
|
-
function checkText(text, kind) {
|
|
581
|
-
const tokens = tokenize(text, keep);
|
|
582
|
-
const accept = withSymbols(text);
|
|
583
|
-
const findings = [];
|
|
584
|
-
for (let index = 0; index < tokens.length;) {
|
|
585
|
-
const hit = longest(text, tokens, index, termLimit, textGap, findTerm, accept);
|
|
586
|
-
if (!hit) {
|
|
587
|
-
index++;
|
|
588
|
-
continue;
|
|
589
|
-
}
|
|
590
|
-
const finding = termFinding(text, hit, kind);
|
|
591
|
-
if (finding) findings.push(finding);
|
|
592
|
-
index = hit.next;
|
|
593
|
-
}
|
|
594
|
-
const findAvoid = (key) => lookup(avoids, key);
|
|
595
|
-
const acceptAvoid = (hit) => avoidFinding(text, hit, kind) ? hit : void 0;
|
|
596
|
-
for (let index = 0; index < tokens.length;) {
|
|
597
|
-
const hit = longest(text, tokens, index, avoidLimit, avoidGap, findAvoid, acceptAvoid);
|
|
598
|
-
const finding = hit && avoidFinding(text, hit, kind);
|
|
599
|
-
if (!hit || !finding) {
|
|
600
|
-
index++;
|
|
601
|
-
continue;
|
|
602
|
-
}
|
|
603
|
-
findings.push(finding);
|
|
604
|
-
index = hit.next;
|
|
605
|
-
}
|
|
606
|
-
const label = labelFinding(text, kind, tokens, findings, accept);
|
|
607
|
-
if (!label?.fix) return label ? [...findings, label] : findings;
|
|
608
|
-
return [...findings.map((finding) => {
|
|
609
|
-
if (finding.fix?.kind !== "autofix") return finding;
|
|
610
|
-
const { fix: _fix, ...rest } = finding;
|
|
611
|
-
return rest;
|
|
612
|
-
}), label];
|
|
613
|
-
}
|
|
614
|
-
/**
|
|
615
|
-
* With `wholeLabel`, a label or header in Title Case or ALL CAPS holding a context term is written in
|
|
616
|
-
* that style as a whole, so fixing the term alone doesn't leave "Day Pass prices".
|
|
617
|
-
*/
|
|
618
|
-
function labelFinding(text, kind, tokens, findings, accept) {
|
|
619
|
-
if (!corpus.wholeLabel || kind !== "label" && kind !== "header") return void 0;
|
|
620
|
-
let styled;
|
|
621
|
-
for (let index = 0; index < tokens.length && !styled;) {
|
|
622
|
-
const hit = longest(text, tokens, index, termLimit, textGap, findTerm, accept);
|
|
623
|
-
if (!hit) {
|
|
624
|
-
index++;
|
|
625
|
-
continue;
|
|
626
|
-
}
|
|
627
|
-
const style = styleOf(hit.value.term, kind);
|
|
628
|
-
if (hit.value.mode === "context" && (style === "title" || style === "upper")) styled = {
|
|
629
|
-
term: hit.value.term,
|
|
630
|
-
style
|
|
631
|
-
};
|
|
632
|
-
index = hit.next;
|
|
633
|
-
}
|
|
634
|
-
if (!styled) return void 0;
|
|
635
|
-
let fixed = text;
|
|
636
|
-
let floor = Infinity;
|
|
637
|
-
for (const finding of [...findings].sort((a, b) => b.start - a.start)) {
|
|
638
|
-
if (finding.fix?.kind !== "autofix" || finding.end > floor) continue;
|
|
639
|
-
fixed = fixed.slice(0, finding.start) + finding.fix.replace + fixed.slice(finding.end);
|
|
640
|
-
floor = finding.start;
|
|
641
|
-
}
|
|
642
|
-
const lead = /^\s*/.exec(fixed)[0].length;
|
|
643
|
-
const body = fixed.trim();
|
|
644
|
-
const want = applyStyle(styled.style, body, true);
|
|
645
|
-
if (want === body || /[\r\n{}|]/.test(body)) return void 0;
|
|
646
|
-
const start = /^\s*/.exec(text)[0].length;
|
|
647
|
-
const end = text.trimEnd().length;
|
|
648
|
-
const where = styled.style === "upper" ? "ALL CAPS" : "Title Case";
|
|
649
|
-
return makeFinding("GLOSSLINT_CASE", start, end, text.slice(start, end), styled.term, `"${body}" is written "${want}": ${kind}s are in ${where}.`, lead === start ? {
|
|
650
|
-
kind: "autofix",
|
|
651
|
-
replace: want
|
|
652
|
-
} : void 0);
|
|
653
|
-
}
|
|
654
|
-
function checkIdentifier(text) {
|
|
655
|
-
const words = identifierWords(text);
|
|
656
|
-
const findings = [];
|
|
657
|
-
const anyGap = () => true;
|
|
658
|
-
const spelled = (key) => {
|
|
659
|
-
const found = lookup(terms, key);
|
|
660
|
-
return found && found.value.mode !== "exact" && !found.value.lead && !found.value.trail ? found : void 0;
|
|
661
|
-
};
|
|
662
|
-
for (let index = 0; index < words.length;) {
|
|
663
|
-
const hit = longest(text, words, index, termLimit, anyGap, spelled);
|
|
664
|
-
if (!hit) {
|
|
665
|
-
index++;
|
|
666
|
-
continue;
|
|
667
|
-
}
|
|
668
|
-
index = hit.next;
|
|
669
|
-
const written = text.slice(hit.start, hit.end);
|
|
670
|
-
if (splitWords(written.slice(0, written.length - hit.suffix.length)).map((word) => word.toLowerCase()).join(" ") === hit.value.words.join(" ") || isAllCaps(written) && !/[_-]/.test(written)) continue;
|
|
671
|
-
const separator = separatorOf(written, text, hit.start, hit.end);
|
|
672
|
-
const replace = identifierStyle(hit.value.words, written, separator) + hit.suffix;
|
|
673
|
-
findings.push(makeFinding("GLOSSLINT_TERM", hit.start, hit.end, written, hit.value.term, `Rename "${written}" to "${replace}": the term is "${hit.value.term.term}".`, {
|
|
674
|
-
kind: "rename",
|
|
675
|
-
replace
|
|
676
|
-
}));
|
|
677
|
-
}
|
|
678
|
-
if (!options.identifierAvoid) return findings;
|
|
679
|
-
for (let index = 0; index < words.length;) {
|
|
680
|
-
const hit = longest(text, words, index, avoidLimit, anyGap, (key) => lookup(avoids, key));
|
|
681
|
-
if (!hit) {
|
|
682
|
-
index++;
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
index = hit.next;
|
|
686
|
-
const written = text.slice(hit.start, hit.end);
|
|
687
|
-
const separator = separatorOf(written, text, hit.start, hit.end);
|
|
688
|
-
const base = identifierStyle(hit.value.entry.words, written, separator);
|
|
689
|
-
const replace = hit.suffix ? plural(base) : base;
|
|
690
|
-
findings.push(makeFinding("GLOSSLINT_AVOID", hit.start, hit.end, written, hit.value.term, `Rename "${written}" to "${replace}".`, {
|
|
691
|
-
kind: "rename",
|
|
692
|
-
replace
|
|
693
|
-
}, hit.value.why));
|
|
694
|
-
}
|
|
695
|
-
return findings;
|
|
696
|
-
}
|
|
697
|
-
function allowed(text) {
|
|
698
|
-
const ranges = [];
|
|
699
|
-
for (const pattern of allow) for (const match of text.matchAll(pattern)) ranges.push([match.index, match.index + match[0].length]);
|
|
700
|
-
return ranges;
|
|
701
|
-
}
|
|
702
|
-
return {
|
|
703
|
-
corpus,
|
|
704
|
-
check(text, kind = "unknown") {
|
|
705
|
-
const findings = kind === "identifier" ? checkIdentifier(text) : checkText(text, kind);
|
|
706
|
-
const ranges = findings.length > 0 ? allowed(text) : [];
|
|
707
|
-
return findings.filter((finding) => !ranges.some(([start, end]) => finding.start >= start && finding.end <= end)).sort((a, b) => a.start - b.start || a.code.localeCompare(b.code));
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
}
|
|
711
|
-
/** Finds every problem in `text`, read as `kind` (default `unknown`). */
|
|
712
|
-
function check(matcher, text, kind) {
|
|
713
|
-
return matcher.check(text, kind);
|
|
714
|
-
}
|
|
715
|
-
export { kindOf as a, termKey as c, classify as i, createMatcher as n, capitalise as o, caseModeOf as r, splitWords as s, check as t };
|