@alxwlw/repo-rules 1.16.1 → 1.17.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 +18 -0
- package/package.json +5 -1
- package/src/baseline.mjs +23 -0
- package/src/cli.mjs +8 -16
- package/src/main.mjs +35 -1
package/README.md
CHANGED
|
@@ -13,10 +13,23 @@ pnpm exec repo-rules diff --base origin/main
|
|
|
13
13
|
repo-rules [-C <dir>] check [--json]
|
|
14
14
|
repo-rules [-C <dir>] diff [--base <ref>] [--json]
|
|
15
15
|
repo-rules [-C <dir>] baseline [--init] [--allow-grow]
|
|
16
|
+
repo-rules [-C <dir>] baseline --drop-fixed
|
|
16
17
|
```
|
|
17
18
|
|
|
18
19
|
`-C <dir>` runs in that directory (any place inside the repository). `--json` prints `{ violations, stale, notes }` instead of text lines. `--help` prints the usage; a missing or unknown command prints it on stderr and exits 2.
|
|
19
20
|
|
|
21
|
+
## Programmatic use
|
|
22
|
+
|
|
23
|
+
The package exports `main` and `run` from its entry point (the `exports` field closes deep imports; only `.` and `./package.json` resolve).
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
import { main, run } from '@alxwlw/repo-rules';
|
|
27
|
+
|
|
28
|
+
const code = main(['check', '--json'], process.env, console.log, console.error);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Both have the signature `(argv, env, out, err) => number`: `argv` is the argument list without the program name, `out` and `err` receive one line per call, the result is the exit code. `main` never throws: an error becomes one `repo-rules: …` line on `err` and code 2. `run` lets an error propagate (a thrown `RepoRulesError` or an internal error); use it when you want to handle the error yourself.
|
|
32
|
+
|
|
20
33
|
## Rules
|
|
21
34
|
|
|
22
35
|
| Rule | Flags |
|
|
@@ -33,6 +46,11 @@ A reason is at least three non-space characters.
|
|
|
33
46
|
|
|
34
47
|
`.repo-rules-baseline.json` records the violations that already exist: line count per long file, hit count per file for the other rules. `check` fails when a file goes over its entry, and also when an entry is higher than reality (stale), so a fixed place cannot be reused by a new one. `baseline` only lowers entries; raising one takes `--allow-grow`, which shows in the PR diff.
|
|
35
48
|
|
|
49
|
+
`baseline --drop-fixed` is the mode for an automated update PR: it lowers the entries that are higher than
|
|
50
|
+
reality and leaves the rest. An entry that grew keeps its count and a new violation is not recorded, so both
|
|
51
|
+
stay for `check` to report. With nothing to lower, or with no baseline file, it writes nothing and exits 0. It
|
|
52
|
+
does not go with `--init` or `--allow-grow`.
|
|
53
|
+
|
|
36
54
|
## Configuration
|
|
37
55
|
|
|
38
56
|
Optional `.repo-rules.json`:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alxwlw/repo-rules",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "Whole-repo lint rules from the alxwlw defect corpus — long files, unjustified suppressions, skipped/focused tests, Number(x) || <default>, removed assertions — with a shrink-only baseline.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
"bin": {
|
|
11
11
|
"repo-rules": "src/cli.mjs"
|
|
12
12
|
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./src/main.mjs",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
13
17
|
"files": [
|
|
14
18
|
"src",
|
|
15
19
|
"!src/**/*.test.ts",
|
package/src/baseline.mjs
CHANGED
|
@@ -117,6 +117,29 @@ export function growth(prev, next) {
|
|
|
117
117
|
return up;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/**
|
|
121
|
+
* `prev` lowered to what `next` has: an entry that is gone is removed, one that shrank takes the lower
|
|
122
|
+
* count; one that grew keeps its count and a new one is not added.
|
|
123
|
+
* @param {Counts} prev
|
|
124
|
+
* @param {Counts} next
|
|
125
|
+
* @returns {{ counts: Counts, fixed: { rule: RepoRule, path: string, from: number, to: number }[] }}
|
|
126
|
+
*/
|
|
127
|
+
export function dropFixed(prev, next) {
|
|
128
|
+
const counts = emptyCounts();
|
|
129
|
+
/** @type {{ rule: RepoRule, path: string, from: number, to: number }[]} */
|
|
130
|
+
const fixed = [];
|
|
131
|
+
for (const rule of REPO_RULES) {
|
|
132
|
+
for (const [path, from] of Object.entries(prev[rule])) {
|
|
133
|
+
const to = next[rule][path] ?? 0;
|
|
134
|
+
if (to < from) {
|
|
135
|
+
fixed.push({ rule, path, from, to });
|
|
136
|
+
if (to > 0) counts[rule][path] = to;
|
|
137
|
+
} else counts[rule][path] = from;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { counts, fixed };
|
|
141
|
+
}
|
|
142
|
+
|
|
120
143
|
/**
|
|
121
144
|
* Keys sorted, the existing file's indentation kept (tab or two spaces), trailing newline.
|
|
122
145
|
* @param {string} root
|
package/src/cli.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// @ts-check
|
|
3
|
-
/** Bin entry: `repo-rules check | diff --base <ref> | baseline [--init] [--allow-grow]`. */
|
|
4
|
-
import {
|
|
5
|
-
import { run } from './main.mjs';
|
|
3
|
+
/** Bin entry: `repo-rules check | diff --base <ref> | baseline [--init] [--allow-grow] | baseline --drop-fixed`. */
|
|
4
|
+
import { main } from './main.mjs';
|
|
6
5
|
|
|
7
6
|
// Attached before the first write: a closed pipe (EPIPE) or a full disk (ENOSPC) on stdout is one
|
|
8
7
|
// error line and exit 2, never an unhandled 'error' stack that exits 1.
|
|
@@ -13,16 +12,9 @@ process.stdout.on('error', (e) => {
|
|
|
13
12
|
process.exitCode = 2;
|
|
14
13
|
});
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
process.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
);
|
|
23
|
-
} catch (e) {
|
|
24
|
-
// 2, never 0 or 1: an error must not read as «clean» or as «violations found».
|
|
25
|
-
if (e instanceof RepoRulesError) process.stderr.write(`repo-rules: ${e.message}\n`);
|
|
26
|
-
else process.stderr.write(`repo-rules: internal error: ${/** @type {Error} */ (e).stack ?? e}\n`);
|
|
27
|
-
process.exitCode = 2;
|
|
28
|
-
}
|
|
15
|
+
process.exitCode = main(
|
|
16
|
+
process.argv.slice(2),
|
|
17
|
+
process.env,
|
|
18
|
+
(s) => process.stdout.write(`${s}\n`),
|
|
19
|
+
(s) => process.stderr.write(`${s}\n`),
|
|
20
|
+
);
|
package/src/main.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import { parseArgs } from 'node:util';
|
|
|
6
6
|
import {
|
|
7
7
|
BASELINE_FILE,
|
|
8
8
|
compare,
|
|
9
|
+
dropFixed,
|
|
9
10
|
emptyCounts,
|
|
10
11
|
growth,
|
|
11
12
|
loadBaseline,
|
|
@@ -18,7 +19,8 @@ import { scan } from './scan.mjs';
|
|
|
18
19
|
|
|
19
20
|
const USAGE = `usage: repo-rules [-C <dir>] check [--json]
|
|
20
21
|
repo-rules [-C <dir>] diff [--base <ref>] [--json]
|
|
21
|
-
repo-rules [-C <dir>] baseline [--init] [--allow-grow]
|
|
22
|
+
repo-rules [-C <dir>] baseline [--init] [--allow-grow]
|
|
23
|
+
repo-rules [-C <dir>] baseline --drop-fixed`;
|
|
22
24
|
|
|
23
25
|
/** @param {string[]} argv */
|
|
24
26
|
function parseOptions(argv) {
|
|
@@ -31,6 +33,7 @@ function parseOptions(argv) {
|
|
|
31
33
|
base: { type: 'string' },
|
|
32
34
|
init: { type: 'boolean' },
|
|
33
35
|
'allow-grow': { type: 'boolean' },
|
|
36
|
+
'drop-fixed': { type: 'boolean' },
|
|
34
37
|
help: { type: 'boolean', short: 'h' },
|
|
35
38
|
version: { type: 'boolean' },
|
|
36
39
|
},
|
|
@@ -119,6 +122,17 @@ export function run(argv, env, out, err) {
|
|
|
119
122
|
}
|
|
120
123
|
|
|
121
124
|
// Last command: `baseline`.
|
|
125
|
+
if (values['drop-fixed']) {
|
|
126
|
+
if (values.init || values['allow-grow'])
|
|
127
|
+
throw new RepoRulesError('--drop-fixed does not go with --init or --allow-grow');
|
|
128
|
+
const recorded = loadBaseline(root);
|
|
129
|
+
if (!recorded) return 0;
|
|
130
|
+
const { counts, fixed } = dropFixed(recorded, scan(root, cfg).actual);
|
|
131
|
+
if (!fixed.length) return 0;
|
|
132
|
+
writeBaseline(root, counts);
|
|
133
|
+
out(`repo-rules baseline: lowered ${fixed.length} entr(ies) in ${BASELINE_FILE}`);
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
122
136
|
const prev = loadBaseline(root);
|
|
123
137
|
if (values.init && prev) throw new RepoRulesError(`${BASELINE_FILE} already exists`);
|
|
124
138
|
if (!values.init && !prev)
|
|
@@ -135,3 +149,23 @@ export function run(argv, env, out, err) {
|
|
|
135
149
|
out(`repo-rules baseline: wrote ${BASELINE_FILE} (${total} entr(ies))`);
|
|
136
150
|
return 0;
|
|
137
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* `run` that never throws: the one place an error becomes a line and exit code 2 (the bin and
|
|
155
|
+
* `stack repo-rules` both call it).
|
|
156
|
+
* @param {string[]} argv
|
|
157
|
+
* @param {NodeJS.ProcessEnv} env
|
|
158
|
+
* @param {(s: string) => void} out stdout line
|
|
159
|
+
* @param {(s: string) => void} err stderr line
|
|
160
|
+
* @returns {number} exit code
|
|
161
|
+
*/
|
|
162
|
+
export function main(argv, env, out, err) {
|
|
163
|
+
try {
|
|
164
|
+
return run(argv, env, out, err);
|
|
165
|
+
} catch (e) {
|
|
166
|
+
// 2, never 0 or 1: an error must not read as «clean» or as «violations found».
|
|
167
|
+
if (e instanceof RepoRulesError) err(`repo-rules: ${e.message}`);
|
|
168
|
+
else err(`repo-rules: internal error: ${/** @type {Error} */ (e).stack ?? e}`);
|
|
169
|
+
return 2;
|
|
170
|
+
}
|
|
171
|
+
}
|