@zgeoff/chex 0.5.0 → 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.
@@ -34,14 +34,10 @@ export async function runTestTimeoutsCheck(
34
34
  );
35
35
 
36
36
  return paths
37
- .filter((path) => checkedFiles.has(basename(path)) || configPattern.test(basename(path)))
37
+ .filter((path) => checkedFiles.has(basename(path)))
38
38
  .flatMap((path) => {
39
39
  const text = runGit(options, ['show', `HEAD:${path}`]);
40
40
 
41
- if (configPattern.test(basename(path))) {
42
- return collectConfigFindings(path, text);
43
- }
44
-
45
41
  return basename(path) === 'package.json'
46
42
  ? collectScriptFindings(path, text, limits)
47
43
  : collectBunfigFindings(
@@ -52,8 +48,6 @@ export async function runTestTimeoutsCheck(
52
48
  });
53
49
  }
54
50
 
55
- const configPattern = /^\.?oxlintrc(?:\.[\w-]+)?\.jsonc?$/u;
56
-
57
51
  const checkedFiles = new Set(['package.json', 'bunfig.toml']);
58
52
 
59
53
  function runGit(options: Readonly<TestTimeoutsCheckOptions>, args: readonly string[]): string {
@@ -111,187 +105,6 @@ async function readLaneLimits(
111
105
  return parsed.limits;
112
106
  }
113
107
 
114
- // A setting that turns a timeout rule off or down to a warning escapes the limits, so it needs a
115
- // `floor:` comment with the evidence, on its line or the lines above it.
116
- function collectConfigFindings(path: string, text: string): TestTimeoutFinding[] {
117
- const tokens = parseJSONCTokens(path, text);
118
- const values = tokens.filter((token) => token.kind !== 'comment');
119
- const comments = tokens.filter((token) => token.kind === 'comment');
120
-
121
- const valueLines = new Set(values.map((token) => token.line));
122
-
123
- const overrides = collectOverrideLines(values);
124
-
125
- return values.flatMap((token, index) => {
126
- const rule =
127
- token.kind === 'string' ? guardedRules.find((name) => name === token.text) : undefined;
128
-
129
- if (rule === undefined || values[index + 1]?.text !== ':') {
130
- return [];
131
- }
132
-
133
- const first = values[index + 2];
134
- const severity = first?.text === '[' ? values[index + 3] : first;
135
-
136
- if (severity === undefined || !weakSeverities.has(severity.text)) {
137
- return [];
138
- }
139
-
140
- const override = overrides.get(index);
141
-
142
- const notes = comments.filter(
143
- (comment) =>
144
- (comment.line >= token.line && comment.line <= severity.line) ||
145
- isCommentAbove(comment, token.line, valueLines) ||
146
- (override !== undefined &&
147
- (comment.line === override || isCommentAbove(comment, override, valueLines))),
148
- );
149
-
150
- if (notes.some((comment) => floorPattern.test(comment.text))) {
151
- return [];
152
- }
153
-
154
- return [
155
- {
156
- path,
157
- message: `line ${token.line} turns ${rule} off or down to a warning; fix the slow test, or give the setting a comment with "floor:" and the evidence that the time cannot move, as the "Slow tests" section of the testing skill describes`,
158
- },
159
- ];
160
- });
161
- }
162
-
163
- const weakSeverities = new Set(['off', 'warn', 'allow', '0', '1']);
164
-
165
- interface JSONCToken {
166
- readonly kind: 'string' | 'comment' | 'other';
167
-
168
- // A string's or a comment's content, or the token's own text.
169
- readonly text: string;
170
-
171
- // The 1-based lines the token starts and ends on.
172
- readonly line: number;
173
- readonly endLine: number;
174
- }
175
-
176
- function parseJSONCTokens(path: string, text: string): JSONCToken[] {
177
- const tokens: JSONCToken[] = [];
178
- let line = 1;
179
- let index = 0;
180
-
181
- const advance = (end: number): number => {
182
- const start = line;
183
-
184
- for (let at = index; at < end; at += 1) {
185
- if (text.charAt(at) === '\n') {
186
- line += 1;
187
- }
188
- }
189
-
190
- index = end;
191
-
192
- return start;
193
- };
194
-
195
- while (index < text.length) {
196
- const character = text.charAt(index);
197
- const next = text.charAt(index + 1);
198
-
199
- if (/\s/u.test(character)) {
200
- advance(index + 1);
201
- } else if (character === '/' && (next === '/' || next === '*')) {
202
- const close = next === '/' ? text.indexOf('\n', index) : text.indexOf('*/', index + 2);
203
- const end = close === -1 ? text.length : close;
204
- const body = text.slice(index + 2, end);
205
- const after = next === '/' || close === -1 ? end : end + 2;
206
- const start = advance(after);
207
-
208
- tokens.push({ kind: 'comment', text: body, line: start, endLine: line });
209
- } else if (character === '"') {
210
- const end = getStringEnd(path, text, index);
211
- const body = parseText(path, text.slice(index, end), (input) => JSON.parse(input));
212
- const start = advance(end);
213
-
214
- tokens.push({ kind: 'string', text: String(body), line: start, endLine: line });
215
- } else {
216
- const word = /^[\w.+-]+/u.exec(text.slice(index))?.[0] ?? character;
217
- const start = advance(index + word.length);
218
-
219
- tokens.push({ kind: 'other', text: word, line: start, endLine: line });
220
- }
221
- }
222
-
223
- return tokens;
224
- }
225
-
226
- function getStringEnd(path: string, text: string, start: number): number {
227
- for (let index = start + 1; index < text.length; index += 1) {
228
- const character = text.charAt(index);
229
-
230
- if (character === '\\') {
231
- index += 1;
232
- } else if (character === '"') {
233
- return index + 1;
234
- }
235
- }
236
-
237
- throw new Error(`cannot parse ${path}: a string never closes`);
238
- }
239
-
240
- function parseText(path: string, text: string, parse: (input: string) => unknown): unknown {
241
- try {
242
- return parse(text);
243
- } catch (error) {
244
- throw new Error(
245
- `cannot parse ${path}: ${error instanceof Error ? error.message : String(error)}`,
246
- { cause: error },
247
- );
248
- }
249
- }
250
-
251
- function collectOverrideLines(values: readonly JSONCToken[]): ReadonlyMap<number, number> {
252
- const lines = new Map<number, number>();
253
-
254
- const open: number[] = [];
255
-
256
- for (const [index, token] of values.entries()) {
257
- if (token.text === '{') {
258
- open.push(index);
259
- } else if (token.text === '}') {
260
- open.pop();
261
- }
262
-
263
- const override = open.length >= 3 ? values[open[1] ?? 0] : undefined;
264
-
265
- if (override !== undefined) {
266
- lines.set(index, override.line);
267
- }
268
- }
269
-
270
- return lines;
271
- }
272
-
273
- // a comment above the setting counts while no other value sits between them
274
- function isCommentAbove(
275
- comment: JSONCToken,
276
- line: number,
277
- valueLines: ReadonlySet<number>,
278
- ): boolean {
279
- if (comment.endLine >= line) {
280
- return false;
281
- }
282
-
283
- for (let between = comment.endLine + 1; between < line; between += 1) {
284
- if (valueLines.has(between)) {
285
- return false;
286
- }
287
- }
288
-
289
- return !valueLines.has(comment.line);
290
- }
291
-
292
- const unitLimitMs = 5000;
293
- const slowTestsHint = 'find its cause in the "Slow tests" section of the testing skill and fix it';
294
-
295
108
  function collectScriptFindings(
296
109
  path: string,
297
110
  text: string,
@@ -329,6 +142,20 @@ function collectScriptFindings(
329
142
  });
330
143
  }
331
144
 
145
+ function parseText(path: string, text: string, parse: (input: string) => unknown): unknown {
146
+ try {
147
+ return parse(text);
148
+ } catch (error) {
149
+ throw new Error(
150
+ `cannot parse ${path}: ${error instanceof Error ? error.message : String(error)}`,
151
+ { cause: error },
152
+ );
153
+ }
154
+ }
155
+
156
+ const unitLimitMs = 5000;
157
+ const slowTestsHint = 'find its cause in the "Slow tests" section of the testing skill and fix it';
158
+
332
159
  interface LaneLimit {
333
160
  readonly lane: string;
334
161
  readonly limitMs: number;
@@ -503,11 +330,6 @@ function findDirectoryLane(path: string, appRoots: ReadonlySet<string>): string
503
330
  return null;
504
331
  }
505
332
 
506
- const guardedRules = ['zgeoff/max-test-timeout'];
507
-
508
- // a block comment's continuation stars are no evidence
509
- const floorPattern = /\bfloor:[\s*]*[^\s*]/u;
510
-
511
333
  // Returns the exit code: 0 when clean, 1 with a finding, and 2 when the check cannot run.
512
334
  export async function runTestTimeoutsCommand(): Promise<number> {
513
335
  try {