@stateward/core 1.2.1 → 1.3.0

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.
Files changed (75) hide show
  1. package/dist/analyses.d.ts +37 -0
  2. package/dist/analyses.js +57 -0
  3. package/dist/backend/program-record.d.ts +7 -0
  4. package/dist/backend/typescript6.d.ts +35 -32
  5. package/dist/backend/typescript6.js +3804 -163
  6. package/dist/baseline.d.ts +1 -1
  7. package/dist/baseline.js +14 -10
  8. package/dist/cache.d.ts +89 -5
  9. package/dist/cache.js +271 -40
  10. package/dist/checker.d.ts +11 -1
  11. package/dist/checker.js +32 -2
  12. package/dist/class-id.d.ts +34 -0
  13. package/dist/class-id.js +93 -0
  14. package/dist/code-text.d.ts +8 -0
  15. package/dist/code-text.js +66 -0
  16. package/dist/config-file.js +7 -5
  17. package/dist/config.d.ts +10 -2
  18. package/dist/config.js +541 -49
  19. package/dist/define.d.ts +8 -2
  20. package/dist/disk-cache.d.ts +2 -0
  21. package/dist/disk-cache.js +77 -11
  22. package/dist/engine.d.ts +51 -1
  23. package/dist/engine.js +517 -42
  24. package/dist/fact-lines.d.ts +31 -0
  25. package/dist/fact-lines.js +197 -0
  26. package/dist/facts-versions.d.ts +27 -0
  27. package/dist/facts-versions.js +99 -0
  28. package/dist/fixes.js +9 -4
  29. package/dist/guarantees.d.ts +63 -11
  30. package/dist/guarantees.js +151 -21
  31. package/dist/ignores.d.ts +11 -0
  32. package/dist/ignores.js +74 -27
  33. package/dist/implementation.d.ts +0 -6
  34. package/dist/implementation.js +0 -17
  35. package/dist/index.d.ts +10 -9
  36. package/dist/index.js +14 -7
  37. package/dist/internal.d.ts +9 -5
  38. package/dist/internal.js +6 -3
  39. package/dist/lazy.d.ts +8 -1
  40. package/dist/lazy.js +27 -2
  41. package/dist/ledger.d.ts +77 -0
  42. package/dist/ledger.js +423 -0
  43. package/dist/off-by-default.d.ts +13 -0
  44. package/dist/off-by-default.js +15 -0
  45. package/dist/options.d.ts +21 -5
  46. package/dist/options.js +22 -7
  47. package/dist/package-facts.d.ts +211 -17
  48. package/dist/package-facts.js +520 -38
  49. package/dist/plan.d.ts +1 -1
  50. package/dist/plan.js +7 -2
  51. package/dist/release.d.ts +12 -21
  52. package/dist/release.js +28 -38
  53. package/dist/renamed.d.ts +22 -3
  54. package/dist/renamed.js +50 -16
  55. package/dist/rollout.js +7 -4
  56. package/dist/safe-calls.d.ts +26 -0
  57. package/dist/safe-calls.js +345 -0
  58. package/dist/severity.d.ts +1 -1
  59. package/dist/severity.js +17 -3
  60. package/dist/stated.d.ts +37 -0
  61. package/dist/stated.js +123 -0
  62. package/dist/test-runner.d.ts +18 -0
  63. package/dist/test-runner.js +483 -0
  64. package/dist/testing-tsc.d.ts +16 -0
  65. package/dist/testing-tsc.js +86 -0
  66. package/dist/testing.d.ts +5 -4
  67. package/dist/testing.js +6 -6
  68. package/dist/timing.d.ts +15 -0
  69. package/dist/timing.js +41 -2
  70. package/dist/types.d.ts +1068 -16
  71. package/dist/unread.d.ts +90 -0
  72. package/dist/unread.js +405 -0
  73. package/dist/vocabulary.d.ts +1 -1
  74. package/dist/vocabulary.js +1 -1
  75. package/package.json +4 -2
package/dist/config.js CHANGED
@@ -1,19 +1,30 @@
1
1
  import path from "node:path";
2
- import { movedNotice, renameNotice, renamedRule } from "./renamed.js";
2
+ import semver from "semver";
3
+ import { ANALYSIS_NAME, readTarget, readsProblem, unpublishedRead } from "./analyses.js";
4
+ import { PLACEHOLDER_CLASS, WILDCARD, factGrainOf } from "./package-facts.js";
5
+ import { movedNotice, removedNotice, removedOf, renameNotice, renamedRule, renamesOf, } from "./renamed.js";
3
6
  import { addonNames, groupIds, ruleIds } from "./severity.js";
4
7
  import { PRESETS, RULE_TIERS, orList } from "./vocabulary.js";
5
8
  import { defaultOptions, optionProblem, parseRuleOptions } from "./options.js";
9
+ import { offByDefaultNotice } from "./off-by-default.js";
6
10
  const DEFAULT_TSCONFIG = "tsconfig.json";
7
11
  /**
8
12
  * What stateward checks when the config does not say: import it to add to the lists instead of
9
- * replacing them, for example `exclude: [...defaults.exclude, "src/generated/**"]`.
13
+ * replacing them, for example `exclude: [...defaults.exclude, "src/generated/**"]`. The packs the
14
+ * config loads add the test patterns of their library to `exclude` (`Plugin.defaultExclude`),
15
+ * unless the config's own list leaves out one of these.
10
16
  */
