@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
@@ -1,282 +1,277 @@
1
- /**
2
- * HFS check 7, R21 duplicate code (knowledge/hfs/slots.yaml ruleParams.<profile>.duplicateBlock = {lines: N, tokens: T},
3
- * the one definition of the threshold): a token-normalised block of at least N source lines and T tokens that appears
4
- * twice in the production program - in two owners, in two files of one owner, or twice in one file - is code copied
5
- * instead of shared.
6
- *
7
- * Normalisation: every production file is flattened into its syntax-node sequence (the compiler's parsed tree, walked in
8
- * source order; comments and whitespace do not exist in it), identifiers become one ID kind, string, template, numeric,
9
- * bigint and regex literals become one LIT kind, keywords and operators keep their own kind. Leading import and
10
- * export-from statements are dropped (imports repeat legitimately). A window is the run of tokens on N consecutive
11
- * physical lines starting at a line start whose last token reaches line N; windows are hashed with a rolling polynomial
12
- * hash (O(total tokens)), equal windows of two different owners are verified token by token, and overlapping hits on the
13
- * same file pair and offset merge into one maximal block. A block whose lines are mostly type declarations
14
- * (fewer than N/2 lines outside interface and type alias declarations) or of fewer than N/3 distinct line shapes
15
- * (a decorated DTO field list repeating one shape) is ignored.
16
- *
17
- * Two kinds of file are uniform by design and are not compared, both found through the slot resolver: the entry main.ts of
18
- * an app (slot appKind + its required main.ts) and the transport doors of a feature (a *.controller.ts, *.resolver.ts,
19
- * *.gateway.ts, *.consumer.ts, *.processor.ts or *.cli.ts of a slot that a protocol app composes). This is not a loophole: logic
20
- * cannot live in either. A door body is dispatch only (eslint transport-is-thin, R88), and main.ts cannot share a
21
- * helper because NestFactory is allowed only in main.ts (BE_ENTRYPOINT_ONLY_IN_APPS), so what repeats there is the
22
- * canonical shape of section 5 of BE-CONVENTION. A door or main.ts that grows logic is refused by those enforcers, not
23
- * excused here; every other file of the same slots (mappers, dto, app.module.ts) stays compared.
24
- * Canon scaffold subtrees (job fences, read/mapping methods, queue producers/definitions, CQRS wiring and data
25
- * props of imported JSX frames) are also dropped through their imported shape markers. Each exclusion ends a
26
- * token segment, so a window cannot join unrelated code across omitted scaffold lines. Custom bodies stay compared.
27
- */
28
1
  import { byCodeUnit } from '../../lib/list.mjs';
29
2
  import { canonCloneNodes } from './canon-clones.mjs';
30
-
31
3
  export const CLONE_RULE_IDS = ['HFS_DUPLICATE_CODE'];
32
-
33
4
  const MAX_VIOLATIONS = 200;
34
5
  const DOOR_ROLES = new Set(['controller', 'resolver', 'gateway', 'consumer', 'processor', 'cli']);
35
6
  const APP_ENTRY = 'main.ts';
36
7
  const ID = -1;
37
8
  const LIT = -2;
38
-
9
+ const entryAt = (entries, index) => entries[index];
39
10
  const literalKindSet = (SyntaxKind) => new Set([SyntaxKind.StringLiteral, SyntaxKind.NumericLiteral, SyntaxKind.BigIntLiteral, SyntaxKind.RegularExpressionLiteral,
40
- SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail, SyntaxKind.JsxText]);
41
-
11
+ SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail, SyntaxKind.JsxText]);
42
12
  function lineOfPosition(lineStarts, position) {
43
- let low = 0;
44
- let high = lineStarts.length - 1;
45
- while (low < high) {
46
- const mid = (low + high + 1) >> 1;
47
- if (lineStarts[mid] <= position) low = mid; else high = mid - 1;
48
- }
49
- return low + 1;
13
+ let low = 0;
14
+ let high = lineStarts.length - 1;
15
+ while (low < high) {
16
+ const mid = (low + high + 1) >> 1;
17
+ if (lineStarts[mid] <= position)
18
+ low = mid;
19
+ else
20
+ high = mid - 1;
21
+ }
22
+ return low + 1;
50
23
  }
