@stateward/core 1.2.2 → 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 (67) 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 +33 -32
  5. package/dist/backend/typescript6.js +1438 -139
  6. package/dist/baseline.d.ts +1 -1
  7. package/dist/baseline.js +14 -10
  8. package/dist/cache.d.ts +74 -4
  9. package/dist/cache.js +211 -37
  10. package/dist/checker.d.ts +6 -1
  11. package/dist/checker.js +10 -0
  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 +490 -48
  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 +33 -1
  23. package/dist/engine.js +344 -24
  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 +4 -1
  29. package/dist/ignores.d.ts +11 -0
  30. package/dist/ignores.js +74 -27
  31. package/dist/implementation.d.ts +0 -6
  32. package/dist/implementation.js +0 -17
  33. package/dist/index.d.ts +8 -9
  34. package/dist/index.js +10 -7
  35. package/dist/internal.d.ts +7 -4
  36. package/dist/internal.js +5 -3
  37. package/dist/lazy.d.ts +8 -1
  38. package/dist/lazy.js +27 -2
  39. package/dist/ledger.d.ts +2 -2
  40. package/dist/ledger.js +2 -2
  41. package/dist/off-by-default.d.ts +13 -0
  42. package/dist/off-by-default.js +15 -0
  43. package/dist/options.d.ts +21 -5
  44. package/dist/options.js +22 -7
  45. package/dist/package-facts.d.ts +211 -17
  46. package/dist/package-facts.js +520 -38
  47. package/dist/plan.d.ts +1 -1
  48. package/dist/plan.js +7 -2
  49. package/dist/release.d.ts +12 -21
  50. package/dist/release.js +26 -38
  51. package/dist/renamed.d.ts +22 -3
  52. package/dist/renamed.js +50 -16
  53. package/dist/rollout.js +7 -4
  54. package/dist/severity.d.ts +1 -1
  55. package/dist/severity.js +17 -3
  56. package/dist/test-runner.d.ts +18 -0
  57. package/dist/test-runner.js +483 -0
  58. package/dist/testing.d.ts +3 -3
  59. package/dist/testing.js +5 -5
  60. package/dist/timing.d.ts +8 -0
  61. package/dist/timing.js +35 -1
  62. package/dist/types.d.ts +692 -19
  63. package/dist/unread.d.ts +90 -0
  64. package/dist/unread.js +405 -0
  65. package/dist/vocabulary.d.ts +1 -1
  66. package/dist/vocabulary.js +1 -1
  67. 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) ||
@@ -310,7 +394,7 @@ function parseInfoFields(value, where, errors) {
310
394
  return undefined;
311
395
  }
312
396
  const { description, summary, defaultSeverity, tier, group, addon, heuristic, widenedBy } = value;
