@starci/eslint-canon-be 3.18.3 → 3.19.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/dist/comments.js +1 -1
- package/dist/explicit-surface.js +1 -1
- package/dist/index.js +1 -1
- package/dist/jwt-alg.js +1 -1
- package/dist/lib/edition.d.ts +1 -1
- package/dist/lib/edition.js +1 -1
- package/dist/lib/hfs.d.ts +0 -6
- package/dist/lib/hfs.js +7 -5
- package/dist/lib/runtime-root.d.ts +6 -1
- package/dist/lib/runtime-root.js +8 -2
- package/dist/lib/subject-values.js +1 -1
- package/dist/module-layering.js +2 -2
- package/dist/project-graph.js +1 -1
- package/dist/third-party-owner.js +1 -1
- package/package.json +3 -1
- package/runtime/bundle.json +1 -190
- package/runtime/scripts/api/git/recorded-lines.mjs +5 -5
- package/runtime/scripts/hfs/project-graph.mjs +6 -6
- package/runtime/scripts/hfs/project-rule.mjs +3 -3
- package/runtime/config.example.yaml +0 -194
- package/runtime/engine/by-code-unit.mjs +0 -12
- package/runtime/engine/config.mjs +0 -461
- package/runtime/engine/invalid-config.mjs +0 -15
- package/runtime/engine/model-config.mjs +0 -112
- package/runtime/engine/orca-config.mjs +0 -34
- package/runtime/engine/plain-object.mjs +0 -5
- package/runtime/engine/release-config.mjs +0 -32
- package/runtime/engine/removed-vocabulary.mjs +0 -27
- package/runtime/engine/resources-config.mjs +0 -59
- package/runtime/engine/runtime-root.mjs +0 -138
- package/runtime/engine/secrets.mjs +0 -202
- package/runtime/engine/sonar-config.mjs +0 -24
- package/runtime/engine/temp-root.mjs +0 -26
- package/runtime/engine/yaml.mjs +0 -161
- package/runtime/knowledge/grammars/index.yaml +0 -114
- package/runtime/knowledge/hfs/canon-pins.yaml +0 -716
- package/runtime/knowledge/hfs/fe-stack.yaml +0 -272
- package/runtime/knowledge/hfs/rules.yaml +0 -6760
- package/runtime/knowledge/hfs/slots.yaml +0 -2783
- package/runtime/knowledge/patterns/fe/folder.yaml +0 -425
- package/runtime/knowledge/sonar-gate.yaml +0 -151
- package/runtime/modules/kernel/removed-vocabulary.yaml +0 -458
- package/runtime/modules/models/registry.yaml +0 -221
- package/runtime/modules/models/runtimes.yaml +0 -556
- package/runtime/modules/models/tiers.yaml +0 -66
- package/runtime/scripts/api/git/cat-file.mjs +0 -4
- package/runtime/scripts/api/git/config-get.mjs +0 -9
- package/runtime/scripts/api/git/diff.mjs +0 -4
- package/runtime/scripts/api/git/lib.mjs +0 -80
- package/runtime/scripts/api/git/ls-files.mjs +0 -5
- package/runtime/scripts/api/git/ls-tree.mjs +0 -4
- package/runtime/scripts/api/git/merge-base.mjs +0 -8
- package/runtime/scripts/api/git/remote.mjs +0 -4
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -4
- package/runtime/scripts/api/git/show.mjs +0 -5
- package/runtime/scripts/api/git/tag.mjs +0 -4
- package/runtime/scripts/hfs/allows.mjs +0 -77
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +0 -250
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +0 -103
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +0 -114
- package/runtime/scripts/hfs/architecture/backend.mjs +0 -462
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +0 -254
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +0 -291
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +0 -224
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +0 -102
- package/runtime/scripts/hfs/architecture/clones.mjs +0 -287
- package/runtime/scripts/hfs/architecture/component-tiers.mjs +0 -161
- package/runtime/scripts/hfs/architecture/config-unread.mjs +0 -16
- package/runtime/scripts/hfs/architecture/config.mjs +0 -407
- package/runtime/scripts/hfs/architecture/connection-map.mjs +0 -350
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +0 -115
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +0 -117
- package/runtime/scripts/hfs/architecture/context-map.mjs +0 -355
- package/runtime/scripts/hfs/architecture/context-owner.mjs +0 -102
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +0 -65
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +0 -75
- package/runtime/scripts/hfs/architecture/contract-bounds.mjs +0 -72
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +0 -177
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +0 -360
- package/runtime/scripts/hfs/architecture/contracts.mjs +0 -489
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +0 -81
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +0 -173
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +0 -420
- package/runtime/scripts/hfs/architecture/default-deny.mjs +0 -175
- package/runtime/scripts/hfs/architecture/doc-language.mjs +0 -45
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +0 -51
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +0 -75
- package/runtime/scripts/hfs/architecture/error-codes.mjs +0 -54
- package/runtime/scripts/hfs/architecture/error-masked.mjs +0 -78
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +0 -44
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +0 -60
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +0 -41
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +0 -112
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +0 -193
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +0 -198
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +0 -368
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +0 -374
- package/runtime/scripts/hfs/architecture/frontend.mjs +0 -248
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +0 -185
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +0 -77
- package/runtime/scripts/hfs/architecture/hfs.mjs +0 -394
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +0 -118
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +0 -213
- package/runtime/scripts/hfs/architecture/identity.mjs +0 -121
- package/runtime/scripts/hfs/architecture/index.mjs +0 -402
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +0 -69
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +0 -231
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +0 -232
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +0 -32
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +0 -237
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +0 -126
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +0 -192
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +0 -374
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +0 -26
- package/runtime/scripts/hfs/architecture/next-data.mjs +0 -425
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +0 -55
- package/runtime/scripts/hfs/architecture/owners.mjs +0 -142
- package/runtime/scripts/hfs/architecture/package-shape.mjs +0 -92
- package/runtime/scripts/hfs/architecture/presentation.mjs +0 -173
- package/runtime/scripts/hfs/architecture/reachability.mjs +0 -330
- package/runtime/scripts/hfs/architecture/register-once.mjs +0 -212
- package/runtime/scripts/hfs/architecture/registration.mjs +0 -372
- package/runtime/scripts/hfs/architecture/render-graph.mjs +0 -449
- package/runtime/scripts/hfs/architecture/render-prop-across-server.mjs +0 -58
- package/runtime/scripts/hfs/architecture/required-files.mjs +0 -237
- package/runtime/scripts/hfs/architecture/root-packages.mjs +0 -78
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +0 -133
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +0 -288
- package/runtime/scripts/hfs/architecture/server-action.mjs +0 -78
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +0 -13
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +0 -320
- package/runtime/scripts/hfs/architecture/source-names.mjs +0 -393
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +0 -344
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +0 -291
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +0 -76
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +0 -117
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +0 -587
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +0 -42
- package/runtime/scripts/hfs/architecture/surface-nesting.mjs +0 -127
- package/runtime/scripts/hfs/architecture/surface.mjs +0 -88
- package/runtime/scripts/hfs/architecture/symbols.mjs +0 -273
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +0 -263
- package/runtime/scripts/hfs/architecture/tiers.mjs +0 -256
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +0 -248
- package/runtime/scripts/hfs/architecture/type-context.mjs +0 -335
- package/runtime/scripts/hfs/architecture/typescript.mjs +0 -477
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +0 -165
- package/runtime/scripts/hfs/declaration-shape.mjs +0 -196
- package/runtime/scripts/hfs/declaration-slots.mjs +0 -89
- package/runtime/scripts/hfs/edition-slots.mjs +0 -80
- package/runtime/scripts/hfs/explicit-surface-rule.mjs +0 -59
- package/runtime/scripts/hfs/linear-text.mjs +0 -30
- package/runtime/scripts/hfs/manifest-shape.mjs +0 -474
- package/runtime/scripts/hfs/param-names.mjs +0 -13
- package/runtime/scripts/hfs/path-findings.mjs +0 -167
- package/runtime/scripts/hfs/repo-identity.mjs +0 -89
- package/runtime/scripts/hfs/rule-catalog.mjs +0 -458
- package/runtime/scripts/hfs/rule-params-shape.mjs +0 -103
- package/runtime/scripts/hfs/rules/database-constants.mjs +0 -25
- package/runtime/scripts/hfs/rules/database-migrations.mjs +0 -684
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +0 -68
- package/runtime/scripts/hfs/rules/read.mjs +0 -45
- package/runtime/scripts/hfs/rules/secrets.mjs +0 -79
- package/runtime/scripts/hfs/rules/spec-placement.mjs +0 -28
- package/runtime/scripts/hfs/slot-app-view.mjs +0 -97
- package/runtime/scripts/hfs/slot-classify.mjs +0 -69
- package/runtime/scripts/hfs/slot-errors.mjs +0 -12
- package/runtime/scripts/hfs/slot-imports.mjs +0 -98
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +0 -111
- package/runtime/scripts/hfs/slot-match.mjs +0 -164
- package/runtime/scripts/hfs/slot-path.mjs +0 -6
- package/runtime/scripts/hfs/slot-required.mjs +0 -95
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +0 -175
- package/runtime/scripts/hfs/slot-side-problems.mjs +0 -32
- package/runtime/scripts/hfs/slots.mjs +0 -306
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -5
- package/runtime/scripts/hfs/trailing-slashes.mjs +0 -8
- package/runtime/scripts/hfs/typescript-programs.mjs +0 -95
- package/runtime/scripts/hfs/view.mjs +0 -180
- package/runtime/scripts/lib/declaration-surface.mjs +0 -166
- package/runtime/scripts/lib/dockerfile.mjs +0 -164
- package/runtime/scripts/lib/env.mjs +0 -18
- package/runtime/scripts/lib/event-contract.mjs +0 -102
- package/runtime/scripts/lib/git-hooks-dir.mjs +0 -30
- package/runtime/scripts/lib/git.mjs +0 -35
- package/runtime/scripts/lib/glob.mjs +0 -32
- package/runtime/scripts/lib/grammar-lint.mjs +0 -57
- package/runtime/scripts/lib/i18n.mjs +0 -82
- package/runtime/scripts/lib/in-order.mjs +0 -90
- package/runtime/scripts/lib/json-clone.mjs +0 -70
- package/runtime/scripts/lib/json.mjs +0 -44
- package/runtime/scripts/lib/language.mjs +0 -233
- package/runtime/scripts/lib/list.mjs +0 -26
- package/runtime/scripts/lib/mutation-fence.mjs +0 -20
- package/runtime/scripts/lib/package-at.mjs +0 -62
- package/runtime/scripts/lib/path-key.mjs +0 -124
- package/runtime/scripts/lib/read-text.mjs +0 -47
- package/runtime/scripts/lib/regex.mjs +0 -3
- package/runtime/scripts/lib/rule-packs.mjs +0 -243
- package/runtime/scripts/lib/same-text.mjs +0 -4
- package/runtime/scripts/lib/secret-patterns.mjs +0 -230
- package/runtime/scripts/lib/sops-envelope.mjs +0 -212
- package/runtime/scripts/lib/stack-services.mjs +0 -191
- package/runtime/scripts/lib/ts-analysis.mjs +0 -39
- package/runtime/scripts/lib/ts-ast.mjs +0 -63
- package/runtime/scripts/lib/walk.mjs +0 -45
- package/runtime/scripts/lib/yaml-cached.mjs +0 -14
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { unwrapExpression as unwrap } from '../../lib/ts-analysis.mjs';
|
|
2
|
-
const TRANSLATION_MODULES = new Set(['next-intl', 'next-intl/server']);
|
|
3
|
-
const TRANSLATION_FACTORIES = new Set(['useTranslations', 'getTranslations']);
|
|
4
|
-
const uppercase = (name) => /^[A-Z]/.test(name);
|
|
5
|
-
function bindName(ts, scope, name, binding) {
|
|
6
|
-
if (ts.isIdentifier(name))
|
|
7
|
-
scope.bindings.set(name.text, binding);
|
|
8
|
-
else if (ts.isObjectBindingPattern(name) || ts.isArrayBindingPattern(name)) {
|
|
9
|
-
for (const element of name.elements)
|
|
10
|
-
if (ts.isBindingElement(element))
|
|
11
|
-
bindName(ts, scope, element.name, binding);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function importBindings(ts, scope, statement) {
|
|
15
|
-
const clause = statement.importClause;
|
|
16
|
-
if (!clause || clause.isTypeOnly || !ts.isStringLiteral(statement.moduleSpecifier))
|
|
17
|
-
return;
|
|
18
|
-
const module = statement.moduleSpecifier.text;
|
|
19
|
-
if (clause.name)
|
|
20
|
-
scope.bindings.set(clause.name.text, { kind: 'component', module, name: 'default' });
|
|
21
|
-
const named = clause.namedBindings;
|
|
22
|
-
if (!named)
|
|
23
|
-
return;
|
|
24
|
-
if (ts.isNamespaceImport(named))
|
|
25
|
-
scope.bindings.set(named.name.text, { kind: 'namespace', module });
|
|
26
|
-
else
|
|
27
|
-
for (const element of named.elements) {
|
|
28
|
-
if (element.isTypeOnly)
|
|
29
|
-
continue;
|
|
30
|
-
const imported = element.propertyName?.text ?? element.name.text;
|
|
31
|
-
const binding = TRANSLATION_MODULES.has(module) && TRANSLATION_FACTORIES.has(imported)
|
|
32
|
-
? { kind: 'translation-factory' } : { kind: 'component', module, name: imported };
|
|
33
|
-
scope.bindings.set(element.name.text, binding);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function lexicalBindings(ts, sourceFile) {
|
|
37
|
-
const scopes = new WeakMap();
|
|
38
|
-
const root = { parent: null, bindings: new Map(), functionScope: null };
|
|
39
|
-
root.functionScope = root;
|
|
40
|
-
const visit = (node, outer) => {
|
|
41
|
-
if ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && node.name)
|
|
42
|
-
bindName(ts, outer, node.name, { kind: 'local' });
|
|
43
|
-
const functionScope = ts.isFunctionLike(node);
|
|
44
|
-
const nested = functionScope || ts.isBlock(node) || ts.isCatchClause(node) || ts.isClassExpression(node)
|
|
45
|
-
|| ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node);
|
|
46
|
-
const scope = nested ? { parent: outer, bindings: new Map(), functionScope: functionScope ? null : outer.functionScope } : outer;
|
|
47
|
-
if (functionScope)
|
|
48
|
-
scope.functionScope = scope;
|
|
49
|
-
scopes.set(node, scope);
|
|
50
|
-
if (ts.isImportDeclaration(node))
|
|
51
|
-
importBindings(ts, scope, node);
|
|
52
|
-
if (ts.isParameter(node))
|
|
53
|
-
bindName(ts, scope, node.name, { kind: 'local' });
|
|
54
|
-
if (ts.isFunctionExpression(node) && node.name)
|
|
55
|
-
bindName(ts, scope, node.name, { kind: 'local' });
|
|
56
|
-
if (ts.isVariableDeclaration(node)) {
|
|
57
|
-
const flags = node.parent?.flags ?? ts.NodeFlags.Const;
|
|
58
|
-
bindName(ts, flags & ts.NodeFlags.BlockScoped ? scope : scope.functionScope, node.name, { kind: flags & ts.NodeFlags.Const ? 'constant' : 'local', declaration: node });
|
|
59
|
-
}
|
|
60
|
-
ts.forEachChild(node, (child) => { visit(child, scope); });
|
|
61
|
-
};
|
|
62
|
-
visit(sourceFile, root);
|
|
63
|
-
return (identifier) => {
|
|
64
|
-
for (let scope = scopes.get(identifier); scope; scope = scope.parent) {
|
|
65
|
-
if (scope.bindings.has(identifier.text))
|
|
66
|
-
return scope.bindings.get(identifier.text);
|
|
67
|
-
}
|
|
68
|
-
return null;
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
function translationFactory(ts, node, bindingOf) {
|
|
72
|
-
node = unwrap(ts, node);
|
|
73
|
-
if (ts.isIdentifier(node))
|
|
74
|
-
return bindingOf(node)?.kind === 'translation-factory';
|
|
75
|
-
if (!(ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && TRANSLATION_FACTORIES.has(node.name.text)))
|
|
76
|
-
return false;
|
|
77
|
-
const namespace = bindingOf(node.expression);
|
|
78
|
-
return namespace?.kind === 'namespace' && TRANSLATION_MODULES.has(namespace.module);
|
|
79
|
-
}
|
|
80
|
-
function translator(ts, node, bindingOf) {
|
|
81
|
-
if (!ts.isIdentifier(node))
|
|
82
|
-
return false;
|
|
83
|
-
const binding = bindingOf(node);
|
|
84
|
-
if (binding?.kind !== 'constant')
|
|
85
|
-
return false;
|
|
86
|
-
let initializer = unwrap(ts, binding.declaration.initializer);
|
|
87
|
-
if (initializer && ts.isAwaitExpression(initializer))
|
|
88
|
-
initializer = unwrap(ts, initializer.expression);
|
|
89
|
-
return Boolean(initializer && ts.isCallExpression(initializer) && translationFactory(ts, initializer.expression, bindingOf));
|
|
90
|
-
}
|
|
91
|
-
function importedComponent(ts, tag, bindingOf) {
|
|
92
|
-
if (ts.isIdentifier(tag))
|
|
93
|
-
return uppercase(tag.text) && bindingOf(tag)?.kind === 'component';
|
|
94
|
-
if (!ts.isPropertyAccessExpression(tag) || !uppercase(tag.name.text))
|
|
95
|
-
return false;
|
|
96
|
-
let root = tag.expression;
|
|
97
|
-
while (ts.isPropertyAccessExpression(root))
|
|
98
|
-
root = root.expression;
|
|
99
|
-
return ts.isIdentifier(root) && bindingOf(root)?.kind === 'namespace';
|
|
100
|
-
}
|
|
101
|
-
function dataProperty(ts, property, mapping) {
|
|
102
|
-
if (ts.isSpreadAssignment(property))
|
|
103
|
-
return mapping(property.expression);
|
|
104
|
-
if (ts.isShorthandPropertyAssignment(property))
|
|
105
|
-
return !property.objectAssignmentInitializer;
|
|
106
|
-
if (!ts.isPropertyAssignment(property))
|
|
107
|
-
return false;
|
|
108
|
-
return (!ts.isComputedPropertyName(property.name) || mapping(property.name.expression)) && mapping(property.initializer);
|
|
109
|
-
}
|
|
110
|
-
function dataMapping(ts, expression, bindingOf) {
|
|
111
|
-
const node = unwrap(ts, expression);
|
|
112
|
-
if (!node)
|
|
113
|
-
return false;
|
|
114
|
-
const mapping = (value) => dataMapping(ts, value, bindingOf);
|
|
115
|
-
if (ts.isIdentifier(node) || ts.isLiteralExpression(node) || node.kind === ts.SyntaxKind.TrueKeyword
|
|
116
|
-
|| node.kind === ts.SyntaxKind.FalseKeyword || node.kind === ts.SyntaxKind.NullKeyword || node.kind === ts.SyntaxKind.ThisKeyword)
|
|
117
|
-
return true;
|
|
118
|
-
if (ts.isPropertyAccessExpression(node))
|
|
119
|
-
return mapping(node.expression);
|
|
120
|
-
if (ts.isElementAccessExpression(node))
|
|
121
|
-
return mapping(node.expression) && mapping(node.argumentExpression);
|
|
122
|
-
if (ts.isObjectLiteralExpression(node))
|
|
123
|
-
return node.properties.every((property) => dataProperty(ts, property, mapping));
|
|
124
|
-
if (ts.isArrayLiteralExpression(node))
|
|
125
|
-
return node.elements.every((element) => ts.isOmittedExpression(element)
|
|
126
|
-
|| mapping(ts.isSpreadElement(element) ? element.expression : element));
|
|
127
|
-
if (ts.isTemplateExpression(node))
|
|
128
|
-
return node.templateSpans.every((span) => mapping(span.expression));
|
|
129
|
-
if (ts.isPrefixUnaryExpression(node))
|
|
130
|
-
return [ts.SyntaxKind.PlusToken, ts.SyntaxKind.MinusToken].includes(node.operator)
|
|
131
|
-
&& ts.isNumericLiteral(node.operand);
|
|
132
|
-
return ts.isCallExpression(node) && translator(ts, node.expression, bindingOf) && node.arguments.every(mapping);
|
|
133
|
-
}
|
|
134
|
-
/** Only object/array values belong to the prop contract; do not descend into a call or function body. */
|
|
135
|
-
function nestedDataObjects(ts, expression, bindingOf, excluded) {
|
|
136
|
-
const node = unwrap(ts, expression);
|
|
137
|
-
if (!node)
|
|
138
|
-
return;
|
|
139
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
140
|
-
if (dataMapping(ts, node, bindingOf)) {
|
|
141
|
-
excluded.add(node);
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
for (const property of node.properties)
|
|
145
|
-
if (ts.isPropertyAssignment(property)) {
|
|
146
|
-
nestedDataObjects(ts, property.initializer, bindingOf, excluded);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
else if (ts.isArrayLiteralExpression(node)) {
|
|
150
|
-
for (const element of node.elements)
|
|
151
|
-
if (!ts.isSpreadElement(element))
|
|
152
|
-
nestedDataObjects(ts, element, bindingOf, excluded);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/** The one app status-owner binding of FE-ERROR-1, shared by clone and body-flow proofs. */
|
|
156
|
-
export function canonicalStatusOwnerBinding(module, name) {
|
|
157
|
-
return name === 'SlotView' && module !== null && /(?:^|\/)components\/composites\/SlotView(?:\/index)?(?:\.[cm]?tsx?)?$/.test(module);
|
|
158
|
-
}
|
|
159
|
-
/** Resolve the canonical status owner through its import binding, including aliases and namespaces. */
|
|
160
|
-
function statusOwner(ts, tag, bindingOf) {
|
|
161
|
-
const binding = ts.isIdentifier(tag) ? bindingOf(tag) : ts.isPropertyAccessExpression(tag) && ts.isIdentifier(tag.expression) ? bindingOf(tag.expression) : null;
|
|
162
|
-
const module = binding?.kind === 'component' || binding?.kind === 'namespace' ? binding.module : null;
|
|
163
|
-
const name = binding?.kind === 'component' ? binding.name : ts.isPropertyAccessExpression(tag) ? tag.name.text : null;
|
|
164
|
-
return canonicalStatusOwnerBinding(module, name);
|
|
165
|
-
}
|
|
166
|
-
/** The mandated status wiring breaks clone windows; its render body remains ordinary compared code. */
|
|
167
|
-
function statusRecipe(ts, opening, bindingOf, excluded) {
|
|
168
|
-
if (!statusOwner(ts, opening.tagName, bindingOf))
|
|
169
|
-
return;
|
|
170
|
-
const attributes = opening.attributes.properties;
|
|
171
|
-
const render = attributes.find(attribute => ts.isJsxAttribute(attribute) && ts.isIdentifier(attribute.name) && attribute.name.text === 'render');
|
|
172
|
-
const value = render && ts.isJsxAttribute(render) && render.initializer && ts.isJsxExpression(render.initializer) ? unwrap(ts, render.initializer.expression) : undefined;
|
|
173
|
-
if (!value || !ts.isArrowFunction(value) || value.parameters.length !== 2 || ts.isBlock(value.body))
|
|
174
|
-
return;
|
|
175
|
-
const waiting = value.parameters[1];
|
|
176
|
-
if (!waiting || !ts.isIdentifier(waiting.name) || waiting.initializer || waiting.dotDotDotToken)
|
|
177
|
-
return;
|
|
178
|
-
const waitingBinding = bindingOf(waiting.name);
|
|
179
|
-
let forwardsLoading = false;
|
|
180
|
-
const visit = (node) => {
|
|
181
|
-
if (ts.isJsxAttribute(node) && ts.isIdentifier(node.name) && node.name.text === 'isLoading'
|
|
182
|
-
&& node.initializer && ts.isJsxExpression(node.initializer)) {
|
|
183
|
-
const flag = unwrap(ts, node.initializer.expression);
|
|
184
|
-
if (flag && ts.isIdentifier(flag) && waitingBinding && bindingOf(flag) === waitingBinding)
|
|
185
|
-
forwardsLoading = true;
|
|
186
|
-
}
|
|
187
|
-
ts.forEachChild(node, visit);
|
|
188
|
-
};
|
|
189
|
-
visit(value.body);
|
|
190
|
-
if (!forwardsLoading)
|
|
191
|
-
return;
|
|
192
|
-
const wiring = attributes.filter(attribute => ts.isJsxSpreadAttribute(attribute) ? dataMapping(ts, attribute.expression, bindingOf)
|
|
193
|
-
: ts.isIdentifier(attribute.name) && ['slot', 'placeholder', 'labels', 'onRetry'].includes(attribute.name.text)
|
|
194
|
-
&& !!attribute.initializer && ts.isJsxExpression(attribute.initializer) && dataMapping(ts, attribute.initializer.expression, bindingOf));
|
|
195
|
-
const hasSlot = wiring.some(attribute => ts.isJsxSpreadAttribute(attribute) || ts.isJsxAttribute(attribute) && ts.isIdentifier(attribute.name) && attribute.name.text === 'slot');
|
|
196
|
-
if (!hasSlot)
|
|
197
|
-
return;
|
|
198
|
-
for (const attribute of wiring)
|
|
199
|
-
excluded.add(attribute);
|
|
200
|
-
}
|
|
201
|
-
/** Canon data prop objects qualify through an imported component binding, never its chosen name or file path. */
|
|
202
|
-
export function canonJsxCloneNodes(ts, sourceFile) {
|
|
203
|
-
const bindingOf = lexicalBindings(ts, sourceFile);
|
|
204
|
-
const excluded = new Set();
|
|
205
|
-
const visit = (node) => {
|
|
206
|
-
if ((ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) && importedComponent(ts, node.tagName, bindingOf)) {
|
|
207
|
-
statusRecipe(ts, node, bindingOf, excluded);
|
|
208
|
-
for (const attribute of node.attributes.properties) {
|
|
209
|
-
if (!ts.isJsxAttribute(attribute) || !attribute.initializer || !ts.isJsxExpression(attribute.initializer))
|
|
210
|
-
continue;
|
|
211
|
-
const value = unwrap(ts, attribute.initializer.expression);
|
|
212
|
-
if (!value || !ts.isObjectLiteralExpression(value))
|
|
213
|
-
continue;
|
|
214
|
-
if (dataMapping(ts, value, bindingOf))
|
|
215
|
-
excluded.add(attribute);
|
|
216
|
-
else
|
|
217
|
-
nestedDataObjects(ts, value, bindingOf, excluded);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
ts.forEachChild(node, visit);
|
|
221
|
-
};
|
|
222
|
-
visit(sourceFile);
|
|
223
|
-
return excluded;
|
|
224
|
-
}
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { builtinModules } from 'node:module';
|
|
2
|
-
import { sourceLocation } from '../../lib/ts-ast.mjs';
|
|
3
|
-
/**
|
|
4
|
-
* R55 `client-reaches-server` (FE_CLIENT_REACHES_SERVER), the repository half of the eslint rule `client-no-server-import`,
|
|
5
|
-
* which sees one hop from a file that carries the directive. The machine walks the resolved import graph from every module
|
|
6
|
-
* whose directive prologue contains `"use client"`, over runtime edges only (a type-only import vanishes at build), and
|
|
7
|
-
* reports any reachable module (the entry itself included) that imports a server-only surface:
|
|
8
|
-
*
|
|
9
|
-
* - `server-only`, `next/headers`, `next/server`, `next-intl/server`;
|
|
10
|
-
* - a Node built-in: `node:*` and every bare name of `module.builtinModules` (`fs`, `path`, `crypto`, ...).
|
|
11
|
-
*
|
|
12
|
-
* The finding sits on the client entry, names the shortest import chain from it to the offending module (multi-hop) and the
|
|
13
|
-
* specifier. Only imports that reach the client bundle count: `import type`, an import whose every named specifier is
|
|
14
|
-
* `type`, and `export type` re-exports are skipped.
|
|
15
|
-
*/
|
|
16
|
-
export const CLIENT_REACHES_SERVER_RULE_IDS = ['FE_CLIENT_REACHES_SERVER'];
|
|
17
|
-
const RULE = 'FE_CLIENT_REACHES_SERVER';
|
|
18
|
-
const SERVER_MODULES = new Set(['server-only', 'next/headers', 'next/server', 'next-intl/server']);
|
|
19
|
-
const NODE_BUILTINS = new Set(builtinModules.map(name => name.replace(/^node:/u, '').split('/')[0]));
|
|
20
|
-
const isServerSpecifier = (specifier) => SERVER_MODULES.has(specifier) || specifier.startsWith('node:') || NODE_BUILTINS.has(specifier.split('/')[0]);
|
|
21
|
-
const isClientEntry = (ts, file) => {
|
|
22
|
-
for (const statement of file.sourceFile.statements) {
|
|
23
|
-
if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression))
|
|
24
|
-
return false;
|
|
25
|
-
if (statement.expression.text === 'use client')
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
return false;
|
|
29
|
-
};
|
|
30
|
-
const reachesBundle = (ts, declaration) => {
|
|
31
|
-
if (ts.isImportDeclaration(declaration)) {
|
|
32
|
-
const clause = declaration.importClause;
|
|
33
|
-
if (!clause)
|
|
34
|
-
return true;
|
|
35
|
-
if (clause.isTypeOnly)
|
|
36
|
-
return false;
|
|
37
|
-
if (!clause.name && clause.namedBindings && ts.isNamedImports(clause.namedBindings)) {
|
|
38
|
-
const elements = clause.namedBindings.elements;
|
|
39
|
-
return elements.length === 0 || !elements.every(element => element.isTypeOnly);
|
|
40
|
-
}
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
return !declaration.isTypeOnly;
|
|
44
|
-
};
|
|
45
|
-
const serverImports = (ts, file) => {
|
|
46
|
-
const found = [];
|
|
47
|
-
const visit = (node) => {
|
|
48
|
-
let specifier = null;
|
|
49
|
-
if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && node.moduleSpecifier && reachesBundle(ts, node))
|
|
50
|
-
specifier = node.moduleSpecifier;
|
|
51
|
-
else if (ts.isCallExpression(node) && (node.expression.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(node.expression) && node.expression.text === 'require')))
|
|
52
|
-
specifier = node.arguments[0];
|
|
53
|
-
if (specifier && ts.isStringLiteralLike(specifier) && isServerSpecifier(specifier.text))
|
|
54
|
-
found.push({ specifier: specifier.text, node });
|
|
55
|
-
ts.forEachChild(node, visit);
|
|
56
|
-
};
|
|
57
|
-
visit(file.sourceFile);
|
|
58
|
-
return found;
|
|
59
|
-
};
|
|
60
|
-
const scanClientEntry = (entry, { graph, runtime, violations }) => {
|
|
61
|
-
const seen = new Map([[entry.rel, { chain: [entry.rel], first: null }]]);
|
|
62
|
-
const queue = [entry.rel];
|
|
63
|
-
for (const rel of queue) {
|
|
64
|
-
const state = seen.get(rel); // every queued module was recorded in `seen` when it was queued
|
|
65
|
-
for (const hit of runtime.serverOf(rel)) {
|
|
66
|
-
const at = state.first ?? sourceLocation(graph.files.get(rel).sourceFile, hit.node);
|
|
67
|
-
violations.push({
|
|
68
|
-
ruleId: RULE, path: entry.rel, line: at.line, column: at.column, clientEntry: entry.rel, importer: rel, specifier: hit.specifier, dependencyChain: state.chain,
|
|
69
|
-
message: `${entry.rel} is a client module ("use client") and reaches ${hit.specifier} through ${[...state.chain, hit.specifier].join(' -> ')}; a server-only surface never enters the client bundle. Read on the server and pass the value down, or move the import out of the client graph.`,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
for (const edge of runtime.edges.get(rel) ?? []) {
|
|
73
|
-
if (seen.has(edge.to))
|
|
74
|
-
continue;
|
|
75
|
-
seen.set(edge.to, { chain: [...state.chain, edge.to], first: state.first ?? { line: edge.line, column: edge.column } });
|
|
76
|
-
queue.push(edge.to);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
export function checkClientReachesServer({ graph, context }) {
|
|
81
|
-
const ts = context.ts;
|
|
82
|
-
const runtime = new Map();
|
|
83
|
-
for (const edge of graph.edges) {
|
|
84
|
-
if (!edge.runtime)
|
|
85
|
-
continue;
|
|
86
|
-
if (!runtime.has(edge.from))
|
|
87
|
-
runtime.set(edge.from, []);
|
|
88
|
-
runtime.get(edge.from)?.push(edge);
|
|
89
|
-
}
|
|
90
|
-
const serverByFile = new Map();
|
|
91
|
-
const serverOf = (rel) => {
|
|
92
|
-
if (!serverByFile.has(rel))
|
|
93
|
-
serverByFile.set(rel, serverImports(ts, graph.files.get(rel)));
|
|
94
|
-
return serverByFile.get(rel);
|
|
95
|
-
};
|
|
96
|
-
const violations = [];
|
|
97
|
-
const entries = [...graph.files.values()].filter(file => isClientEntry(ts, file)).sort((a, b) => a.rel.localeCompare(b.rel));
|
|
98
|
-
const scanRuntime = { ts, graph, edges: runtime, serverOf, violations };
|
|
99
|
-
for (const entry of entries)
|
|
100
|
-
scanClientEntry(entry, { graph, runtime: scanRuntime, violations });
|
|
101
|
-
return { violations, coverage: { status: 'checked', clientEntries: entries.length } };
|
|
102
|
-
}
|
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { byCodeUnit } from '../../lib/list.mjs';
|
|
2
|
-
import { canonCloneNodes } from './canon-clones.mjs';
|
|
3
|
-
export const CLONE_RULE_IDS = ['HFS_DUPLICATE_CODE'];
|
|
4
|
-
const MAX_VIOLATIONS = 200;
|
|
5
|
-
const DOOR_ROLES = new Set(['controller', 'resolver', 'gateway', 'consumer', 'processor', 'cli']);
|
|
6
|
-
const APP_ENTRY = 'main.ts';
|
|
7
|
-
const ID = -1;
|
|
8
|
-
const LIT = -2;
|
|
9
|
-
const entryAt = (entries, index) => entries[index];
|
|
10
|
-
const literalKindSet = (SyntaxKind) => new Set([SyntaxKind.StringLiteral, SyntaxKind.NumericLiteral, SyntaxKind.BigIntLiteral, SyntaxKind.RegularExpressionLiteral,
|
|
11
|
-
SyntaxKind.NoSubstitutionTemplateLiteral, SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail, SyntaxKind.JsxText]);
|
|
12
|
-
function lineOfPosition(lineStarts, position) {
|
|
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;
|
|
23
|
-
}
|
|
24
|
-
/** True for the nodes that carry no token: documentation comments and whitespace-only JSX text. */
|
|
25
|
-
function isTriviaNode(SyntaxKind, node) {
|
|
26
|
-
const kind = node.kind;
|
|
27
|
-
if (kind >= SyntaxKind.FirstJSDocNode && kind <= SyntaxKind.LastJSDocNode)
|
|
28
|
-
return true;
|
|
29
|
-
return kind === SyntaxKind.JsxText && Boolean(node.containsOnlyTriviaWhiteSpaces);
|
|
30
|
-
}
|
|
31
|
-
function normalizedKindOf(SyntaxKind, literalKinds, kind) {
|
|
32
|
-
if (kind === SyntaxKind.Identifier || kind === SyntaxKind.PrivateIdentifier)
|
|
33
|
-
return ID;
|
|
34
|
-
return literalKinds.has(kind) ? LIT : kind;
|
|
35
|
-
}
|
|
36
|
-
function visitToken(walk, node, inType) {
|
|
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); });
|
|
52
|
-
}
|
|
53
|
-
/** Leading import and export-from statements repeat legitimately and are not tokenised. */
|
|
54
|
-
const isDroppedStatement = (SyntaxKind, statement) => statement.kind === SyntaxKind.ImportDeclaration || statement.kind === SyntaxKind.ImportEqualsDeclaration
|
|
55
|
-
|| (statement.kind === SyntaxKind.ExportDeclaration && Boolean(statement.moduleSpecifier));
|
|
56
|
-
export function tokenize(ts, sourceFile, { excluded = null } = {}) {
|
|
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) };
|
|
65
|
-
}
|
|
66
|
-
/** The app a file belongs to, by the slot binding of the manifest; null for a file of no app. */
|
|
67
|
-
const appOfFile = (resolver, rel) => resolver.classifyPath(rel).bindings?.app ?? null;
|
|
68
|
-
/** True for the app entry and the transport doors: their shape is fixed by the convention and their body cannot hold logic. */
|
|
69
|
-
function uniformByDesign(resolver, rel) {
|
|
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');
|
|
81
|
-
}
|
|
82
|
-
function homeText(profile, sameOwner, crossApp) {
|
|
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";
|
|
90
|
-
}
|
|
91
|
-
const cloneBuckets = (entries, N, T) => {
|
|
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;
|
|
109
|
-
};
|
|
110
|
-
function signedInt32(value) {
|
|
111
|
-
return Math.imul(value, 1);
|
|
112
|
-
}
|
|
113
|
-
const prefixHashes = (kinds, B1, B2) => {
|
|
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 };
|
|
122
|
-
};
|
|
123
|
-
const windowKey = (pre, pow, start, end) => signedInt32(pre[end] - Math.imul(pre[start], pow[end - start]));
|
|
124
|
-
const addToBucket = (buckets, key, item) => {
|
|
125
|
-
const list = buckets.get(key);
|
|
126
|
-
if (list)
|
|
127
|
-
list.push(item);
|
|
128
|
-
else
|
|
129
|
-
buckets.set(key, [item]);
|
|
130
|
-
};
|
|
131
|
-
const addFileWindows = (buckets, file, fileIndex, N, T, hashing) => {
|
|
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
|
-
}
|
|
153
|
-
};
|
|
154
|
-
const sameTokens = (entries, a, b) => {
|
|
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;
|
|
161
|
-
};
|
|
162
|
-
/** The first other occurrence is the partner; overlapping windows of one file are one occurrence, not a copy. */
|
|
163
|
-
const partnerOf = (list, item) => list.find((other) => other !== item && (other[0] !== item[0] || other[1] >= item[2] || item[1] >= other[2]));
|
|
164
|
-
const orderedPair = (entries, item, partner) => {
|
|
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];
|
|
168
|
-
};
|
|
169
|
-
const addHit = (hits, a, b) => {
|
|
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]]]);
|
|
176
|
-
};
|
|
177
|
-
const cloneHits = (entries, buckets) => {
|
|
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
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return hits;
|
|
191
|
-
};
|
|
192
|
-
const blockShape = (file, start, end) => {
|
|
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 };
|
|
202
|
-
};
|
|
203
|
-
const appendCloneBlock = (blocks, fileA, fileB, start, end, diag, N) => {
|
|
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] } });
|
|
211
|
-
};
|
|
212
|
-
const cloneBlocks = (entries, hits, N) => {
|
|
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);
|
|
232
|
-
}
|
|
233
|
-
return blocks;
|
|
234
|
-
};
|
|
235
|
-
const differentApps = (resolver, a, b) => {
|
|
236
|
-
const left = appOfFile(resolver, a);
|
|
237
|
-
const right = appOfFile(resolver, b);
|
|
238
|
-
return left !== null && right !== null && left !== right;
|
|
239
|
-
};
|
|
240
|
-
const cloneViolations = (blocks, resolver, profile, N, T) => {
|
|
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
|
-
}));
|
|
246
|
-
};
|
|
247
|
-
const cloneCoverage = (entries, blocks, N, T) => {
|
|
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 } : {}) };
|
|
257
|
-
};
|
|
258
|
-
/**
|
|
259
|
-
* The threshold of the run. The app root (profile app) answers `ruleParams()` with null: its rule parameters are its sides', and
|
|
260
|
-
* duplicateBlock is the one value they share (ruleParams.common), so the root reads it from a side. A run on the app root
|
|
261
|
-
* (the root has its own tsconfig) threw "Cannot destructure property 'lines'" here before.
|
|
262
|
-
*/
|
|
263
|
-
function duplicateBlockOf(resolver) {
|
|
264
|
-
const own = resolver.ruleParams()?.duplicateBlock;
|
|
265
|
-
const block = own ?? Object.values(resolver.sides ?? {}).map(side => side.ruleParams().duplicateBlock).find(found => found !== undefined);
|
|
266
|
-
// reason: the rule-params validators require duplicateBlock on every profile, so a side that runs this check carries it
|
|
267
|
-
return block;
|
|
268
|
-
}
|
|
269
|
-
export function checkClones({ context, graph }) {
|
|
270
|
-
const { lines: N, tokens: T } = duplicateBlockOf(graph.resolver);
|
|
271
|
-
const ts = context.ts ?? context.loaded.ts;
|
|
272
|
-
const entries = [];
|
|
273
|
-
for (const rel of [...graph.files.keys()].sort(byCodeUnit)) {
|
|
274
|
-
if (/\.d\.[cm]?tsx?$/.test(rel) || uniformByDesign(graph.resolver, rel))
|
|
275
|
-
continue;
|
|
276
|
-
const unit = graph.unit(rel);
|
|
277
|
-
if (!unit)
|
|
278
|
-
continue;
|
|
279
|
-
const sourceFile = graph.files.get(rel).sourceFile;
|
|
280
|
-
entries.push({ rel, unit, ...tokenize(ts, sourceFile, { excluded: canonCloneNodes(ts, sourceFile, graph.profile) }) });
|
|
281
|
-
}
|
|
282
|
-
const buckets = cloneBuckets(entries, N, T);
|
|
283
|
-
const blocks = cloneBlocks(entries, cloneHits(entries, buckets), N);
|
|
284
|
-
const profile = graph.profile;
|
|
285
|
-
const violations = cloneViolations(blocks, graph.resolver, profile, N, T);
|
|
286
|
-
return { violations, coverage: cloneCoverage(entries, blocks, N, T) };
|
|
287
|
-
}
|