51
-
52
24
  /** True for the nodes that carry no token: documentation comments and whitespace-only JSX text. */
53
25
  function isTriviaNode(SyntaxKind, node) {
54
- const kind = node.kind;
55
- if (kind >= SyntaxKind.FirstJSDocNode && kind <= SyntaxKind.LastJSDocNode) return true;
56
- return kind === SyntaxKind.JsxText && Boolean(node.containsOnlyTriviaWhiteSpaces);
26
+ const kind = node.kind;
27
+ if (kind >= SyntaxKind.FirstJSDocNode && kind <= SyntaxKind.LastJSDocNode)
28
+ return true;
29
+ return kind === SyntaxKind.JsxText && Boolean(node.containsOnlyTriviaWhiteSpaces);
57
30
  }
58
-
59
31
  function normalizedKindOf(SyntaxKind, literalKinds, kind) {
60
- if (kind === SyntaxKind.Identifier || kind === SyntaxKind.PrivateIdentifier) return ID;
61
- return literalKinds.has(kind) ? LIT : kind;
32
+ if (kind === SyntaxKind.Identifier || kind === SyntaxKind.PrivateIdentifier)
33
+ return ID;
34
+ return literalKinds.has(kind) ? LIT : kind;
62
35
  }
63
-
64
36
  function visitToken(walk, node, inType) {
65
- const { ts, sourceFile, kinds, lines, types, lineStarts, literalKinds } = walk;
66
- const { SyntaxKind } = ts;
67
- if (isTriviaNode(SyntaxKind, node)) return;
68
- if (walk.excluded?.has(node)) { walk.segment += 1; return; }
69
- const kind = node.kind;
70
- const typeScope = inType || kind === SyntaxKind.InterfaceDeclaration || kind === SyntaxKind.TypeAliasDeclaration;
71
- kinds.push(normalizedKindOf(SyntaxKind, literalKinds, kind));
72
- lines.push(lineOfPosition(lineStarts, node.getStart(sourceFile)));
73
- types.push(typeScope ? 1 : 0);
74
- walk.segments.push(walk.segment);
75
- ts.forEachChild(node, (child) => { visitToken(walk, child, typeScope); });
37
+ const { ts, sourceFile, kinds, lines, types, lineStarts, literalKinds } = walk;
38
+ const { SyntaxKind } = ts;
39
+ if (isTriviaNode(SyntaxKind, node))
40
+ return;
41
+ if (walk.excluded?.has(node)) {
42
+ walk.segment += 1;
43
+ return;
44
+ }
45
+ const kind = node.kind;
46
+ const typeScope = inType || kind === SyntaxKind.InterfaceDeclaration || kind === SyntaxKind.TypeAliasDeclaration;
47
+ kinds.push(normalizedKindOf(SyntaxKind, literalKinds, kind));
48
+ lines.push(lineOfPosition(lineStarts, node.getStart(sourceFile)));
49
+ types.push(typeScope ? 1 : 0);
50
+ walk.segments.push(walk.segment);
51
+ ts.forEachChild(node, (child) => { visitToken(walk, child, typeScope); });
76
52
  }
77
-
78
53
  /** Leading import and export-from statements repeat legitimately and are not tokenised. */
79
54
  const isDroppedStatement = (SyntaxKind, statement) => statement.kind === SyntaxKind.ImportDeclaration || statement.kind === SyntaxKind.ImportEqualsDeclaration
