@starci/eslint-canon-be 3.8.19 → 3.9.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 (164) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +1 -1
  3. package/runtime/bundle.json +192 -0
  4. package/runtime/engine/by-code-unit.mjs +6 -6
  5. package/runtime/engine/model-config.mjs +98 -76
  6. package/runtime/engine/orca-config.mjs +22 -17
  7. package/runtime/engine/release-config.mjs +22 -18
  8. package/runtime/engine/removed-vocabulary.mjs +21 -20
  9. package/runtime/engine/resources-config.mjs +42 -35
  10. package/runtime/engine/runtime-root.mjs +11 -13
  11. package/runtime/engine/secrets.mjs +170 -102
  12. package/runtime/engine/sonar-config.mjs +16 -13
  13. package/runtime/engine/temp-root.mjs +3 -9
  14. package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
  15. package/runtime/knowledge/hfs/rules.yaml +6 -6
  16. package/runtime/knowledge/hfs/slots.yaml +2 -2
  17. package/runtime/knowledge/sonar-gate.yaml +1 -1
  18. package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
  19. package/runtime/scripts/api/git/cat-file.mjs +0 -1
  20. package/runtime/scripts/api/git/config-get.mjs +1 -2
  21. package/runtime/scripts/api/git/diff.mjs +0 -1
  22. package/runtime/scripts/api/git/lib.mjs +32 -32
  23. package/runtime/scripts/api/git/ls-files.mjs +0 -1
  24. package/runtime/scripts/api/git/ls-tree.mjs +0 -1
  25. package/runtime/scripts/api/git/merge-base.mjs +2 -3
  26. package/runtime/scripts/api/git/remote.mjs +0 -1
  27. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
  28. package/runtime/scripts/api/git/show.mjs +0 -1
  29. package/runtime/scripts/hfs/allows.mjs +37 -38
  30. package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
  31. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
  32. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
  33. package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
  34. package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
  35. package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
  36. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
  37. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
  38. package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
  39. package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
  40. package/runtime/scripts/hfs/architecture/config.mjs +302 -268
  41. package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
  42. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
  43. package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
  44. package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
  45. package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
  46. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
  47. package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
  48. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
  49. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
  50. package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
  51. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
  52. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
  53. package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
  54. package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
  55. package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
  56. package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
  57. package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
  58. package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
  59. package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
  60. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
  61. package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
  62. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
  63. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
  64. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
  65. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
  66. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
  67. package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
  68. package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
  69. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
  70. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
  71. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
  72. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
  73. package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
  74. package/runtime/scripts/hfs/architecture/index.mjs +272 -272
  75. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
  76. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
  77. package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
  78. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
  79. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
  80. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
  81. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
  82. package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
  83. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
  84. package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
  85. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
  86. package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
  87. package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
  88. package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
  89. package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
  90. package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
  91. package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
  92. package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
  93. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
  94. package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
  95. package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
  96. package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
  97. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
  98. package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
  99. package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
  100. package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
  101. package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
  102. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
  103. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
  104. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
  105. package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
  106. package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
  107. package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
  108. package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
  109. package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
  110. package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
  111. package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
  112. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
  113. package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
  114. package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
  115. package/runtime/scripts/hfs/edition-slots.mjs +43 -40
  116. package/runtime/scripts/hfs/linear-text.mjs +20 -20
  117. package/runtime/scripts/hfs/param-names.mjs +6 -11
  118. package/runtime/scripts/hfs/path-findings.mjs +120 -100
  119. package/runtime/scripts/hfs/repo-identity.mjs +55 -42
  120. package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
  121. package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
  122. package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
  123. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
  124. package/runtime/scripts/hfs/rules/read.mjs +19 -15
  125. package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
  126. package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
  127. package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
  128. package/runtime/scripts/hfs/slot-classify.mjs +42 -38
  129. package/runtime/scripts/hfs/slot-errors.mjs +8 -7
  130. package/runtime/scripts/hfs/slot-imports.mjs +70 -63
  131. package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
  132. package/runtime/scripts/hfs/slot-match.mjs +122 -111
  133. package/runtime/scripts/hfs/slot-path.mjs +0 -2
  134. package/runtime/scripts/hfs/slot-required.mjs +65 -59
  135. package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
  136. package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
  137. package/runtime/scripts/hfs/slots.mjs +175 -152
  138. package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
  139. package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
  140. package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
  141. package/runtime/scripts/hfs/view.mjs +38 -40
  142. package/runtime/scripts/lib/dockerfile.mjs +131 -114
  143. package/runtime/scripts/lib/env.mjs +3 -6
  144. package/runtime/scripts/lib/event-contract.mjs +73 -78
  145. package/runtime/scripts/lib/git.mjs +15 -16
  146. package/runtime/scripts/lib/glob.mjs +23 -14
  147. package/runtime/scripts/lib/grammar-lint.mjs +33 -37
  148. package/runtime/scripts/lib/i18n.mjs +32 -35
  149. package/runtime/scripts/lib/in-order.mjs +57 -38
  150. package/runtime/scripts/lib/json-clone.mjs +45 -36
  151. package/runtime/scripts/lib/json.mjs +26 -14
  152. package/runtime/scripts/lib/language.mjs +133 -129
  153. package/runtime/scripts/lib/list.mjs +9 -14
  154. package/runtime/scripts/lib/mutation-fence.mjs +10 -5
  155. package/runtime/scripts/lib/path-key.mjs +53 -38
  156. package/runtime/scripts/lib/read-text.mjs +30 -17
  157. package/runtime/scripts/lib/regex.mjs +1 -2
  158. package/runtime/scripts/lib/same-text.mjs +0 -1
  159. package/runtime/scripts/lib/sops-envelope.mjs +148 -120
  160. package/runtime/scripts/lib/stack-services.mjs +128 -129
  161. package/runtime/scripts/lib/ts-analysis.mjs +28 -26
  162. package/runtime/scripts/lib/ts-ast.mjs +36 -42
  163. package/runtime/scripts/lib/walk.mjs +33 -27
  164. package/runtime/scripts/lib/yaml-cached.mjs +4 -4