313
- const { needsJsDoc, listUnknowns, yieldsTo } = value;
397
+ const { needsJsDoc, listUnknowns, yieldsTo, whenStated } = value;
314
398
  if (typeof description !== "string") {
315
399
  errors.push(`${where}.meta.description must be a string`);
316
400
  return undefined;
@@ -335,6 +419,10 @@ function parseInfoFields(value, where, errors) {
335
419
  errors.push(`${where}.meta.needsJsDoc must be true when present`);
336
420
  return undefined;
337
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
+ }
338
426
  if (listUnknowns !== undefined && listUnknowns !== true) {
339
427
  errors.push(`${where}.meta.listUnknowns must be true when present`);
340
428
  return undefined;
@@ -347,7 +435,8 @@ function parseInfoFields(value, where, errors) {
347
435
  const base = summary === undefined ? described : { ...described, summary };
348
436
  const guessed = heuristic === true ? { ...base, heuristic: true } : base;
349
437
  const reading = needsJsDoc === true ? { ...guessed, needsJsDoc: true } : guessed;
350
- 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;
351
440
  if (yieldsTo !== undefined && !isStringArray(yieldsTo)) {
352
441
  errors.push(`${where}.meta.yieldsTo must be a list of rule keys of the same pack`);
353
442
  return undefined;
@@ -374,7 +463,25 @@ function parseInfoFields(value, where, errors) {
374
463
  errors.push(`${where}.meta.addon must name the add-on in kebab-case when tier is "addon"`);
375
464
  return undefined;
376
465
  }
377
- 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;
378
485
  }
379
486
  const isLoad = (value) => typeof value === "function";
380
487
  const isLoaded = (value) => typeof value === "function";
@@ -388,14 +495,20 @@ function parseRule(value, where, errors) {
388
495
  const meta = parseMeta(value.meta, where, errors);
389
496
  if (meta === undefined)
390
497
  return undefined;
391
- const { kind, check, summarize, silentReason } = value;
498
+ const { kind, check, summarize, silentReason, reads } = value;
392
499
  if (kind === "file" && isFileCheck(check)) {
393
500
  return { kind, meta, check };
394
501
  }
395
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] } : {};
396
509
  return isSilentReason(silentReason)
397
- ? { kind, meta, check, summarize, silentReason }
398
- : { kind, meta, check, summarize };
510
+ ? { kind, meta, check, summarize, silentReason, ...read }
511
+ : { kind, meta, check, summarize, ...read };
399
512
  }
400
513
  errors.push(`${where} must be a "file" rule with check(), a "project" rule with summarize() and check(), or a "lazy" rule with load()`);
401
514
  return undefined;
@@ -467,7 +580,7 @@ function parsePlugin(value, index, errors) {
467
580
  errors.push(`plugins[${String(index)}] must be an object`);
468
581
  return undefined;
469
582
  }
470
- const { name, description, rules, editor, facts } = value;
583
+ const { name, description, rules, editor, facts, factsVerified, reports, analyses, defaultExclude, renamed, removed, } = value;
471
584
  if (typeof name !== "string" || name === "" || name.includes("/")) {
472
585
  errors.push(`plugins[${String(index)}].name must be a non-empty string without "/"`);
473
586
  return undefined;
@@ -487,6 +600,13 @@ function parsePlugin(value, index, errors) {
487
600
  const parsed = parseRule(rule, `${where}: rule "${key}"`, errors);
488
601
  if (parsed === undefined)
489
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
+ }
490
610
  else
491
611
  parsedRules[key] = parsed;
492
612
  }
@@ -495,17 +615,96 @@ function parsePlugin(value, index, errors) {
495
615
  const shipped = shippedFacts(facts, where, errors);
496
616
  if (shipped === null)
497
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
+ }
498
638
  const parsed = {
499
639
  name,
500
640
  description,
501
641
  rules: parsedRules,
502
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 }),
503
649
  };
504
650
  if (editor === undefined)
505
651
  return parsed;
506
652
  const parsedEditor = parseEditor(editor, where, errors);
507
653
  return parsedEditor === undefined ? undefined : { ...parsed, editor: parsedEditor };
508
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
+ }
509
708
  /** A plugin's own facts (`Plugin.facts`), checked as the config's are; null when one is malformed. */
510
709
  function shippedFacts(value, where, errors) {
511
710
  if (value === undefined)
@@ -531,8 +730,31 @@ function parsePlugins(value, errors) {
531
730
  seen.add(plugin.name);
532
731
  plugins.push(plugin);
533
732
  });
733
+ errors.push(...unpublishedReads(plugins));
534
734
  return plugins;
535
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
+ }
536
758
  /** `"error"`, `"off"` ..., or `[severity, { options }]`: the severity and the raw options, if any. */
