@kirill.konshin/lint 0.5.5 → 0.6.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 +38 -3
- package/commands/spec-author.md +39 -0
- package/dist/configs/base.d.ts +4 -3
- package/dist/configs/base.js +61 -74
- package/dist/configs/imports.d.ts +3 -2
- package/dist/configs/imports.js +95 -146
- package/dist/configs/nx.d.ts +4 -3
- package/dist/configs/nx.js +31 -41
- package/dist/configs/prettier.d.ts +3 -2
- package/dist/configs/prettier.js +19 -16
- package/dist/configs/promise.d.ts +3 -2
- package/dist/configs/promise.js +16 -16
- package/dist/configs/react.d.ts +4 -3
- package/dist/configs/react.js +178 -217
- package/dist/configs/storybook.d.ts +4 -3
- package/dist/configs/storybook.js +22 -22
- package/dist/configs/tailwind.d.ts +4 -3
- package/dist/configs/tailwind.js +67 -91
- package/dist/configs/testing.d.ts +7 -6
- package/dist/configs/testing.js +112 -146
- package/dist/configs/turbo.d.ts +4 -3
- package/dist/configs/turbo.js +19 -27
- package/dist/configs/typescriptTypeAware.d.ts +4 -3
- package/dist/configs/typescriptTypeAware.js +99 -95
- package/dist/configs/unicorn.d.ts +3 -2
- package/dist/configs/unicorn.js +34 -41
- package/dist/exts.d.ts +2 -1
- package/dist/exts.js +10 -11
- package/dist/index.d.ts +44 -42
- package/dist/index.js +53 -85
- package/dist/lib.d.ts +21 -21
- package/dist/lib.js +262 -301
- package/dist/lintPrepare.d.ts +20 -17
- package/dist/lintPrepare.js +341 -371
- package/dist/lintStaged.d.ts +4 -3
- package/dist/lintStaged.js +25 -22
- package/dist/prettier.d.ts +4 -3
- package/dist/prettier.js +16 -17
- package/dist/restrictions.d.ts +11 -10
- package/dist/restrictions.js +18 -22
- package/dist/yarn.cjs +79 -98
- package/dist/yarn.d.cts +3 -2
- package/package.json +22 -8
- package/rules/agent.md +2 -0
- package/rules/openspec.md +1 -1
- package/skills/spec-steward/SKILL.md +4 -12
- package/skills/spec-steward/references/audit.md +18 -43
- package/skills/spec-steward/references/contract.md +9 -9
- package/skills/spec-steward/references/criteria.md +4 -4
- package/skills/spec-steward/references/review-format.md +6 -6
- package/skills/spec-steward/references/wiring.md +2 -2
- package/skills/spec-steward/references/worker.md +67 -0
- package/skills/spec-steward/sweeps.json +49 -0
- package/skills/spec-tools/SKILL.md +48 -0
- package/skills/spec-tools/references/contract.md +111 -0
- package/skills/spec-tools/references/gitlab.md +120 -0
- package/skills/spec-tools/scripts/cli.js +8129 -0
- package/skills/spec-verify/SKILL.md +153 -0
- package/dist/configs/base.d.ts.map +0 -1
- package/dist/configs/imports.d.ts.map +0 -1
- package/dist/configs/nx.d.ts.map +0 -1
- package/dist/configs/prettier.d.ts.map +0 -1
- package/dist/configs/promise.d.ts.map +0 -1
- package/dist/configs/react.d.ts.map +0 -1
- package/dist/configs/storybook.d.ts.map +0 -1
- package/dist/configs/tailwind.d.ts.map +0 -1
- package/dist/configs/testing.d.ts.map +0 -1
- package/dist/configs/turbo.d.ts.map +0 -1
- package/dist/configs/typescriptTypeAware.d.ts.map +0 -1
- package/dist/configs/unicorn.d.ts.map +0 -1
- package/dist/exts.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/lib.d.ts.map +0 -1
- package/dist/lintPrepare.d.ts.map +0 -1
- package/dist/lintStaged.d.ts.map +0 -1
- package/dist/prettier.d.ts.map +0 -1
- package/dist/restrictions.d.ts.map +0 -1
- package/dist/yarn.d.cts.map +0 -1
- package/skills/spec-steward/scripts/lib/align.mjs +0 -127
- package/skills/spec-steward/scripts/lib/checks.mjs +0 -589
- package/skills/spec-steward/scripts/lib/citations.mjs +0 -156
- package/skills/spec-steward/scripts/lib/corpus.mjs +0 -256
- package/skills/spec-steward/scripts/lib/coverage.mjs +0 -123
- package/skills/spec-steward/scripts/lib/evidence.mjs +0 -334
- package/skills/spec-steward/scripts/lib/git.mjs +0 -138
- package/skills/spec-steward/scripts/lib/review.mjs +0 -347
- package/skills/spec-steward/scripts/lib/scan.mjs +0 -364
- package/skills/spec-steward/scripts/lib/util.mjs +0 -161
- package/skills/spec-steward/scripts/lib/wire.mjs +0 -331
- package/skills/spec-steward/scripts/steward.mjs +0 -350
- package/skills/spec-steward/workflows/audit.js +0 -539
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
/**
|
|
3
|
-
* Zero-dependency source reading: which characters of a file are code, comment or string, the extent of every test
|
|
4
|
-
* call and type assertion in a test file, and a file's text with its comments blanked. A call's extent is found by
|
|
5
|
-
* balanced brackets with strings, template literals, regular expressions and comments skipped.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export const CODE = 0;
|
|
9
|
-
export const COMMENT = 1;
|
|
10
|
-
export const STRING = 2;
|
|
11
|
-
|
|
12
|
-
/** Files read with the C-family lexer; every other text file has `#` comments. @param {string} file */
|
|
13
|
-
export const isCLike = (file) => /\.(?:[cm]?[jt]sx?|go)$/i.test(file);
|
|
14
|
-
/** @param {string} file */
|
|
15
|
-
export const isDoc = (file) => /\.mdx?$/i.test(file);
|
|
16
|
-
|
|
17
|
-
const REGEX_AFTER = new Set([...'(,=:[!&|?{};+-*%<>~^', '']);
|
|
18
|
-
const REGEX_KEYWORD = /^(?:return|typeof|instanceof|in|of|new|delete|void|throw|case|do|else|yield|await)$/;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Classify every character of a C-family source as CODE, COMMENT or STRING. A quoted string never spans a line: an
|
|
22
|
-
* unterminated quote (an apostrophe in JSX text) is read as code.
|
|
23
|
-
* @param {string} text
|
|
24
|
-
* @returns {Uint8Array}
|
|
25
|
-
*/
|
|
26
|
-
export function lex(text) {
|
|
27
|
-
const n = text.length;
|
|
28
|
-
const cls = new Uint8Array(n);
|
|
29
|
-
const mark = (/** @type {number} */ from, /** @type {number} */ to, /** @type {number} */ k) =>
|
|
30
|
-
cls.fill(k, from, to);
|
|
31
|
-
|
|
32
|
-
/** @param {number} from just past the opening backtick @returns {number} just past the closing one */
|
|
33
|
-
const template = (from) => {
|
|
34
|
-
let j = from;
|
|
35
|
-
while (j < n) {
|
|
36
|
-
const ch = text[j];
|
|
37
|
-
if (ch === '\\') {
|
|
38
|
-
mark(j, j + 2, STRING);
|
|
39
|
-
j += 2;
|
|
40
|
-
} else if (ch === '`') {
|
|
41
|
-
cls[j] = STRING;
|
|
42
|
-
return j + 1;
|
|
43
|
-
} else if (ch === '$' && text[j + 1] === '{') {
|
|
44
|
-
mark(j, j + 2, STRING);
|
|
45
|
-
j = code(j + 2, true);
|
|
46
|
-
} else {
|
|
47
|
-
cls[j] = STRING;
|
|
48
|
-
j++;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return n;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/** @param {number} from @param {boolean} inTemplate @returns {number} */
|
|
55
|
-
const code = (from, inTemplate) => {
|
|
56
|
-
let j = from;
|
|
57
|
-
let depth = 0;
|
|
58
|
-
let prev = '';
|
|
59
|
-
let word = '';
|
|
60
|
-
while (j < n) {
|
|
61
|
-
const ch = text[j];
|
|
62
|
-
const next = text[j + 1];
|
|
63
|
-
if (ch === '/' && (next === '/' || next === '*')) {
|
|
64
|
-
const e = next === '/' ? text.indexOf('\n', j) : text.indexOf('*/', j + 2);
|
|
65
|
-
const end = e < 0 ? n : next === '/' ? e : e + 2;
|
|
66
|
-
mark(j, end, COMMENT);
|
|
67
|
-
j = end;
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (ch === "'" || ch === '"') {
|
|
71
|
-
let k = j + 1;
|
|
72
|
-
while (k < n && text[k] !== ch && text[k] !== '\n') k += text[k] === '\\' ? 2 : 1;
|
|
73
|
-
if (text[k] === ch) {
|
|
74
|
-
mark(j, k + 1, STRING);
|
|
75
|
-
j = k + 1;
|
|
76
|
-
prev = 'a';
|
|
77
|
-
word = '';
|
|
78
|
-
} else j++;
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
if (ch === '`') {
|
|
82
|
-
cls[j] = STRING;
|
|
83
|
-
j = template(j + 1);
|
|
84
|
-
prev = 'a';
|
|
85
|
-
word = '';
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (ch === '/' && (REGEX_AFTER.has(prev) || REGEX_KEYWORD.test(word))) {
|
|
89
|
-
let k = j + 1;
|
|
90
|
-
let inClass = false;
|
|
91
|
-
while (k < n && text[k] !== '\n') {
|
|
92
|
-
const c = text[k];
|
|
93
|
-
if (c === '\\') k++;
|
|
94
|
-
else if (c === '[') inClass = true;
|
|
95
|
-
else if (c === ']') inClass = false;
|
|
96
|
-
else if (c === '/' && !inClass) break;
|
|
97
|
-
k++;
|
|
98
|
-
}
|
|
99
|
-
if (text[k] === '/') {
|
|
100
|
-
k++;
|
|
101
|
-
while (k < n && /[a-z]/i.test(text[k])) k++;
|
|
102
|
-
mark(j, k, STRING);
|
|
103
|
-
j = k;
|
|
104
|
-
prev = 'a';
|
|
105
|
-
word = '';
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (inTemplate && ch === '{') depth++;
|
|
110
|
-
if (inTemplate && ch === '}') {
|
|
111
|
-
if (depth === 0) {
|
|
112
|
-
cls[j] = STRING;
|
|
113
|
-
return j + 1;
|
|
114
|
-
}
|
|
115
|
-
depth--;
|
|
116
|
-
}
|
|
117
|
-
if (/[\w$]/.test(ch)) {
|
|
118
|
-
let k = j;
|
|
119
|
-
while (k < n && /[\w$]/.test(text[k])) k++;
|
|
120
|
-
word = text.slice(j, k);
|
|
121
|
-
prev = 'a';
|
|
122
|
-
j = k;
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
if (!/\s/.test(ch)) {
|
|
126
|
-
prev = ch;
|
|
127
|
-
word = '';
|
|
128
|
-
}
|
|
129
|
-
j++;
|
|
130
|
-
}
|
|
131
|
-
return n;
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
code(0, false);
|
|
135
|
-
return cls;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const OPEN = { '(': ')', '[': ']', '{': '}' };
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* The index of the bracket closing the one at `open`, counting code characters only; -1 when it never closes.
|
|
142
|
-
* @param {string} text
|
|
143
|
-
* @param {Uint8Array} cls
|
|
144
|
-
* @param {number} open
|
|
145
|
-
*/
|
|
146
|
-
export function matchClose(text, cls, open) {
|
|
147
|
-
/** @type {string[]} */
|
|
148
|
-
const stack = [];
|
|
149
|
-
for (let j = open; j < text.length; j++) {
|
|
150
|
-
if (cls[j] !== CODE) continue;
|
|
151
|
-
const ch = text[j];
|
|
152
|
-
if (ch === '(' || ch === '[' || ch === '{') stack.push(OPEN[ch]);
|
|
153
|
-
else if (ch === ')' || ch === ']' || ch === '}') {
|
|
154
|
-
if (stack.pop() !== ch) return -1;
|
|
155
|
-
if (!stack.length) return j;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return -1;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** The offset each line starts at, and the 1-based line of any offset. @param {string} text */
|
|
162
|
-
function lineIndex(text) {
|
|
163
|
-
const starts = [0];
|
|
164
|
-
for (let i = 0; i < text.length; i++) if (text[i] === '\n') starts.push(i + 1);
|
|
165
|
-
const lineOf = (/** @type {number} */ at) => {
|
|
166
|
-
let lo = 0;
|
|
167
|
-
let hi = starts.length - 1;
|
|
168
|
-
while (lo < hi) {
|
|
169
|
-
const mid = (lo + hi + 1) >> 1;
|
|
170
|
-
if (starts[mid] <= at) lo = mid;
|
|
171
|
-
else hi = mid - 1;
|
|
172
|
-
}
|
|
173
|
-
return lo + 1;
|
|
174
|
-
};
|
|
175
|
-
return { starts, lineOf };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** @param {string} line */
|
|
179
|
-
const indentOf = (line) => /^\s*/.exec(line)?.[0].length ?? 0;
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* @typedef {{ start: number, attach: number, end: number, title: string | null, kind: 'test' | 'type' }} Block
|
|
183
|
-
* 1-based lines: `attach` is the first line of the comment run directly above `start` (or `start` itself).
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
const TEST_NAME = /(?<![\w$.])(?:describe|it|test|suite|bench)(?![\w$])/g;
|
|
187
|
-
/** Chained modifiers a test call may carry: Vitest/Jest (`.only`, `.each(…)`, …) and Playwright (`.describe`, `.step`). */
|
|
188
|
-
const TEST_MODIFIERS = new Set([
|
|
189
|
-
...'only skip concurrent sequential todo each for runIf skipIf fails'.split(' '),
|
|
190
|
-
...'describe step serial parallel fixme fail'.split(' '),
|
|
191
|
-
]);
|
|
192
|
-
const TYPE_ASSERTION = /^\s*(?:export\s+)?type\s+([\w$]+)\b[^=]*=\s*Expect\s*</;
|
|
193
|
-
const TYPE_CALL = /(?<![\w$.])(?:expectTypeOf|assertType)\s*[(<]/;
|
|
194
|
-
const C_COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*)/;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* The first line of the comment run (for Python, also decorators) directly above a 1-based line, no blank between.
|
|
198
|
-
* @param {string[]} lines
|
|
199
|
-
* @param {number} start
|
|
200
|
-
* @param {RegExp} comment
|
|
201
|
-
*/
|
|
202
|
-
function attachedAbove(lines, start, comment) {
|
|
203
|
-
let attach = start;
|
|
204
|
-
while (attach > 1 && comment.test(lines[attach - 2])) attach--;
|
|
205
|
-
return attach;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Where a call that the brackets cannot close ends: the next line at or left of its indent that closes a block.
|
|
210
|
-
* @param {string[]} lines
|
|
211
|
-
* @param {number} start 1-based
|
|
212
|
-
*/
|
|
213
|
-
function endByIndent(lines, start) {
|
|
214
|
-
const indent = indentOf(lines[start - 1]);
|
|
215
|
-
for (let k = start; k < lines.length; k++)
|
|
216
|
-
if (lines[k].trim() && indentOf(lines[k]) <= indent) return /^\s*[})]/.test(lines[k]) ? k + 1 : -1;
|
|
217
|
-
return -1;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/** Test calls and type assertions of a C-family test file. @param {string} text @returns {Block[]} */
|
|
221
|
-
function cLikeBlocks(text) {
|
|
222
|
-
const cls = lex(text);
|
|
223
|
-
const { starts, lineOf } = lineIndex(text);
|
|
224
|
-
const lines = text.split('\n');
|
|
225
|
-
/** @type {Block[]} */
|
|
226
|
-
const out = [];
|
|
227
|
-
const skipSpace = (/** @type {number} */ j) => {
|
|
228
|
-
while (j < text.length && (/\s/.test(text[j]) || cls[j] === COMMENT)) j++;
|
|
229
|
-
return j;
|
|
230
|
-
};
|
|
231
|
-
for (const m of text.matchAll(TEST_NAME)) {
|
|
232
|
-
const at = m.index ?? 0;
|
|
233
|
-
if (cls[at] !== CODE) continue;
|
|
234
|
-
let j = at + m[0].length;
|
|
235
|
-
let open = -1;
|
|
236
|
-
let close = -1;
|
|
237
|
-
for (;;) {
|
|
238
|
-
j = skipSpace(j);
|
|
239
|
-
const modifier = text[j] === '.' && /^\.\s*([\w$]+)/.exec(text.slice(j, j + 40));
|
|
240
|
-
if (modifier) {
|
|
241
|
-
if (!TEST_MODIFIERS.has(modifier[1])) break;
|
|
242
|
-
j += modifier[0].length;
|
|
243
|
-
} else if (text[j] === '`' && cls[j] === STRING) {
|
|
244
|
-
j++;
|
|
245
|
-
while (j < text.length && !(text[j] === '`' && cls[j] === STRING)) j++;
|
|
246
|
-
j++;
|
|
247
|
-
} else if (text[j] === '(') {
|
|
248
|
-
const end = matchClose(text, cls, j);
|
|
249
|
-
const after = end < 0 ? -1 : skipSpace(end + 1);
|
|
250
|
-
if (after >= 0 && (text[after] === '(' || text[after] === '`')) {
|
|
251
|
-
j = after;
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
open = j;
|
|
255
|
-
close = end;
|
|
256
|
-
break;
|
|
257
|
-
} else break;
|
|
258
|
-
}
|
|
259
|
-
if (open < 0) continue;
|
|
260
|
-
const start = lineOf(at);
|
|
261
|
-
const end = close >= 0 ? lineOf(close) : endByIndent(lines, start);
|
|
262
|
-
if (end < 0) continue;
|
|
263
|
-
const arg = skipSpace(open + 1);
|
|
264
|
-
const quoted = /^(['"`])((?:\\.|(?!\1)[^\\])*)\1/.exec(text.slice(arg, arg + 400));
|
|
265
|
-
const title = quoted ? quoted[2] : (/^[^,)]+/.exec(text.slice(arg, arg + 200))?.[0].trim() ?? null);
|
|
266
|
-
out.push({ start, attach: attachedAbove(lines, start, C_COMMENT_LINE), end, title, kind: 'test' });
|
|
267
|
-
}
|
|
268
|
-
lines.forEach((line, i) => {
|
|
269
|
-
const alias = TYPE_ASSERTION.exec(line);
|
|
270
|
-
const call = alias ? null : TYPE_CALL.exec(line);
|
|
271
|
-
if (!alias && !call) return;
|
|
272
|
-
if (cls[starts[i] + (call ? call.index : line.search(/\S/))] !== CODE) return;
|
|
273
|
-
let end = i;
|
|
274
|
-
while (end < Math.min(lines.length - 1, i + 60) && !/;\s*(?:\/\/.*)?$/.test(lines[end])) end++;
|
|
275
|
-
const start = i + 1;
|
|
276
|
-
out.push({
|
|
277
|
-
start,
|
|
278
|
-
attach: attachedAbove(lines, start, C_COMMENT_LINE),
|
|
279
|
-
end: end + 1,
|
|
280
|
-
title: alias ? alias[1] : 'expectTypeOf',
|
|
281
|
-
kind: 'type',
|
|
282
|
-
});
|
|
283
|
-
});
|
|
284
|
-
return out;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/** Test functions and classes of a Python test file, by indentation. @param {string} text @returns {Block[]} */
|
|
288
|
-
function pythonBlocks(text) {
|
|
289
|
-
const lines = text.split('\n');
|
|
290
|
-
/** @type {Block[]} */
|
|
291
|
-
const out = [];
|
|
292
|
-
lines.forEach((line, i) => {
|
|
293
|
-
const m = /^(\s*)(?:async\s+)?def\s+(test_\w+)|^(\s*)class\s+(Test\w*)/.exec(line);
|
|
294
|
-
if (!m) return;
|
|
295
|
-
const indent = (m[1] ?? m[3]).length;
|
|
296
|
-
let end = i;
|
|
297
|
-
for (let k = i + 1; k < lines.length; k++) {
|
|
298
|
-
if (!lines[k].trim() || /^\s*#/.test(lines[k])) continue;
|
|
299
|
-
if (indentOf(lines[k]) <= indent) break;
|
|
300
|
-
end = k;
|
|
301
|
-
}
|
|
302
|
-
const start = i + 1;
|
|
303
|
-
const attach = attachedAbove(lines, start, /^\s*[#@]/);
|
|
304
|
-
out.push({ start, attach, end: end + 1, title: m[2] ?? m[4], kind: 'test' });
|
|
305
|
-
});
|
|
306
|
-
return out;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/** Test functions of a Go test file. @param {string} text @returns {Block[]} */
|
|
310
|
-
function goBlocks(text) {
|
|
311
|
-
const cls = lex(text);
|
|
312
|
-
const { lineOf } = lineIndex(text);
|
|
313
|
-
const lines = text.split('\n');
|
|
314
|
-
return [...text.matchAll(/^func\s+(Test\w*)\s*\(/gm)].flatMap((m) => {
|
|
315
|
-
const brace = text.indexOf('{', m.index ?? 0);
|
|
316
|
-
const end = brace < 0 ? -1 : matchClose(text, cls, brace);
|
|
317
|
-
if (end < 0) return [];
|
|
318
|
-
const start = lineOf(m.index ?? 0);
|
|
319
|
-
const attach = attachedAbove(lines, start, C_COMMENT_LINE);
|
|
320
|
-
return [{ start, attach, end: lineOf(end), title: m[1], kind: /** @type {const} */ ('test') }];
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Every test call and type assertion of a test file.
|
|
326
|
-
* @param {string} file
|
|
327
|
-
* @param {string} text
|
|
328
|
-
* @returns {Block[]}
|
|
329
|
-
*/
|
|
330
|
-
export function testBlocks(file, text) {
|
|
331
|
-
if (/\.py$/.test(file)) return pythonBlocks(text);
|
|
332
|
-
if (/\.go$/.test(file)) return goBlocks(text);
|
|
333
|
-
return isCLike(file) ? cLikeBlocks(text) : [];
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* The innermost block a 1-based line sits in, or in whose attached comment run it sits.
|
|
338
|
-
* @param {Block[]} blocks
|
|
339
|
-
* @param {number} line
|
|
340
|
-
*/
|
|
341
|
-
export function blockAt(blocks, line) {
|
|
342
|
-
let best = /** @type {Block | null} */ (null);
|
|
343
|
-
for (const b of blocks)
|
|
344
|
-
if (b.attach <= line && line <= b.end && (!best || b.end - b.attach < best.end - best.attach)) best = b;
|
|
345
|
-
return best;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* A source's text with its comments blanked to spaces, line breaks kept, so a term search finds code, not prose.
|
|
350
|
-
* @param {string} file
|
|
351
|
-
* @param {string} text
|
|
352
|
-
*/
|
|
353
|
-
export function stripComments(file, text) {
|
|
354
|
-
if (/\.json$/i.test(file)) return text;
|
|
355
|
-
if (!isCLike(file))
|
|
356
|
-
return text
|
|
357
|
-
.split('\n')
|
|
358
|
-
.map((l) => l.replace(/(^|\s)#.*$/, '$1'))
|
|
359
|
-
.join('\n');
|
|
360
|
-
const cls = lex(text);
|
|
361
|
-
const chars = text.split('');
|
|
362
|
-
for (let i = 0; i < chars.length; i++) if (cls[i] === COMMENT && chars[i] !== '\n') chars[i] = ' ';
|
|
363
|
-
return chars.join('');
|
|
364
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
/** Text helpers shared by every steward command: slugs, tokens, RFC 2119 keywords, absolutes. */
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* GitHub/Docusaurus heading slug (github-slugger semantics): lowercase, drop punctuation and symbols, every space
|
|
6
|
-
* becomes a hyphen (runs are NOT collapsed, so `a — b` is `a--b`). `seen` makes repeats unique the way a rendered
|
|
7
|
-
* page does (`x`, `x-1`, `x-2`).
|
|
8
|
-
*
|
|
9
|
-
* @param {string} heading
|
|
10
|
-
* @param {Map<string, number>} [seen]
|
|
11
|
-
*/
|
|
12
|
-
export function slugify(heading, seen) {
|
|
13
|
-
const base = heading
|
|
14
|
-
.toLowerCase()
|
|
15
|
-
.trim()
|
|
16
|
-
.replace(/[^\p{L}\p{N}\p{M}\p{Pc} -]/gu, '')
|
|
17
|
-
.replace(/ /g, '-');
|
|
18
|
-
if (!seen) return base;
|
|
19
|
-
let slug = base;
|
|
20
|
-
while (seen.has(slug)) {
|
|
21
|
-
const next = (seen.get(base) ?? 0) + 1;
|
|
22
|
-
seen.set(base, next);
|
|
23
|
-
slug = `${base}-${next}`;
|
|
24
|
-
}
|
|
25
|
-
seen.set(slug, 0);
|
|
26
|
-
return slug;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Inline code spans removed: a keyword in backticks is mentioned, not issued. @param {string} text */
|
|
30
|
-
export const stripCode = (text) => text.replace(/`[^`\n]*`/g, ' ');
|
|
31
|
-
|
|
32
|
-
/** Whitespace-normalized text, for comparing blocks regardless of wrapping. @param {string} text */
|
|
33
|
-
export const normalize = (text) => text.replace(/\s+/g, ' ').trim();
|
|
34
|
-
|
|
35
|
-
/** The RFC 2119 keywords, a NOT form counted once (NEVER and ALWAYS are not among them). */
|
|
36
|
-
export const STRONG = /\b(MUST NOT|MUST|SHALL NOT|SHALL|REQUIRED)\b/g;
|
|
37
|
-
export const WEAK = /\b(SHOULD NOT|SHOULD|RECOMMENDED|MAY|OPTIONAL)\b/g;
|
|
38
|
-
|
|
39
|
-
/** @param {string} text */
|
|
40
|
-
export const strongCount = (text) => (stripCode(text).match(STRONG) ?? []).length;
|
|
41
|
-
/** @param {string} text */
|
|
42
|
-
export const weakCount = (text) => (stripCode(text).match(WEAK) ?? []).length;
|
|
43
|
-
|
|
44
|
-
export const ABSOLUTE = /\b(never|always|only|every|all|none|no|any)\b/gi;
|
|
45
|
-
export const EXCEPTION =
|
|
46
|
-
/\b(except|unless|where possible|if possible|when feasible|as appropriate|otherwise|instead)\b/gi;
|
|
47
|
-
|
|
48
|
-
/** Sentences of a block, split on terminal punctuation or list bullets. @param {string} text */
|
|
49
|
-
export const sentences = (text) =>
|
|
50
|
-
stripCode(text)
|
|
51
|
-
.split(/(?<=[.!?])\s+|\n\s*[-*]\s+|\n{2,}/)
|
|
52
|
-
.map((s) => s.trim())
|
|
53
|
-
.filter(Boolean);
|
|
54
|
-
|
|
55
|
-
/** Absolutes issued inside obligation sentences (an absolute in plain prose obliges nothing). @param {string} text */
|
|
56
|
-
export const absolutesIn = (text) =>
|
|
57
|
-
sentences(text)
|
|
58
|
-
.filter((s) => /\b(MUST|SHALL|REQUIRED)\b/.test(s))
|
|
59
|
-
.flatMap((s) => (s.match(ABSOLUTE) ?? []).map((w) => w.toLowerCase()));
|
|
60
|
-
|
|
61
|
-
/** @param {string} text */
|
|
62
|
-
export const exceptionsIn = (text) => (stripCode(text).match(EXCEPTION) ?? []).map((w) => w.toLowerCase());
|
|
63
|
-
|
|
64
|
-
const STOP = new Set(
|
|
65
|
-
'the a an and or of to in on for with by from at as is are be been was were it its this that these those when then which who what where while if else not no any all each every one two than into onto via per their there them they his her our your can may must shall should will would could does do did has have had only also more most less such same other another so but nor yet very just over under between within without about after before during because both either neither'.split(
|
|
66
|
-
' ',
|
|
67
|
-
),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
/** Content words (lowercase, ≥ 4 letters, no stopwords). @param {string} text */
|
|
71
|
-
export const words = (text) =>
|
|
72
|
-
(
|
|
73
|
-
stripCode(text)
|
|
74
|
-
.toLowerCase()
|
|
75
|
-
.match(/[\p{L}][\p{L}\p{N}_-]{3,}/gu) ?? []
|
|
76
|
-
)
|
|
77
|
-
.filter((w) => !STOP.has(w))
|
|
78
|
-
.map((w) => (w.length > 4 ? w.replace(/(?:ing|ed|es|s)$/, '') : w));
|
|
79
|
-
|
|
80
|
-
/** Backticked terms — the identifiers a requirement names. @param {string} text */
|
|
81
|
-
export const codeTokens = (text) => [...text.matchAll(/`([^`\n]{2,80})`/g)].map((m) => m[1].trim());
|
|
82
|
-
|
|
83
|
-
/** Share of `a`'s distinct words that also occur in `b`. @param {string[]} a @param {string[]} b */
|
|
84
|
-
export const containment = (a, b) => {
|
|
85
|
-
const sa = new Set(a);
|
|
86
|
-
if (!sa.size) return 0;
|
|
87
|
-
const sb = new Set(b);
|
|
88
|
-
let hit = 0;
|
|
89
|
-
for (const w of sa) if (sb.has(w)) hit++;
|
|
90
|
-
return hit / sa.size;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/** Jaccard similarity of two word lists. @param {string[]} a @param {string[]} b */
|
|
94
|
-
export const jaccard = (a, b) => {
|
|
95
|
-
const sa = new Set(a);
|
|
96
|
-
const sb = new Set(b);
|
|
97
|
-
if (!sa.size && !sb.size) return 1;
|
|
98
|
-
let inter = 0;
|
|
99
|
-
for (const w of sa) if (sb.has(w)) inter++;
|
|
100
|
-
return inter / (sa.size + sb.size - inter);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/** Word count of a block. @param {string} text */
|
|
104
|
-
export const wordCount = (text) => (text.match(/\S+/g) ?? []).length;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Minimal argv parser: `--key value`, `--key=value`, `--flag`, repeated keys collect into arrays. A key listed in
|
|
108
|
-
* `booleans` never takes the next argument as its value.
|
|
109
|
-
* @param {string[]} argv
|
|
110
|
-
* @param {string[]} [repeatable]
|
|
111
|
-
* @param {string[]} [booleans]
|
|
112
|
-
*/
|
|
113
|
-
export function parseArgs(argv, repeatable = [], booleans = []) {
|
|
114
|
-
/** @type {Record<string, any>} */
|
|
115
|
-
const opts = { _: [] };
|
|
116
|
-
for (let i = 0; i < argv.length; i++) {
|
|
117
|
-
const arg = argv[i];
|
|
118
|
-
if (!arg.startsWith('--')) {
|
|
119
|
-
opts._.push(arg);
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
const eq = arg.indexOf('=');
|
|
123
|
-
const key = eq > 0 ? arg.slice(2, eq) : arg.slice(2);
|
|
124
|
-
let value = eq > 0 ? arg.slice(eq + 1) : true;
|
|
125
|
-
if (value === true && !booleans.includes(key) && argv[i + 1] !== undefined && !argv[i + 1].startsWith('--'))
|
|
126
|
-
value = argv[++i];
|
|
127
|
-
if (repeatable.includes(key)) (opts[key] ??= []).push(value);
|
|
128
|
-
else opts[key] = value;
|
|
129
|
-
}
|
|
130
|
-
return opts;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** @param {string} s */
|
|
134
|
-
const escapeRe = (s) => s.replace(/[.+^$()|[\]\\]/g, '\\$&');
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* A repository-relative glob as a RegExp: `**` spans directories, `*` and `?` stay inside one, `{a,b}` alternates.
|
|
138
|
-
* @param {string} glob
|
|
139
|
-
*/
|
|
140
|
-
export function globToRegExp(glob) {
|
|
141
|
-
let re = '';
|
|
142
|
-
for (let i = 0; i < glob.length; i++) {
|
|
143
|
-
const c = glob[i];
|
|
144
|
-
const end = c === '{' ? glob.indexOf('}', i) : -1;
|
|
145
|
-
if (c === '*' && glob[i + 1] === '*') {
|
|
146
|
-
const slash = glob[i + 2] === '/';
|
|
147
|
-
re += slash ? '(?:.*/)?' : '.*';
|
|
148
|
-
i += slash ? 2 : 1;
|
|
149
|
-
} else if (c === '*') re += '[^/]*';
|
|
150
|
-
else if (c === '?') re += '[^/]';
|
|
151
|
-
else if (end > i) {
|
|
152
|
-
re += `(?:${glob
|
|
153
|
-
.slice(i + 1, end)
|
|
154
|
-
.split(',')
|
|
155
|
-
.map(escapeRe)
|
|
156
|
-
.join('|')})`;
|
|
157
|
-
i = end;
|
|
158
|
-
} else re += escapeRe(c);
|
|
159
|
-
}
|
|
160
|
-
return new RegExp(`^${re}$`);
|
|
161
|
-
}
|