11
17
  export const defaults = Object.freeze({
12
18
  include: Object.freeze(["**/*.ts", "**/*.tsx"]),
13
19
  exclude: Object.freeze([
14
20
  "**/*.test.*",
15
21
  "**/*.spec.*",
22
+ // End-to-end and integration tests (Playwright, Cypress, Jest, NestJS): `*.e2e-spec.ts`, an
23
+ // `e2e/` folder, Twenty's `*.integration-spec.ts`.
24
+ "**/*.e2e-spec.*",
25
+ "**/*.integration-spec.*",
16
26
  "**/__tests__/**",
27
+ "**/e2e/**",
17
28
  "**/__scenarios__/**",
18
29
  "**/__fixtures__/**",
19
30
  "**/__mocks__/**",
@@ -23,13 +34,17 @@ export const defaults = Object.freeze({
23
34
  "**/node_modules/**",
24
35
  ]),
25
36
  });
37
+ /** The start of the key of a rule whose tier is `convention`: its id is `<pack>/convention/<rule>`. */
38
+ const CONVENTION_PREFIX = "convention/";
26
39
  const CONFIG_KEYS = [
27
40
  "tsconfig",
28
41
  "include",
29
42
  "exclude",
43
+ "platform",
30
44
  "plugins",
31
45
  "preset",
32
46
  "addons",
47
+ "conventions",
33
48
  "rules",
34
49
  "groups",
35
50
  "rollout",
@@ -83,6 +98,65 @@ const isGuarantees = (value) => typeof value === "function";
83
98
  const isCodeActions = (value) => typeof value === "function";
84
99
  const isTokens = (value) => typeof value === "function";
85
100
  const isLibraryFacts = (value) => typeof value === "function";
101
+ const isReportRun = (value) => typeof value === "function";
102
+ const isAnalysisSummarize = (value) => typeof value === "function";
103
+ const isAnalysisSolve = (value) => typeof value === "function";
104
+ /** A plugin's analyses (`Plugin.analyses`); null when one is malformed (the error says which). */
105
+ function parseAnalyses(value, where, errors) {
106
+ if (!isRecord(value)) {
107
+ errors.push(`${where}: analyses must be an object of analysis names to { summarize, solve }`);
108
+ return null;
109
+ }
110
+ const analyses = {};
111
+ for (const [name, analysis] of Object.entries(value)) {
112
+ if (!ANALYSIS_NAME.test(name)) {
113
+ errors.push(`${where}: analysis "${name}" must be named in kebab-case, like "throws"`);
114
+ return null;
115
+ }
116
+ if (!isRecord(analysis)) {
117
+ errors.push(`${where}: analysis "${name}" must be an object with summarize and solve`);
118
+ return null;
119
+ }
120
+ const { summarize, solve } = analysis;
121
+ if (!isAnalysisSummarize(summarize) || !isAnalysisSolve(solve)) {
122
+ errors.push(`${where}: analysis "${name}": summarize and solve must be functions`);
123
+ return null;
124
+ }
125
+ analyses[name] = { summarize, solve };
126
+ }
127
+ return analyses;
128
+ }
129
+ /** A report's name is a command name: kebab-case, so `stateward <name>` can run it. */
130
+ const REPORT_NAME = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
131
+ /** A plugin's reports (`Plugin.reports`); null when one is malformed (the error says which). */
132
+ function parseReports(value, where, errors) {
133
+ if (!isRecord(value)) {
134
+ errors.push(`${where}: reports must be an object of report names to { help, run }`);
135
+ return null;
136
+ }
137
+ const reports = {};
138
+ for (const [name, report] of Object.entries(value)) {
139
+ if (!REPORT_NAME.test(name)) {
140
+ errors.push(`${where}: report "${name}" must be named in kebab-case, like "routes"`);
141
+ return null;
142
+ }
143
+ if (!isRecord(report)) {
144
+ errors.push(`${where}: report "${name}" must be an object with help and run`);
145
+ return null;
146
+ }
147
+ const { help, run } = report;
148
+ if (typeof help !== "string" || help === "") {
149
+ errors.push(`${where}: report "${name}": help must be a non-empty string`);
150
+ return null;
151
+ }
152
+ if (!isReportRun(run)) {
153
+ errors.push(`${where}: report "${name}": run must be a function`);
154
+ return null;
155
+ }
156
+ reports[name] = { help, run };
157
+ }
158
+ return reports;
159
+ }
86
160
  const OPTION_KINDS = "boolean, number, string, strings, record or severities";
87
161
  /** The entries of `value` that are severities; `optionProblem` then judges the whole default. */
88
162
  function severityMapOf(value) {
@@ -109,7 +183,17 @@ function parseOptionSpec(value, where, errors) {
109
183
  let spec;
110
184
  switch (kind) {
111
185
  case "boolean":
112
- spec = { kind, description, default: value.default === true };
186
+ if (value.convention !== undefined && (value.convention !== true || value.default === true)) {
187
+ errors.push(`${where}.convention must be true, on an option whose default is false`);
188
+ return undefined;
189
+ }
190
+ spec = {
191
+ kind,
192
+ description,
193
+ default: value.default === true,
194
+ ...(value.convention === true ? { convention: true } : {}),
195
+ ...(value.internal === true ? { internal: true } : {}),
196
+ };
113
197
  break;
114
198
  case "number":
115
199
  if (!optionalNumber(min) ||
@@ -215,6 +299,50 @@ function parseNeedsTypes(value) {
215
299
  return "invalid";
216
300
  return { library, modules: [...modules] };
217
301
  }
302
+ /** `RuleProves.flows` as given: undefined when left out, "invalid" when not one. */
303
+ function parseFlows(value) {
304
+ if (value === undefined)
305
+ return undefined;
306
+ if (!isRecord(value))
307
+ return "invalid";
308
+ const { produced, decided, escaped } = value;
309
+ const lists = { produced, decided, escaped };
310
+ if (Object.values(lists).some((list) => list !== undefined && !isStringArray(list))) {
311
+ return "invalid";
312
+ }
313
+ if (Object.keys(value).some((key) => !(key in lists)))
314
+ return "invalid";
315
+ return {
316
+ ...(isStringArray(produced) ? { produced: [...produced] } : {}),
317
+ ...(isStringArray(decided) ? { decided: [...decided] } : {}),
318
+ ...(isStringArray(escaped) ? { escaped: [...escaped] } : {}),
319
+ };
320
+ }
321
+ /** `RuleInfo.proves` as given: undefined when left out, "invalid" when not a `RuleProves`. */
322
+ function parseProves(value) {
323
+ if (value === undefined)
324
+ return undefined;
325
+ if (!isRecord(value))
326
+ return "invalid";
327
+ const { traits, returns, made, holders, tags } = value;
328
+ const lists = { traits, returns, made, holders, tags };
329
+ if (Object.values(lists).some((list) => list !== undefined && !isStringArray(list))) {
330
+ return "invalid";
331
+ }
332
+ if (Object.keys(value).some((key) => !(key in lists) && key !== "flows"))
333
+ return "invalid";
334
+ const flows = parseFlows(value.flows);
335
+ if (flows === "invalid")
336
+ return "invalid";
337
+ return {
338
+ ...(isStringArray(traits) ? { traits: [...traits] } : {}),
339
+ ...(isStringArray(returns) ? { returns: [...returns] } : {}),
340
+ ...(isStringArray(made) ? { made: [...made] } : {}),
341
+ ...(isStringArray(holders) ? { holders: [...holders] } : {}),
342
+ ...(flows === undefined ? {} : { flows }),
343
+ ...(isStringArray(tags) ? { tags: [...tags] } : {}),
344
+ };
345
+ }
218
346
  /** The metadata a pack declares up front (`RuleInfo`): everything but the `explain` text. */
219
347
  function parseInfo(value, where, errors) {
220
348
  const found = parseInfoFields(value, where, errors);
@@ -248,7 +376,13 @@ function parseInfo(value, where, errors) {
248
376
  ...sharing,
249
377
  needsTypes,
250
378
  };
251
- const info = fixNeedsStrictNulls === true ? { ...typed, fixNeedsStrictNulls: true } : typed;
379
+ const proves = parseProves(value.proves);
380
+ if (proves === "invalid") {
381
+ errors.push(`${where}.meta.proves must be { traits?, returns?, made?, holders?, flows?: { produced?, decided?, escaped? }, tags? } (each a list of traits or tag names) when present`);
382
+ return undefined;
383
+ }
384
+ const strict = fixNeedsStrictNulls === true ? { ...typed, fixNeedsStrictNulls: true } : typed;
385
+ const info = proves === undefined ? strict : { ...strict, proves };
252
386
  if (value.options === undefined)
253
387
  return info;
254
388
  const options = parseOptionSpecs(value.options, where, errors);
@@ -260,7 +394,7 @@ function parseInfoFields(value, where, errors) {
260
394
  return undefined;
261
395
  }
262
396
  const { description, summary, defaultSeverity, tier, group, addon, heuristic, widenedBy } = value;
263
- const { needsJsDoc, listUnknowns, yieldsTo } = value;
397
+ const { needsJsDoc, listUnknowns, yieldsTo, whenStated } = value;
264
398
  if (typeof description !== "string") {
265
399
  errors.push(`${where}.meta.description must be a string`);
266
400
  return undefined;
@@ -285,6 +419,10 @@ function parseInfoFields(value, where, errors) {
285
419
  errors.push(`${where}.meta.needsJsDoc must be true when present`);
286
420
  return undefined;
287
421
  }
422
+ if (whenStated !== undefined && (whenStated !== true || tier !== "convention")) {
423
+ errors.push(`${where}.meta.whenStated must be true, and only on a rule of tier "convention"`);
424
+ return undefined;
425
+ }
288
426
  if (listUnknowns !== undefined && listUnknowns !== true) {
289
427
  errors.push(`${where}.meta.listUnknowns must be true when present`);
290
428
  return undefined;
@@ -297,7 +435,8 @@ function parseInfoFields(value, where, errors) {
297
435
  const base = summary === undefined ? described : { ...described, summary };
298
436
  const guessed = heuristic === true ? { ...base, heuristic: true } : base;
299
437
  const reading = needsJsDoc === true ? { ...guessed, needsJsDoc: true } : guessed;
300
- const listing = listUnknowns === true ? { ...reading, listUnknowns: true } : reading;
438
+ const stated = whenStated === true ? { ...reading, whenStated: true } : reading;
439
+ const listing = listUnknowns === true ? { ...stated, listUnknowns: true } : stated;
301
440
  if (yieldsTo !== undefined && !isStringArray(yieldsTo)) {
302
441
  errors.push(`${where}.meta.yieldsTo must be a list of rule keys of the same pack`);
303
442
  return undefined;
@@ -324,7 +463,25 @@ function parseInfoFields(value, where, errors) {
324
463
  errors.push(`${where}.meta.addon must name the add-on in kebab-case when tier is "addon"`);
325
464
  return undefined;
326
465
  }
327
- return { ...flagged, addon };
466
+ if (value.offByDefault === undefined)
467
+ return { ...flagged, addon };
468
+ const offByDefault = parseOffByDefault(value.offByDefault);
469
+ if (offByDefault === undefined) {
470
+ errors.push(`${where}.meta.offByDefault must be { since: "<release>", right: <count>, checked: <count> }, with right at most checked`);
471
+ return undefined;
472
+ }
473
+ return { ...flagged, addon, offByDefault };
474
+ }
475
+ /** `{ since, right, checked }`: counts that are whole numbers, `right` at most `checked`. */
476
+ function parseOffByDefault(value) {
477
+ if (!isRecord(value))
478
+ return undefined;
479
+ const { since, right, checked } = value;
480
+ if (typeof since !== "string" || typeof right !== "number" || typeof checked !== "number") {
481
+ return undefined;
482
+ }
483
+ const counts = Number.isInteger(right) && Number.isInteger(checked) && right >= 0;
484
+ return counts && checked > 0 && right <= checked ? { since, right, checked } : undefined;
328
485
  }
329
486
  const isLoad = (value) => typeof value === "function";
330
487
  const isLoaded = (value) => typeof value === "function";
@@ -338,14 +495,20 @@ function parseRule(value, where, errors) {
338
495
  const meta = parseMeta(value.meta, where, errors);
339
496
  if (meta === undefined)
340
497
  return undefined;
341
- const { kind, check, summarize, silentReason } = value;
498
+ const { kind, check, summarize, silentReason, reads } = value;
342
499
  if (kind === "file" && isFileCheck(check)) {
343
500
  return { kind, meta, check };
344
501
  }
345
502
  if (kind === "project" && isProjectCheck(check) && isSummarize(summarize)) {
503
+ const problem = readsProblem(reads);
504
+ if (problem !== undefined) {
505
+ errors.push(`${where}: ${problem}`);
506
+ return undefined;
507
+ }
508
+ const read = isStringArray(reads) ? { reads: [...reads] } : {};
346
509
  return isSilentReason(silentReason)
347
- ? { kind, meta, check, summarize, silentReason }
348
- : { kind, meta, check, summarize };
510
+ ? { kind, meta, check, summarize, silentReason, ...read }
511
+ : { kind, meta, check, summarize, ...read };
349
512
  }
350
513
  errors.push(`${where} must be a "file" rule with check(), a "project" rule with summarize() and check(), or a "lazy" rule with load()`);
351
514
  return undefined;
@@ -417,7 +580,7 @@ function parsePlugin(value, index, errors) {
417
580
  errors.push(`plugins[${String(index)}] must be an object`);
418
581
  return undefined;
419
582
  }
420
- const { name, description, rules, editor, facts } = value;
583
+ const { name, description, rules, editor, facts, factsVerified, reports, analyses, defaultExclude, renamed, removed, } = value;
421
584
  if (typeof name !== "string" || name === "" || name.includes("/")) {
422
585
  errors.push(`plugins[${String(index)}].name must be a non-empty string without "/"`);
423
586
  return undefined;
@@ -437,6 +600,13 @@ function parsePlugin(value, index, errors) {
437
600
  const parsed = parseRule(rule, `${where}: rule "${key}"`, errors);
438
601
  if (parsed === undefined)
439
602
  valid = false;
603
+ else if ((parsed.meta.tier === "convention") !== key.startsWith(CONVENTION_PREFIX)) {
604
+ // The id says what the tier says: a rule of a team convention is `<pack>/convention/<rule>`.
605
+ errors.push(parsed.meta.tier === "convention"
606
+ ? `${where}: rule "${key}" has tier "convention", so its key must start with "${CONVENTION_PREFIX}"`
607
+ : `${where}: rule "${key}" starts with "${CONVENTION_PREFIX}", so its tier must be "convention"`);
608
+ valid = false;
609
+ }
440
610
  else
441
611
  parsedRules[key] = parsed;
442
612
  }
@@ -445,17 +615,96 @@ function parsePlugin(value, index, errors) {
445
615
  const shipped = shippedFacts(facts, where, errors);
446
616
  if (shipped === null)
447
617
  return undefined;
618
+ const verified = factsVerified === undefined ? undefined : parseFactsVerified(factsVerified, where, errors);
619
+ if (verified === null)
620
+ return undefined;
621
+ const parsedReports = reports === undefined ? undefined : parseReports(reports, where, errors);
622
+ if (parsedReports === null)
623
+ return undefined;
624
+ const parsedAnalyses = analyses === undefined ? undefined : parseAnalyses(analyses, where, errors);
625
+ if (parsedAnalyses === null)
626
+ return undefined;
627
+ if (defaultExclude !== undefined && !isStringArray(defaultExclude)) {
628
+ errors.push(`${where}: defaultExclude must be an array of globs`);
629
+ return undefined;
630
+ }
631
+ const parsedRenamed = renamed === undefined ? undefined : parseRenamed(renamed, where, errors);
632
+ if (parsedRenamed === null)
633
+ return undefined;
634
+ if (removed !== undefined && !(isStringArray(removed) && removed.every(isRuleId))) {
635
+ errors.push(`${where}: removed must be a list of rule ids ("<plugin>/<rule>")`);
636
+ return undefined;
637
+ }
448
638
  const parsed = {
449
639
  name,
450
640
  description,
451
641
  rules: parsedRules,
452
642
  ...(shipped === undefined ? {} : { facts: shipped }),
643
+ ...(verified === undefined ? {} : { factsVerified: verified }),
644
+ ...(parsedReports === undefined ? {} : { reports: parsedReports }),
645
+ ...(parsedAnalyses === undefined ? {} : { analyses: parsedAnalyses }),
646
+ ...(defaultExclude === undefined ? {} : { defaultExclude }),
647
+ ...(parsedRenamed === undefined ? {} : { renamed: parsedRenamed }),
648
+ ...(removed === undefined ? {} : { removed }),
453
649
  };
454
650
  if (editor === undefined)
455
651
  return parsed;
456
652
  const parsedEditor = parseEditor(editor, where, errors);
457
653
  return parsedEditor === undefined ? undefined : { ...parsed, editor: parsedEditor };
458
654
  }
655
+ /**
656
+ * A plugin's renamed rule ids (`Plugin.renamed`): each old rule id to the id the rule has now,
657
+ * both `"<plugin>/<rule>"`; null when one is malformed.
658
+ */
659
+ function parseRenamed(value, where, errors) {
660
+ if (!isRecord(value)) {
661
+ errors.push(`${where}: renamed must be an object of old rule ids to new rule ids`);
662
+ return null;
663
+ }
664
+ const parsed = {};
665
+ for (const [before, now] of Object.entries(value)) {
666
+ if (!isRuleId(before) || typeof now !== "string" || !isRuleId(now)) {
667
+ errors.push(`${where}: renamed "${before}" must map a rule id to a rule id ("<plugin>/<rule>")`);
668
+ return null;
669
+ }
670
+ parsed[before] = now;
671
+ }
672
+ return parsed;
673
+ }
674
+ /** `"<plugin>/<rule>"`, or `"<plugin>/convention/<rule>"` for a rule of a team convention. */
675
+ function isRuleId(value) {
676
+ const parts = value.split("/");
677
+ return ((parts.length === 2 || (parts.length === 3 && parts[1] === "convention")) &&
678
+ parts.every((part) => part !== ""));
679
+ }
680
+ /** The test patterns the packs add to the default `exclude` (`Plugin.defaultExclude`), in order. */
681
+ export function packExcludes(plugins) {
682
+ return plugins.flatMap((plugin) => plugin.defaultExclude ?? []);
683
+ }
684
+ /**
685
+ * The versions a plugin's facts were verified on (`Plugin.factsVerified`): each a semver range
686
+ * with its evidence; null when one is malformed.
687
+ */
688
+ function parseFactsVerified(value, where, errors) {
689
+ if (!isRecord(value)) {
690
+ errors.push(`${where}: factsVerified must be an object of packages to { verified, evidence }`);
691
+ return null;
692
+ }
693
+ const parsed = {};
694
+ for (const [name, entry] of Object.entries(value)) {
695
+ const verified = isRecord(entry) ? entry.verified : undefined;
696
+ const evidence = isRecord(entry) ? entry.evidence : undefined;
697
+ if (typeof verified !== "string" ||
698
+ semver.validRange(verified) === null ||
699
+ typeof evidence !== "string" ||
700
+ evidence === "") {
701
+ errors.push(`${where}: factsVerified["${name}"] must be { verified: "<semver range>", evidence: "<where it was verified>" }`);
702
+ return null;
703
+ }
704
+ parsed[name] = { verified, evidence };
705
+ }
706
+ return parsed;
707
+ }
459
708
  /** A plugin's own facts (`Plugin.facts`), checked as the config's are; null when one is malformed. */
460
709
  function shippedFacts(value, where, errors) {
461
710
  if (value === undefined)
@@ -481,8 +730,31 @@ function parsePlugins(value, errors) {
481
730
  seen.add(plugin.name);
482
731
  plugins.push(plugin);
483
732
  });
733
+ errors.push(...unpublishedReads(plugins));
484
734
  return plugins;
485
735
  }
736
+ /**
737
+ * A project rule that reads an analysis its publisher, though loaded, does not publish: a pack
738
+ * that does not match the version of the other. A publisher left out of the config is not an
739
+ * error: the rule reports that it is not checked. A lazy rule's `reads` is in its code, so it is
740
+ * checked when the rule loads (`loadRules`), with the same message.
741
+ */
742
+ function unpublishedReads(plugins) {
743
+ const found = [];
744
+ for (const plugin of plugins) {
745
+ for (const [key, rule] of Object.entries(plugin.rules)) {
746
+ if (rule.kind !== "project")
747
+ continue;
748
+ for (const id of rule.reads ?? []) {
749
+ const target = readTarget(plugins, id);
750
+ if (target.kind === "no-analysis") {
751
+ found.push(unpublishedRead(plugin.name, key, id, target.plugin));
752
+ }
753
+ }
754
+ }
755
+ }
756
+ return found;
757
+ }
486
758
  /** `"error"`, `"off"` ..., or `[severity, { options }]`: the severity and the raw options, if any. */
487
759
  function splitRuleConfig(value) {
488
760
  if (isRuleSetting(value))
@@ -500,7 +772,7 @@ function unknownRuleId(ruleId, known) {
500
772
  const listed = [...known].sort().join(", ") || "none";
501
773
  return `unknown rule id "${ruleId}"; rules of the loaded plugins: ${listed}`;
502
774
  }
503
- function parseRuleSettings(value, plugins, errors) {
775
+ function parseRuleSettings(value, plugins, conventions, errors) {
504
776
  if (!isRecord(value)) {
505
777
  errors.push("rules must be an object");
506
778
  return { rules: {}, ruleOptions: {} };
@@ -525,10 +797,23 @@ function parseRuleSettings(value, plugins, errors) {
525
797
  for (const [ruleId, rule] of known) {
526
798
  const raw = given.get(ruleId);
527
799
  const specs = rule.meta.options;
528
- if (raw !== undefined)
529
- ruleOptions[ruleId] = parseRuleOptions(ruleId, specs, raw, errors);
800
+ if (raw !== undefined) {
801
+ ruleOptions[ruleId] = parseRuleOptions(ruleId, specs, raw, errors, conventions);
802
+ }
530
803
  else if (specs !== undefined)
531
- ruleOptions[ruleId] = defaultOptions(specs);
804
+ ruleOptions[ruleId] = defaultOptions(specs, conventions);
805
+ }
806
+ // A `whenStated` rule that declares `asked` knows the config named it: by its id here, or with
807
+ // `conventions: true` (the option's own default).
808
+ for (const [ruleId, rule] of known) {
809
+ const current = ruleOptions[ruleId];
810
+ if (rule.meta.whenStated === true &&
811
+ current !== undefined &&
812
+ rule.meta.options?.asked !== undefined &&
813
+ settings[ruleId] !== undefined &&
814
+ settings[ruleId] !== "off") {
815
+ ruleOptions[ruleId] = { ...current, asked: true };
816
+ }
532
817
  }
533
818
  return { rules: settings, ruleOptions };
534
819
  }
@@ -581,6 +866,13 @@ function parsePreset(value, errors, name = "preset") {
581
866
  errors.push(`${name} must be ${orList(PRESETS)}`);
582
867
  return undefined;
583
868
  }
869
+ /** `conventions`: true turns on the checks of team conventions; false when left out. */
870
+ function parseConventions(value, errors) {
871
+ if (value === undefined || typeof value === "boolean")
872
+ return value === true;
873
+ errors.push("conventions must be true or false");
874
+ return false;
875
+ }
584
876
  function parseAddons(value, plugins, errors, name = "addons") {
585
877
  const listed = parseStringList(value, name, [], errors);
586
878
  const known = addonNames(plugins);
@@ -592,28 +884,83 @@ function parseAddons(value, plugins, errors, name = "addons") {
592
884
  }
593
885
  return listed;
594
886
  }
595
- const FACT_KINDS = ["throws", "rejects", "returns", "neverRejects"];
887
+ /** The facts that describe one class itself, not its methods: a `"*"` or `"Class.*"` key cannot hold them. */
888
+ const ONE_CLASS_KINDS = [
889
+ "catches",
890
+ "authenticates",
891
+ "extends",
892
+ "inheritedFacts",
893
+ ];
894
+ const FACT_KINDS = [
895
+ "throws",
896
+ "rejects",
897
+ "returns",
898
+ "neverRejects",
899
+ "cleanup",
900
+ "catches",
901
+ "authenticates",
902
+ "external",
903
+ "extends",
904
+ "inheritedFacts",
905
+ ];
906
+ /**
907
+ * Fact kinds stateward no longer reads: a line that still has one loads, with one warning that it
908
+ * has no effect. `reads` said what a package's guard reads; a guard from a package is now left out
909
+ * of `nestjs/guard-ignores-class-decorator` (R132).
910
+ */
911
+ const REMOVED_KINDS = {
912
+ reads: "a guard from a package is not checked, so what it reads is not needed",
913
+ };
596
914
  const RETURNS_SHAPE = '"release" (call the returned function) or { release: "<method>" }';
597
- /** `returns: "release"` or `returns: { release: "unsubscribe" }`: the release trait of a package function. */
598
- function parseReturns(value, where, errors) {
915
+ const METHOD_NAME = /^[A-Za-z_$][\w$]*$/;
916
+ /**
917
+ * The keys a `returns` fact took until R132 (a second obligation on the value: `opens`,
918
+ * `closedBy`, `consequence`, `openState`, and what it `held`). The rule that read them is gone:
919
+ * a line that still has one loads, and a warning says to delete it.
920
+ */
921
+ const REMOVED_RETURNS_KEYS = [
922
+ "opens",
923
+ "closedBy",
924
+ "consequence",
925
+ "openState",
926
+ "held",
927
+ ];
928
+ /** `returns: "release"` or `returns: { release: "unsubscribe" }`. */
929
+ function parseReturns(value, where, errors, warnings) {
599
930
  if (value === "release")
600
931
  return value;
601
932
  if (isRecord(value)) {
602
933
  const keys = Object.keys(value);
603
934
  const method = value.release;
604
- if (keys.length === 1 && typeof method === "string" && /^[A-Za-z_$][\w$]*$/.test(method)) {
935
+ const removed = keys.filter((key) => REMOVED_RETURNS_KEYS.includes(key));
936
+ const known = keys.every((key) => key === "release" || removed.includes(key));
937
+ if (known && typeof method === "string") {
938
+ if (!METHOD_NAME.test(method)) {
939
+ errors.push(`${where}.returns.release must be a method name, such as "unsubscribe"`);
940
+ return undefined;
941
+ }
942
+ if (removed.length > 0) {
943
+ const names = removed.map((key) => `\`${key}\``).join(", ");
944
+ warnings?.add(`${names} ${removed.length === 1 ? "was" : "were"} removed from stateward and ${removed.length === 1 ? "has" : "have"} no effect; delete ${removed.length === 1 ? "it" : "them"} from ${where}.returns`);
945
+ }
605
946
  return { release: method };
606
947
  }
607
- if (keys.length === 1 && typeof method === "string") {
608
- errors.push(`${where}.returns.release must be a method name, such as "unsubscribe"`);
609
- return undefined;
610
- }
611
948
  }
612
- errors.push(`${where}.returns must be ${RETURNS_SHAPE}`);
949
+ // `returns: "external"` states a value from outside the program, which is its own fact.
950
+ const external = value === "external" ? "; did you mean `external: true`?" : "";
951
+ errors.push(`${where}.returns must be ${RETURNS_SHAPE}${external}`);
613
952
  return undefined;
614
953
  }
954
+ /** The key of a package's lines in `facts` that gives the range of versions they are for. */
955
+ export const VERSIONS_KEY = "@versions";
615
956
  /** `facts: { axios: { get: { throws: ["AxiosError"] } } }`. */
616
- function parseFacts(value, errors) {
957
+ function parseFacts(value, errors,
958
+ /** The config file's line that names a package's function or class, for the error. */
959
+ lineOf = () => undefined,
960
+ /** Where a package's `"@versions"` range goes; a plugin's facts take none (`factsVerified`). */
961
+ versions,
962
+ /** Where a line that still names the placeholder class goes (`PLACEHOLDER_CLASS`). */
963
+ warnings) {
617
964
  if (value === undefined)
618
965
  return {};
619
966
  if (!isRecord(value)) {
@@ -628,14 +975,44 @@ function parseFacts(value, errors) {
628
975
  }
629
976
  const parsed = {};
630
977
  for (const [fn, entry] of Object.entries(functions)) {
631
- const where = `facts["${name}"]["${fn}"]`;
978
+ if (fn === VERSIONS_KEY) {
979
+ if (versions === undefined) {
980
+ errors.push(`facts["${name}"]: "${VERSIONS_KEY}" is for the config's facts; a pack states the versions its facts were verified on with factsVerified`);
981
+ }
982
+ else if (typeof entry === "string" && semver.validRange(entry) !== null) {
983
+ versions[name] = entry;
984
+ }
985
+ else {
986
+ errors.push(`facts["${name}"]["${VERSIONS_KEY}"] must be a semver range of the versions these facts are for, such as "^3.2.0"`);
987
+ }
988
+ continue;
989
+ }
990
+ const line = lineOf(name, fn);
991
+ const where = `facts["${name}"]["${fn}"]${line === undefined ? "" : ` (line ${String(line)})`}`;
992
+ const grain = factGrainOf(fn);
993
+ const owner = grain.kind === "class" ? grain.owner : fn;
994
+ if (grain.kind !== "package" && owner.includes(WILDCARD)) {
995
+ errors.push(`${where}: \`${WILDCARD}\` stands for every name, alone (\`"${WILDCARD}"\`) or after a class (\`"Repository.${WILDCARD}"\`)`);
996
+ continue;
997
+ }
998
+ if (grain.kind !== "function" && isRecord(entry)) {
999
+ const single = Object.keys(entry).find((kind) => ONE_CLASS_KINDS.includes(kind));
1000
+ if (single !== undefined) {
1001
+ errors.push(`${where}: \`${single}\` states what one class does, so it cannot apply to \`${fn}\`; give the class's own name as the key`);
1002
+ continue;
1003
+ }
1004
+ }
632
1005
  if (!isRecord(entry)) {
633
- errors.push(`${where} must be an object: { throws: [...], rejects: [...], returns: "release", neverRejects: true }`);
1006
+ errors.push(`${where} must be an object: { throws: [...], rejects: [...], returns: "release", neverRejects: true }, or, for a NestJS class, { catches: [...], authenticates: true }`);
634
1007
  continue;
635
1008
  }
636
1009
  const one = {};
637
1010
  for (const [kind, list] of Object.entries(entry)) {
638
- if (!FACT_KINDS.includes(kind)) {
1011
+ const removed = REMOVED_KINDS[kind];
1012
+ if (removed !== undefined) {
1013
+ warnings?.add(`${where}.${kind} was removed and has no effect: ${removed}`);
1014
+ }
1015
+ else if (!FACT_KINDS.includes(kind)) {
639
1016
  errors.push(`${where}: unknown fact "${kind}"; facts are ${orList(FACT_KINDS)}`);
640
1017
  }
641
1018
  else if (kind === "neverRejects") {
@@ -644,19 +1021,71 @@ function parseFacts(value, errors) {
644
1021
  else
645
1022
  errors.push(`${where}.neverRejects must be true (leave it out when it can reject)`);
646
1023
  }
1024
+ else if (kind === "cleanup") {
1025
+ const argument = isRecord(list) ? list.argument : undefined;
1026
+ if (isRecord(list) &&
1027
+ Object.keys(list).length === 1 &&
1028
+ typeof argument === "number" &&
1029
+ Number.isInteger(argument) &&
1030
+ argument >= 0) {
1031
+ one.cleanup = { argument };
1032
+ }
1033
+ else {
1034
+ errors.push(`${where}.cleanup must be { argument: <index> }: the argument whose function returns the cleanup`);
1035
+ }
1036
+ }
1037
+ else if (kind === "external") {
1038
+ if (list === true)
1039
+ one.external = true;
1040
+ else
1041
+ errors.push(`${where}.external must be true (leave it out for a value the program creates)`);
1042
+ }
1043
+ else if (kind === "inheritedFacts") {
1044
+ if (list === false)
1045
+ one.inheritedFacts = false;
1046
+ else {
1047
+ errors.push(`${where}.inheritedFacts must be false (leave it out when the class's inherited methods do what their facts say)`);
1048
+ }
1049
+ }
1050
+ else if (kind === "extends") {
1051
+ if (typeof list === "string" && METHOD_NAME.test(list))
1052
+ one.extends = list;
1053
+ else {
1054
+ errors.push(`${where}.extends must be the name of the class it extends, as the package exports it or a platform error, such as "Error"`);
1055
+ }
1056
+ }
1057
+ else if (kind === "authenticates") {
1058
+ if (typeof list === "boolean")
1059
+ one.authenticates = list;
1060
+ else
1061
+ errors.push(`${where}.authenticates must be true or false`);
1062
+ }
1063
+ else if (kind === "catches") {
1064
+ if (isStringArray(list) &&
1065
+ list.length > 0 &&
1066
+ (!list.includes("*") || list.length === 1)) {
1067
+ one.catches = list;
1068
+ }
1069
+ else {
1070
+ errors.push(`${where}.catches must be a non-empty array of error classes, like ["package:axios#AxiosError"], or ["*"] for every error`);
1071
+ }
1072
+ }
647
1073
  else if (kind === "returns") {
648
- const returns = parseReturns(list, where, errors);
1074
+ const returns = parseReturns(list, where, errors, warnings);
649
1075
  if (returns !== undefined)
650
1076
  one.returns = returns;
651
1077
  }
652
1078
  else if (!isStringArray(list)) {
653
1079
  errors.push(`${where}.${kind} must be an array of class names`);
654
1080
  }
655
- else if (kind === "throws") {
656
- one.throws = list;
657
- }
658
1081
  else {
659
- one.rejects = list;
1082
+ if (list.includes(PLACEHOLDER_CLASS)) {
1083
+ warnings?.add(`${where}.${kind} names \`${PLACEHOLDER_CLASS}\`, which stands for a class: replace it with the class the call ${kind === "throws" ? "throws" : "rejects with"}, or leave the list empty when there is none`);
1084
+ }
1085
+ if (kind === "throws")
1086
+ one.throws = list;
1087
+ else
1088
+ one.rejects = list;
660
1089
  }
661
1090
  }
662
1091
  if (one.neverRejects === true && (one.rejects ?? []).length > 0) {
@@ -937,6 +1366,7 @@ function mergeChain(chain, errors) {
937
1366
  "include",
938
1367
  "exclude",
939
1368
  "preset",
1369
+ "conventions",
940
1370
  "rollout",
941
1371
  "unknowns",
942
1372
  "maxMemory",
@@ -985,15 +1415,19 @@ function mergeChain(chain, errors) {
985
1415
  /**
986
1416
  * An object keyed by rule ids with each renamed id written as it is now (`renamed.ts`), unless a
987
1417
  * loaded plugin still has the old id; an entry under the new id wins over the old one. An entry
988
- * whose rule moved to a pack no loaded plugin is is dropped. `notices` gets one line per old id
989
- * found.
1418
+ * whose rule moved to a pack no loaded plugin is, or whose rule a release removed, is dropped.
1419
+ * `notices` gets one line per old or removed id found.
990
1420
  */
991
- function migratedRuleKeys(value, current, notices, where) {
1421
+ function migratedRuleKeys(value, { renames, removed, current }, notices, where) {
992
1422
  if (!isRecord(value))
993
1423
  return value;
994
1424
  const renamed = {};
995
1425
  for (const [id, entry] of Object.entries(value)) {
996
- const now = current.has(id) ? undefined : renamedRule(id);
1426
+ if (!current.has(id) && removed.has(id)) {
1427
+ notices.add(removedNotice(id, where(id)));
1428
+ continue;
1429
+ }
1430
+ const now = current.has(id) ? undefined : renamedRule(renames, id);
997
1431
  if (now === undefined) {
998
1432
  renamed[id] = entry;
999
1433
  continue;
@@ -1009,18 +1443,43 @@ function migratedRuleKeys(value, current, notices, where) {
1009
1443
  }
1010
1444
  return renamed;
1011
1445
  }
1012
- /** The config with renamed rule ids written as they are now, in `rules` and in each override's. */
1013
- function migrateRuleIds(input, current, notices, where) {
1446
+ /** A list of add-ons without the ones a release removed, with a notice for each; others as given. */
1447
+ function withoutRemoved(value, { removed }, notices, where) {
1448
+ if (!isStringArray(value))
1449
+ return value;
1450
+ return value.filter((id) => {
1451
+ if (!removed.has(id))
1452
+ return true;
1453
+ notices.add(removedNotice(id, where(id)));
1454
+ return false;
1455
+ });
1456
+ }
1457
+ /**
1458
+ * The config with renamed rule ids written as they are now and removed ones dropped, in `rules`,
1459
+ * `addons` and each override's.
1460
+ */
1461
+ function migrateRuleIds(input, changed, notices, where) {
1014
1462
  const migrated = { ...input };
1015
1463
  if (input.rules !== undefined) {
1016
- migrated.rules = migratedRuleKeys(input.rules, current, notices, where);
1464
+ migrated.rules = migratedRuleKeys(input.rules, changed, notices, where);
1465
+ }
1466
+ if (input.addons !== undefined) {
1467
+ migrated.addons = withoutRemoved(input.addons, changed, notices, where);
1017
1468
  }
1018
1469
  const { overrides } = input;
1019
1470
  if (Array.isArray(overrides)) {
1020
1471
  const list = overrides;
1021
- migrated.overrides = list.map((item) => isRecord(item) && item.rules !== undefined
1022
- ? { ...item, rules: migratedRuleKeys(item.rules, current, notices, where) }
1023
- : item);
1472
+ migrated.overrides = list.map((item) => {
1473
+ if (!isRecord(item))
1474
+ return item;
1475
+ const rules = item.rules === undefined
1476
+ ? {}
1477
+ : { rules: migratedRuleKeys(item.rules, changed, notices, where) };
1478
+ const addons = item.addons === undefined
1479
+ ? {}
1480
+ : { addons: withoutRemoved(item.addons, changed, notices, where) };
1481
+ return { ...item, ...rules, ...addons };
1482
+ });
1024
1483
  }
1025
1484
  return migrated;
1026
1485
  }
@@ -1045,7 +1504,9 @@ function parseTsconfig(value, errors) {
1045
1504
  */
1046
1505
  export function parseConfig(given, configDir, configName = "your config",
1047
1506
  /** The line the config file names an old rule id on, so the notice says where to rename it. */
1048
- lineOf = () => undefined) {
1507
+ lineOf = () => undefined,
1508
+ /** The line the config file names a package's function or class on under `facts`. */
1509
+ factLineOf = () => undefined) {
1049
1510
  if (!isRecord(given))
1050
1511
  return { ok: false, errors: ["config must be an object"] };
1051
1512
  const errors = [];
@@ -1057,18 +1518,29 @@ lineOf = () => undefined) {
1057
1518
  const line = lineOf(id);
1058
1519
  return line === undefined ? configName : `${configName}:${String(line)}`;
1059
1520
  };
1060
- const input = migrateRuleIds(merged, new Set(ruleIds(plugins)), notices, where);
1521
+ const input = migrateRuleIds(merged, {
1522
+ renames: renamesOf(plugins),
1523
+ removed: removedOf(plugins),
1524
+ current: new Set(ruleIds(plugins)),
1525
+ }, notices, where);
1061
1526
  const preset = parsePreset(input.preset, errors);
1062
1527
  const addons = parseAddons(input.addons, plugins, errors);
1063
- const { rules, ruleOptions } = parseRuleSettings(input.rules ?? {}, plugins, errors);
1528
+ const conventions = parseConventions(input.conventions, errors);
1529
+ const { rules, ruleOptions } = parseRuleSettings(input.rules ?? {}, plugins, conventions, errors);
1530
+ const notes = offByDefaultNotes(plugins, rules, addons);
1064
1531
  const groups = parseGroupSettings(input.groups ?? {}, plugins, errors);
1065
1532
  const include = parseStringList(input.include, "include", defaults.include, errors);
1066
- const exclude = parseStringList(input.exclude, "exclude", defaults.exclude, errors);
1533
+ const written = parseStringList(input.exclude, "exclude", defaults.exclude, errors);
1534
+ // The packs' test patterns join the list unless the config wrote its own without the defaults.
1535
+ const keepsDefaults = defaults.exclude.every((entry) => written.includes(entry));
1536
+ const exclude = keepsDefaults ? [...new Set([...written, ...packExcludes(plugins)])] : written;
1537
+ const platform = parseStringList(input.platform, "platform", [], errors);
1067
1538
  const overrides = parseOverrides(input.overrides, plugins, errors);
1068
1539
  const ignores = parseIgnores(input.ignores, errors);
1069
1540
  const rollout = parseRollout(input.rollout, errors);
1070
1541
  const unknowns = parseUnknowns(input.unknowns, errors);
1071
- const facts = parseFacts(input.facts, errors);
1542
+ const factVersions = {};
1543
+ const facts = parseFacts(input.facts, errors, factLineOf, factVersions, notices);
1072
1544
  const maxMemory = parseMaxMemory(input.maxMemory, errors);
1073
1545
  if (errors.length > 0 || tsconfig === undefined)
1074
1546
  return { ok: false, errors };
@@ -1079,22 +1551,42 @@ lineOf = () => undefined) {
1079
1551
  tsconfigAsWritten: tsconfig,
1080
1552
  include,
1081
1553
  exclude,
1082
- replacesDefaultExclude: input.exclude !== undefined && !defaults.exclude.every((entry) => exclude.includes(entry)),
1554
+ replacesDefaultExclude: input.exclude !== undefined && !keepsDefaults,
1555
+ platform,
1083
1556
  plugins,
1084
1557
  preset,
1085
1558
  addons,
1559
+ conventions,
1086
1560
  rules,
1087
1561
  groups,
1088
1562
  ruleOptions,
1089
1563
  rollout,
1090
1564
  configDir: path.resolve(configDir),
1091
1565
  facts,
1566
+ factVersions,
1092
1567
  overrides,
1093
1568
  ignores,
1094
1569
  unknowns,
1095
1570
  maxMemory,
1096
1571
  warnings: [...notices],
1572
+ notes,
1097
1573
  },
1098
1574
  };
1099
1575
  }
1576
+ /**
1577
+ * A note for each rule a release made off by default that `rules` turns on, unless `addons`
1578
+ * turns it on too: the config then chose it as an add-on.
1579
+ */
1580
+ function offByDefaultNotes(plugins, rules, addons) {
1581
+ return plugins.flatMap((plugin) => Object.entries(plugin.rules).flatMap(([key, rule]) => {
1582
+ const ruleId = `${plugin.name}/${key}`;
1583
+ const { offByDefault, addon } = rule.meta;
1584
+ const setting = rules[ruleId];
1585
+ if (offByDefault === undefined || setting === undefined || setting === "off")
1586
+ return [];
1587
+ if (addon !== undefined && addons.includes(`${plugin.name}/${addon}`))
1588
+ return [];
1589
+ return [offByDefaultNotice(ruleId, offByDefault)];
1590
+ }));
1591
+ }
1100
1592
  //# sourceMappingURL=config.js.map