80
- || (statement.kind === SyntaxKind.ExportDeclaration && statement.moduleSpecifier);
81
-
55
+ || (statement.kind === SyntaxKind.ExportDeclaration && Boolean(statement.moduleSpecifier));
82
56
  export function tokenize(ts, sourceFile, { excluded = null } = {}) {
83
- const { SyntaxKind } = ts;
84
- const walk = { ts, sourceFile, kinds: [], lines: [], types: [], segments: [], segment: 0, excluded,
85
- lineStarts: sourceFile.getLineStarts(), literalKinds: literalKindSet(SyntaxKind) };
86
- for (const statement of sourceFile.statements) {
87
- if (!isDroppedStatement(SyntaxKind, statement)) visitToken(walk, statement, false);
88
- }
89
- return { kinds: Int32Array.from(walk.kinds), lines: Int32Array.from(walk.lines), types: Uint8Array.from(walk.types), segments: Int32Array.from(walk.segments) };
57
+ const { SyntaxKind } = ts;
58
+ const walk = { ts, sourceFile, kinds: [], lines: [], types: [], segments: [], segment: 0, excluded,
59
+ lineStarts: sourceFile.getLineStarts(), literalKinds: literalKindSet(SyntaxKind) };
60
+ for (const statement of sourceFile.statements) {
61
+ if (!isDroppedStatement(SyntaxKind, statement))
62
+ visitToken(walk, statement, false);
63
+ }
64
+ return { kinds: Int32Array.from(walk.kinds), lines: Int32Array.from(walk.lines), types: Uint8Array.from(walk.types), segments: Int32Array.from(walk.segments) };
90
65
  }
91
-
92
66
  /** The app a file belongs to, by the slot binding of the manifest; null for a file of no app. */
93
67
  const appOfFile = (resolver, rel) => resolver.classifyPath(rel).bindings?.app ?? null;
94
-
95
68
  /** True for the app entry and the transport doors: their shape is fixed by the convention and their body cannot hold logic. */
96
69
  function uniformByDesign(resolver, rel) {
97
- const classified = resolver.classifyPath(rel);
98
- if (classified.status !== 'owned' || !classified.slot) return false;
99
- const slot = resolver.slot(classified.slot);
100
- const base = rel.slice(rel.lastIndexOf('/') + 1);
101
- if (slot.appKind !== undefined) return base === APP_ENTRY && (slot.requires ?? []).includes(APP_ENTRY);
102
- const role = base.split('.').at(-2);
103
- return !slot.owner && (slot.composedBy?.length ?? 0) > 0 && DOOR_ROLES.has(role) && base.endsWith('.ts');
70
+ const classified = resolver.classifyPath(rel);
71
+ if (classified.status !== 'owned' || !classified.slot)
72
+ return false;
73
+ const slot = resolver.slot(classified.slot);
74
+ if (!slot)
75
+ return false;
76
+ const base = rel.slice(rel.lastIndexOf('/') + 1);
77
+ if (slot.appKind !== undefined)
78
+ return base === APP_ENTRY && (slot.requires ?? []).includes(APP_ENTRY);
79
+ const role = base.split('.').at(-2);
80
+ return !slot.owner && (slot.composedBy?.length ?? 0) > 0 && role !== undefined && DOOR_ROLES.has(role) && base.endsWith('.ts');
104
81
  }
105
-
106
82
  function homeText(profile, sameOwner, crossApp) {
107
- if (crossApp) return 'move it to a package (packages/<pkg>, the shared-code slot of the repository) and import it from both apps';
108
- if (sameOwner) return 'extract it once inside the owner and call it from both places';
109
- if (profile === 'fe') return 'extract it once inside the app (apps/<app>/src/modules/<capability>/ when pure, apps/<app>/src/hooks/<domain>/ for a React hook) and import it from both';
110
- return "move the shared helper to src/modules/platform/primitives/ or into the owning capability's src/modules/domain/<capability>/ and import it from both";
83
+ if (crossApp)
84
+ return 'move it to a package (packages/<pkg>, the shared-code slot of the repository) and import it from both apps';
85
+ if (sameOwner)
86
+ return 'extract it once inside the owner and call it from both places';
87
+ if (profile === 'fe')
88
+ return 'extract it once inside the app (apps/<app>/src/modules/<capability>/ when pure, apps/<app>/src/hooks/<domain>/ for a React hook) and import it from both';
89
+ return "move the shared helper to src/modules/platform/primitives/ or into the owning capability's src/modules/domain/<capability>/ and import it from both";
111
90
  }