@@ -5,10 +5,8 @@
5
5
  import path from 'node:path';
6
6
  import { allowsFile } from './allows.mjs';
7
7
  import { openHfs } from './slots.mjs';
8
-
9
8
  const posix = (p) => String(p).replaceAll('\\', '/');
10
9
  const isAbsoluteAny = (text) => path.isAbsolute(text) || /^[A-Za-z]:\//.test(text);
11
-
12
10
  /**
13
11
  * The view rules read, over an opened resolver.
14
12
  *
@@ -17,44 +15,45 @@ const isAbsoluteAny = (text) => path.isAbsolute(text) || /^[A-Za-z]:\//.test(tex
17
15
  * @returns {object} The frozen view: profile, apps, connections, patterns, ruleParams, relative, classify, slotOf, tierOf, ownerOf, slot.
18
16
  */
19
17
  function hfsView(opened, repoRoot) {
20
- const rel = (file) => {
21
- const text = posix(file);
22
- if (!isAbsoluteAny(text)) return text.replace(/^\.\//, '');
23
- return posix(path.relative(repoRoot, file));
24
- };
25
- const slotCache = new Map();
26
- const classify = (file) => {
27
- const key = rel(file);
28
- if (!slotCache.has(key)) slotCache.set(key, opened.classifyPath(key));
29
- return slotCache.get(key);
30
- };
31
- return Object.freeze({
32
- repoRoot,
33
- profile: opened.repo.profile,
34
- side: opened.repo.side ?? null,
35
- edition: opened.repo.edition ?? 'full',
36
- ...(opened.repo.grammar === undefined ? {} : { grammar: opened.repo.grammar }),
37
- apps: opened.repo.apps,
38
- connections: opened.repo.connections,
39
- patterns: Object.freeze([...(opened.repo.patterns ?? [])]),
40
- ruleParams: opened.ruleParams(),
41
- /** The repository-relative, forward-slash form of a linted filename. */
42
- relative: rel,
43
- /** The classification of a file: `{ slot, tier, owner?, bindings? ... }` or a no-slot status. */
44
- classify,
45
- /** The slot id that owns a file, or null when no slot does. */
46
- slotOf: (file) => classify(file).slot ?? null,
47
- /** The tier of a file, or null. */
48
- tierOf: (file) => opened.tierOf(rel(file)) ?? null,
49
- /** The owner root of a file, or null. */
50
- ownerOf: (file) => opened.ownerOf(rel(file))?.root ?? null,
51
- /** What the slot that owns a file says about it: `{ slot, root, relative, allowed, entry?, forbiddenBy?, allows }`, or null when the slot names no `allows`. */
52
- allows: (file) => allowsFile(opened, rel(file)),
53
- /** A slot definition by id. */
54
- slot: (id) => opened.slot(id),
55
- });
18
+ const rel = (file) => {
19
+ const text = posix(file);
20
+ if (!isAbsoluteAny(text))
21
+ return text.replace(/^\.\//, '');
22
+ return posix(path.relative(repoRoot, file));
23
+ };
24
+ const slotCache = new Map();
25
+ const classify = (file) => {
26
+ const key = rel(file);
27
+ if (!slotCache.has(key))
28
+ slotCache.set(key, opened.classifyPath(key));
29
+ return slotCache.get(key);
30
+ };
31
+ return Object.freeze({
32
+ repoRoot,
33
+ profile: opened.repo.profile,
34
+ side: opened.repo.side ?? null,
35
+ edition: opened.repo.edition ?? 'full',
36
+ ...(opened.repo.grammar === undefined ? {} : { grammar: opened.repo.grammar }),
37
+ apps: opened.repo.apps,
38
+ connections: opened.repo.connections,
39
+ patterns: Object.freeze([...(opened.repo.patterns ?? [])]),
40
+ ruleParams: opened.ruleParams(),
41
+ /** The repository-relative, forward-slash form of a linted filename. */
42
+ relative: rel,
43
+ /** The classification of a file: `{ slot, tier, owner?, bindings? ... }` or a no-slot status. */
44
+ classify,
45
+ /** The slot id that owns a file, or null when no slot does. */
46
+ slotOf: (file) => classify(file).slot ?? null,
47
+ /** The tier of a file, or null. */
48
+ tierOf: (file) => opened.tierOf(rel(file)) ?? null,
49
+ /** The owner root of a file, or null. */
50
+ ownerOf: (file) => opened.ownerOf(rel(file))?.root ?? null,
51
+ /** What the slot that owns a file says about it: `{ slot, root, relative, allowed, entry?, forbiddenBy?, allows }`, or null when the slot names no `allows`. */
52
+ allows: (file) => allowsFile(opened, rel(file)),
53
+ /** A slot definition by id. */
54
+ slot: (id) => opened.slot(id),
55
+ });
56
56
  }
57
-
58
57
  /**
59
58
  * The view of the app folder on disk at `repoRoot`, against the manifest under `runtimeRoot`: a side folder (be/, fe/) finds the
60
59
  * app-root hfs.json one level up and gets that side's view, so every linted file below it is judged by its own side's slots.
@@ -63,7 +62,6 @@ function hfsView(opened, repoRoot) {
63
62
  * @returns {object} The frozen view.
64
63
  */
65
64
  export const openHfsView = ({ runtimeRoot, repoRoot }) => hfsView(openHfs({ root: runtimeRoot, repoRoot }), repoRoot);
66
-
67
65
  /**
68
66
  * The view of one side of an in-memory app declaration (rule tests): no file is read except the manifest.
69
67
  *
@@ -2,146 +2,163 @@
2
2
  // one statement per logical line (a trailing backslash continues it, comment lines inside a continuation are dropped), the
3
3
  // instruction keyword is the first word, the stages open at each FROM. Parser directives (`# syntax=`, `# escape=`) and
4
4
  // comments are kept apart from the instructions. Here-documents are not part of the subset the canon uses and are read as text.
5
-
6
5
  /** Split a shell-form or exec-form argument string into its words (quotes group a word and are removed). */
7
6
  export function words(text) {
8
- const out = [];
9
- let current = '';
10
- let quote = null;
11
- let started = false;
12
- for (const char of text) {
13
- if (quote) {
14
- if (char === quote) quote = null;
15
- else current += char;
16
- } else if (char === '"' || char === "'") {
17
- quote = char;
18
- started = true;
19
- } else if (char === ' ' || char === '\t' || char === '\n') {
20
- if (started || current) out.push(current);
21
- current = '';
22
- started = false;
23
- } else {
24
- current += char;
7
+ const out = [];
8
+ let current = '';
9
+ let quote = null;
10
+ let started = false;
11
+ for (const char of text) {
12
+ if (quote) {
13
+ if (char === quote)
14
+ quote = null;
15
+ else
16
+ current += char;
17
+ }
18
+ else if (char === '"' || char === "'") {
19
+ quote = char;
20
+ started = true;
21
+ }
22
+ else if (char === ' ' || char === '\t' || char === '\n') {
23
+ if (started || current)
24
+ out.push(current);
25
+ current = '';
26
+ started = false;
27
+ }
28
+ else {
29
+ current += char;
30
+ }
25
31
  }
26
- }
27
- if (started || current) out.push(current);
28
- return out;
32
+ if (started || current)
33
+ out.push(current);
34
+ return out;
29
35
  }
30
-
31
36
  /** The exec-form array of an instruction's argument text (`["node", "x.js"]`), or null when it is not a JSON array of strings. */
32
37
  export function execForm(text) {
33
- const trimmed = text.trim();
34
- if (!trimmed.startsWith('[')) return null;
35
- try {
36
- const value = JSON.parse(trimmed);
37
- return Array.isArray(value) && value.every((item) => typeof item === 'string') ? value : null;
38
- } catch {
39
- return null;
40
- }
38
+ const trimmed = text.trim();
39
+ if (!trimmed.startsWith('['))
40
+ return null;
41
+ try {
42
+ const value = JSON.parse(trimmed);
43
+ return Array.isArray(value) && value.every((item) => typeof item === 'string') ? value : null;
44
+ }
45
+ catch {
46
+ return null;
47
+ }
41
48
  }
42
-
43
49
  /** The commands of a shell-form RUN body: the text split at `&&`, `||`, `;` and `|` outside quotes, each as its words. */
44
50
  export function shellCommands(body) {
45
- const commands = [];
46
- let current = '';
47
- let quote = null;
48
- const flush = () => {
49
- const parts = words(current);
50
- if (parts.length) commands.push(parts);
51
- current = '';
52
- };
53
- for (let i = 0; i < body.length; i += 1) {
54
- const char = body[i];
55
- if (quote) {
56
- current += char;
57
- if (char === quote) quote = null;
58
- } else if (char === '"' || char === "'") {
59
- quote = char;
60
- current += char;
61
- } else if (isCommandSeparator(body, i, char)) {
62
- i += commandSeparatorAdvance(body, i, char);
63
- flush();
64
- } else if (char === '\\' && body[i + 1] === '\n') {
65
- i += 1;
66
- } else {
67
- current += char;
51
+ const commands = [];
52
+ let current = '';
53
+ let quote = null;
54
+ const flush = () => {
55
+ const parts = words(current);
56
+ if (parts.length)
57
+ commands.push(parts);
58
+ current = '';
59
+ };
60
+ for (let i = 0; i < body.length; i += 1) {
61
+ const char = body.charAt(i);
62
+ if (quote) {
63
+ current += char;
64
+ if (char === quote)
65
+ quote = null;
66
+ }
67
+ else if (char === '"' || char === "'") {
68
+ quote = char;
69
+ current += char;
70
+ }
71
+ else if (isCommandSeparator(body, i, char)) {
72
+ i += commandSeparatorAdvance(body, i, char);
73
+ flush();
74
+ }
75
+ else if (char === '\\' && body[i + 1] === '\n') {
76
+ i += 1;
77
+ }
78
+ else {
79
+ current += char;
80
+ }
68
81
  }
69
- }
70
- flush();
71
- return commands;
82
+ flush();
83
+ return commands;
72
84
  }
73
-
74
85
  const isCommandSeparator = (body, index, char) => char === ';' || char === '|' || (char === '&' && body[index + 1] === '&');
75
86
  const commandSeparatorAdvance = (body, index, char) => char !== ';' && body[index + 1] === char ? 1 : 0;
76
-
77
87
  /**
78
88
  * Parse a Dockerfile: { instructions: [{ keyword, text, line, stage }], stages: [{ index, name, image, line, instructions }],
79
89
  * comments: [{ text, line }], preamble: [instruction before the first FROM (ARG)] }. `keyword` is upper-case; `text` is the
80
90
  * argument text of the logical line; `stage` is the index of the stage the instruction belongs to (-1 before the first FROM).
81
91
  */
82
92
  export function parseDockerfile(source) {
83
- const lines = String(source).replaceAll('\r\n', '\n').split('\n');
84
- const { instructions, comments } = parseInstructions(lines);
85
- const stages = [];
86
- const preamble = [];
87
- for (const instruction of instructions) {
88
- if (instruction.keyword === 'FROM') {
89
- const parts = words(instruction.text).filter((part) => !part.startsWith('--'));
90
- const asAt = parts.findIndex((part, index) => index > 0 && part.toUpperCase() === 'AS');
91
- stages.push({ index: stages.length, name: asAt > 0 ? parts[asAt + 1] ?? null : null, image: parts[0] ?? '', line: instruction.line, instructions: [] });
93
+ const lines = String(source).replaceAll('\r\n', '\n').split('\n');
94
+ const { instructions, comments } = parseInstructions(lines);
95
+ const stages = [];
96
+ const preamble = [];
97
+ for (const instruction of instructions) {
98
+ if (instruction.keyword === 'FROM') {
99
+ const parts = words(instruction.text).filter((part) => !part.startsWith('--'));
100
+ const asAt = parts.findIndex((part, index) => index > 0 && part.toUpperCase() === 'AS');
101
+ stages.push({ index: stages.length, name: asAt > 0 ? parts[asAt + 1] ?? null : null, image: parts[0] ?? '', line: instruction.line, instructions: [] });
102
+ }
103
+ instruction.stage = stages.length - 1;
104
+ if (stages.length)
105
+ stages.at(-1).instructions.push(instruction);
106
+ else
107
+ preamble.push(instruction);
92
108
  }
93
- instruction.stage = stages.length - 1;
94
- if (stages.length) stages.at(-1).instructions.push(instruction);
95
- else preamble.push(instruction);
96
- }
97
- return { instructions, stages, comments, preamble };
109
+ return { instructions, stages, comments, preamble };
98
110
  }
99
-
100
111
  function parseInstructions(lines) {
101
- const instructions = [];
102
- const comments = [];
103
- const state = { escape: '\\', directives: true };
104
- let i = 0;
105
- while (i < lines.length) {
106
- const parsed = parseInstructionLine(lines, i, state);
107
- if (parsed.comment) comments.push(parsed.comment);
108
- if (parsed.instruction) instructions.push(parsed.instruction);
109
- i = parsed.nextLine + 1;
110
- }
111
- return { instructions, comments };
112
+ const instructions = [];
113
+ const comments = [];
114
+ const state = { escape: '\\', directives: true };
115
+ let i = 0;
116
+ while (i < lines.length) {
117
+ const parsed = parseInstructionLine(lines, i, state);
118
+ if (parsed.comment)
119
+ comments.push(parsed.comment);
120
+ if (parsed.instruction)
121
+ instructions.push(parsed.instruction);
122
+ i = parsed.nextLine + 1;
123
+ }
124
+ return { instructions, comments };
112
125
  }
113
-
114
126
  function parseInstructionLine(lines, index, state) {
115
- const trimmed = lines[index].trim();
116
- if (trimmed === '') { state.directives = false; return { nextLine: index }; }
117
- if (trimmed.startsWith('#')) return parseCommentLine(trimmed, index, state);
118
- state.directives = false;
119
- const { logical, nextLine } = logicalInstructionOf(lines, index, state.escape);
120
- const split = logical.search(/\s/);
121
- const keyword = (split < 0 ? logical : logical.slice(0, split)).toUpperCase();
122
- return { nextLine, instruction: { keyword, text: split < 0 ? '' : logical.slice(split).trim(), line: index + 1, stage: -1 } };
127
+ const trimmed = lines[index].trim();
128
+ if (trimmed === '') {
129
+ state.directives = false;
130
+ return { nextLine: index };
131
+ }
132
+ if (trimmed.startsWith('#'))
133
+ return parseCommentLine(trimmed, index, state);
134
+ state.directives = false;
135
+ const { logical, nextLine } = logicalInstructionOf(lines, index, state.escape);
136
+ const split = logical.search(/\s/);
137
+ const keyword = (split < 0 ? logical : logical.slice(0, split)).toUpperCase();
138
+ return { nextLine, instruction: { keyword, text: split < 0 ? '' : logical.slice(split).trim(), line: index + 1, stage: -1 } };
123
139
  }
124
-
125
140
  function parseCommentLine(trimmed, index, state) {
126
- const body = trimmed.slice(1).trim();
127
- const directive = state.directives ? /^(syntax|escape|check)\s*=\s*(\S+)$/i.exec(body) : null;
128
- if (directive) {
129
- if (directive[1].toLowerCase() === 'escape') state.escape = directive[2];
130
- return { nextLine: index };
131
- }
132
- state.directives = false;
133
- return { nextLine: index, comment: { text: body, line: index + 1 } };
141
+ const body = trimmed.slice(1).trim();
142
+ const directive = state.directives ? /^(syntax|escape|check)\s*=\s*(\S+)$/i.exec(body) : null;
143
+ if (directive) {
144
+ if (directive[1].toLowerCase() === 'escape')
145
+ state.escape = directive[2];
146
+ return { nextLine: index };
147
+ }
148
+ state.directives = false;
149
+ return { nextLine: index, comment: { text: body, line: index + 1 } };
134
150
  }
135
-
136
151
  function logicalInstructionOf(lines, index, escape) {
137
- let logical = lines[index].trim();
138
- let nextLine = index;
139
- while (logical.endsWith(escape) && nextLine + 1 < lines.length) {
140
- logical = logical.slice(0, -1).trimEnd();
141
- nextLine += 1;
142
- while (nextLine < lines.length && lines[nextLine].trim().startsWith('#')) nextLine += 1;
143
- if (nextLine >= lines.length) break;
144
- logical = `${logical} ${lines[nextLine].trim()}`;
145
- }
146
- return { logical, nextLine };
152
+ let logical = lines[index].trim();
153
+ let nextLine = index;
154
+ while (logical.endsWith(escape) && nextLine + 1 < lines.length) {
155
+ logical = logical.slice(0, -1).trimEnd();
156
+ nextLine += 1;
157
+ while (nextLine < lines.length && lines[nextLine].trim().startsWith('#'))
158
+ nextLine += 1;
159
+ if (nextLine >= lines.length)
160
+ break;
161
+ logical = `${logical} ${lines[nextLine].trim()}`;
162
+ }
163
+ return { logical, nextLine };
147
164
  }
@@ -2,20 +2,17 @@
2
2
  // modules/schemas/env.yaml catalogues every variable with its purpose and kind. Production code reads a variable here
3
3
  // (readEnv) or takes an injected `env` parameter; it never reads `process.env.NAME` itself. The test runner is observed
4
4
  // in exactly one place, isSpecRun: code that must not touch the host's real state during a spec asks it.
5
-
6
5
  /** The value of the environment variable `name` in `env` (default: the process environment), or undefined. */
7
6
  export const readEnv = (name, env = process.env) => env[name];
8
-
9
7
  /** True when `env` (default: the process environment) belongs to a spec run (`node --test` marks its children with NODE_TEST_CONTEXT). */
10
8
  export const isSpecRun = (env = process.env) => Boolean(env.NODE_TEST_CONTEXT);
11
-
12
9
  /**
13
10
  * The environment for a child that is itself a test runner: `env` without the mark of an enclosing spec run. A `node --test` that inherits NODE_TEST_CONTEXT believes it is a
14
11
  * subtest of that run: it reports to its parent and exits 0 whatever its tests did, so a verb that runs specs as a child (the package clean-test, the scaffold e2e, the release cut)
15
12
  * would pass red specs from inside a test process. The runner marks the spec files it starts itself, so isSpecRun stays true inside them.
16
13
  */
17
14
  export function withoutTestRunner(env = process.env) {
18
- const rest = { ...env };
19
- delete rest.NODE_TEST_CONTEXT;
20
- return rest;
15
+ const rest = { ...env };
16
+ delete rest.NODE_TEST_CONTEXT;
17
+ return rest;
21
18
  }
@@ -1,107 +1,102 @@
1
- // event-contract.mjs - the async contract between the services of one product (R166 HFS_EVENT_CONTRACT).
2
- // The one source of the contract is the typed event classes of a service, `be/src/modules/events/<service>/<event>.event.ts`:
3
- // export interface OrderPlacedPayload { readonly orderId: string; readonly totalMinorUnits: number }
4
- // export class OrderPlacedEvent extends BaseEvent {
5
- // static readonly eventName = "order.placed"; static readonly version = 1
6
- // static readonly compensates = "order.placed" // only an event that reports the failure of a step
7
- // static create(payload: OrderPlacedPayload): OrderPlacedEvent { ... }
8
- // }
9
- // `starci app emit` writes `be/contracts/<service>/events.json` from them (name, version, compensates, and the payload fields of the
10
- // interface `create` takes); `starci app check` compares the committed file with the same reader. Nothing is executed: both walk the TypeScript
11
- // syntax tree. The functions take the `ts` module the caller loaded (the repository's own, else the runtime's); this file imports only the code-unit comparator.
12
1
  import { byCodeUnit } from './list.mjs';
13
-
14
2
  const EVENT_CONTRACT_SCHEMA = 'starci/event-contract@1';
15
3
  const FIELD_TYPE = /^(?:string|number|boolean|string\[\]|number\[\])\??$/;
16
-
17
4
  const hasStatic = (ts, member) => member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword) === true;
18
-
19
5
  /** The literal of a `static readonly <name> = <literal>` member: `{ text }` for a string, `{ number }` for an integer, or null. */
20
6
  function staticLiteral(ts, member) {
21
- if (!ts.isPropertyDeclaration(member) || !hasStatic(ts, member) || !ts.isIdentifier(member.name) || member.initializer === undefined) return null;
22
- let value = member.initializer;
23
- while (ts.isAsExpression(value) || ts.isParenthesizedExpression(value)) value = value.expression;
24
- if (ts.isStringLiteralLike(value)) return { name: member.name.text, text: value.text };
25
- if (ts.isNumericLiteral(value) && Number.isInteger(Number(value.text))) return { name: member.name.text, number: Number(value.text) };
26
- return null;
7
+ if (!ts.isPropertyDeclaration(member) || !hasStatic(ts, member) || !ts.isIdentifier(member.name) || member.initializer === undefined)
8
+ return null;
9
+ let value = member.initializer;
10
+ while (ts.isAsExpression(value) || ts.isParenthesizedExpression(value))
11
+ value = value.expression;
12
+ if (ts.isStringLiteralLike(value))
13
+ return { name: member.name.text, text: value.text };
14
+ if (ts.isNumericLiteral(value) && Number.isInteger(Number(value.text)))
15
+ return { name: member.name.text, number: Number(value.text) };
16
+ return null;
27
17
  }
28
-
29
18
  /** The text of a type node when it is one of the wire field types (`string`, `number`, `boolean`, `string[]`, `number[]`), else null. */
30
19
  function fieldType(ts, node) {
31
- if (node.kind === ts.SyntaxKind.StringKeyword) return 'string';
32
- if (node.kind === ts.SyntaxKind.NumberKeyword) return 'number';
33
- if (node.kind === ts.SyntaxKind.BooleanKeyword) return 'boolean';
34
- if (ts.isArrayTypeNode(node)) {
35
- const element = fieldType(ts, node.elementType);
36
- return element === 'string' || element === 'number' ? `${element}[]` : null;
37
- }
38
- return null;
20
+ if (node.kind === ts.SyntaxKind.StringKeyword)
21
+ return 'string';
22
+ if (node.kind === ts.SyntaxKind.NumberKeyword)
23
+ return 'number';
24
+ if (node.kind === ts.SyntaxKind.BooleanKeyword)
25
+ return 'boolean';
26
+ if (ts.isArrayTypeNode(node)) {
27
+ const element = fieldType(ts, node.elementType);
28
+ return element === 'string' || element === 'number' ? `${element}[]` : null;
29
+ }
30
+ return null;
39
31
  }
40
-
41
32
  function appendPayloadMember(ts, member, typeName, problems, where, payload) {
42
- if (!ts.isPropertySignature(member) || !ts.isIdentifier(member.name) || member.type === undefined) {
43
- problems.push(`${where}: the payload ${typeName} holds a member that is not \`name: type\``);
44
- return;
45
- }
46
- const type = fieldType(ts, member.type);
47
- let written = null;
48
- if (type !== null) written = `${type}${member.questionToken ? '?' : ''}`;
49
- if (written === null || !FIELD_TYPE.test(written)) problems.push(`${where}: payload field ${member.name.text} must be string, number, boolean, string[] or number[]`);
50
- else payload[member.name.text] = written;
33
+ if (!ts.isPropertySignature(member) || !ts.isIdentifier(member.name) || member.type === undefined) {
34
+ problems.push(`${where}: the payload ${typeName} holds a member that is not \`name: type\``);
35
+ return;
36
+ }
37
+ const type = fieldType(ts, member.type);
38
+ let written = null;
39
+ if (type !== null)
40
+ written = `${type}${member.questionToken ? '?' : ''}`;
41
+ if (written === null || !FIELD_TYPE.test(written))
42
+ problems.push(`${where}: payload field ${member.name.text} must be string, number, boolean, string[] or number[]`);
43
+ else
44
+ payload[member.name.text] = written;
51
45
  }
52
-
53
46
  /** The payload fields of the interface or type literal named `typeName` in the file: `{ <field>: <type> }`, optional fields with a `?`. */
54
47
  function payloadOf(ts, sourceFile, typeName, problems, where) {
55
- let members = null;
56
- for (const statement of sourceFile.statements) {
57
- if (ts.isInterfaceDeclaration(statement) && statement.name.text === typeName) members = statement.members;
58
- if (ts.isTypeAliasDeclaration(statement) && statement.name.text === typeName && ts.isTypeLiteralNode(statement.type)) members = statement.type.members;
59
- }
60
- if (members === null) {
61
- problems.push(`${where}: the payload type ${typeName} is not an interface or type literal of the file`);
62
- return {};
63
- }
64
- const payload = {};
65
- for (const member of members) appendPayloadMember(ts, member, typeName, problems, where, payload);
66
- return payload;
48
+ let members = null;
49
+ for (const statement of sourceFile.statements) {
50
+ if (ts.isInterfaceDeclaration(statement) && statement.name.text === typeName)
51
+ members = statement.members;
52
+ if (ts.isTypeAliasDeclaration(statement) && statement.name.text === typeName && ts.isTypeLiteralNode(statement.type))
53
+ members = statement.type.members;
54
+ }
55
+ if (members === null) {
56
+ problems.push(`${where}: the payload type ${typeName} is not an interface or type literal of the file`);
57
+ return {};
58
+ }
59
+ const payload = {};
60
+ for (const member of members)
61
+ appendPayloadMember(ts, member, typeName, problems, where, payload);
62
+ return payload;
67
63
  }
68
-
69
64
  /**
70
65
  * The event classes of one source text: `{ events: [{ className, name, version, compensates, payload }], problems: [text] }`. A class is an event
71
66
  * when it declares `static readonly eventName`; `version` is required, `create(payload: <Interface>)` names the payload.
72
67
  */
73
68
  export function readEventClasses(ts, text, file = 'event.ts') {
74
- const sourceFile = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
75
- const events = [];
76
- const problems = [];
77
- for (const statement of sourceFile.statements) {
78
- if (!ts.isClassDeclaration(statement) || statement.name === undefined) continue;
79
- const literals = statement.members.map((member) => staticLiteral(ts, member)).filter((literal) => literal !== null);
80
- const name = literals.find((literal) => literal.name === 'eventName')?.text;
81
- if (name === undefined) continue;
82
- const className = statement.name.text;
83
- const version = literals.find((literal) => literal.name === 'version')?.number;
84
- const compensates = literals.find((literal) => literal.name === 'compensates')?.text;
85
- if (version === undefined || version < 1) problems.push(`${className} needs \`static readonly version\` as a positive integer`);
86
- const create = statement.members.find((member) => ts.isMethodDeclaration(member) && hasStatic(ts, member) && ts.isIdentifier(member.name) && member.name.text === 'create');
87
- const parameter = create?.parameters[0]?.type;
88
- if (parameter === undefined || !ts.isTypeReferenceNode(parameter) || !ts.isIdentifier(parameter.typeName)) {
89
- problems.push(`${className} needs \`static create(payload: <Interface>)\` naming its payload interface`);
90
- continue;
69
+ const sourceFile = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
70
+ const events = [];
71
+ const problems = [];
72
+ for (const statement of sourceFile.statements) {
73
+ if (!ts.isClassDeclaration(statement) || statement.name === undefined)
74
+ continue;
75
+ const literals = statement.members.map((member) => staticLiteral(ts, member)).filter((literal) => literal !== null);
76
+ const name = literals.find((literal) => literal.name === 'eventName')?.text;
77
+ if (name === undefined)
78
+ continue;
79
+ const className = statement.name.text;
80
+ const version = literals.find((literal) => literal.name === 'version')?.number;
81
+ const compensates = literals.find((literal) => literal.name === 'compensates')?.text;
82
+ if (version === undefined || version < 1)
83
+ problems.push(`${className} needs \`static readonly version\` as a positive integer`);
84
+ const create = statement.members.find((member) => ts.isMethodDeclaration(member) && hasStatic(ts, member) && ts.isIdentifier(member.name) && member.name.text === 'create');
85
+ const parameter = create?.parameters[0]?.type;
86
+ if (parameter === undefined || !ts.isTypeReferenceNode(parameter) || !ts.isIdentifier(parameter.typeName)) {
87
+ problems.push(`${className} needs \`static create(payload: <Interface>)\` naming its payload interface`);
88
+ continue;
89
+ }
90
+ events.push({ className, name, version: version ?? 0, ...(compensates === undefined ? {} : { compensates }), payload: payloadOf(ts, sourceFile, parameter.typeName.text, problems, className) });
91
91
  }
92
- events.push({ className, name, version: version ?? 0, ...(compensates === undefined ? {} : { compensates }), payload: payloadOf(ts, sourceFile, parameter.typeName.text, problems, className) });
93
- }
94
- return { events, problems };
92
+ return { events, problems };
95
93
  }
96
-
97
94
  /** The sorted object: keys in code-point order, recursively (what the snapshot prints). */
98
95
  const sorted = (value) => (value !== null && typeof value === 'object' && !Array.isArray(value) ? Object.fromEntries(Object.keys(value).sort(byCodeUnit).map((key) => [key, sorted(value[key])])) : value);
99
-
100
96
  /** The text of the snapshot of a service from its event classes: 2-space JSON, sorted keys, one final newline. */
101
97
  export function snapshotText(service, events) {
102
- const table = Object.fromEntries(events.map(({ name, version, compensates, payload }) => [name, { version, payload, ...(compensates === undefined ? {} : { compensates }) }]));
103
- return `${JSON.stringify(sorted({ schema: EVENT_CONTRACT_SCHEMA, service, events: table }), null, 2)}\n`;
98
+ const table = Object.fromEntries(events.map(({ name, version, compensates, payload }) => [name, { version, payload, ...(compensates === undefined ? {} : { compensates }) }]));
99
+ return `${JSON.stringify(sorted({ schema: EVENT_CONTRACT_SCHEMA, service, events: table }), null, 2)}\n`;
104
100
  }
105
-
106
101
  /** The snapshot's text with line endings folded, for comparing a committed file to a fresh one. */
107
102
  export const folded = (text) => text.replaceAll('\r\n', '\n');