@opendesignai/harness 0.0.0-stage → 0.1.0-alpha.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/LICENSE +201 -0
- package/README.md +127 -2
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +380 -0
- package/dist/eslint-comments.d.mts +24 -0
- package/dist/eslint-comments.mjs +50 -0
- package/dist/paths-DBoYPrGy.mjs +59 -0
- package/dist/plugin.d.mts +25 -0
- package/dist/plugin.mjs +1040 -0
- package/docs/adopting.md +157 -0
- package/docs/decisions/0001-oxlint.md +74 -0
- package/docs/decisions/0002-react-compiler-rules.md +34 -0
- package/docs/ratchet.md +49 -0
- package/docs/rules/no-api-path-literal.md +35 -0
- package/docs/rules/no-cjk-literal.md +38 -0
- package/docs/rules/no-constant-flag.md +33 -0
- package/docs/rules/no-error-message-match.md +55 -0
- package/docs/rules/no-locale-branch.md +51 -0
- package/docs/rules/no-process-env.md +27 -0
- package/docs/rules/no-product-literal.md +35 -0
- package/docs/rules/no-raw-fetch.md +33 -0
- package/docs/rules/no-raw-storage.md +41 -0
- package/docs/rules/no-shell-imports-product.md +46 -0
- package/docs/rules/no-swallowed-catch.md +75 -0
- package/docs/rules/no-window-bus.md +35 -0
- package/docs/size.md +51 -0
- package/package.json +73 -3
- package/presets/base.json +7 -0
- package/presets/platform-boundary.json +4 -0
- package/presets/product.json +4 -0
- package/presets/react-compiler.json +13 -0
- package/presets/rules/base.json +47 -0
- package/presets/rules/platform-boundary.json +6 -0
- package/presets/rules/product.json +5 -0
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { t as compilePathPatterns } from "./paths-DBoYPrGy.mjs";
|
|
7
|
+
// src/cli/git.ts
|
|
8
|
+
var UsageError = class extends Error {
|
|
9
|
+
name = "UsageError";
|
|
10
|
+
};
|
|
11
|
+
function git(cwd, args) {
|
|
12
|
+
return execFileSync("git", args, {
|
|
13
|
+
cwd,
|
|
14
|
+
encoding: "utf8",
|
|
15
|
+
maxBuffer: 268435456
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** Tracked files plus untracked files that are not ignored, repo-relative with forward slashes. */
|
|
19
|
+
function listFiles(cwd) {
|
|
20
|
+
return git(cwd, [
|
|
21
|
+
"ls-files",
|
|
22
|
+
"-z",
|
|
23
|
+
"--cached",
|
|
24
|
+
"--others",
|
|
25
|
+
"--exclude-standard"
|
|
26
|
+
]).split("\0").filter(Boolean);
|
|
27
|
+
}
|
|
28
|
+
function resolveRef(cwd, ref) {
|
|
29
|
+
const result = spawnSync("git", [
|
|
30
|
+
"rev-parse",
|
|
31
|
+
"--verify",
|
|
32
|
+
"--quiet",
|
|
33
|
+
`${ref}^{commit}`
|
|
34
|
+
], {
|
|
35
|
+
cwd,
|
|
36
|
+
encoding: "utf8"
|
|
37
|
+
});
|
|
38
|
+
if (result.status !== 0) throw new UsageError(`cannot resolve base ref "${ref}". In CI, check out with full history (fetch-depth: 0) or fetch the base commit first.`);
|
|
39
|
+
return result.stdout.trim();
|
|
40
|
+
}
|
|
41
|
+
/** `<commit>:<path>` with the path read relative to the working directory, like the working tree. */
|
|
42
|
+
function revPath(commit, file) {
|
|
43
|
+
return `${commit}:${file.startsWith("./") || file.startsWith("../") ? file : `./${file}`}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Contents of `file` (relative to `cwd`) at `commit`, or null when the file does not exist there.
|
|
47
|
+
*/
|
|
48
|
+
function showFile(cwd, commit, file) {
|
|
49
|
+
const result = spawnSync("git", ["show", revPath(commit, file)], {
|
|
50
|
+
cwd,
|
|
51
|
+
encoding: "utf8",
|
|
52
|
+
maxBuffer: 268435456
|
|
53
|
+
});
|
|
54
|
+
return result.status === 0 ? result.stdout : null;
|
|
55
|
+
}
|
|
56
|
+
/** Files under `cwd` at `commit`, relative to `cwd` with forward slashes (like `listFiles`). */
|
|
57
|
+
function listFilesAt(cwd, commit) {
|
|
58
|
+
return new Set(git(cwd, [
|
|
59
|
+
"ls-tree",
|
|
60
|
+
"-r",
|
|
61
|
+
"--name-only",
|
|
62
|
+
"-z",
|
|
63
|
+
commit
|
|
64
|
+
]).split("\0").filter(Boolean));
|
|
65
|
+
}
|
|
66
|
+
// src/cli/size.ts
|
|
67
|
+
const SOURCE_LIMIT = 800;
|
|
68
|
+
const LARGE_LIMIT = 1500;
|
|
69
|
+
const SIZE_BASELINE = ".harness/size-baseline.json";
|
|
70
|
+
const HARNESS_CONFIG = ".harness/config.json";
|
|
71
|
+
const SIZE_DOCS = "https://github.com/nexu-io/od-harness/blob/main/docs/size.md";
|
|
72
|
+
const SOURCE_EXT = /\.(?:[cm]?[jt]sx?|css)$/;
|
|
73
|
+
const LARGE_FILE = /(?:^|\/)__tests__\/|\.(?:test|spec)\.[^/]*$|\.css$/;
|
|
74
|
+
function countLines(text) {
|
|
75
|
+
if (text.length === 0) return 0;
|
|
76
|
+
let lines = 0;
|
|
77
|
+
for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) lines++;
|
|
78
|
+
return text.endsWith("\n") ? lines : lines + 1;
|
|
79
|
+
}
|
|
80
|
+
function limitFor(file) {
|
|
81
|
+
return LARGE_FILE.test(file) ? LARGE_LIMIT : SOURCE_LIMIT;
|
|
82
|
+
}
|
|
83
|
+
function readSizeBaseline(text, origin) {
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = JSON.parse(text);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
throw new UsageError(`${origin} is not valid JSON: ${String(error)}`);
|
|
89
|
+
}
|
|
90
|
+
const files = parsed.files;
|
|
91
|
+
if (typeof files !== "object" || files === null || Array.isArray(files)) throw new UsageError(`${origin} must look like {"files": {"path": lines}}`);
|
|
92
|
+
for (const [file, lines] of Object.entries(files)) if (!Number.isInteger(lines) || lines < 0) throw new UsageError(`${origin}: "${file}" must map to a non-negative integer`);
|
|
93
|
+
return { files };
|
|
94
|
+
}
|
|
95
|
+
function formatSizeBaseline(baseline) {
|
|
96
|
+
const files = Object.fromEntries(Object.entries(baseline.files).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0));
|
|
97
|
+
return `${JSON.stringify({ files }, null, 2)}\n`;
|
|
98
|
+
}
|
|
99
|
+
function readIgnore(cwd) {
|
|
100
|
+
const configPath = path.join(cwd, HARNESS_CONFIG);
|
|
101
|
+
if (!existsSync(configPath)) return () => false;
|
|
102
|
+
let config;
|
|
103
|
+
try {
|
|
104
|
+
config = JSON.parse(readFileSync(configPath, "utf8"));
|
|
105
|
+
} catch (error) {
|
|
106
|
+
throw new UsageError(`${HARNESS_CONFIG} is not valid JSON (${String(error)}). Expected {"size": {"ignore": ["path/pattern/**"]}}.`);
|
|
107
|
+
}
|
|
108
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) throw new UsageError(`${HARNESS_CONFIG} must be a JSON object, for example {"size": {"ignore": []}}.`);
|
|
109
|
+
const ignore = config.size?.ignore ?? [];
|
|
110
|
+
if (!Array.isArray(ignore) || !ignore.every((p) => typeof p === "string")) throw new UsageError(`${HARNESS_CONFIG}: size.ignore must be an array of path patterns`);
|
|
111
|
+
return compilePathPatterns(ignore);
|
|
112
|
+
}
|
|
113
|
+
function runSize(options) {
|
|
114
|
+
const { cwd } = options;
|
|
115
|
+
const baselinePath = path.join(cwd, SIZE_BASELINE);
|
|
116
|
+
const hasBaseline = existsSync(baselinePath);
|
|
117
|
+
if (options.init && hasBaseline) throw new UsageError(`${SIZE_BASELINE} already exists. --init only creates the first baseline; use --update to tighten it.`);
|
|
118
|
+
const baseline = hasBaseline ? readSizeBaseline(readFileSync(baselinePath, "utf8"), SIZE_BASELINE) : { files: {} };
|
|
119
|
+
const ignored = readIgnore(cwd);
|
|
120
|
+
const sizes = /* @__PURE__ */ new Map();
|
|
121
|
+
for (const file of listFiles(cwd)) {
|
|
122
|
+
if (!SOURCE_EXT.test(file) || ignored(file)) continue;
|
|
123
|
+
const abs = path.join(cwd, file);
|
|
124
|
+
if (!existsSync(abs)) continue;
|
|
125
|
+
sizes.set(file, countLines(readFileSync(abs, "utf8")));
|
|
126
|
+
}
|
|
127
|
+
const errors = [];
|
|
128
|
+
const notes = [];
|
|
129
|
+
const next = { files: {} };
|
|
130
|
+
const tighten = "Run `od-harness size --update` and commit .harness/size-baseline.json.";
|
|
131
|
+
for (const [file, frozen] of Object.entries(baseline.files)) {
|
|
132
|
+
const lines = sizes.get(file);
|
|
133
|
+
const limit = limitFor(file);
|
|
134
|
+
if (lines === void 0) {
|
|
135
|
+
const why = existsSync(path.join(cwd, file)) ? `is excluded by ${HARNESS_CONFIG} (size.ignore)` : "no longer exists";
|
|
136
|
+
if (options.update) notes.push(`${file}: removed from the baseline (the file ${why}).`);
|
|
137
|
+
else errors.push(`${file}: listed in ${SIZE_BASELINE} but the file ${why}. ${tighten}`);
|
|
138
|
+
} else if (lines <= limit) {
|
|
139
|
+
if (options.update) notes.push(`${file}: back within budget (${lines}/${limit}); removed from the baseline.`);
|
|
140
|
+
else errors.push(`${file}: now ${lines} lines, within the ${limit}-line budget, but still listed in the baseline. ${tighten}`);
|
|
141
|
+
} else if (lines > frozen) {
|
|
142
|
+
errors.push(`${file}: ${lines} lines, frozen at ${frozen} (budget ${limit}). This file is over budget and may only shrink. Fix: move the code you added (or more) into a new or existing module instead of growing this file. Do not raise the baseline.`);
|
|
143
|
+
next.files[file] = frozen;
|
|
144
|
+
} else if (lines < frozen) {
|
|
145
|
+
if (options.update) notes.push(`${file}: baseline tightened ${frozen} → ${lines}.`);
|
|
146
|
+
else errors.push(`${file}: shrank to ${lines} lines but the baseline still allows ${frozen}. ${tighten}`);
|
|
147
|
+
next.files[file] = options.update ? lines : frozen;
|
|
148
|
+
} else next.files[file] = frozen;
|
|
149
|
+
}
|
|
150
|
+
for (const [file, lines] of sizes) {
|
|
151
|
+
const limit = limitFor(file);
|
|
152
|
+
if (lines <= limit || file in baseline.files) continue;
|
|
153
|
+
if (options.init) {
|
|
154
|
+
next.files[file] = lines;
|
|
155
|
+
notes.push(`${file}: frozen at ${lines} lines (budget ${limit}).`);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const kind = limit === LARGE_LIMIT ? "tests and stylesheets" : "source files";
|
|
159
|
+
errors.push(`${file}: ${lines} lines, over the ${limit}-line budget for ${kind}. Fix: split it into smaller modules by responsibility (keep each under ${limit} lines). New files cannot be added to the baseline; only files that were over budget when the repository adopted od-harness are frozen there.`);
|
|
160
|
+
}
|
|
161
|
+
if (options.update && hasBaseline || options.init) {
|
|
162
|
+
if (Object.keys(next.files).length === 0 && options.update) notes.push(`${SIZE_BASELINE} is now empty; every file is within budget.`);
|
|
163
|
+
mkdirSync(path.dirname(baselinePath), { recursive: true });
|
|
164
|
+
writeFileSync(baselinePath, formatSizeBaseline(next));
|
|
165
|
+
}
|
|
166
|
+
if (errors.length > 0) errors.push(`See ${SIZE_DOCS}`);
|
|
167
|
+
return {
|
|
168
|
+
ok: errors.length === 0,
|
|
169
|
+
checked: sizes.size,
|
|
170
|
+
errors,
|
|
171
|
+
notes
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
// src/cli/ratchet.ts
|
|
175
|
+
const SUPPRESSIONS = "oxlint-suppressions.json";
|
|
176
|
+
const RATCHET_DOCS = "https://github.com/nexu-io/od-harness/blob/main/docs/ratchet.md";
|
|
177
|
+
const HARNESS_PACKAGE = "@opendesignai/harness";
|
|
178
|
+
function parseSuppressions(text, origin) {
|
|
179
|
+
let parsed;
|
|
180
|
+
try {
|
|
181
|
+
parsed = JSON.parse(text);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
throw new UsageError(`${origin} is not valid JSON: ${String(error)}`);
|
|
184
|
+
}
|
|
185
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new UsageError(`${origin} must map file paths to {"rule": {"count": n}}`);
|
|
186
|
+
for (const [file, rules] of Object.entries(parsed)) for (const [rule, entry] of Object.entries(rules ?? {})) {
|
|
187
|
+
const count = entry?.count;
|
|
188
|
+
if (!Number.isInteger(count) || count < 0) throw new UsageError(`${origin}: ${file} → ${rule} needs a non-negative integer "count"`);
|
|
189
|
+
}
|
|
190
|
+
return parsed;
|
|
191
|
+
}
|
|
192
|
+
function totals(suppressions) {
|
|
193
|
+
const out = /* @__PURE__ */ new Map();
|
|
194
|
+
for (const rules of Object.values(suppressions)) for (const [rule, { count }] of Object.entries(rules)) out.set(rule, (out.get(rule) ?? 0) + count);
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
function readWorking(cwd, file) {
|
|
198
|
+
const abs = path.join(cwd, file);
|
|
199
|
+
return existsSync(abs) ? readFileSync(abs, "utf8") : null;
|
|
200
|
+
}
|
|
201
|
+
function harnessVersion(packageJson) {
|
|
202
|
+
if (packageJson === null) return null;
|
|
203
|
+
try {
|
|
204
|
+
const pkg = JSON.parse(packageJson);
|
|
205
|
+
return pkg.devDependencies?.[HARNESS_PACKAGE] ?? pkg.dependencies?.[HARNESS_PACKAGE] ?? null;
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Why this change may add new rule IDs to an existing baseline: it changed the lint config or the
|
|
212
|
+
* harness version (both owner-reviewed). Null when it changed neither.
|
|
213
|
+
*/
|
|
214
|
+
function ownerReviewedChange(cwd, commit, lintConfig, before, after) {
|
|
215
|
+
const reasons = [];
|
|
216
|
+
if (showFile(cwd, commit, lintConfig) !== readWorking(cwd, lintConfig)) reasons.push(`${lintConfig} changed`);
|
|
217
|
+
if (before !== after) reasons.push(`${HARNESS_PACKAGE} ${before ?? "(none)"} → ${after ?? "(none)"}`);
|
|
218
|
+
return reasons.length > 0 ? reasons.join(", ") : null;
|
|
219
|
+
}
|
|
220
|
+
function runRatchet(options) {
|
|
221
|
+
const { cwd } = options;
|
|
222
|
+
if (!options.base) throw new UsageError("missing --base <ref>");
|
|
223
|
+
const commit = resolveRef(cwd, options.base);
|
|
224
|
+
const suppressionsPath = options.suppressions ?? SUPPRESSIONS;
|
|
225
|
+
const lintConfig = options.lintConfig ?? ".oxlintrc.json";
|
|
226
|
+
const errors = [];
|
|
227
|
+
const notes = [];
|
|
228
|
+
const at = `${options.base} (${commit.slice(0, 10)})`;
|
|
229
|
+
const harnessBefore = harnessVersion(showFile(cwd, commit, "package.json"));
|
|
230
|
+
const harnessAfter = harnessVersion(readWorking(cwd, "package.json"));
|
|
231
|
+
const ownerChange = ownerReviewedChange(cwd, commit, lintConfig, harnessBefore, harnessAfter);
|
|
232
|
+
const adoption = harnessBefore === null && harnessAfter !== null ? `${HARNESS_PACKAGE} (none) → ${harnessAfter}` : null;
|
|
233
|
+
const baseFiles = listFilesAt(cwd, commit);
|
|
234
|
+
const newBaseline = (file, fix, command) => {
|
|
235
|
+
if (adoption !== null) {
|
|
236
|
+
notes.push(`${file}: absent at ${at}; adoption change (${adoption}), comparison with the base skipped.`);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
const why = harnessBefore === null ? `This change does not add it.` : `${at} already depends on it (${harnessBefore}): a repository that adopted the harness without this baseline never creates one, also not when it upgrades ${HARNESS_PACKAGE} or changes ${lintConfig}. Violations that an upgrade or a config change brings are fixed in that change, not frozen.`;
|
|
240
|
+
errors.push(`${file} is new in this change, but ${at} has no such file. A baseline may only be introduced by the adoption change, which adds ${HARNESS_PACKAGE} to package.json. ${why} Fix: ${fix} Never run \`${command}\` to get a change through.`);
|
|
241
|
+
return false;
|
|
242
|
+
};
|
|
243
|
+
const headText = readWorking(cwd, suppressionsPath);
|
|
244
|
+
const baseText = showFile(cwd, commit, suppressionsPath);
|
|
245
|
+
if (headText === null) notes.push(`${suppressionsPath}: not present; nothing to compare.`);
|
|
246
|
+
else {
|
|
247
|
+
const head = parseSuppressions(headText, suppressionsPath);
|
|
248
|
+
for (const file of Object.keys(head)) if (!existsSync(path.join(cwd, file))) errors.push(`${suppressionsPath}: still lists ${file}, which no longer exists. Fix: run \`oxlint --prune-suppressions\` (or delete the entry) and commit the result. If the file moved, its debt moves with it only through an owner-reviewed baseline move; do not re-suppress it under the new path.`);
|
|
249
|
+
const rejected = baseText === null && !newBaseline(suppressionsPath, `delete ${suppressionsPath}, run oxlint and fix what it reports.`, "oxlint --suppress-all");
|
|
250
|
+
const added = Object.entries(head).filter(([file]) => !baseFiles.has(file) && existsSync(path.join(cwd, file))).map(([file, rules]) => {
|
|
251
|
+
const counts = Object.entries(rules).filter(([, { count }]) => count > 0).map(([rule, { count }]) => `${rule} ${count}`);
|
|
252
|
+
return counts.length > 0 ? `${file} (${counts.join(", ")})` : null;
|
|
253
|
+
}).filter((entry) => entry !== null);
|
|
254
|
+
if (added.length > 0 && !rejected) errors.push(`${suppressionsPath}: suppresses violations in ${added.length === 1 ? "a file" : `${added.length} files`} that ${at} does not have: ${added.slice(0, 10).join("; ")}${added.length > 10 ? "; …" : ""}. New files do not get a baseline; it only freezes debt that existed when a rule was adopted. Fix: restore the entries from ${options.base}, fix the violations oxlint reports in these files, then run \`oxlint --prune-suppressions\`. If a file was moved or renamed, its debt does not move with it until the owner-reviewed \`od-harness baseline move\` exists; fix the violations in the moved file instead.`);
|
|
255
|
+
if (baseText !== null) {
|
|
256
|
+
const base = parseSuppressions(baseText, `${suppressionsPath}@${options.base}`);
|
|
257
|
+
const baseTotals = totals(base);
|
|
258
|
+
const headTotals = totals(head);
|
|
259
|
+
for (const [rule, headTotal] of [...headTotals].sort(([a], [b]) => a.localeCompare(b))) {
|
|
260
|
+
const baseTotal = baseTotals.get(rule) ?? 0;
|
|
261
|
+
if (headTotal <= baseTotal) {
|
|
262
|
+
if (headTotal < baseTotal) notes.push(`${rule}: ${baseTotal} → ${headTotal}`);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (baseTotal === 0 && ownerChange !== null) {
|
|
266
|
+
notes.push(`${rule}: enters the baseline with ${headTotal} (allowed: ${ownerChange} in this change, which the owner reviews).`);
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const grown = Object.entries(head).map(([file, rules]) => [file, (rules[rule]?.count ?? 0) - (base[file]?.[rule]?.count ?? 0)]).filter(([, delta]) => delta > 0).map(([file, delta]) => `${file} +${delta}`);
|
|
270
|
+
const where = grown.length > 0 ? ` (${grown.slice(0, 10).join(", ")}${grown.length > 10 ? ", …" : ""})` : "";
|
|
271
|
+
errors.push(`${suppressionsPath}: ${rule} grew from ${baseTotal} to ${headTotal}${where}. The baseline only freezes debt that existed when the rule was adopted; new violations must be fixed, not suppressed. Fix: restore ${suppressionsPath} from ${options.base}, fix the violations oxlint reports, then run \`oxlint --prune-suppressions\` (it can only shrink the file). Never run \`oxlint --suppress-all\` to get a change through.`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const headSize = readWorking(cwd, SIZE_BASELINE);
|
|
276
|
+
const baseSize = showFile(cwd, commit, SIZE_BASELINE);
|
|
277
|
+
if (headSize === null) notes.push(`${SIZE_BASELINE}: not present; nothing to compare.`);
|
|
278
|
+
else {
|
|
279
|
+
const head = readSizeBaseline(headSize, SIZE_BASELINE);
|
|
280
|
+
for (const file of Object.keys(head.files)) if (!existsSync(path.join(cwd, file))) errors.push(`${SIZE_BASELINE}: still lists ${file}, which no longer exists. Fix: run \`od-harness size --update\` and commit the result.`);
|
|
281
|
+
if (baseSize === null) {
|
|
282
|
+
const accepted = newBaseline(SIZE_BASELINE, `delete ${SIZE_BASELINE} and split every file \`od-harness size\` reports.`, "od-harness size --init");
|
|
283
|
+
for (const [file, lines] of Object.entries(head.files)) if (accepted && !baseFiles.has(file) && existsSync(path.join(cwd, file))) errors.push(`${SIZE_BASELINE}: freezes ${file} (${lines} lines), which ${at} does not have. Only files that were over budget before adoption are frozen; a new file must be split instead. Fix: remove the entry and split the file.`);
|
|
284
|
+
} else {
|
|
285
|
+
const base = readSizeBaseline(baseSize, `${SIZE_BASELINE}@${options.base}`);
|
|
286
|
+
for (const [file, lines] of Object.entries(head.files)) {
|
|
287
|
+
const before = base.files[file];
|
|
288
|
+
if (before === void 0) errors.push(`${SIZE_BASELINE}: adds ${file} (${lines} lines). Only files that were over budget at adoption are frozen; a new or newly oversized file must be split instead. Fix: remove the entry and split the file.`);
|
|
289
|
+
else if (lines > before) errors.push(`${SIZE_BASELINE}: raises ${file} from ${before} to ${lines} lines. Frozen files may only shrink. Fix: restore the entry to ${before} and move code out of the file.`);
|
|
290
|
+
else if (lines < before) notes.push(`${file}: size baseline ${before} → ${lines}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (errors.length > 0) errors.push(`See ${RATCHET_DOCS}`);
|
|
295
|
+
return {
|
|
296
|
+
ok: errors.length === 0,
|
|
297
|
+
errors,
|
|
298
|
+
notes
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
// src/cli/main.ts
|
|
302
|
+
const HELP = `Usage: od-harness <command> [options]
|
|
303
|
+
|
|
304
|
+
Commands:
|
|
305
|
+
size [--update | --init] File size budget: 800 lines for source, 1,500 for tests and CSS.
|
|
306
|
+
Files frozen in .harness/size-baseline.json may only shrink.
|
|
307
|
+
--update tighten the baseline after files shrank (never loosens)
|
|
308
|
+
--init create the first baseline (adoption only)
|
|
309
|
+
ratchet --base <ref> Fail if oxlint-suppressions.json or the size baseline grew
|
|
310
|
+
relative to <ref>, lists files <ref> does not have, still
|
|
311
|
+
lists deleted files, or appears outside the adoption change.
|
|
312
|
+
--suppressions <path> default: oxlint-suppressions.json
|
|
313
|
+
--lint-config <path> default: .oxlintrc.json
|
|
314
|
+
|
|
315
|
+
Run from the repository root.`;
|
|
316
|
+
function print(command, outcome, summary) {
|
|
317
|
+
for (const note of outcome.notes) console.log(`od-harness ${command}: ${note}`);
|
|
318
|
+
if (outcome.ok) {
|
|
319
|
+
console.log(`od-harness ${command}: OK${summary ? ` (${summary})` : ""}`);
|
|
320
|
+
return 0;
|
|
321
|
+
}
|
|
322
|
+
console.error(`od-harness ${command}: FAILED`);
|
|
323
|
+
for (const error of outcome.errors) console.error(` ${error}`);
|
|
324
|
+
return 1;
|
|
325
|
+
}
|
|
326
|
+
function main(argv, cwd = process.cwd()) {
|
|
327
|
+
const [command, ...rest] = argv;
|
|
328
|
+
try {
|
|
329
|
+
if (command === "size") {
|
|
330
|
+
const { values } = parseArgs({
|
|
331
|
+
args: rest,
|
|
332
|
+
options: {
|
|
333
|
+
update: { type: "boolean" },
|
|
334
|
+
init: { type: "boolean" }
|
|
335
|
+
},
|
|
336
|
+
strict: true
|
|
337
|
+
});
|
|
338
|
+
if (values.update && values.init) throw new UsageError("use either --update or --init");
|
|
339
|
+
const result = runSize({
|
|
340
|
+
cwd,
|
|
341
|
+
update: values.update ?? false,
|
|
342
|
+
init: values.init ?? false
|
|
343
|
+
});
|
|
344
|
+
return print("size", result, `${result.checked} files checked`);
|
|
345
|
+
}
|
|
346
|
+
if (command === "ratchet") {
|
|
347
|
+
const { values } = parseArgs({
|
|
348
|
+
args: rest,
|
|
349
|
+
options: {
|
|
350
|
+
base: { type: "string" },
|
|
351
|
+
suppressions: { type: "string" },
|
|
352
|
+
"lint-config": { type: "string" }
|
|
353
|
+
},
|
|
354
|
+
strict: true
|
|
355
|
+
});
|
|
356
|
+
const options = {
|
|
357
|
+
cwd,
|
|
358
|
+
base: values.base ?? ""
|
|
359
|
+
};
|
|
360
|
+
if (values.suppressions) options.suppressions = values.suppressions;
|
|
361
|
+
if (values["lint-config"]) options.lintConfig = values["lint-config"];
|
|
362
|
+
return print("ratchet", runRatchet(options), "");
|
|
363
|
+
}
|
|
364
|
+
if (command === void 0 || command === "help" || command === "--help" || command === "-h") {
|
|
365
|
+
console.log(HELP);
|
|
366
|
+
return command === void 0 ? 2 : 0;
|
|
367
|
+
}
|
|
368
|
+
throw new UsageError(`unknown command "${command}"`);
|
|
369
|
+
} catch (error) {
|
|
370
|
+
if (error instanceof UsageError || error.code?.startsWith("ERR_PARSE_ARGS")) {
|
|
371
|
+
console.error(`od-harness: ${error.message}\n\n${HELP}`);
|
|
372
|
+
return 2;
|
|
373
|
+
}
|
|
374
|
+
console.error(`od-harness: ${error.stack ?? String(error)}`);
|
|
375
|
+
return 2;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// src/cli/index.ts
|
|
379
|
+
process.exitCode = main(process.argv.slice(2));
|
|
380
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface Position {
|
|
2
|
+
line: number;
|
|
3
|
+
column: number;
|
|
4
|
+
}
|
|
5
|
+
type Loc = Position | {
|
|
6
|
+
start: Position;
|
|
7
|
+
end?: Position | null;
|
|
8
|
+
};
|
|
9
|
+
interface UpstreamRule {
|
|
10
|
+
meta?: unknown;
|
|
11
|
+
create(context: object): object;
|
|
12
|
+
}
|
|
13
|
+
export interface WrappedPlugin {
|
|
14
|
+
meta: {
|
|
15
|
+
name: string;
|
|
16
|
+
};
|
|
17
|
+
rules: Record<string, UpstreamRule>;
|
|
18
|
+
}
|
|
19
|
+
/** Moves negative columns to 0; a negative start becomes a zero-width location at column 0. */
|
|
20
|
+
export declare function clampLoc(loc: Loc): Loc;
|
|
21
|
+
/** The rule with `context.report` locations passed through `clampLoc`. */
|
|
22
|
+
export declare function wrapRule(rule: UpstreamRule): UpstreamRule;
|
|
23
|
+
declare const plugin: WrappedPlugin;
|
|
24
|
+
export { plugin as default };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import upstream from "@eslint-community/eslint-plugin-eslint-comments";
|
|
2
|
+
// src/eslint-comments/index.ts
|
|
3
|
+
function atColumnZero(position) {
|
|
4
|
+
return {
|
|
5
|
+
line: position.line,
|
|
6
|
+
column: 0
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Moves negative columns to 0; a negative start becomes a zero-width location at column 0. */
|
|
10
|
+
function clampLoc(loc) {
|
|
11
|
+
if (!("start" in loc)) return loc.column < 0 ? atColumnZero(loc) : loc;
|
|
12
|
+
const { start, end } = loc;
|
|
13
|
+
if (start.column < 0) {
|
|
14
|
+
const at = atColumnZero(start);
|
|
15
|
+
return {
|
|
16
|
+
start: at,
|
|
17
|
+
end: at
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (end && end.column < 0) return {
|
|
21
|
+
start,
|
|
22
|
+
end: start
|
|
23
|
+
};
|
|
24
|
+
return loc;
|
|
25
|
+
}
|
|
26
|
+
function clampDescriptor(descriptor) {
|
|
27
|
+
if (typeof descriptor !== "object" || descriptor === null || !("loc" in descriptor)) return descriptor;
|
|
28
|
+
const { loc } = descriptor;
|
|
29
|
+
if (typeof loc !== "object" || loc === null) return descriptor;
|
|
30
|
+
return {
|
|
31
|
+
...descriptor,
|
|
32
|
+
loc: clampLoc(loc)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** The rule with `context.report` locations passed through `clampLoc`. */
|
|
36
|
+
function wrapRule(rule) {
|
|
37
|
+
return {
|
|
38
|
+
...rule,
|
|
39
|
+
create(context) {
|
|
40
|
+
const report = context.report;
|
|
41
|
+
const derived = Object.create(context, { report: { value: (descriptor, ...rest) => report.call(context, clampDescriptor(descriptor), ...rest) } });
|
|
42
|
+
return rule.create(derived);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const plugin = {
|
|
47
|
+
meta: { name: "eslint-comments" },
|
|
48
|
+
rules: Object.fromEntries(Object.entries(upstream.rules).map(([name, rule]) => [name, wrapRule(rule)]))
|
|
49
|
+
};
|
|
50
|
+
export { clampLoc, plugin as default, wrapRule };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
// src/plugin/utils/paths.ts
|
|
4
|
+
function toPosix(p) {
|
|
5
|
+
return p.split(path.sep).join("/");
|
|
6
|
+
}
|
|
7
|
+
const realCache = /* @__PURE__ */ new Map();
|
|
8
|
+
function real(p) {
|
|
9
|
+
let cached = realCache.get(p);
|
|
10
|
+
if (cached === void 0) {
|
|
11
|
+
try {
|
|
12
|
+
cached = realpathSync(p);
|
|
13
|
+
} catch {
|
|
14
|
+
cached = p;
|
|
15
|
+
}
|
|
16
|
+
realCache.set(p, cached);
|
|
17
|
+
}
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Path of the linted file relative to the working directory the linter was started in (the
|
|
22
|
+
* repository root in CI and hooks), with forward slashes. Both linters report absolute paths, but
|
|
23
|
+
* one of them may have resolved symlinks (for example /tmp → /private/tmp on macOS).
|
|
24
|
+
*/
|
|
25
|
+
function repoRelativePath(context) {
|
|
26
|
+
const cwd = context.cwd;
|
|
27
|
+
const file = path.isAbsolute(context.filename) ? context.filename : path.join(cwd, context.filename);
|
|
28
|
+
let rel = path.relative(cwd, file);
|
|
29
|
+
if (rel.startsWith("..")) rel = path.relative(real(cwd), real(file));
|
|
30
|
+
return toPosix(rel);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Compile repo-relative path patterns. Supported forms:
|
|
34
|
+
* - `dir/` — the directory and everything below it;
|
|
35
|
+
* - globs with `**` (any depth), `*` (one segment) and `?`; `dir/**` also matches `dir` itself;
|
|
36
|
+
* - anything else is an exact path.
|
|
37
|
+
*/
|
|
38
|
+
function compilePathPatterns(patterns) {
|
|
39
|
+
const regexes = patterns.map(patternToRegExp);
|
|
40
|
+
return (rel) => regexes.some((re) => re.test(rel));
|
|
41
|
+
}
|
|
42
|
+
function patternToRegExp(pattern) {
|
|
43
|
+
const glob = pattern.endsWith("/") ? `${pattern}**` : pattern;
|
|
44
|
+
let out = "";
|
|
45
|
+
for (let i = 0; i < glob.length; i++) {
|
|
46
|
+
const ch = glob[i];
|
|
47
|
+
if (ch === "*") {
|
|
48
|
+
if (glob[i + 1] === "*") {
|
|
49
|
+
const slashAfter = glob[i + 2] === "/";
|
|
50
|
+
if (i + 2 === glob.length && out.endsWith("/")) out = `${out.slice(0, -1)}(?:/.*)?`;
|
|
51
|
+
else out += slashAfter ? "(?:.*/)?" : ".*";
|
|
52
|
+
i += slashAfter ? 2 : 1;
|
|
53
|
+
} else out += "[^/]*";
|
|
54
|
+
} else if (ch === "?") out += "[^/]";
|
|
55
|
+
else out += ch.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
56
|
+
}
|
|
57
|
+
return new RegExp(`^${out}$`);
|
|
58
|
+
}
|
|
59
|
+
export { repoRelativePath as n, compilePathPatterns as t };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
type RuleListener = Record<string, (node: never) => void>;
|
|
2
|
+
interface RuleMeta {
|
|
3
|
+
type: "problem" | "suggestion" | "layout";
|
|
4
|
+
docs: {
|
|
5
|
+
description: string;
|
|
6
|
+
url: string;
|
|
7
|
+
};
|
|
8
|
+
schema: readonly unknown[];
|
|
9
|
+
messages: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
interface PluginRule {
|
|
12
|
+
meta: RuleMeta;
|
|
13
|
+
create(context: never): RuleListener;
|
|
14
|
+
}
|
|
15
|
+
interface Plugin {
|
|
16
|
+
meta: {
|
|
17
|
+
name: string;
|
|
18
|
+
version: string;
|
|
19
|
+
};
|
|
20
|
+
rules: Record<string, PluginRule>;
|
|
21
|
+
}
|
|
22
|
+
/** Kept in sync with package.json by test/plugin.test.ts. */
|
|
23
|
+
export declare const VERSION = "0.1.0-alpha.0";
|
|
24
|
+
declare const plugin: Plugin;
|
|
25
|
+
export { type Plugin, type PluginRule, type RuleMeta, plugin as default };
|