112
-
113
91
  const cloneBuckets = (entries, N, T) => {
114
- const B1 = 1000003;
115
- const B2 = 92821;
116
- let maxTokens = 0;
117
- for (const file of entries) maxTokens = Math.max(maxTokens, file.kinds.length);
118
- const pow1 = new Int32Array(maxTokens + 1);
119
- const pow2 = new Int32Array(maxTokens + 1);
120
- pow1[0] = 1; pow2[0] = 1;
121
- for (let i = 1; i <= maxTokens; i += 1) { pow1[i] = Math.imul(pow1[i - 1], B1); pow2[i] = Math.imul(pow2[i - 1], B2); }
122
- const buckets = new Map();
123
- const hashing = { pow1, pow2, B1, B2 };
124
- entries.forEach((file, fileIndex) => addFileWindows(buckets, file, fileIndex, N, T, hashing));
125
- return buckets;
92
+ const B1 = 1000003;
93
+ const B2 = 92821;
94
+ let maxTokens = 0;
95
+ for (const file of entries)
96
+ maxTokens = Math.max(maxTokens, file.kinds.length);
97
+ const pow1 = new Int32Array(maxTokens + 1);
98
+ const pow2 = new Int32Array(maxTokens + 1);
99
+ pow1[0] = 1;
100
+ pow2[0] = 1;
101
+ for (let i = 1; i <= maxTokens; i += 1) {
102
+ pow1[i] = Math.imul(pow1[i - 1], B1);
103
+ pow2[i] = Math.imul(pow2[i - 1], B2);
104
+ }
105
+ const buckets = new Map();
106
+ const hashing = { pow1, pow2, B1, B2 };
107
+ entries.forEach((file, fileIndex) => addFileWindows(buckets, file, fileIndex, N, T, hashing));
108
+ return buckets;
126
109
  };
127
-
128
110
  function signedInt32(value) {
129
- return Math.imul(value, 1);
111
+ return Math.imul(value, 1);
130
112
  }
131
-
132
113
  const prefixHashes = (kinds, B1, B2) => {
133
- const count = kinds.length;
134
- const pre1 = new Int32Array(count + 1);
135
- const pre2 = new Int32Array(count + 1);
136
- for (let i = 0; i < count; i += 1) {
137
- pre1[i + 1] = Math.imul(pre1[i], B1) + kinds[i] + 3;
138
- pre2[i + 1] = Math.imul(pre2[i], B2) + kinds[i] + 7;
139
- }
140
- return { pre1, pre2 };
114
+ const count = kinds.length;
115
+ const pre1 = new Int32Array(count + 1);
116
+ const pre2 = new Int32Array(count + 1);
117
+ for (let i = 0; i < count; i += 1) {
118
+ pre1[i + 1] = Math.imul(pre1[i], B1) + kinds[i] + 3;
119
+ pre2[i + 1] = Math.imul(pre2[i], B2) + kinds[i] + 7;
120
+ }
121
+ return { pre1, pre2 };
141
122
  };
142
-
143
123
  const windowKey = (pre, pow, start, end) => signedInt32(pre[end] - Math.imul(pre[start], pow[end - start]));
144
-
145
124
  const addToBucket = (buckets, key, item) => {
146
- const list = buckets.get(key);
147
- if (list) list.push(item); else buckets.set(key, [item]);
125
+ const list = buckets.get(key);
126
+ if (list)
127
+ list.push(item);
128
+ else
129
+ buckets.set(key, [item]);
148
130
  };
