@zgeoff/chex 0.4.8 → 0.6.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 +2 -2
- package/lefthook.yml +2 -0
- package/package.json +9 -7
- package/src/cli/check-names.ts +1 -0
- package/src/cli.ts +2 -0
- package/src/lint-overrides/collect-lint-override-findings.ts +130 -0
- package/src/lint-overrides/parse-jsonc-tokens.ts +84 -0
- package/src/lint-overrides/parse-jsonc-tree.ts +104 -0
- package/src/lint-overrides/run-lint-overrides-check.ts +81 -0
- package/src/lint-overrides/std-helper-paths.ts +11 -0
- package/src/test-timeouts/run-test-timeouts-check.ts +15 -196
package/README.md
CHANGED
|
@@ -57,6 +57,7 @@ The repo checks read the branch, the pushed commits and the repo's config:
|
|
|
57
57
|
| `commits` | a pushed commit whose message fails `@zgeoff/commitlint-config` | `--pushed-refs` |
|
|
58
58
|
| `local-paths` | a line a pushed commit adds that holds the home folder or the host name | `--pushed-refs` |
|
|
59
59
|
| `test-timeouts` | a `bun test` timeout above its lane's limit, in a script or a `bunfig.toml` | none |
|
|
60
|
+
| `lint-overrides` | an `.oxlintrc*.json` that turns a `zgeoff/*` rule, `consistent-type-assertions` or `ban-ts-comment` off or down to a warning | none |
|
|
60
61
|
| `test-layout` | a test folder, file name or import outside [the test layout](../../docs/guides/test-layout.md); test code includes a relative `[test] preload` file, a `.nix`-only `tests/` folder and a private workspace that only test code imports | none |
|
|
61
62
|
| `workflows` | nothing: it prints each fault of the shared workflow lint, and fails when there is one | the paths |
|
|
62
63
|
| `dependency-gates` | a stale age-gate exclude, an override or an audit ignore that nothing needs, a first-party package with no age-gate exclude, or a pinned release younger than the age gate ([details](../../docs/architecture/dependency-updates.md#the-dependency-gates-check)) | none |
|
|
@@ -69,8 +70,7 @@ under `docs/`, each spike's `results.md`, and the Markdown under `.agents/skills
|
|
|
69
70
|
stdin, as a pre-push hook gets them; without it, `commits` and `local-paths` read the branch's
|
|
70
71
|
commits since its base.
|
|
71
72
|
|
|
72
|
-
Some docs checks skip files
|
|
73
|
-
skipped file back:
|
|
73
|
+
Some docs checks skip files even when you name them:
|
|
74
74
|
|
|
75
75
|
- `prose` needs `perl` and a `git` with PCRE, and fails with exit 2 when it lacks one.
|
|
76
76
|
- `prose` skips the `docs-writing`, `project-docs-writing`, `skill-writing` and `deslop` skills
|
package/lefthook.yml
CHANGED
|
@@ -139,6 +139,8 @@ pre-push:
|
|
|
139
139
|
--pushed-refs # {files}'
|
|
140
140
|
- name: chex-test-timeouts
|
|
141
141
|
run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex test-timeouts'
|
|
142
|
+
- name: chex-lint-overrides
|
|
143
|
+
run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex lint-overrides'
|
|
142
144
|
- name: chex-dependency-gates
|
|
143
145
|
run: '{clear-git-env} bun x --no-install diet -- bun x --no-install chex dependency-gates'
|
|
144
146
|
- name: chex-typecheck
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/chex",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Run each shared docs and repo check as one subcommand, with the lefthook pre-push baseline that calls them",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"checks",
|
|
@@ -77,20 +77,22 @@
|
|
|
77
77
|
"typecheck": "tsrs --fast"
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@zgeoff/std": "0.
|
|
81
|
-
"oxc-parser": "0.
|
|
80
|
+
"@zgeoff/std": "0.6.0",
|
|
81
|
+
"oxc-parser": "0.160.0",
|
|
82
82
|
"ts-pattern": "5.9.2"
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
85
|
"@types/bun": "1.4.4",
|
|
86
|
-
"@zgeoff/
|
|
87
|
-
"@zgeoff/
|
|
88
|
-
"@zgeoff/
|
|
86
|
+
"@zgeoff/oxlint-config": "0.13.0",
|
|
87
|
+
"@zgeoff/rig": "0.3.5",
|
|
88
|
+
"@zgeoff/std": "0.6.0",
|
|
89
|
+
"@zgeoff/testing": "0.5.1",
|
|
89
90
|
"@zgeoff/tools-scripts": "0.0.0",
|
|
90
91
|
"msw": "3.0.4",
|
|
91
92
|
"tiny-invariant": "1.3.3",
|
|
92
93
|
"tsrs": "0.25.0-ts7.1.0-beta",
|
|
93
|
-
"typescript": "7.0.2"
|
|
94
|
+
"typescript": "7.0.2",
|
|
95
|
+
"zod": "4.6.7"
|
|
94
96
|
},
|
|
95
97
|
"engines": {
|
|
96
98
|
"bun": ">=1.3"
|
package/src/cli/check-names.ts
CHANGED
package/src/cli.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { runChex } from './cli/run-chex.ts';
|
|
|
8
8
|
import { runCommitsCommand } from './commits/run-commits-check.ts';
|
|
9
9
|
import { runDependencyGatesCommand } from './dependency-gates/run-dependency-gates-check.ts';
|
|
10
10
|
import { runInit } from './init/run-init.ts';
|
|
11
|
+
import { runLintOverridesCommand } from './lint-overrides/run-lint-overrides-check.ts';
|
|
11
12
|
import { runLocalPathsCommand } from './local-paths/run-local-paths-check.ts';
|
|
12
13
|
import { writeTextFile } from './output/write-text-file.ts';
|
|
13
14
|
import { runPrBodyCheck } from './pr-body/run-pr-body-check.ts';
|
|
@@ -24,6 +25,7 @@ process.exitCode = await runChex(process.argv.slice(2), {
|
|
|
24
25
|
commits: runCommitsCommand,
|
|
25
26
|
'local-paths': runLocalPathsCommand,
|
|
26
27
|
'test-timeouts': runTestTimeoutsCommand,
|
|
28
|
+
'lint-overrides': runLintOverridesCommand,
|
|
27
29
|
'test-layout': runTestLayoutCommand,
|
|
28
30
|
workflows: runWorkflowsCommand,
|
|
29
31
|
'dependency-gates': runDependencyGatesCommand,
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { JSONCEntry, JSONCNode } from './parse-jsonc-tree.ts';
|
|
2
|
+
import { parseJSONCTree } from './parse-jsonc-tree.ts';
|
|
3
|
+
import { stdHelperPaths } from './std-helper-paths.ts';
|
|
4
|
+
|
|
5
|
+
export interface LintOverrideFinding {
|
|
6
|
+
// Relative to the repo root.
|
|
7
|
+
readonly path: string;
|
|
8
|
+
|
|
9
|
+
readonly message: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function collectLintOverrideFindings(
|
|
13
|
+
path: string,
|
|
14
|
+
text: string,
|
|
15
|
+
isToolsRepo: boolean,
|
|
16
|
+
): LintOverrideFinding[] {
|
|
17
|
+
return collectSettings(parseJSONCTree(path, text), isToolsRepo).map((setting) => ({
|
|
18
|
+
path,
|
|
19
|
+
message: `line ${setting.line} sets ${setting.rule} to ${setting.severity}; ${pickFix(setting.rule)}`,
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface WeakSetting {
|
|
24
|
+
readonly rule: string;
|
|
25
|
+
readonly severity: string;
|
|
26
|
+
readonly line: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// oxlint reads the rules of the config root and of each override, and no `rules` key deeper, such as
|
|
30
|
+
// one under `settings`
|
|
31
|
+
function collectSettings(root: JSONCNode, isToolsRepo: boolean): WeakSetting[] {
|
|
32
|
+
if (root.kind !== 'object') {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const overrides = findEntry(root.entries, 'overrides')?.value;
|
|
37
|
+
const items = overrides?.kind === 'array' ? overrides.items : [];
|
|
38
|
+
|
|
39
|
+
return [
|
|
40
|
+
...collectRuleSettings(root.entries, false),
|
|
41
|
+
...items.flatMap((item) =>
|
|
42
|
+
item.kind === 'object'
|
|
43
|
+
? collectRuleSettings(item.entries, isToolsRepo && isStdHelperFiles(item.entries))
|
|
44
|
+
: [],
|
|
45
|
+
),
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function findEntry(entries: readonly JSONCEntry[], key: string): JSONCEntry | undefined {
|
|
50
|
+
return entries.find((entry) => entry.key === key);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function collectRuleSettings(
|
|
54
|
+
entries: readonly JSONCEntry[],
|
|
55
|
+
isHelperOverride: boolean,
|
|
56
|
+
): WeakSetting[] {
|
|
57
|
+
const rules = findEntry(entries, 'rules')?.value;
|
|
58
|
+
|
|
59
|
+
return rules?.kind === 'object'
|
|
60
|
+
? rules.entries.flatMap((rule) => collectWeakSettings(rule, isHelperOverride))
|
|
61
|
+
: [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// oxlint reads a rule under any plugin prefix, such as `@typescript-eslint/ban-ts-comment`, or under
|
|
65
|
+
// its bare name.
|
|
66
|
+
const lockedPattern = /^(?:[^/\s]+\/)?(?:consistent-type-assertions|ban-ts-comment)$/u;
|
|
67
|
+
|
|
68
|
+
const weakSeverities = new Set(['off', 'warn', 'allow', '0', '1']);
|
|
69
|
+
|
|
70
|
+
const helperRule = 'typescript/consistent-type-assertions';
|
|
71
|
+
|
|
72
|
+
function collectWeakSettings(entry: JSONCEntry, isHelperOverride: boolean): WeakSetting[] {
|
|
73
|
+
const isRefused = entry.key.startsWith('zgeoff/') || lockedPattern.test(entry.key);
|
|
74
|
+
const severity = pickSeverity(entry.value);
|
|
75
|
+
|
|
76
|
+
if (!isRefused || severity === null || !weakSeverities.has(severity)) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (isHelperOverride && entry.key === helperRule && isBareOff(entry.value)) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return [{ rule: entry.key, severity, line: entry.line }];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// the severity is the value, or the first element of an array
|
|
88
|
+
function pickSeverity(value: JSONCNode): string | null {
|
|
89
|
+
const first = value.kind === 'array' ? value.items[0] : value;
|
|
90
|
+
|
|
91
|
+
return first?.kind === 'literal' ? first.text : null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// the lint rule accepts the string alone, and no array that holds it
|
|
95
|
+
function isBareOff(value: JSONCNode): boolean {
|
|
96
|
+
return value.kind === 'literal' && value.text === 'off';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function isStdHelperFiles(entries: readonly JSONCEntry[]): boolean {
|
|
100
|
+
const files = entries.find((entry) => entry.key === 'files')?.value;
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
files?.kind === 'array' &&
|
|
104
|
+
files.items.length > 0 &&
|
|
105
|
+
files.items.every((item) => item.kind === 'literal' && stdHelperPaths.includes(item.text))
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const optionFixes = new Map([
|
|
110
|
+
['zgeoff/no-abbreviated-names', 'add the key to the `allowedKeys` option of the rule'],
|
|
111
|
+
['zgeoff/file-named-for-export', 'add the file to the `loaderFiles` option of the rule'],
|
|
112
|
+
['zgeoff/function-verb', 'add the name to the `exemptNames` option of the rule'],
|
|
113
|
+
['zgeoff/verb-contract', 'add the name to the `exemptNames` option of the rule'],
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
function pickFix(rule: string): string {
|
|
117
|
+
if (lockedPattern.test(rule)) {
|
|
118
|
+
return 'fix the types with a type guard, an annotation, `satisfies`, a schema parse or a `@zgeoff/std` helper, and set no override';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const optionFix = optionFixes.get(rule);
|
|
122
|
+
|
|
123
|
+
if (optionFix !== undefined) {
|
|
124
|
+
return `fix the code, or ${optionFix}; delete the setting`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return rule === 'zgeoff/max-test-timeout'
|
|
128
|
+
? "find the slow test's cause, then rewrite or split it to finish within its lane's limit; delete the setting"
|
|
129
|
+
: 'fix the code and delete the setting: no override exempts this rule';
|
|
130
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export interface JSONCToken {
|
|
2
|
+
readonly kind: 'string' | 'other';
|
|
3
|
+
|
|
4
|
+
// A string's content, or the token's own text.
|
|
5
|
+
readonly text: string;
|
|
6
|
+
|
|
7
|
+
// The 1-based line the token starts on.
|
|
8
|
+
readonly line: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Reads the tokens of a JSON file that holds comments and trailing commas, as oxlint accepts. The
|
|
12
|
+
// comments are dropped.
|
|
13
|
+
export function parseJSONCTokens(path: string, text: string): JSONCToken[] {
|
|
14
|
+
const tokens: JSONCToken[] = [];
|
|
15
|
+
let line = 1;
|
|
16
|
+
let index = 0;
|
|
17
|
+
|
|
18
|
+
const advance = (end: number): number => {
|
|
19
|
+
const start = line;
|
|
20
|
+
|
|
21
|
+
for (let at = index; at < end; at += 1) {
|
|
22
|
+
if (text.charAt(at) === '\n') {
|
|
23
|
+
line += 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
index = end;
|
|
28
|
+
|
|
29
|
+
return start;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
while (index < text.length) {
|
|
33
|
+
const character = text.charAt(index);
|
|
34
|
+
const next = text.charAt(index + 1);
|
|
35
|
+
|
|
36
|
+
if (/\s/u.test(character)) {
|
|
37
|
+
advance(index + 1);
|
|
38
|
+
} else if (character === '/' && (next === '/' || next === '*')) {
|
|
39
|
+
const close = next === '/' ? text.indexOf('\n', index) : text.indexOf('*/', index + 2);
|
|
40
|
+
const end = close === -1 ? text.length : close;
|
|
41
|
+
const after = next === '/' || close === -1 ? end : end + 2;
|
|
42
|
+
|
|
43
|
+
advance(after);
|
|
44
|
+
} else if (character === '"') {
|
|
45
|
+
const end = getStringEnd(path, text, index);
|
|
46
|
+
const body = parseString(path, text.slice(index, end));
|
|
47
|
+
const start = advance(end);
|
|
48
|
+
|
|
49
|
+
tokens.push({ kind: 'string', text: body, line: start });
|
|
50
|
+
} else {
|
|
51
|
+
const word = /^[\w.+-]+/u.exec(text.slice(index))?.[0] ?? character;
|
|
52
|
+
const start = advance(index + word.length);
|
|
53
|
+
|
|
54
|
+
tokens.push({ kind: 'other', text: word, line: start });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return tokens;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getStringEnd(path: string, text: string, start: number): number {
|
|
62
|
+
for (let index = start + 1; index < text.length; index += 1) {
|
|
63
|
+
const character = text.charAt(index);
|
|
64
|
+
|
|
65
|
+
if (character === '\\') {
|
|
66
|
+
index += 1;
|
|
67
|
+
} else if (character === '"') {
|
|
68
|
+
return index + 1;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
throw new Error(`cannot parse ${path}: a string never closes`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parseString(path: string, literal: string): string {
|
|
76
|
+
try {
|
|
77
|
+
return String(JSON.parse(literal));
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`cannot parse ${path}: ${error instanceof Error ? error.message : String(error)}`,
|
|
81
|
+
{ cause: error },
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { JSONCToken } from './parse-jsonc-tokens.ts';
|
|
2
|
+
import { parseJSONCTokens } from './parse-jsonc-tokens.ts';
|
|
3
|
+
|
|
4
|
+
export type JSONCNode =
|
|
5
|
+
| { readonly kind: 'object'; readonly entries: readonly JSONCEntry[] }
|
|
6
|
+
| { readonly kind: 'array'; readonly items: readonly JSONCNode[] }
|
|
7
|
+
| { readonly kind: 'literal'; readonly text: string; readonly line: number };
|
|
8
|
+
|
|
9
|
+
// Reads a JSON file that holds comments and trailing commas into a tree that keeps each key's line.
|
|
10
|
+
export function parseJSONCTree(path: string, text: string): JSONCNode {
|
|
11
|
+
const tokens = parseJSONCTokens(path, text);
|
|
12
|
+
const cursor = { index: 0 };
|
|
13
|
+
const root = parseNode(path, tokens, cursor);
|
|
14
|
+
|
|
15
|
+
if (cursor.index < tokens.length) {
|
|
16
|
+
throw new Error(`cannot parse ${path}: unexpected text after the first value`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return root;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Cursor {
|
|
23
|
+
index: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function parseNode(path: string, tokens: readonly JSONCToken[], cursor: Cursor): JSONCNode {
|
|
27
|
+
const token = tokens[cursor.index];
|
|
28
|
+
|
|
29
|
+
if (token === undefined) {
|
|
30
|
+
throw new Error(`cannot parse ${path}: the file ends inside a value`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
cursor.index += 1;
|
|
34
|
+
|
|
35
|
+
if (token.kind === 'other' && token.text === '{') {
|
|
36
|
+
return { kind: 'object', entries: parseEntries(path, tokens, cursor) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (token.kind === 'other' && token.text === '[') {
|
|
40
|
+
return { kind: 'array', items: parseItems(path, tokens, cursor) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { kind: 'literal', text: token.text, line: token.line };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface JSONCEntry {
|
|
47
|
+
readonly key: string;
|
|
48
|
+
readonly line: number;
|
|
49
|
+
readonly value: JSONCNode;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function parseEntries(path: string, tokens: readonly JSONCToken[], cursor: Cursor): JSONCEntry[] {
|
|
53
|
+
const entries: JSONCEntry[] = [];
|
|
54
|
+
|
|
55
|
+
for (;;) {
|
|
56
|
+
const key = tokens[cursor.index];
|
|
57
|
+
|
|
58
|
+
if (key === undefined) {
|
|
59
|
+
throw new Error(`cannot parse ${path}: an object never closes`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
cursor.index += 1;
|
|
63
|
+
|
|
64
|
+
if (key.kind === 'other' && key.text === '}') {
|
|
65
|
+
return entries;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (key.kind === 'other' && key.text === ',') {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (key.kind !== 'string' || tokens[cursor.index]?.text !== ':') {
|
|
73
|
+
throw new Error(`cannot parse ${path}: line ${key.line} holds no key`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
cursor.index += 1;
|
|
77
|
+
|
|
78
|
+
entries.push({ key: key.text, line: key.line, value: parseNode(path, tokens, cursor) });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseItems(path: string, tokens: readonly JSONCToken[], cursor: Cursor): JSONCNode[] {
|
|
83
|
+
const items: JSONCNode[] = [];
|
|
84
|
+
|
|
85
|
+
for (;;) {
|
|
86
|
+
const next = tokens[cursor.index];
|
|
87
|
+
|
|
88
|
+
if (next === undefined) {
|
|
89
|
+
throw new Error(`cannot parse ${path}: an array never closes`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (next.kind === 'other' && next.text === ']') {
|
|
93
|
+
cursor.index += 1;
|
|
94
|
+
|
|
95
|
+
return items;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (next.kind === 'other' && next.text === ',') {
|
|
99
|
+
cursor.index += 1;
|
|
100
|
+
} else {
|
|
101
|
+
items.push(parseNode(path, tokens, cursor));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import type { RunProgram } from '../program/run-program.ts';
|
|
3
|
+
import { runProgram } from '../program/run-program.ts';
|
|
4
|
+
import { collectLintOverrideFindings } from './collect-lint-override-findings.ts';
|
|
5
|
+
import type { LintOverrideFinding } from './collect-lint-override-findings.ts';
|
|
6
|
+
|
|
7
|
+
export interface LintOverridesCheckOptions {
|
|
8
|
+
readonly cwd: string;
|
|
9
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
10
|
+
readonly runProgram: RunProgram;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const configPattern = /^\.?oxlintrc(?:\.[\w-]+)*\.jsonc?$/u;
|
|
14
|
+
|
|
15
|
+
// The check reads the files as HEAD holds them, so a push checks what it sends.
|
|
16
|
+
export function runLintOverridesCheck(
|
|
17
|
+
options: Readonly<LintOverridesCheckOptions>,
|
|
18
|
+
): LintOverrideFinding[] {
|
|
19
|
+
const paths = runGit(options, ['ls-tree', '-r', '-z', '--name-only', 'HEAD']).split('\0');
|
|
20
|
+
|
|
21
|
+
const isToolsRepo =
|
|
22
|
+
readManifestName(options, paths, 'package.json') === 'tools' &&
|
|
23
|
+
readManifestName(options, paths, 'packages/std/package.json') === '@zgeoff/std';
|
|
24
|
+
|
|
25
|
+
return paths
|
|
26
|
+
.filter((path) => configPattern.test(basename(path)))
|
|
27
|
+
.flatMap((path) =>
|
|
28
|
+
collectLintOverrideFindings(path, runGit(options, ['show', `HEAD:${path}`]), isToolsRepo),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function runGit(options: Readonly<LintOverridesCheckOptions>, args: readonly string[]): string {
|
|
33
|
+
const result = options.runProgram(['git', ...args], { cwd: options.cwd, env: options.env });
|
|
34
|
+
|
|
35
|
+
if (result.exitCode !== 0) {
|
|
36
|
+
throw new Error(`git ${args.join(' ')} failed: ${result.stderr.trim()}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return result.stdout;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function readManifestName(
|
|
43
|
+
options: Readonly<LintOverridesCheckOptions>,
|
|
44
|
+
paths: readonly string[],
|
|
45
|
+
manifestPath: string,
|
|
46
|
+
): unknown {
|
|
47
|
+
if (!paths.includes(manifestPath)) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const manifest: unknown = JSON.parse(runGit(options, ['show', `HEAD:${manifestPath}`]));
|
|
53
|
+
|
|
54
|
+
return typeof manifest === 'object' && manifest !== null ? Reflect.get(manifest, 'name') : null;
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Returns the exit code: 0 when clean, 1 with a finding, and 2 when the check cannot run.
|
|
61
|
+
export function runLintOverridesCommand(): number {
|
|
62
|
+
try {
|
|
63
|
+
const findings = runLintOverridesCheck({ cwd: process.cwd(), env: process.env, runProgram });
|
|
64
|
+
|
|
65
|
+
for (const finding of findings) {
|
|
66
|
+
process.stdout.write(`${finding.path}: ${finding.message}\n`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return findings.length === 0 ? 0 : 1;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
process.stderr.write(
|
|
72
|
+
`lint-overrides: ${error instanceof Error ? error.message : String(error)}\n`,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
return 2;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (import.meta.main) {
|
|
80
|
+
process.exitCode = runLintOverridesCommand();
|
|
81
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The files of @zgeoff/std that hold a type assertion. The tools repo alone may switch off
|
|
2
|
+
// typescript/consistent-type-assertions for these. @zgeoff/oxlint-config holds the same list, and a
|
|
3
|
+
// test keeps the two equal.
|
|
4
|
+
export const stdHelperPaths: readonly string[] = [
|
|
5
|
+
'packages/std/src/objects/merge-deep.ts',
|
|
6
|
+
'packages/std/src/types/machine-types.ts',
|
|
7
|
+
'packages/std/src/types/to-exact-optional.ts',
|
|
8
|
+
'packages/std/src/types/to-readonly-deep.ts',
|
|
9
|
+
'packages/std/src/types/to-rows.ts',
|
|
10
|
+
'packages/std/src/types/to-tagged.ts',
|
|
11
|
+
];
|
|
@@ -34,14 +34,10 @@ export async function runTestTimeoutsCheck(
|
|
|
34
34
|
);
|
|
35
35
|
|
|
36
36
|
return paths
|
|
37
|
-
.filter((path) => checkedFiles.has(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,189 +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 as a disable
|
|
115
|
-
// comment does, so it needs the same ticket, in a comment 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 (
|
|
151
|
-
notes.some((comment) => ticketPattern.test(comment.text) || floorPattern.test(comment.text))
|
|
152
|
-
) {
|
|
153
|
-
return [];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return [
|
|
157
|
-
{
|
|
158
|
-
path,
|
|
159
|
-
message: `line ${token.line} turns ${rule} off or down to a warning; give the setting a comment that names the ticket that tracks the fix, or "floor:" and the evidence that the time cannot move, as the "Slow tests" section of the testing skill describes`,
|
|
160
|
-
},
|
|
161
|
-
];
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const weakSeverities = new Set(['off', 'warn', 'allow', '0', '1']);
|
|
166
|
-
|
|
167
|
-
interface JSONCToken {
|
|
168
|
-
readonly kind: 'string' | 'comment' | 'other';
|
|
169
|
-
|
|
170
|
-
// A string's or a comment's content, or the token's own text.
|
|
171
|
-
readonly text: string;
|
|
172
|
-
|
|
173
|
-
// The 1-based lines the token starts and ends on.
|
|
174
|
-
readonly line: number;
|
|
175
|
-
readonly endLine: number;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function parseJSONCTokens(path: string, text: string): JSONCToken[] {
|
|
179
|
-
const tokens: JSONCToken[] = [];
|
|
180
|
-
let line = 1;
|
|
181
|
-
let index = 0;
|
|
182
|
-
|
|
183
|
-
const advance = (end: number): number => {
|
|
184
|
-
const start = line;
|
|
185
|
-
|
|
186
|
-
for (let at = index; at < end; at += 1) {
|
|
187
|
-
if (text.charAt(at) === '\n') {
|
|
188
|
-
line += 1;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
index = end;
|
|
193
|
-
|
|
194
|
-
return start;
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
while (index < text.length) {
|
|
198
|
-
const character = text.charAt(index);
|
|
199
|
-
const next = text.charAt(index + 1);
|
|
200
|
-
|
|
201
|
-
if (/\s/u.test(character)) {
|
|
202
|
-
advance(index + 1);
|
|
203
|
-
} else if (character === '/' && (next === '/' || next === '*')) {
|
|
204
|
-
const close = next === '/' ? text.indexOf('\n', index) : text.indexOf('*/', index + 2);
|
|
205
|
-
const end = close === -1 ? text.length : close;
|
|
206
|
-
const body = text.slice(index + 2, end);
|
|
207
|
-
const after = next === '/' || close === -1 ? end : end + 2;
|
|
208
|
-
const start = advance(after);
|
|
209
|
-
|
|
210
|
-
tokens.push({ kind: 'comment', text: body, line: start, endLine: line });
|
|
211
|
-
} else if (character === '"') {
|
|
212
|
-
const end = getStringEnd(path, text, index);
|
|
213
|
-
const body = parseText(path, text.slice(index, end), (input) => JSON.parse(input));
|
|
214
|
-
const start = advance(end);
|
|
215
|
-
|
|
216
|
-
tokens.push({ kind: 'string', text: String(body), line: start, endLine: line });
|
|
217
|
-
} else {
|
|
218
|
-
const word = /^[\w.+-]+/u.exec(text.slice(index))?.[0] ?? character;
|
|
219
|
-
const start = advance(index + word.length);
|
|
220
|
-
|
|
221
|
-
tokens.push({ kind: 'other', text: word, line: start, endLine: line });
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return tokens;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function getStringEnd(path: string, text: string, start: number): number {
|
|
229
|
-
for (let index = start + 1; index < text.length; index += 1) {
|
|
230
|
-
const character = text.charAt(index);
|
|
231
|
-
|
|
232
|
-
if (character === '\\') {
|
|
233
|
-
index += 1;
|
|
234
|
-
} else if (character === '"') {
|
|
235
|
-
return index + 1;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
throw new Error(`cannot parse ${path}: a string never closes`);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function parseText(path: string, text: string, parse: (input: string) => unknown): unknown {
|
|
243
|
-
try {
|
|
244
|
-
return parse(text);
|
|
245
|
-
} catch (error) {
|
|
246
|
-
throw new Error(
|
|
247
|
-
`cannot parse ${path}: ${error instanceof Error ? error.message : String(error)}`,
|
|
248
|
-
{ cause: error },
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function collectOverrideLines(values: readonly JSONCToken[]): ReadonlyMap<number, number> {
|
|
254
|
-
const lines = new Map<number, number>();
|
|
255
|
-
|
|
256
|
-
const open: number[] = [];
|
|
257
|
-
|
|
258
|
-
for (const [index, token] of values.entries()) {
|
|
259
|
-
if (token.text === '{') {
|
|
260
|
-
open.push(index);
|
|
261
|
-
} else if (token.text === '}') {
|
|
262
|
-
open.pop();
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const override = open.length >= 3 ? values[open[1] ?? 0] : undefined;
|
|
266
|
-
|
|
267
|
-
if (override !== undefined) {
|
|
268
|
-
lines.set(index, override.line);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
return lines;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// a comment above the setting counts while no other value sits between them
|
|
276
|
-
function isCommentAbove(
|
|
277
|
-
comment: JSONCToken,
|
|
278
|
-
line: number,
|
|
279
|
-
valueLines: ReadonlySet<number>,
|
|
280
|
-
): boolean {
|
|
281
|
-
if (comment.endLine >= line) {
|
|
282
|
-
return false;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
for (let between = comment.endLine + 1; between < line; between += 1) {
|
|
286
|
-
if (valueLines.has(between)) {
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return !valueLines.has(comment.line);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const unitLimitMs = 5000;
|
|
295
|
-
const slowTestsHint = 'find its cause in the "Slow tests" section of the testing skill and fix it';
|
|
296
|
-
|
|
297
108
|
function collectScriptFindings(
|
|
298
109
|
path: string,
|
|
299
110
|
text: string,
|
|
@@ -331,6 +142,20 @@ function collectScriptFindings(
|
|
|
331
142
|
});
|
|
332
143
|
}
|
|
333
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
|
+
|
|
334
159
|
interface LaneLimit {
|
|
335
160
|
readonly lane: string;
|
|
336
161
|
readonly limitMs: number;
|
|
@@ -505,12 +330,6 @@ function findDirectoryLane(path: string, appRoots: ReadonlySet<string>): string
|
|
|
505
330
|
return null;
|
|
506
331
|
}
|
|
507
332
|
|
|
508
|
-
const guardedRules = ['zgeoff/max-test-timeout', 'zgeoff/test-timeout-exception-ticket'];
|
|
509
|
-
const ticketPattern = /\b[A-Z][A-Z0-9]*-\d+\b/u;
|
|
510
|
-
|
|
511
|
-
// a block comment's continuation stars are no evidence
|
|
512
|
-
const floorPattern = /\bfloor:[\s*]*[^\s*]/u;
|
|
513
|
-
|
|
514
333
|
// Returns the exit code: 0 when clean, 1 with a finding, and 2 when the check cannot run.
|
|
515
334
|
export async function runTestTimeoutsCommand(): Promise<number> {
|
|
516
335
|
try {
|