@jfalava/ts-standard 0.3.1 → 0.3.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jfalava/ts-standard",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
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", "lint/pending"]);
27
+
25
28
  const matches = (e: Exception, f: Finding): boolean =>
26
29
  e.rule === f.rule &&
27
- (e.where === undefined || e.where === f.where || f.where.startsWith(`${e.where}:`)) &&
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 exceptions = config.exceptions ?? [];
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>:<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?: Readonly<Partial<Record<string, Level | readonly [Level, ...unknown[]]>>>;
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 =>
@@ -96,37 +98,67 @@ const loads = (config: Resolved, plugin: Plugin): JsPlugin | undefined =>
96
98
  return spec === plugin.specifier || (spec.startsWith("/") && isFileOf(spec, plugin));
97
99
  });
98
100
 
99
- const isOff = (value: Level | readonly [Level, ...unknown[]]): boolean => {
100
- const level = typeof value === "object" ? value[0] : value;
101
- return level === "off" || level === 0;
101
+ type Setting = Level | readonly [Level, ...unknown[]];
102
+ const levelOf = (value: Setting): Level => (typeof value === "object" ? value[0] : value);
103
+ const isWarn = (value: Setting): boolean => {
104
+ const level = levelOf(value);
105
+ return level === "warn" || level === 1;
106
+ };
107
+ /** Off, or a warning: a warning fails nothing, so it parks findings the same way off does. */
108
+ const isQuiet = (value: Setting): boolean => {
109
+ const level = levelOf(value);
110
+ return level === "off" || level === 0 || isWarn(value);
111
+ };
112
+ const ruleId = (rule: string): string =>
113
+ rule.replaceAll("/", String.raw`\/`).replaceAll("-", String.raw`\-`);
114
+ const QUIET = `\\[?\\s*(?:["'](?:off|warn)["']|[01]\\b)`;
115
+
116
+ /**
117
+ * The repo files a config is built from: the config and every file it imports by relative path,
118
+ * such as a package config that spreads the root one.
119
+ */
120
+ const sourcesOf = (path: string, seen = new Map<string, string>()): Map<string, string> => {
121
+ if (seen.has(path) || !existsSync(path)) {
122
+ return seen;
123
+ }
124
+ const text = readFileSync(path, "utf8");
125
+ seen.set(path, text);
126
+ for (const [, spec] of text.matchAll(/\bfrom\s+["'](\.{1,2}\/[^"']+)["']/g)) {
127
+ if (spec !== undefined) {
128
+ const target = resolve(dirname(path), spec);
129
+ sourcesOf(existsSync(target) ? target : `${target}.ts`, seen);
130
+ }
131
+ }
132
+ return seen;
102
133
  };
103
134
 
104
135
  /**
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.
136
+ * A rule kept off or at `warn` on purpose sits in a pending map with its finding count, as
137
+ * `"anti-slop/x": "off", // 4` or `"x": "warn", // 4`, in one of the files the package's config
138
+ * is built from.
107
139
  */
108
140
  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`);
141
+ const marker = new RegExp(`["']${ruleId(rule)}["']\\s*:\\s*${QUIET}[^\\n]*?//\\s*\\d`);
142
+ return texts.some((text) => marker.test(text));
143
+ };
144
+
145
+ /** Whether the repo's own config files set the rule to `warn`, rather than a preset. */
146
+ const warnedIn = (texts: readonly string[], rule: string): boolean => {
147
+ const marker = new RegExp(`["']${ruleId(rule)}["']\\s*:\\s*\\[?\\s*(?:["']warn["']|1\\b)`);
111
148
  return texts.some((text) => marker.test(text));
112
149
  };
113
150
 
114
151
  /**
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.
152
+ * Loads each linted package's oxlint config, as oxlint would, and reads its `jsPlugins`, `rules`
153
+ * and `overrides`: the anti-slop plugin everywhere, the Effect plugin exactly where the package
154
+ * depends on `effect`, and every rule of a loaded plugin on or pending, overrides included.
118
155
  */
119
156
  const pluginRules = (repo: Repo): Finding[] => {
120
157
  const out: Finding[] = [];
121
158
  const f = (where: string, message: string) =>
122
159
  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"));
160
+ const pending = (where: string, message: string) =>
161
+ out.push({ rule: "lint/pending", where, message });
130
162
  for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
131
163
  const path = lintConfigOf(repo, pkg);
132
164
  if (path === undefined) {
@@ -141,30 +173,53 @@ const pluginRules = (repo: Repo): Finding[] => {
141
173
  f(pkg.dir, `could not load \`${relative(repo.root, path)}\`: ${String(error)}`);
142
174
  continue;
143
175
  }
144
- if (!configs.has(path)) {
145
- configs.add(path);
146
- texts.push(readFileSync(path, "utf8"));
176
+ const texts = [...sourcesOf(path).values()];
177
+ // A rule the repo sets to `warn` is pending work that passes the gate: it carries its count.
178
+ // Warnings a preset sets, such as @effect/tsgo's diagnostics, are the preset's policy.
179
+ const scopes = [
180
+ { label: "", rules: config.rules },
181
+ ...(config.overrides ?? []).map((o, i) => ({
182
+ label: ` in \`overrides[${i}]\` (${(o.files ?? []).join(", ")})`,
183
+ rules: o.rules,
184
+ })),
185
+ ];
186
+ for (const { label, rules } of scopes) {
187
+ for (const [rule, value] of Object.entries(rules ?? {})) {
188
+ if (
189
+ value !== undefined &&
190
+ !rule.startsWith("anti-slop") &&
191
+ isWarn(value) &&
192
+ warnedIn(texts, rule) &&
193
+ !pendingIn(texts, rule)
194
+ ) {
195
+ pending(
196
+ `${pkg.dir}:${rule}`,
197
+ `is \`warn\`${label} with no pending count; turn it on, or list it as \`"${rule}": "warn", // <findings>\``,
198
+ );
199
+ }
200
+ }
147
201
  }
148
202
  const effect = usesEffect(pkg);
149
203
  for (const plugin of [GENERIC, EFFECT]) {
204
+ const at = `${pkg.dir}:${plugin.name}`;
150
205
  const entry = loads(config, plugin);
151
206
  if (entry === undefined) {
152
207
  if (plugin === GENERIC) {
153
- f(pkg.dir, `does not load \`${GENERIC.specifier}\``);
208
+ f(at, `does not load \`${GENERIC.specifier}\``);
154
209
  } else if (effect) {
155
- f(pkg.dir, `depends on \`effect\` but does not load \`${EFFECT.specifier}\``);
210
+ f(at, `depends on \`effect\` but does not load \`${EFFECT.specifier}\``);
156
211
  }
157
212
  continue;
158
213
  }
159
214
  if (plugin === EFFECT && !effect) {
160
215
  f(
161
- pkg.dir,
216
+ at,
162
217
  `loads \`${EFFECT.specifier}\` without depending on \`effect\`; its rules match code shape and report code that is not Effect`,
163
218
  );
164
219
  continue;
165
220
  }
166
221
  if (typeof entry !== "string" && entry.name !== plugin.name) {
167
- f(pkg.dir, `loads \`${plugin.specifier}\` as "${entry.name}"; name it "${plugin.name}"`);
222
+ f(at, `loads \`${plugin.specifier}\` as "${entry.name}"; name it "${plugin.name}"`);
168
223
  continue;
169
224
  }
170
225
  for (const name of plugin.rules) {
@@ -172,12 +227,22 @@ const pluginRules = (repo: Repo): Finding[] => {
172
227
  const value = config.rules?.[rule];
173
228
  if (value === undefined) {
174
229
  f(`${pkg.dir}:${rule}`, "is not configured, so it never runs; turn it on");
175
- } else if (isOff(value) && !pendingIn(texts, rule)) {
230
+ } else if (isQuiet(value) && !pendingIn(texts, rule)) {
176
231
  f(
177
232
  `${pkg.dir}:${rule}`,
178
- `is off with no pending count; turn it on, or list it as \`"${rule}": "off", // <findings>\``,
233
+ `is \`${String(levelOf(value))}\` with no pending count; turn it on, or list it as \`"${rule}": "off", // <findings>\``,
179
234
  );
180
235
  }
236
+ // An override that quiets a kept rule for some files is the same as quieting it.
237
+ for (const [i, override] of (config.overrides ?? []).entries()) {
238
+ const local = override.rules?.[rule];
239
+ if (local !== undefined && isQuiet(local) && !pendingIn(texts, rule)) {
240
+ f(
241
+ `${pkg.dir}:${rule}`,
242
+ `is \`${String(levelOf(local))}\` in \`overrides[${i}]\` (${(override.files ?? []).join(", ")}) with no pending count; turn it on there, or list it as \`"${rule}": "off", // <findings>\``,
243
+ );
244
+ }
245
+ }
181
246
  }
182
247
  }
183
248
  }
package/src/repo.ts CHANGED
@@ -42,7 +42,11 @@ const expandWorkspaceGlobs = (root: string, patterns: readonly string[]): string
42
42
  for (const pattern of patterns) {
43
43
  if (pattern.endsWith("/*")) {
44
44
  const parent = pattern.slice(0, -2);
45
- for (const entry of readdirSync(join(root, parent), { withFileTypes: true })) {
45
+ // Sorted: readdir order differs between filesystems, and findings follow this order.
46
+ const entries = readdirSync(join(root, parent), { withFileTypes: true }).toSorted((a, b) =>
47
+ a.name.localeCompare(b.name),
48
+ );
49
+ for (const entry of entries) {
46
50
  if (entry.isDirectory() && existsSync(join(root, parent, entry.name, "package.json"))) {
47
51
  dirs.push(`${parent}/${entry.name}`);
48
52
  }