149
-
150
131
  const addFileWindows = (buckets, file, fileIndex, N, T, hashing) => {
151
- const { pow1, pow2, B1, B2 } = hashing;
152
- const { kinds, lines, segments } = file;
153
- const count = kinds.length;
154
- const { pre1, pre2 } = prefixHashes(kinds, B1, B2);
155
- let end = 0;
156
- for (let start = 0; start < count; start += 1) {
157
- if (start > 0 && lines[start] === lines[start - 1] && segments[start] === segments[start - 1]) continue;
158
- const limit = lines[start] + N;
159
- if (end < start) end = start;
160
- while (end < count && lines[end] < limit && segments[end] === segments[start]) end += 1;
161
- if (end === start || lines[end - 1] - lines[start] + 1 < N) continue;
162
- const length = end - start;
163
- if (length < T) continue;
164
- const key = `${windowKey(pre1, pow1, start, end)}:${windowKey(pre2, pow2, start, end)}:${length}`;
165
- addToBucket(buckets, key, [fileIndex, start, end]);
166
- }
132
+ const { pow1, pow2, B1, B2 } = hashing;
133
+ const { kinds, lines, segments } = file;
134
+ const count = kinds.length;
135
+ const { pre1, pre2 } = prefixHashes(kinds, B1, B2);
136
+ let end = 0;
137
+ for (let start = 0; start < count; start += 1) {
138
+ if (start > 0 && lines[start] === lines[start - 1] && segments[start] === segments[start - 1])
139
+ continue;
140
+ const limit = lines[start] + N;
141
+ if (end < start)
142
+ end = start;
143
+ while (end < count && lines[end] < limit && segments[end] === segments[start])
144
+ end += 1;
145
+ if (end === start || lines[end - 1] - lines[start] + 1 < N)
146
+ continue;
147
+ const length = end - start;
148
+ if (length < T)
149
+ continue;
150
+ const key = `${windowKey(pre1, pow1, start, end)}:${windowKey(pre2, pow2, start, end)}:${length}`;
151
+ addToBucket(buckets, key, [fileIndex, start, end]);
152
+ }
167
153
  };
168
-
169
154
  const sameTokens = (entries, a, b) => {
170
- const ka = entries[a[0]].kinds;
171
- const kb = entries[b[0]].kinds;
172
- for (let i = 0; i < a[2] - a[1]; i += 1) if (ka[a[1] + i] !== kb[b[1] + i]) return false;
173
- return true;
155
+ const ka = entryAt(entries, a[0]).kinds;
156
+ const kb = entryAt(entries, b[0]).kinds;
157
+ for (let i = 0; i < a[2] - a[1]; i += 1)
158
+ if (ka[a[1] + i] !== kb[b[1] + i])
159
+ return false;
160
+ return true;
174
161
  };
175
-
176
162
  /** The first other occurrence is the partner; overlapping windows of one file are one occurrence, not a copy. */
177
163
  const partnerOf = (list, item) => list.find((other) => other !== item && (other[0] !== item[0] || other[1] >= item[2] || item[1] >= other[2]));
178
-
179
164
  const orderedPair = (entries, item, partner) => {
180
- const sameFile = entries[item[0]].rel === entries[partner[0]].rel;
181
- const itemFirst = sameFile ? item[1] < partner[1] : entries[item[0]].rel < entries[partner[0]].rel;
182
- return itemFirst ? [item, partner] : [partner, item];
165
+ const sameFile = entryAt(entries, item[0]).rel === entryAt(entries, partner[0]).rel;
166
+ const itemFirst = sameFile ? item[1] < partner[1] : entryAt(entries, item[0]).rel < entryAt(entries, partner[0]).rel;
167
+ return itemFirst ? [item, partner] : [partner, item];
183
168
  };
184
-
185
169
  const addHit = (hits, a, b) => {
186
- const key = `${a[0]}|${b[0]}|${b[1] - a[1]}`;
187
- const group = hits.get(key);
188
- if (group) group.push([a[1], a[2]]); else hits.set(key, [[a[1], a[2]]]);
170
+ const key = `${a[0]}|${b[0]}|${b[1] - a[1]}`;
171
+ const group = hits.get(key);
172
+ if (group)
173
+ group.push([a[1], a[2]]);
174
+ else
175
+ hits.set(key, [[a[1], a[2]]]);
189
176
  };
