@starci/eslint-canon-be 3.8.20 → 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.
- package/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/runtime/bundle.json +192 -0
- package/runtime/engine/by-code-unit.mjs +6 -6
- package/runtime/engine/model-config.mjs +98 -76
- package/runtime/engine/orca-config.mjs +22 -17
- package/runtime/engine/release-config.mjs +22 -18
- package/runtime/engine/removed-vocabulary.mjs +21 -20
- package/runtime/engine/resources-config.mjs +42 -35
- package/runtime/engine/runtime-root.mjs +11 -13
- package/runtime/engine/secrets.mjs +170 -102
- package/runtime/engine/sonar-config.mjs +16 -13
- package/runtime/engine/temp-root.mjs +3 -9
- package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
- package/runtime/knowledge/hfs/rules.yaml +6 -6
- package/runtime/knowledge/hfs/slots.yaml +2 -2
- package/runtime/knowledge/sonar-gate.yaml +1 -1
- package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
- package/runtime/scripts/api/git/cat-file.mjs +0 -1
- package/runtime/scripts/api/git/config-get.mjs +1 -2
- package/runtime/scripts/api/git/diff.mjs +0 -1
- package/runtime/scripts/api/git/lib.mjs +32 -32
- package/runtime/scripts/api/git/ls-files.mjs +0 -1
- package/runtime/scripts/api/git/ls-tree.mjs +0 -1
- package/runtime/scripts/api/git/merge-base.mjs +2 -3
- package/runtime/scripts/api/git/remote.mjs +0 -1
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
- package/runtime/scripts/api/git/show.mjs +0 -1
- package/runtime/scripts/hfs/allows.mjs +37 -38
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
- package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
- package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
- package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
- package/runtime/scripts/hfs/architecture/config.mjs +302 -268
- package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
- package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
- package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
- package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
- package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
- package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
- package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
- package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
- package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
- package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
- package/runtime/scripts/hfs/architecture/index.mjs +272 -272
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
- package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
- package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
- package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
- package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
- package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
- package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
- package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
- package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
- package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
- package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
- package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
- package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
- package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
- package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
- package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
- package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
- package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
- package/runtime/scripts/hfs/edition-slots.mjs +43 -40
- package/runtime/scripts/hfs/linear-text.mjs +20 -20
- package/runtime/scripts/hfs/param-names.mjs +6 -11
- package/runtime/scripts/hfs/path-findings.mjs +120 -100
- package/runtime/scripts/hfs/repo-identity.mjs +55 -42
- package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
- package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
- package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
- package/runtime/scripts/hfs/rules/read.mjs +19 -15
- package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
- package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
- package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
- package/runtime/scripts/hfs/slot-classify.mjs +42 -38
- package/runtime/scripts/hfs/slot-errors.mjs +8 -7
- package/runtime/scripts/hfs/slot-imports.mjs +70 -63
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
- package/runtime/scripts/hfs/slot-match.mjs +122 -111
- package/runtime/scripts/hfs/slot-path.mjs +0 -2
- package/runtime/scripts/hfs/slot-required.mjs +65 -59
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
- package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
- package/runtime/scripts/hfs/slots.mjs +175 -152
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
- package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
- package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
- package/runtime/scripts/hfs/view.mjs +38 -40
- package/runtime/scripts/lib/dockerfile.mjs +131 -114
- package/runtime/scripts/lib/env.mjs +3 -6
- package/runtime/scripts/lib/event-contract.mjs +73 -78
- package/runtime/scripts/lib/git.mjs +15 -16
- package/runtime/scripts/lib/glob.mjs +23 -14
- package/runtime/scripts/lib/grammar-lint.mjs +33 -37
- package/runtime/scripts/lib/i18n.mjs +32 -35
- package/runtime/scripts/lib/in-order.mjs +57 -38
- package/runtime/scripts/lib/json-clone.mjs +45 -36
- package/runtime/scripts/lib/json.mjs +26 -14
- package/runtime/scripts/lib/language.mjs +133 -129
- package/runtime/scripts/lib/list.mjs +9 -14
- package/runtime/scripts/lib/mutation-fence.mjs +10 -5
- package/runtime/scripts/lib/path-key.mjs +53 -38
- package/runtime/scripts/lib/read-text.mjs +30 -17
- package/runtime/scripts/lib/regex.mjs +1 -2
- package/runtime/scripts/lib/same-text.mjs +0 -1
- package/runtime/scripts/lib/sops-envelope.mjs +148 -120
- package/runtime/scripts/lib/stack-services.mjs +128 -129
- package/runtime/scripts/lib/ts-analysis.mjs +28 -26
- package/runtime/scripts/lib/ts-ast.mjs +36 -42
- package/runtime/scripts/lib/walk.mjs +33 -27
- 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
|
-
|
|
41
|
-
|
|
11
|
+
SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail, SyntaxKind.JsxText]);
|
|
42
12
|
function lineOfPosition(lineStarts, position) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
81
|
-
|
|
55
|
+
|| (statement.kind === SyntaxKind.ExportDeclaration && Boolean(statement.moduleSpecifier));
|
|
82
56
|
export function tokenize(ts, sourceFile, { excluded = null } = {}) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
|
|
111
|
+
return Math.imul(value, 1);
|
|
130
112
|
}
|
|
131
|
-
|
|
132
113
|
const prefixHashes = (kinds, B1, B2) => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
147
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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
|
-
|
|
240
|
-
}
|
|
241
|
-
return blocks;
|
|
233
|
+
return blocks;
|
|
242
234
|
};
|
|
243
|
-
|
|
244
235
|
const differentApps = (resolver, a, b) => {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
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
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
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
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
}
|