@jfalava/ts-standard 0.3.1 → 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 +42 -20
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
|
@@ -72,9 +72,11 @@ const lintConfigOf = (repo: Repo, pkg: Pkg): string | undefined => {
|
|
|
72
72
|
|
|
73
73
|
type JsPlugin = string | { readonly name: string; readonly specifier: string };
|
|
74
74
|
type Level = "allow" | "off" | "warn" | "error" | "deny" | number;
|
|
75
|
+
type Rules = Readonly<Partial<Record<string, Level | readonly [Level, ...unknown[]]>>>;
|
|
75
76
|
interface Resolved {
|
|
76
77
|
readonly jsPlugins?: readonly JsPlugin[];
|
|
77
|
-
readonly rules?:
|
|
78
|
+
readonly rules?: Rules;
|
|
79
|
+
readonly overrides?: readonly { readonly files?: readonly string[]; readonly rules?: Rules }[];
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
const specifierOf = (p: JsPlugin): string =>
|
|
@@ -101,9 +103,28 @@ const isOff = (value: Level | readonly [Level, ...unknown[]]): boolean => {
|
|
|
101
103
|
return level === "off" || level === 0;
|
|
102
104
|
};
|
|
103
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
|
+
|
|
104
125
|
/**
|
|
105
126
|
* 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
|
|
127
|
+
* `"anti-slop/x": "off", // 4`, in one of the files the package's config is built from.
|
|
107
128
|
*/
|
|
108
129
|
const pendingIn = (texts: readonly string[], rule: string): boolean => {
|
|
109
130
|
const id = rule.replaceAll("/", String.raw`\/`).replaceAll("-", String.raw`\-`);
|
|
@@ -112,21 +133,14 @@ const pendingIn = (texts: readonly string[], rule: string): boolean => {
|
|
|
112
133
|
};
|
|
113
134
|
|
|
114
135
|
/**
|
|
115
|
-
* Loads each linted package's oxlint config, as oxlint would, and reads its `jsPlugins`
|
|
116
|
-
*
|
|
117
|
-
*
|
|
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.
|
|
118
139
|
*/
|
|
119
140
|
const pluginRules = (repo: Repo): Finding[] => {
|
|
120
141
|
const out: Finding[] = [];
|
|
121
142
|
const f = (where: string, message: string) =>
|
|
122
143
|
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
144
|
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
131
145
|
const path = lintConfigOf(repo, pkg);
|
|
132
146
|
if (path === undefined) {
|
|
@@ -141,30 +155,28 @@ const pluginRules = (repo: Repo): Finding[] => {
|
|
|
141
155
|
f(pkg.dir, `could not load \`${relative(repo.root, path)}\`: ${String(error)}`);
|
|
142
156
|
continue;
|
|
143
157
|
}
|
|
144
|
-
|
|
145
|
-
configs.add(path);
|
|
146
|
-
texts.push(readFileSync(path, "utf8"));
|
|
147
|
-
}
|
|
158
|
+
const texts = [...sourcesOf(path).values()];
|
|
148
159
|
const effect = usesEffect(pkg);
|
|
149
160
|
for (const plugin of [GENERIC, EFFECT]) {
|
|
161
|
+
const at = `${pkg.dir}:${plugin.name}`;
|
|
150
162
|
const entry = loads(config, plugin);
|
|
151
163
|
if (entry === undefined) {
|
|
152
164
|
if (plugin === GENERIC) {
|
|
153
|
-
f(
|
|
165
|
+
f(at, `does not load \`${GENERIC.specifier}\``);
|
|
154
166
|
} else if (effect) {
|
|
155
|
-
f(
|
|
167
|
+
f(at, `depends on \`effect\` but does not load \`${EFFECT.specifier}\``);
|
|
156
168
|
}
|
|
157
169
|
continue;
|
|
158
170
|
}
|
|
159
171
|
if (plugin === EFFECT && !effect) {
|
|
160
172
|
f(
|
|
161
|
-
|
|
173
|
+
at,
|
|
162
174
|
`loads \`${EFFECT.specifier}\` without depending on \`effect\`; its rules match code shape and report code that is not Effect`,
|
|
163
175
|
);
|
|
164
176
|
continue;
|
|
165
177
|
}
|
|
166
178
|
if (typeof entry !== "string" && entry.name !== plugin.name) {
|
|
167
|
-
f(
|
|
179
|
+
f(at, `loads \`${plugin.specifier}\` as "${entry.name}"; name it "${plugin.name}"`);
|
|
168
180
|
continue;
|
|
169
181
|
}
|
|
170
182
|
for (const name of plugin.rules) {
|
|
@@ -178,6 +190,16 @@ const pluginRules = (repo: Repo): Finding[] => {
|
|
|
178
190
|
`is off with no pending count; turn it on, or list it as \`"${rule}": "off", // <findings>\``,
|
|
179
191
|
);
|
|
180
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
|
+
}
|
|
181
203
|
}
|
|
182
204
|
}
|
|
183
205
|
}
|