190
-
191
177
  const cloneHits = (entries, buckets) => {
192
- const hits = new Map();
193
- for (const list of buckets.values()) {
194
- if (list.length < 2) continue;
195
- for (const item of list) {
196
- const partner = partnerOf(list, item);
197
- if (!partner || !sameTokens(entries, item, partner)) continue;
198
- const [a, b] = orderedPair(entries, item, partner);
199
- addHit(hits, a, b);
178
+ const hits = new Map();
179
+ for (const list of buckets.values()) {
180
+ if (list.length < 2)
181
+ continue;
182
+ for (const item of list) {
183
+ const partner = partnerOf(list, item);
184
+ if (!partner || !sameTokens(entries, item, partner))
185
+ continue;
186
+ const [a, b] = orderedPair(entries, item, partner);
187
+ addHit(hits, a, b);
188
+ }
200
189
  }
201
- }
202
- return hits;
190
+ return hits;
203
191
  };
204
-
205
192
  const blockShape = (file, start, end) => {
206
- const nonType = new Set();
207
- const shapes = new Map();
208
- for (let i = start; i < end; i += 1) {
209
- if (!file.types[i]) nonType.add(file.lines[i]);
210
- shapes.set(file.lines[i], signedInt32((shapes.get(file.lines[i]) ?? 7) * 31 + file.kinds[i] + 5));
211
- }
212
- return { nonType: nonType.size, distinct: new Set(shapes.values()).size };
193
+ const nonType = new Set();
194
+ const shapes = new Map();
195
+ for (let i = start; i < end; i += 1) {
196
+ const line = file.lines[i];
197
+ if (!file.types[i])
198
+ nonType.add(line);
199
+ shapes.set(line, signedInt32((shapes.get(line) ?? 7) * 31 + file.kinds[i] + 5));
200
+ }
201
+ return { nonType: nonType.size, distinct: new Set(shapes.values()).size };
213
202
  };
214
-
215
203
  const appendCloneBlock = (blocks, fileA, fileB, start, end, diag, N) => {
216
- const shape = blockShape(fileA, start, end);
217
- if (shape.nonType < Math.ceil(N / 2) || shape.distinct < Math.ceil(N / 3)) return;
218
- const lineA = fileA.lines[start];
219
- const endA = fileA.lines[end - 1];
220
- blocks.push({ a: fileA, b: fileB, line: lineA, endLine: endA, lines: endA - lineA + 1,
221
- twin: { path: fileB.rel, line: fileB.lines[start + diag], endLine: fileB.lines[end - 1 + diag] } });
204
+ const shape = blockShape(fileA, start, end);
205
+ if (shape.nonType < Math.ceil(N / 2) || shape.distinct < Math.ceil(N / 3))
206
+ return;
207
+ const lineA = fileA.lines[start];
208
+ const endA = fileA.lines[end - 1];
209
+ blocks.push({ a: fileA, b: fileB, line: lineA, endLine: endA, lines: endA - lineA + 1,
210
+ twin: { path: fileB.rel, line: fileB.lines[start + diag], endLine: fileB.lines[end - 1 + diag] } });
222
211
  };
223
-
224
212
  const cloneBlocks = (entries, hits, N) => {
225
- const blocks = [];
226
- for (const [key, group] of hits) {
227
- const [ai, bi, diagText] = key.split('|');
228
- const fileA = entries[Number(ai)];
229
- const fileB = entries[Number(bi)];
230
- const diag = Number(diagText);
231
- group.sort((x, y) => x[0] - y[0]);
232
- let [start, end] = group[0];
233
- for (const [nextStart, nextEnd] of group.slice(1)) {
234
- if (nextStart <= end && fileA.segments[nextStart] === fileA.segments[start]
235
- && fileB.segments[nextStart + diag] === fileB.segments[start + diag]) { end = Math.max(end, nextEnd); continue; }
236
- appendCloneBlock(blocks, fileA, fileB, start, end, diag, N);
237
- start = nextStart; end = nextEnd;
213
+ const blocks = [];
214
+ for (const [key, group] of hits) {
215
+ const [ai, bi, diagText] = key.split('|');
216
+ const fileA = entryAt(entries, Number(ai));
217
+ const fileB = entryAt(entries, Number(bi));
218
+ const diag = Number(diagText);
219
+ group.sort((x, y) => x[0] - y[0]);
220
+ let [start, end] = group[0];
221
+ for (const [nextStart, nextEnd] of group.slice(1)) {
222
+ if (nextStart <= end && fileA.segments[nextStart] === fileA.segments[start]
223
+ && fileB.segments[nextStart + diag] === fileB.segments[start + diag]) {
224
+ end = Math.max(end, nextEnd);
225
+ continue;
226
+ }
227
+ appendCloneBlock(blocks, fileA, fileB, start, end, diag, N);
228
+ start = nextStart;
229
+ end = nextEnd;
230
+ }
231
+ appendCloneBlock(blocks, fileA, fileB, start, end, diag, N);
238
232
  }
239
- appendCloneBlock(blocks, fileA, fileB, start, end, diag, N);
240
- }
241
- return blocks;
233
+ return blocks;
242
234
  };
243
-
244
235
  const differentApps = (resolver, a, b) => {
245
- const left = appOfFile(resolver, a);
246
- const right = appOfFile(resolver, b);
247
- return left !== null && right !== null && left !== right;
236
+ const left = appOfFile(resolver, a);
237
+ const right = appOfFile(resolver, b);
238
+ return left !== null && right !== null && left !== right;
248
239
  };
249
-
250
240
  const cloneViolations = (blocks, resolver, profile, N, T) => {
251
- blocks.sort((x, y) => x.a.rel.localeCompare(y.a.rel) || x.line - y.line || x.b.rel.localeCompare(y.b.rel));
252
- return blocks.slice(0, MAX_VIOLATIONS).map((block) => ({
253
- ruleId: 'HFS_DUPLICATE_CODE', path: block.a.rel, line: block.line, endLine: block.endLine, twin: block.twin, lines: block.lines,
254
- message: `${block.a.rel}:${block.line}-${block.endLine} duplicates ${block.twin.path}:${block.twin.line}-${block.twin.endLine} (${block.lines} lines, threshold ${N} lines / ${T} tokens, identifiers and literals ignored); ${homeText(profile, block.a.unit === block.b.unit, differentApps(resolver, block.a.rel, block.b.rel))}.`,
255
- }));
241
+ blocks.sort((x, y) => x.a.rel.localeCompare(y.a.rel) || x.line - y.line || x.b.rel.localeCompare(y.b.rel));
242
+ return blocks.slice(0, MAX_VIOLATIONS).map((block) => ({
243
+ ruleId: 'HFS_DUPLICATE_CODE', path: block.a.rel, line: block.line, endLine: block.endLine, twin: block.twin, lines: block.lines,
244
+ message: `${block.a.rel}:${block.line}-${block.endLine} duplicates ${block.twin.path}:${block.twin.line}-${block.twin.endLine} (${block.lines} lines, threshold ${N} lines / ${T} tokens, identifiers and literals ignored); ${homeText(profile, block.a.unit === block.b.unit, differentApps(resolver, block.a.rel, block.b.rel))}.`,
245
+ }));
256
246
  };
257
-
258
247
  const cloneCoverage = (entries, blocks, N, T) => {
259
- const owners = new Set();
260
- let duplicatedLines = 0;
261
- for (const block of blocks) { owners.add(block.a.unit); owners.add(block.b.unit); duplicatedLines += block.lines; }
262
- return { status: 'checked', minLines: N, minTokens: T, files: entries.length, cloneBlocks: blocks.length, ownersInvolved: owners.size, duplicatedLines,
263
- ...(blocks.length > MAX_VIOLATIONS ? { truncated: true } : {}) };
248
+ const owners = new Set();
249
+ let duplicatedLines = 0;
250
+ for (const block of blocks) {
251
+ owners.add(block.a.unit);
252
+ owners.add(block.b.unit);
253
+ duplicatedLines += block.lines;
254
+ }
255
+ return { status: 'checked', minLines: N, minTokens: T, files: entries.length, cloneBlocks: blocks.length, ownersInvolved: owners.size, duplicatedLines,
256
+ ...(blocks.length > MAX_VIOLATIONS ? { truncated: true } : {}) };
264
257
  };
265
-
266
- export function checkClones({ config, context, graph } = {}) {
267
- const { lines: N, tokens: T } = graph.resolver.ruleParams().duplicateBlock;
268
- const ts = context.ts ?? context.loaded.ts;
269
- const entries = [];
270
- for (const rel of [...graph.files.keys()].sort(byCodeUnit)) {
271
- if (/\.d\.[cm]?tsx?$/.test(rel) || uniformByDesign(graph.resolver, rel)) continue;
272
- const unit = graph.unit(rel);
273
- if (!unit) continue;
274
- const sourceFile = graph.files.get(rel).sourceFile;
275
- entries.push({ rel, unit, ...tokenize(ts, sourceFile, { excluded: canonCloneNodes(ts, sourceFile, graph.profile) }) });
276
- }
277
- const buckets = cloneBuckets(entries, N, T);
278
- const blocks = cloneBlocks(entries, cloneHits(entries, buckets), N);
279
- const profile = graph.profile;
280
- const violations = cloneViolations(blocks, graph.resolver, profile, N, T);
281
- return { violations, coverage: cloneCoverage(entries, blocks, N, T) };
258
+ export function checkClones({ context, graph }) {
259
+ // reason: the rule-params validators require duplicateBlock on every profile that runs this check
260
+ const { lines: N, tokens: T } = graph.resolver.ruleParams()?.duplicateBlock;
261
+ const ts = context.ts ?? context.loaded.ts;
262
+ const entries = [];
263
+ for (const rel of [...graph.files.keys()].sort(byCodeUnit)) {
264
+ if (/\.d\.[cm]?tsx?$/.test(rel) || uniformByDesign(graph.resolver, rel))
265
+ continue;
266
+ const unit = graph.unit(rel);
267
+ if (!unit)
268
+ continue;
269
+ const sourceFile = graph.files.get(rel).sourceFile;
270
+ entries.push({ rel, unit, ...tokenize(ts, sourceFile, { excluded: canonCloneNodes(ts, sourceFile, graph.profile) }) });
271
+ }
272
+ const buckets = cloneBuckets(entries, N, T);
273
+ const blocks = cloneBlocks(entries, cloneHits(entries, buckets), N);
274
+ const profile = graph.profile;
275
+ const violations = cloneViolations(blocks, graph.resolver, profile, N, T);
276
+ return { violations, coverage: cloneCoverage(entries, blocks, N, T) };
282
277
  }
@@ -4,15 +4,13 @@
4
4
  * program holds no production source file.
5
5
  */
6
6
  export const CONFIG_UNREAD_RULE_IDS = ['HFS_ARCH_CONFIG_UNREAD'];
7
-
8
7
  const RULE = 'HFS_ARCH_CONFIG_UNREAD';
9
-
10
8
  export function checkConfigUnread({ context }) {
11
- const violations = [];
12
- const analysed = context.files.length;
13
- if (analysed === 0) {
14
- violations.push({ ruleId: RULE, path: 'hfs.json', line: 1, column: 1,
15
- message: 'hfs.json and the TypeScript projects load but the machine analysed no production source file, so it judged nothing. Declare the apps that hold source in hfs.json, or fix the tsconfig include so the source is in the program.' });
16
- }
17
- return { violations, coverage: { status: 'checked', analysed } };
9
+ const violations = [];
10
+ const analysed = [...context.files].length;
11
+ if (analysed === 0) {
12
+ violations.push({ ruleId: RULE, path: 'hfs.json', line: 1, column: 1,
13
+ message: 'hfs.json and the TypeScript projects load but the machine analysed no production source file, so it judged nothing. Declare the apps that hold source in hfs.json, or fix the tsconfig include so the source is in the program.' });
14
+ }
15
+ return { violations, coverage: { status: 'checked', analysed } };
18
16
  }