@mark1russell7/conventions 0.8.0 → 0.8.2
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 +15 -2
- package/build/cli.js +1 -1
- package/build/index.d.ts +32 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -1
- package/build/{run-d-5ceKnT.js → run-DD72yQn7.js} +348 -85
- package/build/run-DD72yQn7.js.map +1 -0
- package/package.json +1 -1
- package/build/run-d-5ceKnT.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { spawnSync } from "node:child_process";
|
|
3
|
-
import { appendFileSync, existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
4
|
-
import { dirname, extname, join, posix, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { appendFileSync, existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, dirname, extname, join, posix, relative, resolve, sep } from "node:path";
|
|
5
5
|
// src/config.ts
|
|
6
6
|
/** The fields of the configuration of a rule that are not options. */
|
|
7
7
|
const RULE_FIELDS = [
|
|
@@ -130,12 +130,18 @@ function globToRegExp(glob) {
|
|
|
130
130
|
}
|
|
131
131
|
/** The characters that make a path segment a pattern. */
|
|
132
132
|
const GLOB_CHARS = /[*?{]/;
|
|
133
|
+
/** This function removes the `/` characters at the end of a path, with a loop in place of a regular expression. */
|
|
134
|
+
function trimEndSlashes(path) {
|
|
135
|
+
let end = path.length;
|
|
136
|
+
while (end > 0 && path.charAt(end - 1) === "/") end -= 1;
|
|
137
|
+
return path.slice(0, end);
|
|
138
|
+
}
|
|
133
139
|
/**
|
|
134
140
|
* This function gives the folders under `root` that a glob names, as `/` paths relative to `root`,
|
|
135
141
|
* in the order of the code units. It does not go into `node_modules` or a folder that starts with `.`.
|
|
136
142
|
*/
|
|
137
143
|
function globFolders(root, glob) {
|
|
138
|
-
const clean = glob.
|
|
144
|
+
const clean = trimEndSlashes(glob.startsWith("./") ? glob.slice(2) : glob);
|
|
139
145
|
const segments = clean.split("/");
|
|
140
146
|
const firstPattern = segments.findIndex((segment) => GLOB_CHARS.test(segment));
|
|
141
147
|
if (firstPattern < 0) return existsSync(join(root, clean)) ? [clean] : [];
|
|
@@ -205,6 +211,13 @@ function packageFolders(root, globs = workspaceGlobs(root)) {
|
|
|
205
211
|
/**
|
|
206
212
|
* The shape of a rule, the facts that a rule reads (the context), and small helpers for the rules.
|
|
207
213
|
*/
|
|
214
|
+
/** This function gives `text` with the line `line` (from 1) replaced by `lines`, with the line ends of `text`. An empty `lines` removes the line. */
|
|
215
|
+
function replaceLine(text, line, lines) {
|
|
216
|
+
const eol = text.includes("\r\n") ? "\r\n" : "\n";
|
|
217
|
+
const all = text.split(/\r?\n/);
|
|
218
|
+
all.splice(line - 1, 1, ...lines);
|
|
219
|
+
return all.join(eol);
|
|
220
|
+
}
|
|
208
221
|
/** The fields of `package.json` that hold dependencies. */
|
|
209
222
|
const DEPENDENCY_FIELDS = [
|
|
210
223
|
"dependencies",
|
|
@@ -1838,11 +1851,33 @@ function jobsOf(lines) {
|
|
|
1838
1851
|
}
|
|
1839
1852
|
const hasTopGroup = (lines) => lines.some((line) => indentOf(line) === 0 && line.startsWith("concurrency:"));
|
|
1840
1853
|
/**
|
|
1854
|
+
* This function gives the edit that adds a concurrency group at the top of a workflow, before `jobs:`. A new run of a pull
|
|
1855
|
+
* request stops the old run. A run of another event waits for the old run, so that no release or scheduled run stops.
|
|
1856
|
+
*/
|
|
1857
|
+
function groupEdits(file, text, lines) {
|
|
1858
|
+
const jobs = lines.findIndex((line) => indentOf(line) === 0 && line.startsWith("jobs:"));
|
|
1859
|
+
if (jobs < 0) return [];
|
|
1860
|
+
const pad = " ".repeat(childIndent(lines, jobs) ?? 2);
|
|
1861
|
+
const original = text.split(/\r?\n/)[jobs] ?? "jobs:";
|
|
1862
|
+
const group = [
|
|
1863
|
+
"concurrency:",
|
|
1864
|
+
pad + "group: ${{ github.workflow }}-${{ github.ref }}",
|
|
1865
|
+
pad + "cancel-in-progress: ${{ github.event_name == 'pull_request' }}",
|
|
1866
|
+
"",
|
|
1867
|
+
original
|
|
1868
|
+
];
|
|
1869
|
+
return [{
|
|
1870
|
+
file,
|
|
1871
|
+
before: text,
|
|
1872
|
+
after: replaceLine(text, jobs + 1, group)
|
|
1873
|
+
}];
|
|
1874
|
+
}
|
|
1875
|
+
/**
|
|
1841
1876
|
* This function gives the breaks of the concurrency groups. A workflow with no group at the top needs one in each job.
|
|
1842
1877
|
* A job that calls a shared workflow of `grouped` gets its group from the jobs of that workflow. A shared workflow gets
|
|
1843
1878
|
* its group from its caller, but a shared workflow of `grouped` has a group in each job.
|
|
1844
1879
|
*/
|
|
1845
|
-
function concurrencyBreaks(file, lines, grouped) {
|
|
1880
|
+
function concurrencyBreaks(file, text, lines, grouped) {
|
|
1846
1881
|
const name = file.slice(file.lastIndexOf("/") + 1);
|
|
1847
1882
|
const jobs = jobsOf(lines);
|
|
1848
1883
|
if (isShared(lines)) {
|
|
@@ -1854,49 +1889,98 @@ function concurrencyBreaks(file, lines, grouped) {
|
|
|
1854
1889
|
}));
|
|
1855
1890
|
}
|
|
1856
1891
|
if (hasTopGroup(lines)) return [];
|
|
1857
|
-
|
|
1892
|
+
const missing = jobs.filter((job) => !job.grouped && !(job.calls !== void 0 && grouped.includes(job.calls)));
|
|
1893
|
+
const edits = missing.length === 0 ? [] : groupEdits(file, text, lines);
|
|
1894
|
+
return missing.map((job) => ({
|
|
1858
1895
|
file,
|
|
1859
1896
|
line: job.line,
|
|
1860
|
-
message: `${file}: the job ${job.name} has no concurrency group, and the workflow has none
|
|
1897
|
+
message: `${file}: the job ${job.name} has no concurrency group, and the workflow has none.`,
|
|
1898
|
+
edits
|
|
1861
1899
|
}));
|
|
1862
1900
|
}
|
|
1863
1901
|
/** This function gives the breaks of the matrices: each `strategy` with a `matrix` has a `max-parallel` of `limit` or less. */
|
|
1864
|
-
function matrixBreaks(file, lines, limit) {
|
|
1902
|
+
function matrixBreaks(file, text, lines, limit) {
|
|
1865
1903
|
const breaks = [];
|
|
1904
|
+
const originals = text.split(/\r?\n/);
|
|
1866
1905
|
lines.forEach((line, index) => {
|
|
1867
1906
|
if (keyValue(line, "strategy") === void 0) return;
|
|
1868
1907
|
const keyIndent = childIndent(lines, index);
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1908
|
+
if (keyIndent === void 0) return;
|
|
1909
|
+
const keys = [];
|
|
1910
|
+
for (let i = index + 1; i < blockEnd(lines, index); i++) if ((lines[i] ?? "").trim() !== "" && indentOf(lines[i] ?? "") === keyIndent) keys.push(i);
|
|
1911
|
+
if (!keys.some((i) => keyValue(lines[i] ?? "", "matrix") !== void 0)) return;
|
|
1912
|
+
const at = keys.find((i) => keyValue(lines[i] ?? "", "max-parallel") !== void 0);
|
|
1913
|
+
const raw = at === void 0 ? void 0 : keyValue(lines[at] ?? "", "max-parallel");
|
|
1872
1914
|
const value = raw === void 0 ? NaN : Number(raw);
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1915
|
+
const wanted = `${" ".repeat(keyIndent)}max-parallel: ${String(limit)}`;
|
|
1916
|
+
if (at === void 0 || raw === void 0) {
|
|
1917
|
+
const after = replaceLine(text, index + 1, [originals[index] ?? line, wanted]);
|
|
1918
|
+
breaks.push({
|
|
1919
|
+
file,
|
|
1920
|
+
line: index + 1,
|
|
1921
|
+
message: `${file}: a matrix has no max-parallel.`,
|
|
1922
|
+
edits: [{
|
|
1923
|
+
file,
|
|
1924
|
+
before: text,
|
|
1925
|
+
after
|
|
1926
|
+
}]
|
|
1927
|
+
});
|
|
1928
|
+
} else if (!Number.isInteger(value) || value < 1 || value > limit) {
|
|
1929
|
+
const after = replaceLine(text, at + 1, [wanted]);
|
|
1930
|
+
breaks.push({
|
|
1931
|
+
file,
|
|
1932
|
+
line: index + 1,
|
|
1933
|
+
message: `${file}: a matrix has max-parallel ${raw}, and the most is ${String(limit)}.`,
|
|
1934
|
+
edits: [{
|
|
1935
|
+
file,
|
|
1936
|
+
before: text,
|
|
1937
|
+
after
|
|
1938
|
+
}]
|
|
1939
|
+
});
|
|
1940
|
+
}
|
|
1883
1941
|
});
|
|
1884
1942
|
return breaks;
|
|
1885
1943
|
}
|
|
1886
|
-
/**
|
|
1887
|
-
function
|
|
1944
|
+
/**
|
|
1945
|
+
* This function gives the minute field of a schedule that starts `perHour` times in each hour, at equal steps. One start is at
|
|
1946
|
+
* `minute`: `"18"` for 1, `"18,48"` for 2. The list is in the order of the numbers.
|
|
1947
|
+
*/
|
|
1948
|
+
function minuteField(minute, perHour) {
|
|
1949
|
+
const step = 60 / perHour;
|
|
1950
|
+
return Array.from({ length: perHour }, (_, k) => (minute + k * step) % 60).sort((a, b) => a - b).join(",");
|
|
1951
|
+
}
|
|
1952
|
+
/** This function gives the number of starts in each hour of a minute field (`"*\/30"` or `"5,35"` give 2), or 1 when it cannot tell. */
|
|
1953
|
+
function startsPerHour(field) {
|
|
1954
|
+
const step = /^\*\/(\d+)$/.exec(field);
|
|
1955
|
+
const count = step ? 60 / Number(step[1]) : field.split(",").length;
|
|
1956
|
+
return Number.isInteger(count) && count >= 1 && 60 % count === 0 ? count : 1;
|
|
1957
|
+
}
|
|
1958
|
+
/**
|
|
1959
|
+
* This function gives the breaks of the schedules: the minute of each `cron` is the minute of the repository and the file. A
|
|
1960
|
+
* schedule that starts more than once in each hour starts at equal steps from that minute (`18,48` for each 30 minutes).
|
|
1961
|
+
*/
|
|
1962
|
+
function cronBreaks(file, text, lines, minute) {
|
|
1888
1963
|
const breaks = [];
|
|
1964
|
+
const originals = text.split(/\r?\n/);
|
|
1889
1965
|
lines.forEach((line, index) => {
|
|
1890
1966
|
const value = keyValue(line, "cron");
|
|
1891
1967
|
if (value === void 0) return;
|
|
1892
1968
|
const fields = value.split(" ").filter((field) => field !== "");
|
|
1893
|
-
|
|
1894
|
-
|
|
1969
|
+
const wanted = minuteField(minute, startsPerHour(fields[0] ?? ""));
|
|
1970
|
+
if (fields[0] === wanted) return;
|
|
1971
|
+
const expected = [wanted, ...fields.slice(1)].join(" ");
|
|
1972
|
+
const original = originals[index] ?? line;
|
|
1973
|
+
const edits = original.includes(value) ? [{
|
|
1974
|
+
file,
|
|
1975
|
+
before: text,
|
|
1976
|
+
after: replaceLine(text, index + 1, [original.replace(value, () => expected)])
|
|
1977
|
+
}] : [];
|
|
1895
1978
|
breaks.push({
|
|
1896
1979
|
file,
|
|
1897
1980
|
line: index + 1,
|
|
1898
1981
|
message: `${file}: the schedule "${value}" starts at minute ${fields[0] ?? "?"}, and the minute of this repository and file is ${String(minute)}.`,
|
|
1899
|
-
fix: `cron: "${expected}"
|
|
1982
|
+
fix: `cron: "${expected}"`,
|
|
1983
|
+
edits
|
|
1900
1984
|
});
|
|
1901
1985
|
});
|
|
1902
1986
|
return breaks;
|
|
@@ -1950,13 +2034,13 @@ const runnerBudget = {
|
|
|
1950
2034
|
continue;
|
|
1951
2035
|
}
|
|
1952
2036
|
const lines = text.split(/\r?\n/).map((line) => line.trimStart().startsWith("#") ? "" : line);
|
|
1953
|
-
breaks.push(...matrixBreaks(file, lines, limit), ...concurrencyBreaks(file, lines, grouped));
|
|
2037
|
+
breaks.push(...matrixBreaks(file, text, lines, limit), ...concurrencyBreaks(file, text, lines, grouped));
|
|
1954
2038
|
if (options["minute"] !== false) {
|
|
1955
2039
|
if (lines.some((line) => keyValue(line, "cron") !== void 0) && repository === void 0) breaks.push({
|
|
1956
2040
|
file,
|
|
1957
2041
|
message: `${file} has a schedule, and the rule cannot find the repository (GITHUB_REPOSITORY, or a GitHub URL of the remote origin).`
|
|
1958
2042
|
});
|
|
1959
|
-
else if (repository !== void 0) breaks.push(...cronBreaks(file, lines, scheduleMinute(repository, file)));
|
|
2043
|
+
else if (repository !== void 0) breaks.push(...cronBreaks(file, text, lines, scheduleMinute(repository, file)));
|
|
1960
2044
|
}
|
|
1961
2045
|
}
|
|
1962
2046
|
return breaks;
|
|
@@ -2097,57 +2181,31 @@ const sourceMain = {
|
|
|
2097
2181
|
return breaks;
|
|
2098
2182
|
}
|
|
2099
2183
|
};
|
|
2100
|
-
// src/rules/tests-required.ts
|
|
2101
|
-
/**
|
|
2102
|
-
* The rule `tests-required`: no test runner passes when it finds no test (`passWithNoTests`).
|
|
2103
|
-
*/
|
|
2104
|
-
/** The files that set the options of a test runner: its configs, and package.json for the scripts. */
|
|
2105
|
-
const RUNNER_FILE = /(?:^|\/)(?:(?:vitest|vite|jest|playwright)(?:\.[\w-]+)*\.config\.[cm]?[jt]s|vitest\.workspace\.[cm]?[jt]s|package\.json)$/;
|
|
2106
|
-
/** The option in a config (`passWithNoTests: true`) or on a command line (`--passWithNoTests`, `--pass-with-no-tests`). */
|
|
2107
|
-
const PASS_WITH_NO_TESTS = /passWithNoTests\s*:\s*true|--passWithNoTests(?!=false)|--pass-with-no-tests(?!=false)/;
|
|
2108
|
-
const testsRequired = {
|
|
2109
|
-
id: "tests-required",
|
|
2110
|
-
title: "No test runner passes when it finds no test: no `passWithNoTests: true` in a config, and no `--passWithNoTests` in a script.",
|
|
2111
|
-
reason: "With the option, a test step that finds 0 tests passes. A broken include pattern, a moved folder or a renamed file then turns the gate green with no test run. In the fleet audit of 2026-10-30, vex had the option in all 9 vitest configs and client in 2 packages that have tests.",
|
|
2112
|
-
fix: "Remove the option. A package with no tests has no test script, so the shared CI skips it. A package whose tests a later change adds keeps no test script until then.",
|
|
2113
|
-
sources: [{
|
|
2114
|
-
repo: "fleet",
|
|
2115
|
-
rule: "fail-closed",
|
|
2116
|
-
doc: "docs/STRATEGY.md (2026-10-30, gates and probes fail closed)"
|
|
2117
|
-
}],
|
|
2118
|
-
options: {},
|
|
2119
|
-
check(context) {
|
|
2120
|
-
const files = context.gitFiles();
|
|
2121
|
-
if (files === void 0) return { skipped: "the root is not in a git repository" };
|
|
2122
|
-
const breaks = [];
|
|
2123
|
-
for (const file of files.filter((path) => RUNNER_FILE.test(path)).sort(byCode)) {
|
|
2124
|
-
let text;
|
|
2125
|
-
try {
|
|
2126
|
-
text = readFileSync(join(context.root, file), "utf8");
|
|
2127
|
-
} catch {
|
|
2128
|
-
continue;
|
|
2129
|
-
}
|
|
2130
|
-
const index = text.split(/\r?\n/).findIndex((line) => PASS_WITH_NO_TESTS.test(line));
|
|
2131
|
-
if (index >= 0) breaks.push({
|
|
2132
|
-
file,
|
|
2133
|
-
line: index + 1,
|
|
2134
|
-
message: `${file} lets the tests pass when they find no test (passWithNoTests).`
|
|
2135
|
-
});
|
|
2136
|
-
}
|
|
2137
|
-
return breaks;
|
|
2138
|
-
}
|
|
2139
|
-
};
|
|
2140
2184
|
// src/rules/tsconfig-test.ts
|
|
2141
2185
|
/**
|
|
2142
2186
|
* The rule `tsconfig-test`: each TypeScript package has a `tsconfig.test.json`, and its typecheck
|
|
2143
2187
|
* examines it.
|
|
2144
2188
|
*/
|
|
2145
|
-
/** The
|
|
2146
|
-
const
|
|
2189
|
+
/** The ends of the name of a test file: `.test.` or `.spec.`, then an extension of JavaScript or TypeScript. */
|
|
2190
|
+
const TEST_ENDS = ["test", "spec"].flatMap((kind) => [
|
|
2191
|
+
"",
|
|
2192
|
+
"c",
|
|
2193
|
+
"m"
|
|
2194
|
+
].flatMap((module) => ["j", "t"].flatMap((lang) => ["", "x"].map((jsx) => `.${kind}.${module}${lang}s${jsx}`))));
|
|
2195
|
+
/**
|
|
2196
|
+
* This function tells if a path is a test file: `*.test.*` or `*.spec.*` of JavaScript or TypeScript, or a file in a
|
|
2197
|
+
* folder `__tests__`. It uses string operations, with no regular expression, so a long path takes linear time.
|
|
2198
|
+
*/
|
|
2199
|
+
function isTestFile(path) {
|
|
2200
|
+
const segments = path.split("/");
|
|
2201
|
+
const name = segments[segments.length - 1] ?? "";
|
|
2202
|
+
if (name !== "" && segments.slice(0, -1).includes("__tests__")) return true;
|
|
2203
|
+
return TEST_ENDS.some((end) => name.length > end.length && name.endsWith(end));
|
|
2204
|
+
}
|
|
2147
2205
|
/** This function tells if git tracks a test file in the package folder `folder` (`""` is the root). */
|
|
2148
2206
|
function hasTests(files, folder) {
|
|
2149
2207
|
const prefix = folder === "" ? "" : `${folder}/`;
|
|
2150
|
-
return files.some((path) => path.startsWith(prefix) &&
|
|
2208
|
+
return files.some((path) => path.startsWith(prefix) && isTestFile(path.slice(prefix.length)));
|
|
2151
2209
|
}
|
|
2152
2210
|
const tsconfigTest = {
|
|
2153
2211
|
id: "tsconfig-test",
|
|
@@ -2230,6 +2288,79 @@ const tsconfigTest = {
|
|
|
2230
2288
|
return breaks;
|
|
2231
2289
|
}
|
|
2232
2290
|
};
|
|
2291
|
+
// src/rules/tests-required.ts
|
|
2292
|
+
/**
|
|
2293
|
+
* The rule `tests-required`: no test runner passes when it finds no test (`passWithNoTests`).
|
|
2294
|
+
*/
|
|
2295
|
+
/** The files that set the options of a test runner: its configs, and package.json for the scripts. */
|
|
2296
|
+
const RUNNER_FILE = /(?:^|\/)(?:(?:vitest|vite|jest|playwright)(?:\.[\w-]+)*\.config\.[cm]?[jt]s|vitest\.workspace\.[cm]?[jt]s|package\.json)$/;
|
|
2297
|
+
/** The option in a config (`passWithNoTests: true`) or on a command line (`--passWithNoTests`, `--pass-with-no-tests`). */
|
|
2298
|
+
const PASS_WITH_NO_TESTS = /passWithNoTests\s*:\s*true|--passWithNoTests(?!=false)|--pass-with-no-tests(?!=false)/;
|
|
2299
|
+
/** A line that holds only the option in a config. `conventions fix` removes such a line. */
|
|
2300
|
+
const OPTION_LINE = /^\s*passWithNoTests\s*:\s*true\s*,?\s*$/;
|
|
2301
|
+
/** The option in a line of a config with more keys. The first form has the comma after it, and the second form has the comma before it. */
|
|
2302
|
+
const OPTION_IN_LINE = [/\bpassWithNoTests\s*:\s*true\s*,\s*/, /,?\s*\bpassWithNoTests\s*:\s*true\b/];
|
|
2303
|
+
/** The option on a command line, with the space before it. */
|
|
2304
|
+
const FLAG = / --(?:passWithNoTests|pass-with-no-tests)(?:=true)?(?=\s|"|$)/g;
|
|
2305
|
+
/** This function gives the folder of the package that holds `file`: the longest folder of a package that starts its path. */
|
|
2306
|
+
function packageOf(context, file) {
|
|
2307
|
+
let best = "";
|
|
2308
|
+
for (const info of context.packages) if (info.folder !== "" && file.startsWith(`${info.folder}/`) && info.folder.length > best.length) best = info.folder;
|
|
2309
|
+
return best;
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* This function gives the edit that removes the option from one line, or no edit. The edit exists only when the package has
|
|
2313
|
+
* test files: then the test step still finds tests. A package with no tests needs a person, who removes its test script.
|
|
2314
|
+
*/
|
|
2315
|
+
function removeOption(context, files, file, text, index) {
|
|
2316
|
+
if (!hasTests(files, packageOf(context, file))) return [];
|
|
2317
|
+
const line = text.split(/\r?\n/)[index] ?? "";
|
|
2318
|
+
if (OPTION_LINE.test(line)) return [{
|
|
2319
|
+
file,
|
|
2320
|
+
before: text,
|
|
2321
|
+
after: replaceLine(text, index + 1, [])
|
|
2322
|
+
}];
|
|
2323
|
+
const form = OPTION_IN_LINE.find((pattern) => pattern.test(line));
|
|
2324
|
+
const without = file.endsWith("package.json") ? line.replace(FLAG, "") : form === void 0 ? line : line.replace(form, "");
|
|
2325
|
+
return without === line ? [] : [{
|
|
2326
|
+
file,
|
|
2327
|
+
before: text,
|
|
2328
|
+
after: replaceLine(text, index + 1, [without])
|
|
2329
|
+
}];
|
|
2330
|
+
}
|
|
2331
|
+
const testsRequired = {
|
|
2332
|
+
id: "tests-required",
|
|
2333
|
+
title: "No test runner passes when it finds no test: no `passWithNoTests: true` in a config, and no `--passWithNoTests` in a script.",
|
|
2334
|
+
reason: "With the option, a test step that finds 0 tests passes. A broken include pattern, a moved folder or a renamed file then turns the gate green with no test run. In the fleet audit of 2026-10-30, vex had the option in all 9 vitest configs and client in 2 packages that have tests.",
|
|
2335
|
+
fix: "Remove the option. A package with no tests has no test script, so the shared CI skips it. A package whose tests a later change adds keeps no test script until then.",
|
|
2336
|
+
sources: [{
|
|
2337
|
+
repo: "fleet",
|
|
2338
|
+
rule: "fail-closed",
|
|
2339
|
+
doc: "docs/STRATEGY.md (2026-10-30, gates and probes fail closed)"
|
|
2340
|
+
}],
|
|
2341
|
+
options: {},
|
|
2342
|
+
check(context) {
|
|
2343
|
+
const files = context.gitFiles();
|
|
2344
|
+
if (files === void 0) return { skipped: "the root is not in a git repository" };
|
|
2345
|
+
const breaks = [];
|
|
2346
|
+
for (const file of files.filter((path) => RUNNER_FILE.test(path)).sort(byCode)) {
|
|
2347
|
+
let text;
|
|
2348
|
+
try {
|
|
2349
|
+
text = readFileSync(join(context.root, file), "utf8");
|
|
2350
|
+
} catch {
|
|
2351
|
+
continue;
|
|
2352
|
+
}
|
|
2353
|
+
const index = text.split(/\r?\n/).findIndex((line) => PASS_WITH_NO_TESTS.test(line));
|
|
2354
|
+
if (index >= 0) breaks.push({
|
|
2355
|
+
file,
|
|
2356
|
+
line: index + 1,
|
|
2357
|
+
message: `${file} lets the tests pass when they find no test (passWithNoTests).`,
|
|
2358
|
+
edits: removeOption(context, files, file, text, index)
|
|
2359
|
+
});
|
|
2360
|
+
}
|
|
2361
|
+
return breaks;
|
|
2362
|
+
}
|
|
2363
|
+
};
|
|
2233
2364
|
// src/rules/typed-files.ts
|
|
2234
2365
|
/**
|
|
2235
2366
|
* The rule `typed-files`: each TypeScript file that git keeps is in a tsconfig that a `typecheck`
|
|
@@ -2353,6 +2484,65 @@ function referencesOf(config) {
|
|
|
2353
2484
|
if (!Array.isArray(references)) return [];
|
|
2354
2485
|
return references.flatMap((item) => typeof item === "object" && item !== null && typeof item.path === "string" ? [resolve(dirname(config), item.path)] : []);
|
|
2355
2486
|
}
|
|
2487
|
+
/** A config file of a tool (`vitest.config.ts`), with its extension. */
|
|
2488
|
+
const CONFIG_FILE = /\.config(\.[cm]?ts)$/;
|
|
2489
|
+
/** A `rootDir` that is the folder of the tsconfig, so that the config files at the root are in it. */
|
|
2490
|
+
const ROOT_DIRS = /* @__PURE__ */ new Set([
|
|
2491
|
+
".",
|
|
2492
|
+
"./",
|
|
2493
|
+
"${configDir}",
|
|
2494
|
+
"${configDir}/"
|
|
2495
|
+
]);
|
|
2496
|
+
/**
|
|
2497
|
+
* This function gives the edit that adds the config files at the root of a package to its `tsconfig.test.json`. It adds
|
|
2498
|
+
* the include `*.config.ts`. When the `rootDir` is a subfolder, it sets `rootDir: "."`. The edit changes only these
|
|
2499
|
+
* values and keeps the format of the file. It gives no edit for a tsconfig with comments, so that no comment goes. It
|
|
2500
|
+
* also gives no edit when the changed text does not parse as the intended config.
|
|
2501
|
+
*/
|
|
2502
|
+
function configEdit(root, tsconfig, extension) {
|
|
2503
|
+
const text = readFileSync(tsconfig, "utf8");
|
|
2504
|
+
let json;
|
|
2505
|
+
try {
|
|
2506
|
+
json = JSON.parse(text);
|
|
2507
|
+
} catch {
|
|
2508
|
+
return [];
|
|
2509
|
+
}
|
|
2510
|
+
const globs = json.include;
|
|
2511
|
+
if (!Array.isArray(globs)) return [];
|
|
2512
|
+
const glob = `${globs.length > 0 && globs.every((glob) => typeof glob === "string" && glob.startsWith("${configDir}/")) ? "${configDir}/" : ""}*.config${extension}`;
|
|
2513
|
+
const eol = text.includes("\r\n") ? "\r\n" : "\n";
|
|
2514
|
+
let after = text;
|
|
2515
|
+
if (!globs.includes(glob)) after = after.replace(/("include"\s*:\s*\[)([^\]]*)\]/, (_all, start, items) => {
|
|
2516
|
+
const kept = items.replace(/\s+$/, "");
|
|
2517
|
+
const end = items.slice(kept.length);
|
|
2518
|
+
if (kept.trim() === "") return `${start}${JSON.stringify(glob)}]`;
|
|
2519
|
+
const indent = kept.includes("\n") ? /\n([ \t]*)[^\n]*$/.exec(kept)?.[1] ?? "" : void 0;
|
|
2520
|
+
return `${start}${kept},${indent === void 0 ? " " : `${eol}${indent}`}${JSON.stringify(glob)}${end}]`;
|
|
2521
|
+
});
|
|
2522
|
+
const rootDir = json.compilerOptions?.["rootDir"];
|
|
2523
|
+
const moveRoot = typeof rootDir === "string" && !ROOT_DIRS.has(rootDir);
|
|
2524
|
+
if (moveRoot) after = after.replace(/("rootDir"\s*:\s*)"(?:[^"\\]|\\.)*"/, "$1\".\"");
|
|
2525
|
+
const intended = {
|
|
2526
|
+
...json,
|
|
2527
|
+
...moveRoot ? { compilerOptions: {
|
|
2528
|
+
...json.compilerOptions,
|
|
2529
|
+
rootDir: "."
|
|
2530
|
+
} } : {},
|
|
2531
|
+
include: globs.includes(glob) ? globs : [...globs, glob]
|
|
2532
|
+
};
|
|
2533
|
+
let parsed;
|
|
2534
|
+
try {
|
|
2535
|
+
parsed = JSON.parse(after);
|
|
2536
|
+
} catch {
|
|
2537
|
+
return [];
|
|
2538
|
+
}
|
|
2539
|
+
if (JSON.stringify(parsed) !== JSON.stringify(intended)) return [];
|
|
2540
|
+
return after === text ? [] : [{
|
|
2541
|
+
file: fromRoot(root, tsconfig),
|
|
2542
|
+
before: text,
|
|
2543
|
+
after
|
|
2544
|
+
}];
|
|
2545
|
+
}
|
|
2356
2546
|
/** The path of `file` relative to `root`, with `/`. */
|
|
2357
2547
|
const fromRoot = (root, file) => relative(root, file).split("\\").join("/");
|
|
2358
2548
|
const typedFiles = {
|
|
@@ -2383,6 +2573,7 @@ const typedFiles = {
|
|
|
2383
2573
|
}];
|
|
2384
2574
|
const breaks = [];
|
|
2385
2575
|
const queue = [];
|
|
2576
|
+
const testConfigs = /* @__PURE__ */ new Map();
|
|
2386
2577
|
const packages = [context.rootPackage, ...codePackages(context).filter((info) => info !== context.rootPackage)];
|
|
2387
2578
|
for (const info of packages) {
|
|
2388
2579
|
const scripts = info.manifest["scripts"];
|
|
@@ -2400,6 +2591,7 @@ const typedFiles = {
|
|
|
2400
2591
|
continue;
|
|
2401
2592
|
}
|
|
2402
2593
|
queue.push(path);
|
|
2594
|
+
if (basename(path) === "tsconfig.test.json" && resolve(dirname(path)) === resolve(info.dir)) testConfigs.set(info.folder, path);
|
|
2403
2595
|
}
|
|
2404
2596
|
}
|
|
2405
2597
|
const extra = Array.isArray(options["configs"]) ? options["configs"] : [];
|
|
@@ -2434,10 +2626,18 @@ const typedFiles = {
|
|
|
2434
2626
|
message: "No typecheck script names a tsconfig, so no TypeScript file has a type check.",
|
|
2435
2627
|
fix: "Add a script typecheck (`tsc -p tsconfig.json`) to each package."
|
|
2436
2628
|
});
|
|
2437
|
-
for (const file of files)
|
|
2438
|
-
file
|
|
2439
|
-
|
|
2440
|
-
|
|
2629
|
+
for (const file of files) {
|
|
2630
|
+
if (covered.has(file)) continue;
|
|
2631
|
+
const folder = file.includes("/") ? file.slice(0, file.lastIndexOf("/")) : "";
|
|
2632
|
+
const extension = CONFIG_FILE.exec(file)?.[1];
|
|
2633
|
+
const tsconfig = testConfigs.get(folder);
|
|
2634
|
+
const edits = extension === void 0 || tsconfig === void 0 ? [] : configEdit(context.root, tsconfig, extension);
|
|
2635
|
+
breaks.push({
|
|
2636
|
+
file,
|
|
2637
|
+
message: `${file} is in no tsconfig that a typecheck script examines.`,
|
|
2638
|
+
edits
|
|
2639
|
+
});
|
|
2640
|
+
}
|
|
2441
2641
|
return breaks;
|
|
2442
2642
|
}
|
|
2443
2643
|
};
|
|
@@ -2629,8 +2829,8 @@ const RULES = [
|
|
|
2629
2829
|
];
|
|
2630
2830
|
// src/run.ts
|
|
2631
2831
|
/**
|
|
2632
|
-
* The commands `check` and `--list` of the bin `conventions`, as functions. `runChecks` starts the
|
|
2633
|
-
* rules, and `runCli` reads the arguments and writes the result.
|
|
2832
|
+
* The commands `check`, `fix` and `--list` of the bin `conventions`, as functions. `runChecks` starts the
|
|
2833
|
+
* rules, `runFix` writes the edits of the breaks, and `runCli` reads the arguments and writes the result.
|
|
2634
2834
|
*/
|
|
2635
2835
|
/** This function reads a `package.json` file. */
|
|
2636
2836
|
function readPackage(root, dir) {
|
|
@@ -2746,6 +2946,51 @@ function runChecks(options) {
|
|
|
2746
2946
|
failed: results.some((result) => result.status === "break" || result.status === "error")
|
|
2747
2947
|
};
|
|
2748
2948
|
}
|
|
2949
|
+
/** The most checks of `runFix`. Each check writes at most one edit of each file. */
|
|
2950
|
+
const MAX_PASSES = 20;
|
|
2951
|
+
/**
|
|
2952
|
+
* This function writes the edits of the breaks, and checks again, until no edit applies. In each pass it writes the first
|
|
2953
|
+
* edit of each file whose `before` is the current text of the file. The other edits of that file wait for the next check,
|
|
2954
|
+
* which gives them again from the new text. Thus a fix does not write over a change that it did not see.
|
|
2955
|
+
*/
|
|
2956
|
+
function runFix(options) {
|
|
2957
|
+
const root = resolve(options.root);
|
|
2958
|
+
const changed = [];
|
|
2959
|
+
let result = runChecks(options);
|
|
2960
|
+
let passes = 1;
|
|
2961
|
+
while (passes < MAX_PASSES) {
|
|
2962
|
+
const written = /* @__PURE__ */ new Set();
|
|
2963
|
+
for (const rule of result.rules) for (const edit of rule.breaks.flatMap((item) => item.edits ?? [])) {
|
|
2964
|
+
if (written.has(edit.file)) continue;
|
|
2965
|
+
const path = join(root, edit.file);
|
|
2966
|
+
if (!existsSync(path) || readFileSync(path, "utf8") !== edit.before) continue;
|
|
2967
|
+
writeFileSync(path, edit.after);
|
|
2968
|
+
written.add(edit.file);
|
|
2969
|
+
if (!changed.includes(edit.file)) changed.push(edit.file);
|
|
2970
|
+
}
|
|
2971
|
+
if (written.size === 0) break;
|
|
2972
|
+
result = runChecks(options);
|
|
2973
|
+
passes += 1;
|
|
2974
|
+
}
|
|
2975
|
+
return {
|
|
2976
|
+
changed,
|
|
2977
|
+
passes,
|
|
2978
|
+
result
|
|
2979
|
+
};
|
|
2980
|
+
}
|
|
2981
|
+
/** This function gives a result for JSON: each break without the texts of its edits, and with `fixable`. */
|
|
2982
|
+
function withoutEdits(result) {
|
|
2983
|
+
return {
|
|
2984
|
+
...result,
|
|
2985
|
+
rules: result.rules.map((rule) => ({
|
|
2986
|
+
...rule,
|
|
2987
|
+
breaks: rule.breaks.map(({ edits, ...item }) => ({
|
|
2988
|
+
...item,
|
|
2989
|
+
fixable: (edits ?? []).length > 0
|
|
2990
|
+
}))
|
|
2991
|
+
}))
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2749
2994
|
/** The most breaks of one rule in the text output. The JSON output has each break. */
|
|
2750
2995
|
const MAX_SHOWN = 50;
|
|
2751
2996
|
/** This function gives the place of a break as `file:line`. */
|
|
@@ -2766,7 +3011,7 @@ function formatResult(result) {
|
|
|
2766
3011
|
const extra = rule.ignored > 0 ? ` (${rule.ignored} ignored: ${rule.note ?? ""})` : rule.note !== void 0 && rule.status !== "pass" ? ` (${rule.note})` : "";
|
|
2767
3012
|
out.push(`${label[rule.status]} ${rule.id}${rule.breaks.length > 0 ? `: ${rule.breaks.length} break(s)` : ""}${extra}`);
|
|
2768
3013
|
for (const item of rule.breaks.slice(0, MAX_SHOWN)) {
|
|
2769
|
-
out.push(` ${placeOf(item)} ${item.message}`);
|
|
3014
|
+
out.push(` ${placeOf(item)} ${item.message}${(item.edits ?? []).length > 0 ? " (conventions fix writes the fix)" : ""}`);
|
|
2770
3015
|
if (item.fix !== void 0) out.push(` fix: ${item.fix}`);
|
|
2771
3016
|
}
|
|
2772
3017
|
if (rule.breaks.length > MAX_SHOWN) out.push(` ... ${rule.breaks.length - MAX_SHOWN} more. Start \`conventions check --json\` for each break.`);
|
|
@@ -2775,8 +3020,8 @@ function formatResult(result) {
|
|
|
2775
3020
|
if (result.failed) out.push("", "To turn a rule off or to keep an exception, add a reason in the field conventions of package.json (`conventions --list`).");
|
|
2776
3021
|
return `${out.join("\n")}\n`;
|
|
2777
3022
|
}
|
|
2778
|
-
/** This function escapes the text of a cell of a Markdown table
|
|
2779
|
-
const cell = (text) => text.replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
|
|
3023
|
+
/** This function escapes the text of a cell of a Markdown table: first each backslash, then each `|`. */
|
|
3024
|
+
const cell = (text) => text.replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
|
|
2780
3025
|
/** This function gives the result as Markdown for the summary of a GitHub Actions run. */
|
|
2781
3026
|
function summaryMarkdown(result) {
|
|
2782
3027
|
const out = [
|
|
@@ -2830,9 +3075,11 @@ function listRules(config, rules = RULES) {
|
|
|
2830
3075
|
}
|
|
2831
3076
|
/** The help text of the bin. */
|
|
2832
3077
|
const USAGE = `Usage: conventions check [options]
|
|
3078
|
+
conventions fix [options]
|
|
2833
3079
|
conventions --list
|
|
2834
3080
|
|
|
2835
3081
|
check Start each rule. Exit code 1 when a rule has a break.
|
|
3082
|
+
fix Write the fixes of the breaks that have one, and check again. Exit code 1 when a break stays.
|
|
2836
3083
|
--list Show each rule: why, fix, options, state and the audit rows that it comes from.
|
|
2837
3084
|
|
|
2838
3085
|
--root <folder> The root of the repository. The default is the current folder.
|
|
@@ -2853,6 +3100,7 @@ var UsageError = class extends Error {};
|
|
|
2853
3100
|
/** This function starts the bin with the arguments `args`, and gives its exit code. */
|
|
2854
3101
|
function runCli(args, io) {
|
|
2855
3102
|
let check = false;
|
|
3103
|
+
let fix = false;
|
|
2856
3104
|
let list = false;
|
|
2857
3105
|
let json = false;
|
|
2858
3106
|
let root = io.cwd;
|
|
@@ -2867,6 +3115,7 @@ function runCli(args, io) {
|
|
|
2867
3115
|
return next;
|
|
2868
3116
|
};
|
|
2869
3117
|
if (arg === "check") check = true;
|
|
3118
|
+
else if (arg === "fix") fix = true;
|
|
2870
3119
|
else if (arg === "--list" || arg === "list") list = true;
|
|
2871
3120
|
else if (arg === "--json") json = true;
|
|
2872
3121
|
else if (arg === "--root") root = resolve(io.cwd, value());
|
|
@@ -2877,7 +3126,7 @@ function runCli(args, io) {
|
|
|
2877
3126
|
return 0;
|
|
2878
3127
|
} else throw new UsageError(`An unknown argument: ${arg}`);
|
|
2879
3128
|
}
|
|
2880
|
-
if (!check && !list) throw new UsageError("Give the command check, or --list.");
|
|
3129
|
+
if (!check && !fix && !list) throw new UsageError("Give the command check or fix, or --list.");
|
|
2881
3130
|
} catch (error) {
|
|
2882
3131
|
if (!(error instanceof UsageError)) throw error;
|
|
2883
3132
|
io.stderr(`conventions: ${error.message}\n\n${USAGE}`);
|
|
@@ -2895,18 +3144,32 @@ function runCli(args, io) {
|
|
|
2895
3144
|
return 0;
|
|
2896
3145
|
}
|
|
2897
3146
|
let result;
|
|
3147
|
+
let fixed;
|
|
2898
3148
|
try {
|
|
2899
|
-
|
|
3149
|
+
const options = {
|
|
2900
3150
|
root,
|
|
2901
3151
|
only,
|
|
2902
3152
|
env: io.env,
|
|
2903
3153
|
...base === void 0 ? {} : { base }
|
|
2904
|
-
}
|
|
3154
|
+
};
|
|
3155
|
+
if (fix) {
|
|
3156
|
+
fixed = runFix(options);
|
|
3157
|
+
result = fixed.result;
|
|
3158
|
+
} else result = runChecks(options);
|
|
2905
3159
|
} catch (error) {
|
|
2906
3160
|
io.stderr(`conventions: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
2907
3161
|
return 1;
|
|
2908
3162
|
}
|
|
2909
|
-
if (json)
|
|
3163
|
+
if (fixed !== void 0 && !json) {
|
|
3164
|
+
const files = fixed.changed.map((file) => ` ${file}\n`).join("");
|
|
3165
|
+
io.stdout(fixed.changed.length === 0 ? "conventions fix: no break has a fix that applies.\n" : `conventions fix: changed ${fixed.changed.length} file(s) in ${fixed.passes} check(s):\n${files}\n`);
|
|
3166
|
+
}
|
|
3167
|
+
const shown = fixed === void 0 ? withoutEdits(result) : {
|
|
3168
|
+
changed: fixed.changed,
|
|
3169
|
+
passes: fixed.passes,
|
|
3170
|
+
...withoutEdits(result)
|
|
3171
|
+
};
|
|
3172
|
+
if (json) io.stdout(`${JSON.stringify(shown, null, 2)}\n`);
|
|
2910
3173
|
else {
|
|
2911
3174
|
if (io.env["GITHUB_ACTIONS"] === "true") io.stdout(annotations(result));
|
|
2912
3175
|
io.stdout(formatResult(result));
|
|
@@ -2915,6 +3178,6 @@ function runCli(args, io) {
|
|
|
2915
3178
|
if (summary) appendFileSync(summary, summaryMarkdown(result));
|
|
2916
3179
|
return result.failed ? 1 : 0;
|
|
2917
3180
|
}
|
|
2918
|
-
export {
|
|
3181
|
+
export { globToRegExp as $, isGitSpecifier as A, cleanPath as B, repositoryOfRemote as C, roadmapParses as D, roadmapOfLine as E, noLocalPaths as F, lineOf as G, hasFile as H, noBuildOutput as I, readJsonc as J, matchesAny as K, STRICT_OPTIONS as L, pinnedGitDeps as M, DEFAULT_ALLOWED as N, requiredScripts as O, HOME_PATTERNS as P, globFolders as Q, cuePreset as R, sourceMain as S, scheduleMinute as T, inFolder as U, codePackages as V, keyLine as W, runGit as X, replaceLine as Y, specifiersOf as Z, testsRequired as _, listRules as a, isSourcePath as b, runCli as c, RULES as d, packageFolders as et, workspaceProtocol as f, typedFiles as g, scriptsOf as h, formatResult as i, isPinned as j, releaseFiles as k, runFix as l, configsOfScript as m, annotations as n, defaultOptions as nt, readConfig as o, typedScripts as p, parseJsonc as q, createContext as r, parseConfig as rt, runChecks as s, USAGE as t, workspaceGlobs as tt, summaryMarkdown as u, hasTests as v, runnerBudget as w, pathsOf as x, tsconfigTest as y, DEPENDENCY_FIELDS as z };
|
|
2919
3182
|
|
|
2920
|
-
//# sourceMappingURL=run-
|
|
3183
|
+
//# sourceMappingURL=run-DD72yQn7.js.map
|