537
759
  function splitRuleConfig(value) {
538
760
  if (isRuleSetting(value))
@@ -550,7 +772,7 @@ function unknownRuleId(ruleId, known) {
550
772
  const listed = [...known].sort().join(", ") || "none";
551
773
  return `unknown rule id "${ruleId}"; rules of the loaded plugins: ${listed}`;
552
774
  }
553
- function parseRuleSettings(value, plugins, errors) {
775
+ function parseRuleSettings(value, plugins, conventions, errors) {
554
776
  if (!isRecord(value)) {
555
777
  errors.push("rules must be an object");
556
778
  return { rules: {}, ruleOptions: {} };
@@ -575,10 +797,23 @@ function parseRuleSettings(value, plugins, errors) {
575
797
  for (const [ruleId, rule] of known) {
576
798
  const raw = given.get(ruleId);
577
799
  const specs = rule.meta.options;
578
- if (raw !== undefined)
579
- ruleOptions[ruleId] = parseRuleOptions(ruleId, specs, raw, errors);
800
+ if (raw !== undefined) {
801
+ ruleOptions[ruleId] = parseRuleOptions(ruleId, specs, raw, errors, conventions);
802
+ }
580
803
  else if (specs !== undefined)
581
- 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
+ }
582
817
  }
583
818
  return { rules: settings, ruleOptions };
584
819
  }
@@ -631,6 +866,13 @@ function parsePreset(value, errors, name = "preset") {
631
866
  errors.push(`${name} must be ${orList(PRESETS)}`);
632
867
  return undefined;
633
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
+ }
634
876
  function parseAddons(value, plugins, errors, name = "addons") {
635
877
  const listed = parseStringList(value, name, [], errors);
636
878
  const known = addonNames(plugins);
@@ -642,28 +884,83 @@ function parseAddons(value, plugins, errors, name = "addons") {
642
884
  }
643
885
  return listed;
644
886
  }
645
- 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
+ };
646
914
  const RETURNS_SHAPE = '"release" (call the returned function) or { release: "<method>" }';
647
- /** `returns: "release"` or `returns: { release: "unsubscribe" }`: the release trait of a package function. */
648
- 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) {
649
930
  if (value === "release")
650
931
  return value;
651
932
  if (isRecord(value)) {
652
933
  const keys = Object.keys(value);
653
934
  const method = value.release;
654
- 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
+ }
655
946
  return { release: method };
656
947
  }
657
- if (keys.length === 1 && typeof method === "string") {
658
- errors.push(`${where}.returns.release must be a method name, such as "unsubscribe"`);
659
- return undefined;
660
- }
661
948
  }
662
- 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}`);
663
952
  return undefined;
664
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";
665
956
  /** `facts: { axios: { get: { throws: ["AxiosError"] } } }`. */
666
- 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) {
667
964
  if (value === undefined)
668
965
  return {};
669
966
  if (!isRecord(value)) {
@@ -678,14 +975,44 @@ function parseFacts(value, errors) {
678
975
  }
679
976
  const parsed = {};
680
977
  for (const [fn, entry] of Object.entries(functions)) {
681
- 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
+ }
682
1005
  if (!isRecord(entry)) {
683
- 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 }`);
684
1007
  continue;
685
1008
  }
686
1009
  const one = {};
687
1010
  for (const [kind, list] of Object.entries(entry)) {
688
- 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)) {
689
1016
  errors.push(`${where}: unknown fact "${kind}"; facts are ${orList(FACT_KINDS)}`);
690
1017
  }
691
1018
  else if (kind === "neverRejects") {
@@ -694,19 +1021,71 @@ function parseFacts(value, errors) {
694
1021
  else
695
1022
  errors.push(`${where}.neverRejects must be true (leave it out when it can reject)`);
696
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
+ }
697
1073
  else if (kind === "returns") {
698
- const returns = parseReturns(list, where, errors);
1074
+ const returns = parseReturns(list, where, errors, warnings);
699
1075
  if (returns !== undefined)
700
1076
  one.returns = returns;
701
1077
  }
702
1078
  else if (!isStringArray(list)) {
703
1079
  errors.push(`${where}.${kind} must be an array of class names`);
704
1080
  }
705
- else if (kind === "throws") {
706
- one.throws = list;
707
- }
708
1081
  else {
709
- 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;
710
1089
  }
711
1090
  }
