@jfalava/ts-standard 0.3.0 → 0.3.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/package.json +1 -1
- package/src/check.ts +17 -3
- package/src/lint.ts +199 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jfalava/ts-standard",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Checks a Bun repo against the TypeScript scripts and workflows standard: package.json scripts, pins, tsconfig, lint setup and GitHub workflows. Also ships the anti-slop oxlint plugins the standard uses.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/src/check.ts
CHANGED
|
@@ -22,11 +22,20 @@ export interface Result {
|
|
|
22
22
|
readonly excepted: number;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/** Its exceptions name one plugin or rule of one package and match it exactly, not by prefix. */
|
|
26
|
+
const EXACT_RULES: ReadonlySet<string> = new Set(["lint/anti-slop"]);
|
|
27
|
+
|
|
25
28
|
const matches = (e: Exception, f: Finding): boolean =>
|
|
26
29
|
e.rule === f.rule &&
|
|
27
|
-
(
|
|
30
|
+
(EXACT_RULES.has(e.rule)
|
|
31
|
+
? e.where === f.where
|
|
32
|
+
: e.where === undefined || e.where === f.where || f.where.startsWith(`${e.where}:`)) &&
|
|
28
33
|
(e.script === undefined || e.script === f.script);
|
|
29
34
|
|
|
35
|
+
/** An exception for an exact rule that names no plugin or rule would excuse a whole package. */
|
|
36
|
+
const isBroad = (e: Exception): boolean =>
|
|
37
|
+
EXACT_RULES.has(e.rule) && (e.where === undefined || !e.where.includes(":"));
|
|
38
|
+
|
|
30
39
|
/** Runs every rule against the repo at `root`. Reads files only. */
|
|
31
40
|
export const check = (root: string, config: StandardConfig): Result => {
|
|
32
41
|
const repo = loadRepo(root, config);
|
|
@@ -36,7 +45,12 @@ export const check = (root: string, config: StandardConfig): Result => {
|
|
|
36
45
|
...lintRules(repo),
|
|
37
46
|
...workflowRules(repo),
|
|
38
47
|
];
|
|
39
|
-
const
|
|
48
|
+
const broad: Finding[] = (config.exceptions ?? []).filter(isBroad).map((e) => ({
|
|
49
|
+
rule: "standard/broad-exception",
|
|
50
|
+
where: e.where ?? "*",
|
|
51
|
+
message: `exception for \`${e.rule}\` must name a plugin or rule, as \`<package dir>:<plugin>\` or \`<package dir>:<plugin>/<rule>\``,
|
|
52
|
+
}));
|
|
53
|
+
const exceptions = (config.exceptions ?? []).filter((e) => !isBroad(e));
|
|
40
54
|
const used = new Set<Exception>();
|
|
41
55
|
const open = findings.filter((f) => {
|
|
42
56
|
const e = exceptions.find((x) => matches(x, f));
|
|
@@ -55,7 +69,7 @@ export const check = (root: string, config: StandardConfig): Result => {
|
|
|
55
69
|
}));
|
|
56
70
|
return {
|
|
57
71
|
repo: repo.name,
|
|
58
|
-
errors: [...open.filter((f) => severityOf(f.rule) === "error"), ...stale],
|
|
72
|
+
errors: [...open.filter((f) => severityOf(f.rule) === "error"), ...broad, ...stale],
|
|
59
73
|
warnings: open.filter((f) => severityOf(f.rule) === "warn"),
|
|
60
74
|
excepted: findings.length - open.length,
|
|
61
75
|
};
|
package/src/lint.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { join, relative } from "node:path";
|
|
2
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
3
3
|
|
|
4
4
|
import type { Finding } from "./finding.ts";
|
|
5
|
-
import type { Repo } from "./repo.ts";
|
|
5
|
+
import type { Pkg, Repo } from "./repo.ts";
|
|
6
6
|
|
|
7
7
|
const PRESET = "@cloudflare/oxlint-config";
|
|
8
8
|
/** The anti-slop rules the preset does not carry, as this package publishes them. */
|
|
@@ -10,6 +10,202 @@ const ANTI_SLOP = "@jfalava/ts-standard/oxlint";
|
|
|
10
10
|
/** Where repos vendored those rules before this package published them. */
|
|
11
11
|
const VENDORED = "tools/oxlint/anti-slop";
|
|
12
12
|
|
|
13
|
+
interface Plugin {
|
|
14
|
+
/** The plugin name, which prefixes its rule ids. */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly specifier: string;
|
|
17
|
+
/** The built file the specifier resolves to, inside this package. */
|
|
18
|
+
readonly file: string;
|
|
19
|
+
readonly rules: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const GENERIC: Plugin = {
|
|
23
|
+
name: "anti-slop",
|
|
24
|
+
specifier: ANTI_SLOP,
|
|
25
|
+
file: "dist/oxlint/index.js",
|
|
26
|
+
rules: ["no-module-mocking", "require-safety-comment-for-type-assertion"],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Its rules match code shape, such as any `x._tag === "A"`, not imports from `effect`: a package
|
|
30
|
+
// without Effect that loads it gets false positives, and one with Effect that does not loses them.
|
|
31
|
+
const EFFECT: Plugin = {
|
|
32
|
+
name: "anti-slop-effect",
|
|
33
|
+
specifier: `${ANTI_SLOP}/effect`,
|
|
34
|
+
file: "dist/oxlint/effect/index.js",
|
|
35
|
+
rules: [
|
|
36
|
+
"no-manual-effect-error-tag",
|
|
37
|
+
"no-manual-tag-comparison",
|
|
38
|
+
"no-manual-tagged-construction",
|
|
39
|
+
"no-service-constructor-imports",
|
|
40
|
+
"prefer-effect-match",
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const DEP_FIELDS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
|
|
45
|
+
const usesEffect = (pkg: Pkg): boolean => DEP_FIELDS.some((f) => pkg.json[f]?.effect !== undefined);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The oxlint config a package's scripts lint with: the `-c`/`--config` one scripts pass, or else
|
|
49
|
+
* the nearest `oxlint.config.ts` up to the root. Undefined when no script runs oxlint.
|
|
50
|
+
*/
|
|
51
|
+
const lintConfigOf = (repo: Repo, pkg: Pkg): string | undefined => {
|
|
52
|
+
const runs = Object.values(pkg.scripts).filter((s) => /(?:^|[\s;&|(])oxlint(?:\s|$)/.test(s));
|
|
53
|
+
if (runs.length === 0) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
for (const script of runs) {
|
|
57
|
+
const flag = /(?:^|\s)(?:-c|--config)[ =](\S+)/.exec(script);
|
|
58
|
+
if (flag?.[1] !== undefined) {
|
|
59
|
+
return resolve(pkg.abs, flag[1]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (let dir = pkg.abs; ; dir = dirname(dir)) {
|
|
63
|
+
const path = join(dir, "oxlint.config.ts");
|
|
64
|
+
if (existsSync(path)) {
|
|
65
|
+
return path;
|
|
66
|
+
}
|
|
67
|
+
if (dir === repo.root || dir === dirname(dir)) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type JsPlugin = string | { readonly name: string; readonly specifier: string };
|
|
74
|
+
type Level = "allow" | "off" | "warn" | "error" | "deny" | number;
|
|
75
|
+
type Rules = Readonly<Partial<Record<string, Level | readonly [Level, ...unknown[]]>>>;
|
|
76
|
+
interface Resolved {
|
|
77
|
+
readonly jsPlugins?: readonly JsPlugin[];
|
|
78
|
+
readonly rules?: Rules;
|
|
79
|
+
readonly overrides?: readonly { readonly files?: readonly string[]; readonly rules?: Rules }[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const specifierOf = (p: JsPlugin): string =>
|
|
83
|
+
(typeof p === "string" ? p : p.specifier).replace(/^file:\/\//, "");
|
|
84
|
+
/** Whether a resolved specifier is `plugin.file` inside this package, however it was installed. */
|
|
85
|
+
const isFileOf = (spec: string, plugin: Plugin): boolean => {
|
|
86
|
+
for (let dir = dirname(spec); dir !== dirname(dir); dir = dirname(dir)) {
|
|
87
|
+
const manifest = join(dir, "package.json");
|
|
88
|
+
if (existsSync(manifest)) {
|
|
89
|
+
const { name } = JSON.parse(readFileSync(manifest, "utf8")) as { name?: string };
|
|
90
|
+
return name === "@jfalava/ts-standard" && relative(dir, spec) === plugin.file;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
};
|
|
95
|
+
const loads = (config: Resolved, plugin: Plugin): JsPlugin | undefined =>
|
|
96
|
+
(config.jsPlugins ?? []).find((p) => {
|
|
97
|
+
const spec = specifierOf(p);
|
|
98
|
+
return spec === plugin.specifier || (spec.startsWith("/") && isFileOf(spec, plugin));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const isOff = (value: Level | readonly [Level, ...unknown[]]): boolean => {
|
|
102
|
+
const level = typeof value === "object" ? value[0] : value;
|
|
103
|
+
return level === "off" || level === 0;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The repo files a config is built from: the config and every file it imports by relative path,
|
|
108
|
+
* such as a package config that spreads the root one.
|
|
109
|
+
*/
|
|
110
|
+
const sourcesOf = (path: string, seen = new Map<string, string>()): Map<string, string> => {
|
|
111
|
+
if (seen.has(path) || !existsSync(path)) {
|
|
112
|
+
return seen;
|
|
113
|
+
}
|
|
114
|
+
const text = readFileSync(path, "utf8");
|
|
115
|
+
seen.set(path, text);
|
|
116
|
+
for (const [, spec] of text.matchAll(/\bfrom\s+["'](\.{1,2}\/[^"']+)["']/g)) {
|
|
117
|
+
if (spec !== undefined) {
|
|
118
|
+
const target = resolve(dirname(path), spec);
|
|
119
|
+
sourcesOf(existsSync(target) ? target : `${target}.ts`, seen);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return seen;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* A rule kept off on purpose sits in a pending map with its finding count, as
|
|
127
|
+
* `"anti-slop/x": "off", // 4`, in one of the files the package's config is built from.
|
|
128
|
+
*/
|
|
129
|
+
const pendingIn = (texts: readonly string[], rule: string): boolean => {
|
|
130
|
+
const id = rule.replaceAll("/", String.raw`\/`).replaceAll("-", String.raw`\-`);
|
|
131
|
+
const marker = new RegExp(`["']${id}["']\\s*:\\s*["']off["']\\s*,?\\s*//\\s*\\d`);
|
|
132
|
+
return texts.some((text) => marker.test(text));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Loads each linted package's oxlint config, as oxlint would, and reads its `jsPlugins`, `rules`
|
|
137
|
+
* and `overrides`: the anti-slop plugin everywhere, the Effect plugin exactly where the package
|
|
138
|
+
* depends on `effect`, and every rule of a loaded plugin on or pending, overrides included.
|
|
139
|
+
*/
|
|
140
|
+
const pluginRules = (repo: Repo): Finding[] => {
|
|
141
|
+
const out: Finding[] = [];
|
|
142
|
+
const f = (where: string, message: string) =>
|
|
143
|
+
out.push({ rule: "lint/anti-slop", where, message });
|
|
144
|
+
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
145
|
+
const path = lintConfigOf(repo, pkg);
|
|
146
|
+
if (path === undefined) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
let config: Resolved;
|
|
150
|
+
try {
|
|
151
|
+
// oxlint imports the config the same way; it runs the repo's own code.
|
|
152
|
+
const mod = import.meta.require(path);
|
|
153
|
+
config = mod.default ?? mod;
|
|
154
|
+
} catch (error) {
|
|
155
|
+
f(pkg.dir, `could not load \`${relative(repo.root, path)}\`: ${String(error)}`);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const texts = [...sourcesOf(path).values()];
|
|
159
|
+
const effect = usesEffect(pkg);
|
|
160
|
+
for (const plugin of [GENERIC, EFFECT]) {
|
|
161
|
+
const at = `${pkg.dir}:${plugin.name}`;
|
|
162
|
+
const entry = loads(config, plugin);
|
|
163
|
+
if (entry === undefined) {
|
|
164
|
+
if (plugin === GENERIC) {
|
|
165
|
+
f(at, `does not load \`${GENERIC.specifier}\``);
|
|
166
|
+
} else if (effect) {
|
|
167
|
+
f(at, `depends on \`effect\` but does not load \`${EFFECT.specifier}\``);
|
|
168
|
+
}
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (plugin === EFFECT && !effect) {
|
|
172
|
+
f(
|
|
173
|
+
at,
|
|
174
|
+
`loads \`${EFFECT.specifier}\` without depending on \`effect\`; its rules match code shape and report code that is not Effect`,
|
|
175
|
+
);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (typeof entry !== "string" && entry.name !== plugin.name) {
|
|
179
|
+
f(at, `loads \`${plugin.specifier}\` as "${entry.name}"; name it "${plugin.name}"`);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
for (const name of plugin.rules) {
|
|
183
|
+
const rule = `${plugin.name}/${name}`;
|
|
184
|
+
const value = config.rules?.[rule];
|
|
185
|
+
if (value === undefined) {
|
|
186
|
+
f(`${pkg.dir}:${rule}`, "is not configured, so it never runs; turn it on");
|
|
187
|
+
} else if (isOff(value) && !pendingIn(texts, rule)) {
|
|
188
|
+
f(
|
|
189
|
+
`${pkg.dir}:${rule}`,
|
|
190
|
+
`is off with no pending count; turn it on, or list it as \`"${rule}": "off", // <findings>\``,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
// An override that turns a kept rule off for some files is the same as turning it off.
|
|
194
|
+
for (const [i, override] of (config.overrides ?? []).entries()) {
|
|
195
|
+
const local = override.rules?.[rule];
|
|
196
|
+
if (local !== undefined && isOff(local) && !pendingIn(texts, rule)) {
|
|
197
|
+
f(
|
|
198
|
+
`${pkg.dir}:${rule}`,
|
|
199
|
+
`is off in \`overrides[${i}]\` (${(override.files ?? []).join(", ")}) with no pending count; turn it on there, or list it as \`"${rule}": "off", // <findings>\``,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
};
|
|
208
|
+
|
|
13
209
|
export const lintRules = (repo: Repo): Finding[] => {
|
|
14
210
|
const out: Finding[] = [];
|
|
15
211
|
const f = (rule: string, where: string, message: string) => out.push({ rule, where, message });
|
|
@@ -41,9 +237,6 @@ export const lintRules = (repo: Repo): Finding[] => {
|
|
|
41
237
|
"set `categories.correctness`; the preset turns the category off",
|
|
42
238
|
);
|
|
43
239
|
}
|
|
44
|
-
if (!text.includes(`"${ANTI_SLOP}"`) && !text.includes(`'${ANTI_SLOP}'`)) {
|
|
45
|
-
f("lint/anti-slop", "oxlint.config.ts", `does not load the \`${ANTI_SLOP}\` plugin`);
|
|
46
|
-
}
|
|
47
240
|
}
|
|
48
241
|
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
49
242
|
const path = join(pkg.abs, "oxlint.config.ts");
|
|
@@ -63,5 +256,6 @@ export const lintRules = (repo: Repo): Finding[] => {
|
|
|
63
256
|
`a vendored copy; load \`${ANTI_SLOP}\` (and \`${ANTI_SLOP}/effect\`) and delete it`,
|
|
64
257
|
);
|
|
65
258
|
}
|
|
259
|
+
out.push(...pluginRules(repo));
|
|
66
260
|
return out;
|
|
67
261
|
};
|