@jfalava/ts-standard 0.3.0 → 0.3.1
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/lint.ts +177 -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.1",
|
|
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/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,180 @@ 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
|
+
interface Resolved {
|
|
76
|
+
readonly jsPlugins?: readonly JsPlugin[];
|
|
77
|
+
readonly rules?: Readonly<Partial<Record<string, Level | readonly [Level, ...unknown[]]>>>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const specifierOf = (p: JsPlugin): string =>
|
|
81
|
+
(typeof p === "string" ? p : p.specifier).replace(/^file:\/\//, "");
|
|
82
|
+
/** Whether a resolved specifier is `plugin.file` inside this package, however it was installed. */
|
|
83
|
+
const isFileOf = (spec: string, plugin: Plugin): boolean => {
|
|
84
|
+
for (let dir = dirname(spec); dir !== dirname(dir); dir = dirname(dir)) {
|
|
85
|
+
const manifest = join(dir, "package.json");
|
|
86
|
+
if (existsSync(manifest)) {
|
|
87
|
+
const { name } = JSON.parse(readFileSync(manifest, "utf8")) as { name?: string };
|
|
88
|
+
return name === "@jfalava/ts-standard" && relative(dir, spec) === plugin.file;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
};
|
|
93
|
+
const loads = (config: Resolved, plugin: Plugin): JsPlugin | undefined =>
|
|
94
|
+
(config.jsPlugins ?? []).find((p) => {
|
|
95
|
+
const spec = specifierOf(p);
|
|
96
|
+
return spec === plugin.specifier || (spec.startsWith("/") && isFileOf(spec, plugin));
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const isOff = (value: Level | readonly [Level, ...unknown[]]): boolean => {
|
|
100
|
+
const level = typeof value === "object" ? value[0] : value;
|
|
101
|
+
return level === "off" || level === 0;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A rule kept off on purpose sits in a pending map with its finding count, as
|
|
106
|
+
* `"anti-slop/x": "off", // 4`, in one of the repo's oxlint configs.
|
|
107
|
+
*/
|
|
108
|
+
const pendingIn = (texts: readonly string[], rule: string): boolean => {
|
|
109
|
+
const id = rule.replaceAll("/", String.raw`\/`).replaceAll("-", String.raw`\-`);
|
|
110
|
+
const marker = new RegExp(`["']${id}["']\\s*:\\s*["']off["']\\s*,?\\s*//\\s*\\d`);
|
|
111
|
+
return texts.some((text) => marker.test(text));
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Loads each linted package's oxlint config, as oxlint would, and reads its `jsPlugins` and
|
|
116
|
+
* top-level `rules`: the anti-slop plugin everywhere, the Effect plugin exactly where the
|
|
117
|
+
* package depends on `effect`, and every rule of a loaded plugin on or pending.
|
|
118
|
+
*/
|
|
119
|
+
const pluginRules = (repo: Repo): Finding[] => {
|
|
120
|
+
const out: Finding[] = [];
|
|
121
|
+
const f = (where: string, message: string) =>
|
|
122
|
+
out.push({ rule: "lint/anti-slop", where, message });
|
|
123
|
+
const configs = new Set(
|
|
124
|
+
[repo.rootPkg, ...repo.workspaces].flatMap((pkg) => {
|
|
125
|
+
const path = join(pkg.abs, "oxlint.config.ts");
|
|
126
|
+
return existsSync(path) ? [path] : [];
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
const texts = [...configs].map((path) => readFileSync(path, "utf8"));
|
|
130
|
+
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
131
|
+
const path = lintConfigOf(repo, pkg);
|
|
132
|
+
if (path === undefined) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
let config: Resolved;
|
|
136
|
+
try {
|
|
137
|
+
// oxlint imports the config the same way; it runs the repo's own code.
|
|
138
|
+
const mod = import.meta.require(path);
|
|
139
|
+
config = mod.default ?? mod;
|
|
140
|
+
} catch (error) {
|
|
141
|
+
f(pkg.dir, `could not load \`${relative(repo.root, path)}\`: ${String(error)}`);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (!configs.has(path)) {
|
|
145
|
+
configs.add(path);
|
|
146
|
+
texts.push(readFileSync(path, "utf8"));
|
|
147
|
+
}
|
|
148
|
+
const effect = usesEffect(pkg);
|
|
149
|
+
for (const plugin of [GENERIC, EFFECT]) {
|
|
150
|
+
const entry = loads(config, plugin);
|
|
151
|
+
if (entry === undefined) {
|
|
152
|
+
if (plugin === GENERIC) {
|
|
153
|
+
f(pkg.dir, `does not load \`${GENERIC.specifier}\``);
|
|
154
|
+
} else if (effect) {
|
|
155
|
+
f(pkg.dir, `depends on \`effect\` but does not load \`${EFFECT.specifier}\``);
|
|
156
|
+
}
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (plugin === EFFECT && !effect) {
|
|
160
|
+
f(
|
|
161
|
+
pkg.dir,
|
|
162
|
+
`loads \`${EFFECT.specifier}\` without depending on \`effect\`; its rules match code shape and report code that is not Effect`,
|
|
163
|
+
);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (typeof entry !== "string" && entry.name !== plugin.name) {
|
|
167
|
+
f(pkg.dir, `loads \`${plugin.specifier}\` as "${entry.name}"; name it "${plugin.name}"`);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
for (const name of plugin.rules) {
|
|
171
|
+
const rule = `${plugin.name}/${name}`;
|
|
172
|
+
const value = config.rules?.[rule];
|
|
173
|
+
if (value === undefined) {
|
|
174
|
+
f(`${pkg.dir}:${rule}`, "is not configured, so it never runs; turn it on");
|
|
175
|
+
} else if (isOff(value) && !pendingIn(texts, rule)) {
|
|
176
|
+
f(
|
|
177
|
+
`${pkg.dir}:${rule}`,
|
|
178
|
+
`is off with no pending count; turn it on, or list it as \`"${rule}": "off", // <findings>\``,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
};
|
|
186
|
+
|
|
13
187
|
export const lintRules = (repo: Repo): Finding[] => {
|
|
14
188
|
const out: Finding[] = [];
|
|
15
189
|
const f = (rule: string, where: string, message: string) => out.push({ rule, where, message });
|
|
@@ -41,9 +215,6 @@ export const lintRules = (repo: Repo): Finding[] => {
|
|
|
41
215
|
"set `categories.correctness`; the preset turns the category off",
|
|
42
216
|
);
|
|
43
217
|
}
|
|
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
218
|
}
|
|
48
219
|
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
49
220
|
const path = join(pkg.abs, "oxlint.config.ts");
|
|
@@ -63,5 +234,6 @@ export const lintRules = (repo: Repo): Finding[] => {
|
|
|
63
234
|
`a vendored copy; load \`${ANTI_SLOP}\` (and \`${ANTI_SLOP}/effect\`) and delete it`,
|
|
64
235
|
);
|
|
65
236
|
}
|
|
237
|
+
out.push(...pluginRules(repo));
|
|
66
238
|
return out;
|
|
67
239
|
};
|