@jfalava/ts-standard 0.1.0 → 0.2.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/src/scripts.ts CHANGED
@@ -2,7 +2,8 @@ import { join } from "node:path";
2
2
 
3
3
  import type { Finding } from "./finding.ts";
4
4
  import type { Pkg, Repo } from "./repo.ts";
5
- import { asBunRun, type Command, isBareBunTest, isBunx, isWriter, parseScript } from "./shell.ts";
5
+ import { asBunRun, isBareBunTest, isBunx, isWriter, parseScript } from "./shell.ts";
6
+ import type { Command } from "./shell.ts";
6
7
 
7
8
  const VERBS = [
8
9
  "check",
@@ -15,6 +16,7 @@ const VERBS = [
15
16
  "test",
16
17
  ] as const;
17
18
  const DEPLOY = ["plan", "deploy", "ci:deploy", "destroy"] as const;
19
+ const UNUSED_DIRECTIVES = "--report-unused-disable-directives-severity=error";
18
20
  const LEAF_REQUIRED = ["check", "fix", "lint", "lint:fix", "format", "format:check", "typecheck"];
19
21
  const ROOT_REQUIRED = ["check", "fix", "lint", "format", "format:check", "typecheck", "test"];
20
22
 
@@ -37,10 +39,14 @@ const walk = (
37
39
  seen = new Set<string>(),
38
40
  ): void => {
39
41
  const key = `${pkg.dir}#${script}`;
40
- if (seen.has(key)) return;
42
+ if (seen.has(key)) {
43
+ return;
44
+ }
41
45
  seen.add(key);
42
46
  const body = pkg.scripts[script];
43
- if (body === undefined) return;
47
+ if (body === undefined) {
48
+ return;
49
+ }
44
50
  on.script?.(pkg, script);
45
51
  for (const cmd of parseScript(body)) {
46
52
  const run = asBunRun(cmd);
@@ -52,9 +58,13 @@ const walk = (
52
58
  ? repo.workspaces
53
59
  : [run.cwd ? repo.pkgAt(join(pkg.dir, run.cwd)) : pkg];
54
60
  for (const target of targets) {
55
- if (!target) continue;
61
+ if (!target) {
62
+ continue;
63
+ }
56
64
  if (target.scripts[run.script] === undefined) {
57
- if (!run.ifPresent) on.missing?.(target, run.script);
65
+ if (!run.ifPresent) {
66
+ on.missing?.(target, run.script);
67
+ }
58
68
  continue;
59
69
  }
60
70
  walk(repo, target, run.script, on, seen);
@@ -76,13 +86,16 @@ export const scriptRules = (repo: Repo): Finding[] => {
76
86
  // --- names that must exist
77
87
  for (const pkg of leaves(repo)) {
78
88
  for (const name of LEAF_REQUIRED) {
79
- if (pkg.scripts[name] === undefined) f("scripts/required", pkg, name, `missing \`${name}\``);
89
+ if (pkg.scripts[name] === undefined) {
90
+ f("scripts/required", pkg, name, `missing \`${name}\``);
91
+ }
80
92
  }
81
93
  }
82
94
  if (repo.isWorkspace) {
83
95
  for (const name of ROOT_REQUIRED) {
84
- if (repo.rootPkg.scripts[name] === undefined)
96
+ if (repo.rootPkg.scripts[name] === undefined) {
85
97
  f("scripts/required", repo.rootPkg, name, `missing \`${name}\``);
98
+ }
86
99
  }
87
100
  }
88
101
 
@@ -156,6 +169,21 @@ export const scriptRules = (repo: Repo): Finding[] => {
156
169
  `\`${cmd.raw}\` lacks \`--type-aware --type-check\``,
157
170
  );
158
171
  }
172
+ // A workspace config spreads the root's, so it cannot carry the preset's root-only
173
+ // `reportUnusedDisableDirectives`; the flag puts it back.
174
+ if (
175
+ cmd.argv[0] === "oxlint" &&
176
+ repo.isWorkspace &&
177
+ pkg !== repo.rootPkg &&
178
+ !cmd.argv.includes(UNUSED_DIRECTIVES)
179
+ ) {
180
+ f(
181
+ "scripts/lint-unused-directives",
182
+ pkg,
183
+ script,
184
+ `\`${cmd.raw}\` lacks \`${UNUSED_DIRECTIVES}\``,
185
+ );
186
+ }
159
187
  },
160
188
  });
161
189
  }
@@ -181,7 +209,9 @@ export const scriptRules = (repo: Repo): Finding[] => {
181
209
  for (const pkg of repo.isWorkspace ? leaves(repo) : [repo.rootPkg]) {
182
210
  for (const [name, body] of Object.entries(pkg.scripts)) {
183
211
  const fixBody = pkg.scripts[`${name}:fix`];
184
- if (!name.startsWith("lint") || name.endsWith(":fix") || fixBody === undefined) continue;
212
+ if (!name.startsWith("lint") || name.endsWith(":fix") || fixBody === undefined) {
213
+ continue;
214
+ }
185
215
  const a = parseScript(body);
186
216
  const b = parseScript(fixBody);
187
217
  // Each command is the same, or the same plus `--fix`, or `bun run X` → `bun run X:fix`; at least one must fix.
@@ -193,10 +223,14 @@ export const scriptRules = (repo: Repo): Finding[] => {
193
223
  const ra = asBunRun(ca);
194
224
  const rb = asBunRun(cb);
195
225
  if (ra && rb) {
196
- if (rb.script === `${ra.script}:fix`) fixes = true;
226
+ if (rb.script === `${ra.script}:fix`) {
227
+ fixes = true;
228
+ }
197
229
  return rb.script === `${ra.script}:fix` || rb.script === ra.script;
198
230
  }
199
- if (sameArgv(ca.argv, cb.argv)) return true;
231
+ if (sameArgv(ca.argv, cb.argv)) {
232
+ return true;
233
+ }
200
234
  const plusFix =
201
235
  !ca.argv.includes("--fix") &&
202
236
  cb.argv.includes("--fix") &&
@@ -204,17 +238,20 @@ export const scriptRules = (repo: Repo): Finding[] => {
204
238
  cb.argv.filter((x) => x !== "--fix"),
205
239
  ca.argv,
206
240
  );
207
- if (plusFix) fixes = true;
241
+ if (plusFix) {
242
+ fixes = true;
243
+ }
208
244
  return plusFix;
209
245
  }) &&
210
246
  fixes;
211
- if (!ok)
247
+ if (!ok) {
212
248
  f(
213
249
  "scripts/fix-pairs",
214
250
  pkg,
215
251
  `${name}:fix`,
216
252
  `\`${name}:fix\` should be \`${name}\` with \`--fix\`; got \`${fixBody}\``,
217
253
  );
254
+ }
218
255
  }
219
256
  const fmt = pkg.scripts.format;
220
257
  const fmtCheck = pkg.scripts["format:check"];
@@ -226,19 +263,22 @@ export const scriptRules = (repo: Repo): Finding[] => {
226
263
  const a = parseScript(fmt);
227
264
  const b = parseScript(fmtCheck);
228
265
  const toCheck = (argv: readonly string[]) => {
229
- if (argv.includes("--write")) return argv.map((x) => (x === "--write" ? "--check" : x));
266
+ if (argv.includes("--write")) {
267
+ return argv.map((x) => (x === "--write" ? "--check" : x));
268
+ }
230
269
  return [...argv, "--check"];
231
270
  };
232
271
  const ok =
233
272
  a.length === b.length &&
234
273
  a.every((ca, i) => sameArgv([...b[i]!.argv].sort(), [...toCheck(ca.argv)].sort()));
235
- if (!ok)
274
+ if (!ok) {
236
275
  f(
237
276
  "scripts/format-pair",
238
277
  pkg,
239
278
  "format:check",
240
279
  `\`format:check\` should be \`format\` in check mode; got \`${fmtCheck}\` for \`${fmt}\``,
241
280
  );
281
+ }
242
282
  }
243
283
  }
244
284
 
@@ -247,7 +287,9 @@ export const scriptRules = (repo: Repo): Finding[] => {
247
287
  const root = repo.rootPkg;
248
288
  for (const verb of VERBS) {
249
289
  const body = root.scripts[verb];
250
- if (body === undefined) continue;
290
+ if (body === undefined) {
291
+ continue;
292
+ }
251
293
  const cmds = parseScript(body);
252
294
  const fanIdx = cmds.findIndex((c) => {
253
295
  const r = asBunRun(c);
@@ -293,16 +335,18 @@ export const scriptRules = (repo: Repo): Finding[] => {
293
335
  const reachedFromCheck = new Map<Pkg, Set<string>>();
294
336
  for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
295
337
  for (const [name, body] of Object.entries(pkg.scripts)) {
296
- if (name.endsWith(":global"))
338
+ if (name.endsWith(":global")) {
297
339
  f(
298
340
  "scripts/no-global-suffix",
299
341
  pkg,
300
342
  name,
301
343
  "`:global` names are replaced by root fan-outs under the same name",
302
344
  );
345
+ }
303
346
  for (const cmd of parseScript(body)) {
304
- if (isBunx(cmd.argv))
347
+ if (isBunx(cmd.argv)) {
305
348
  f("scripts/no-bunx", pkg, name, `\`${cmd.raw}\`: install the tool and call it directly`);
349
+ }
306
350
  if (isBareBunTest(cmd.argv) && name !== "test" && !name.startsWith("test:")) {
307
351
  f(
308
352
  "scripts/no-bare-bun-test",
@@ -312,13 +356,14 @@ export const scriptRules = (repo: Repo): Finding[] => {
312
356
  );
313
357
  }
314
358
  const run = asBunRun(cmd);
315
- if (run?.filter !== undefined)
359
+ if (run?.filter !== undefined) {
316
360
  f(
317
361
  "scripts/no-filter",
318
362
  pkg,
319
363
  name,
320
364
  `\`${cmd.raw}\`: use \`--workspaces\` or \`--cwd\`; \`--filter\` drops stdin`,
321
365
  );
366
+ }
322
367
  }
323
368
  if (
324
369
  name.startsWith("pre") &&
@@ -337,7 +382,9 @@ export const scriptRules = (repo: Repo): Finding[] => {
337
382
  const reached = new Set<string>();
338
383
  walk(repo, pkg, "check", {
339
384
  script: (p, s) => {
340
- if (p === pkg) reached.add(s);
385
+ if (p === pkg) {
386
+ reached.add(s);
387
+ }
341
388
  },
342
389
  leaf: ({ pkg: lp, script, cmd }) => {
343
390
  if (isWriter(cmd.argv)) {
@@ -350,13 +397,14 @@ export const scriptRules = (repo: Repo): Finding[] => {
350
397
  }
351
398
  },
352
399
  missing: (p, s) => {
353
- if (s === "check")
400
+ if (s === "check") {
354
401
  out.push({
355
402
  rule: "scripts/root-fan-out",
356
403
  where: p.dir,
357
404
  script: "check",
358
405
  message: "package has no `check`, so the root fan-out fails",
359
406
  });
407
+ }
360
408
  },
361
409
  });
362
410
  reachedFromCheck.set(pkg, reached);
@@ -409,8 +457,9 @@ export const scriptRules = (repo: Repo): Finding[] => {
409
457
  f("scripts/ci-build", root, "ci:build", "`ci:deploy` exists but `ci:build` does not");
410
458
  }
411
459
  for (const pkg of repo.workspaces) {
412
- if (pkg.scripts["ci:build"] !== undefined)
460
+ if (pkg.scripts["ci:build"] !== undefined) {
413
461
  f("scripts/ci-build", pkg, "ci:build", "`ci:build` belongs to the repo root only");
462
+ }
414
463
  }
415
464
 
416
465
  out.push(...deployRules(repo));
@@ -421,8 +470,11 @@ const dedupe = (out: Finding[]) => {
421
470
  const seen = new Set<string>();
422
471
  for (let i = out.length - 1; i >= 0; i--) {
423
472
  const k = JSON.stringify(out[i]);
424
- if (seen.has(k)) out.splice(i, 1);
425
- else seen.add(k);
473
+ if (seen.has(k)) {
474
+ out.splice(i, 1);
475
+ } else {
476
+ seen.add(k);
477
+ }
426
478
  }
427
479
  };
428
480
 
@@ -447,10 +499,14 @@ const deployRules = (repo: Repo): Finding[] => {
447
499
  const targets = new Map<string, string | undefined>();
448
500
  for (const name of DEPLOY) {
449
501
  const body = pkg.scripts[name];
450
- if (body === undefined) continue;
502
+ if (body === undefined) {
503
+ continue;
504
+ }
451
505
  for (const cmd of parseScript(body)) {
452
506
  const [bin, sub] = cmd.argv;
453
- if (bin !== "alchemy" && bin !== "pulumi") continue;
507
+ if (bin !== "alchemy" && bin !== "pulumi") {
508
+ continue;
509
+ }
454
510
  const target = targetOf(cmd);
455
511
  targets.set(name, target);
456
512
  if (target === undefined) {
@@ -462,13 +518,14 @@ const deployRules = (repo: Repo): Finding[] => {
462
518
  });
463
519
  }
464
520
  const yes = cmd.argv.includes("--yes") || cmd.argv.includes("-y");
465
- if (name === "ci:deploy" && !yes)
521
+ if (name === "ci:deploy" && !yes) {
466
522
  out.push({
467
523
  rule: "deploy/interactive",
468
524
  where: pkg.dir,
469
525
  script: name,
470
526
  message: "`ci:deploy` must pass `--yes`",
471
527
  });
528
+ }
472
529
  if ((name === "deploy" || name === "destroy") && yes) {
473
530
  out.push({
474
531
  rule: "deploy/interactive",
@@ -504,20 +561,25 @@ const deployRules = (repo: Repo): Finding[] => {
504
561
  });
505
562
  }
506
563
  for (const name of ["deploy", "ci:deploy"]) {
507
- if (pkg.scripts[name] === undefined) continue;
564
+ if (pkg.scripts[name] === undefined) {
565
+ continue;
566
+ }
508
567
  const checks: string[] = [];
509
568
  walk(repo, pkg, name, {
510
569
  script: (_p, s) => {
511
- if ((VERBS as readonly string[]).includes(s)) checks.push(s);
570
+ if ((VERBS as readonly string[]).includes(s)) {
571
+ checks.push(s);
572
+ }
512
573
  },
513
574
  leaf: ({ pkg: lp, script, cmd }) => {
514
- if (isWriter(cmd.argv))
575
+ if (isWriter(cmd.argv)) {
515
576
  out.push({
516
577
  rule: "deploy/no-checks",
517
578
  where: lp.dir,
518
579
  script,
519
580
  message: `\`${cmd.raw}\` rewrites tracked files during \`${name}\``,
520
581
  });
582
+ }
521
583
  },
522
584
  });
523
585
  if (checks.length > 0) {
package/src/shell.ts CHANGED
@@ -14,20 +14,27 @@ const tokenize = (body: string): string[][] => {
14
14
  let hasToken = false;
15
15
  let quote: "'" | '"' | null = null;
16
16
  const pushToken = () => {
17
- if (hasToken) current.push(token);
17
+ if (hasToken) {
18
+ current.push(token);
19
+ }
18
20
  token = "";
19
21
  hasToken = false;
20
22
  };
21
23
  const pushCommand = () => {
22
24
  pushToken();
23
- if (current.length > 0) commands.push(current);
25
+ if (current.length > 0) {
26
+ commands.push(current);
27
+ }
24
28
  current = [];
25
29
  };
26
30
  for (let i = 0; i < body.length; i++) {
27
31
  const c = body[i]!;
28
32
  if (quote) {
29
- if (c === quote) quote = null;
30
- else token += c;
33
+ if (c === quote) {
34
+ quote = null;
35
+ } else {
36
+ token += c;
37
+ }
31
38
  continue;
32
39
  }
33
40
  // GitHub expressions such as `${{ matrix.target }}` are one word.
@@ -51,7 +58,9 @@ const tokenize = (body: string): string[][] => {
51
58
  }
52
59
  if (c === "|" || c === ";") {
53
60
  pushCommand();
54
- if (body[i + 1] === "|") i++;
61
+ if (body[i + 1] === "|") {
62
+ i++;
63
+ }
55
64
  continue;
56
65
  }
57
66
  if (c === " " || c === "\t" || c === "\n") {
@@ -71,7 +80,9 @@ export const parseScript = (body: string): Command[] =>
71
80
  let i = 0;
72
81
  for (; i < tokens.length; i++) {
73
82
  const m = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s.exec(tokens[i]!);
74
- if (!m) break;
83
+ if (!m) {
84
+ break;
85
+ }
75
86
  env.set(m[1]!, m[2]!);
76
87
  }
77
88
  const argv = tokens.slice(i);
@@ -90,7 +101,9 @@ export interface BunRun {
90
101
  /** `bun run [flags] <script> [args]`, or undefined for anything else (including `bun run file.ts`). */
91
102
  export const asBunRun = (cmd: Command): BunRun | undefined => {
92
103
  const [bin, sub, ...rest] = cmd.argv;
93
- if (bin !== "bun" || sub !== "run") return undefined;
104
+ if (bin !== "bun" || sub !== "run") {
105
+ return undefined;
106
+ }
94
107
  let workspaces = false;
95
108
  let ifPresent = false;
96
109
  let cwd: string | undefined;
@@ -98,17 +111,28 @@ export const asBunRun = (cmd: Command): BunRun | undefined => {
98
111
  let i = 0;
99
112
  for (; i < rest.length; i++) {
100
113
  const a = rest[i]!;
101
- if (a === "--workspaces") workspaces = true;
102
- else if (a === "--if-present") ifPresent = true;
103
- else if (a === "--sequential" || a === "--silent" || a === "--bun") continue;
104
- else if (a === "--cwd") cwd = rest[++i];
105
- else if (a.startsWith("--cwd=")) cwd = a.slice(6);
106
- else if (a === "--filter" || a === "-F") filter = rest[++i];
107
- else if (a.startsWith("--filter=")) filter = a.slice(9);
108
- else break;
114
+ if (a === "--workspaces") {
115
+ workspaces = true;
116
+ } else if (a === "--if-present") {
117
+ ifPresent = true;
118
+ } else if (a === "--sequential" || a === "--silent" || a === "--bun") {
119
+ continue;
120
+ } else if (a === "--cwd") {
121
+ cwd = rest[++i];
122
+ } else if (a.startsWith("--cwd=")) {
123
+ cwd = a.slice(6);
124
+ } else if (a === "--filter" || a === "-F") {
125
+ filter = rest[++i];
126
+ } else if (a.startsWith("--filter=")) {
127
+ filter = a.slice(9);
128
+ } else {
129
+ break;
130
+ }
109
131
  }
110
132
  const script = rest[i];
111
- if (script === undefined || /\.(m?[jt]s|tsx?)$/.test(script)) return undefined;
133
+ if (script === undefined || /\.(m?[jt]s|tsx?)$/.test(script)) {
134
+ return undefined;
135
+ }
112
136
  return { workspaces, ifPresent, cwd, filter, script, args: rest.slice(i + 1) };
113
137
  };
114
138
 
@@ -116,11 +140,21 @@ export const asBunRun = (cmd: Command): BunRun | undefined => {
116
140
  export const isWriter = (argv: readonly string[]): boolean => {
117
141
  const [bin, sub] = argv;
118
142
  const has = (flag: string) => argv.includes(flag);
119
- if (has("--fix") || has("--write") || has("-w")) return true;
120
- if (bin === "oxfmt") return !has("--check") && !has("--list-different");
121
- if (bin === "vp" && sub === "fmt") return !has("--check");
122
- if (bin === "cargo" && sub === "fmt") return !has("--check");
123
- if (bin === "biome" && (sub === "format" || sub === "check")) return has("--write");
143
+ if (has("--fix") || has("--write") || has("-w")) {
144
+ return true;
145
+ }
146
+ if (bin === "oxfmt") {
147
+ return !has("--check") && !has("--list-different");
148
+ }
149
+ if (bin === "vp" && sub === "fmt") {
150
+ return !has("--check");
151
+ }
152
+ if (bin === "cargo" && sub === "fmt") {
153
+ return !has("--check");
154
+ }
155
+ if (bin === "biome" && (sub === "format" || sub === "check")) {
156
+ return has("--write");
157
+ }
124
158
  return false;
125
159
  };
126
160