712
1091
  if (one.neverRejects === true && (one.rejects ?? []).length > 0) {
@@ -987,6 +1366,7 @@ function mergeChain(chain, errors) {
987
1366
  "include",
988
1367
  "exclude",
989
1368
  "preset",
1369
+ "conventions",
990
1370
  "rollout",
991
1371
  "unknowns",
992
1372
  "maxMemory",
@@ -1035,15 +1415,19 @@ function mergeChain(chain, errors) {
1035
1415
  /**
1036
1416
  * An object keyed by rule ids with each renamed id written as it is now (`renamed.ts`), unless a
1037
1417
  * loaded plugin still has the old id; an entry under the new id wins over the old one. An entry
1038
- * whose rule moved to a pack no loaded plugin is is dropped. `notices` gets one line per old id
1039
- * 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.
1040
1420
  */
1041
- function migratedRuleKeys(value, current, notices, where) {
1421
+ function migratedRuleKeys(value, { renames, removed, current }, notices, where) {
1042
1422
  if (!isRecord(value))
1043
1423
  return value;
1044
1424
  const renamed = {};
1045
1425
  for (const [id, entry] of Object.entries(value)) {
1046
- 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);
1047
1431
  if (now === undefined) {
1048
1432
  renamed[id] = entry;
1049
1433
  continue;
@@ -1059,18 +1443,43 @@ function migratedRuleKeys(value, current, notices, where) {
1059
1443
  }
1060
1444
  return renamed;
1061
1445
  }
1062
- /** The config with renamed rule ids written as they are now, in `rules` and in each override's. */
1063
- 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) {
1064
1462
  const migrated = { ...input };
1065
1463
  if (input.rules !== undefined) {
1066
- 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);
1067
1468
  }
1068
1469
  const { overrides } = input;
1069
1470
  if (Array.isArray(overrides)) {
1070
1471
  const list = overrides;
1071
- migrated.overrides = list.map((item) => isRecord(item) && item.rules !== undefined
1072
- ? { ...item, rules: migratedRuleKeys(item.rules, current, notices, where) }
1073
- : 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
+ });
1074
1483
  }
1075
1484
  return migrated;
1076
1485
  }
@@ -1095,7 +1504,9 @@ function parseTsconfig(value, errors) {
1095
1504
  */
1096
1505
  export function parseConfig(given, configDir, configName = "your config",
1097
1506
  /** The line the config file names an old rule id on, so the notice says where to rename it. */
1098
- lineOf = () => undefined) {
1507
+ lineOf = () => undefined,
1508
+ /** The line the config file names a package's function or class on under `facts`. */
1509
+ factLineOf = () => undefined) {
1099
1510
  if (!isRecord(given))
1100
1511
  return { ok: false, errors: ["config must be an object"] };
1101
1512
  const errors = [];
@@ -1107,18 +1518,29 @@ lineOf = () => undefined) {
1107
1518
  const line = lineOf(id);
1108
1519
  return line === undefined ? configName : `${configName}:${String(line)}`;
1109
1520
  };
1110
- 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);
1111
1526
  const preset = parsePreset(input.preset, errors);
1112
1527
  const addons = parseAddons(input.addons, plugins, errors);
1113
- 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);
1114
1531
  const groups = parseGroupSettings(input.groups ?? {}, plugins, errors);
1115
1532
  const include = parseStringList(input.include, "include", defaults.include, errors);
1116
- 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);
1117
1538
  const overrides = parseOverrides(input.overrides, plugins, errors);
1118
1539
  const ignores = parseIgnores(input.ignores, errors);
1119
1540
  const rollout = parseRollout(input.rollout, errors);
1120
1541
  const unknowns = parseUnknowns(input.unknowns, errors);
1121
- const facts = parseFacts(input.facts, errors);
1542
+ const factVersions = {};
1543
+ const facts = parseFacts(input.facts, errors, factLineOf, factVersions, notices);
1122
1544
  const maxMemory = parseMaxMemory(input.maxMemory, errors);
1123
1545
  if (errors.length > 0 || tsconfig === undefined)
1124
1546
  return { ok: false, errors };
@@ -1129,22 +1551,42 @@ lineOf = () => undefined) {
1129
1551
  tsconfigAsWritten: tsconfig,
1130
1552
  include,
1131
1553
  exclude,
1132
- replacesDefaultExclude: input.exclude !== undefined && !defaults.exclude.every((entry) => exclude.includes(entry)),
1554
+ replacesDefaultExclude: input.exclude !== undefined && !keepsDefaults,
1555
+ platform,
1133
1556
  plugins,
1134
1557
  preset,
1135
1558
  addons,
1559
+ conventions,
1136
1560
  rules,
1137
1561
  groups,
1138
1562
  ruleOptions,
1139
1563
  rollout,
1140
1564
  configDir: path.resolve(configDir),
1141
1565
  facts,
1566
+ factVersions,
1142
1567
  overrides,
1143
1568
  ignores,
1144
1569
  unknowns,
1145
1570
  maxMemory,
1146
1571
  warnings: [...notices],
1572
+ notes,
1147
1573
  },
1148
1574
  };
1149
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
+ }
1150
1592
  //# sourceMappingURL=config.js.map