@starci/eslint-canon-be 3.18.4 → 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 +4 -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 -720
- 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,173 +0,0 @@
|
|
|
1
|
-
// Import and namespace consumption analysis shared by dead-export edges.
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
/** Disk/staged test consumers have no production checker; bind their local names without resolving dependencies. */
|
|
4
|
-
const namespaceChecker = (context, sourceFile, namespace) => {
|
|
5
|
-
const cached = context.namespaceCheckers.get(sourceFile);
|
|
6
|
-
if (cached)
|
|
7
|
-
return cached;
|
|
8
|
-
const { ts } = context;
|
|
9
|
-
let checker = context.checkerFor?.(sourceFile.fileName);
|
|
10
|
-
if (!checker?.getSymbolAtLocation(namespace.name)) {
|
|
11
|
-
const options = { noResolve: true, noLib: true, types: [], allowJs: true, target: ts.ScriptTarget.Latest };
|
|
12
|
-
const host = ts.createCompilerHost(options);
|
|
13
|
-
const filename = path.resolve(sourceFile.fileName);
|
|
14
|
-
host.getSourceFile = file => path.resolve(file) === filename ? sourceFile : undefined;
|
|
15
|
-
host.fileExists = file => path.resolve(file) === filename;
|
|
16
|
-
host.readFile = file => path.resolve(file) === filename ? sourceFile.text : undefined;
|
|
17
|
-
checker = ts.createProgram([filename], options, host).getTypeChecker();
|
|
18
|
-
}
|
|
19
|
-
context.namespaceCheckers.set(sourceFile, checker);
|
|
20
|
-
return checker;
|
|
21
|
-
};
|
|
22
|
-
const transparentExpression = (ts, node) => !!node && (ts.isParenthesizedExpression(node) || ts.isAsExpression(node)
|
|
23
|
-
|| ts.isTypeAssertionExpression(node) || ts.isNonNullExpression(node) || ts.isSatisfiesExpression(node));
|
|
24
|
-
const literalMember = (ts, input) => {
|
|
25
|
-
let node = input;
|
|
26
|
-
while (transparentExpression(ts, node))
|
|
27
|
-
node = node.expression;
|
|
28
|
-
return node && (ts.isStringLiteralLike(node) || ts.isNumericLiteral(node)) ? node.text : null;
|
|
29
|
-
};
|
|
30
|
-
const destructuredMember = (ts, name) => {
|
|
31
|
-
if (ts.isIdentifier(name))
|
|
32
|
-
return name.text;
|
|
33
|
-
if (ts.isComputedPropertyName(name))
|
|
34
|
-
return literalMember(ts, name.expression);
|
|
35
|
-
return literalMember(ts, name);
|
|
36
|
-
};
|
|
37
|
-
/** A destructuring alias consumes the property's name, while a rest/computed binding may consume every export. */
|
|
38
|
-
const destructuredBindings = (ts, pattern) => {
|
|
39
|
-
const names = [];
|
|
40
|
-
const elements = ts.isObjectBindingPattern(pattern) ? pattern.elements : pattern.properties;
|
|
41
|
-
for (const element of elements) {
|
|
42
|
-
if (ts.isSpreadAssignment(element) || (ts.isBindingElement(element) && element.dotDotDotToken))
|
|
43
|
-
return { all: true, pairs: [] };
|
|
44
|
-
const name = destructuredMember(ts, ts.isBindingElement(element) ? element.propertyName ?? element.name : element.name);
|
|
45
|
-
if (name === null)
|
|
46
|
-
return { all: true, pairs: [] };
|
|
47
|
-
names.push(name);
|
|
48
|
-
}
|
|
49
|
-
return { all: false, pairs: names.map(imported => ({ imported, exported: null })) };
|
|
50
|
-
};
|
|
51
|
-
const namespaceReference = (ts, checker, symbol, node) => {
|
|
52
|
-
if (checker.getSymbolAtLocation(node) === symbol)
|
|
53
|
-
return true;
|
|
54
|
-
const parent = node.parent;
|
|
55
|
-
if (ts.isShorthandPropertyAssignment(parent) && parent.name === node)
|
|
56
|
-
return checker.getShorthandAssignmentValueSymbol(parent) === symbol;
|
|
57
|
-
return ts.isExportSpecifier(parent) && node === (parent.propertyName ?? parent.name)
|
|
58
|
-
&& checker.getExportSpecifierLocalTargetSymbol(parent) === symbol;
|
|
59
|
-
};
|
|
60
|
-
const unwrappedNamespace = (ts, input) => {
|
|
61
|
-
let node = input;
|
|
62
|
-
while (transparentExpression(ts, node.parent))
|
|
63
|
-
node = node.parent;
|
|
64
|
-
return node;
|
|
65
|
-
};
|
|
66
|
-
const indexedNamespaceMember = (ts, node) => {
|
|
67
|
-
if (!ts.isTypeQueryNode(node.parent) || node.parent.exprName !== node)
|
|
68
|
-
return null;
|
|
69
|
-
let query = node.parent;
|
|
70
|
-
while (ts.isParenthesizedTypeNode(query.parent))
|
|
71
|
-
query = query.parent;
|
|
72
|
-
const indexed = query.parent;
|
|
73
|
-
return ts.isIndexedAccessTypeNode(indexed) && indexed.objectType === query && ts.isLiteralTypeNode(indexed.indexType)
|
|
74
|
-
? literalMember(ts, indexed.indexType.literal) : null;
|
|
75
|
-
};
|
|
76
|
-
/** The first member is the entry export even in a chain such as namespace.Enum.Member or namespace.Type.Inner. */
|
|
77
|
-
const namespaceUse = (ts, reference) => {
|
|
78
|
-
const node = unwrappedNamespace(ts, reference);
|
|
79
|
-
const parent = node.parent;
|
|
80
|
-
let name = null;
|
|
81
|
-
if (ts.isPropertyAccessExpression(parent) && parent.expression === node)
|
|
82
|
-
name = parent.name.text;
|
|
83
|
-
else if (ts.isQualifiedName(parent) && parent.left === node)
|
|
84
|
-
name = parent.right.text;
|
|
85
|
-
else if (ts.isElementAccessExpression(parent) && parent.expression === node)
|
|
86
|
-
name = literalMember(ts, parent.argumentExpression);
|
|
87
|
-
else if (ts.isVariableDeclaration(parent) && parent.initializer === node && ts.isObjectBindingPattern(parent.name))
|
|
88
|
-
return destructuredBindings(ts, parent.name);
|
|
89
|
-
else if (ts.isBinaryExpression(parent) && parent.right === node && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken
|
|
90
|
-
&& ts.isObjectLiteralExpression(parent.left))
|
|
91
|
-
return destructuredBindings(ts, parent.left);
|
|
92
|
-
else
|
|
93
|
-
name = indexedNamespaceMember(ts, node);
|
|
94
|
-
return name === null ? { all: true, pairs: [] } : { all: false, pairs: [{ imported: name, exported: null }] };
|
|
95
|
-
};
|
|
96
|
-
const namespaceBindings = (context, declaration, namespace) => {
|
|
97
|
-
const cached = context.namespaceUses.get(namespace);
|
|
98
|
-
if (cached)
|
|
99
|
-
return cached;
|
|
100
|
-
const { ts } = context;
|
|
101
|
-
const sourceFile = declaration.getSourceFile();
|
|
102
|
-
const checker = namespaceChecker(context, sourceFile, namespace);
|
|
103
|
-
const symbol = checker.getSymbolAtLocation(namespace.name);
|
|
104
|
-
const result = { all: false, pairs: [] };
|
|
105
|
-
const visit = (node) => {
|
|
106
|
-
if (ts.isJSDocSeeTag(node) || ts.isJSDocLink(node) || ts.isJSDocLinkCode(node) || ts.isJSDocLinkPlain(node))
|
|
107
|
-
return;
|
|
108
|
-
if (ts.isIdentifier(node) && node !== namespace.name && namespaceReference(ts, checker, symbol, node)) {
|
|
109
|
-
const used = namespaceUse(ts, node);
|
|
110
|
-
result.all ||= used.all;
|
|
111
|
-
result.pairs.push(...used.pairs);
|
|
112
|
-
}
|
|
113
|
-
if (!result.all) {
|
|
114
|
-
ts.forEachChild(node, visit);
|
|
115
|
-
// `jsDoc` is a compiler-internal field of the nodes that carry documentation comments.
|
|
116
|
-
for (const doc of node.jsDoc ?? [])
|
|
117
|
-
visit(doc);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
// An unavailable binding must never turn an unknown consumer into a false unused-export finding.
|
|
121
|
-
if (symbol)
|
|
122
|
-
visit(sourceFile);
|
|
123
|
-
else
|
|
124
|
-
result.all = true;
|
|
125
|
-
context.namespaceUses.set(namespace, result);
|
|
126
|
-
return result;
|
|
127
|
-
};
|
|
128
|
-
const importBindings = (context, declaration) => {
|
|
129
|
-
const { ts } = context;
|
|
130
|
-
const clause = declaration.importClause;
|
|
131
|
-
if (!clause)
|
|
132
|
-
return { all: false, pairs: [] }; // side-effect import: uses nothing
|
|
133
|
-
const pairs = [];
|
|
134
|
-
if (clause.name)
|
|
135
|
-
pairs.push({ imported: 'default', exported: null });
|
|
136
|
-
const named = clause.namedBindings;
|
|
137
|
-
if (named && named.kind === ts.SyntaxKind.NamespaceImport) {
|
|
138
|
-
const namespace = namespaceBindings(context, declaration, named);
|
|
139
|
-
return { all: namespace.all, pairs: [...pairs, ...namespace.pairs] };
|
|
140
|
-
}
|
|
141
|
-
if (named)
|
|
142
|
-
for (const element of named.elements)
|
|
143
|
-
pairs.push({ imported: (element.propertyName ?? element.name).text, exported: null });
|
|
144
|
-
return { all: false, pairs };
|
|
145
|
-
};
|
|
146
|
-
const exportBindings = (ts, declaration) => {
|
|
147
|
-
const clause = declaration.exportClause;
|
|
148
|
-
if (!clause || clause.kind === ts.SyntaxKind.NamespaceExport)
|
|
149
|
-
return { all: true, pairs: [] };
|
|
150
|
-
return { all: false, pairs: clause.elements.map(element => ({ imported: (element.propertyName ?? element.name).text, exported: element.name.text })) };
|
|
151
|
-
};
|
|
152
|
-
const importTypeBindings = (ts, declaration) => {
|
|
153
|
-
let qualifier = declaration.qualifier;
|
|
154
|
-
if (!qualifier)
|
|
155
|
-
return { all: true, pairs: [] };
|
|
156
|
-
while (qualifier.kind === ts.SyntaxKind.QualifiedName)
|
|
157
|
-
qualifier = qualifier.left;
|
|
158
|
-
return { all: false, pairs: [{ imported: qualifier.text, exported: null }] };
|
|
159
|
-
};
|
|
160
|
-
/** Names an import/export declaration takes from its target: {all} or {pairs: [{imported, exported}]}. */
|
|
161
|
-
export function bindingsOf(context, declaration) {
|
|
162
|
-
const { ts } = context;
|
|
163
|
-
if (!declaration)
|
|
164
|
-
return { all: true, pairs: [] };
|
|
165
|
-
if (ts.isImportDeclaration(declaration))
|
|
166
|
-
return importBindings(context, declaration);
|
|
167
|
-
if (ts.isExportDeclaration(declaration))
|
|
168
|
-
return exportBindings(ts, declaration);
|
|
169
|
-
if (ts.isImportTypeNode(declaration))
|
|
170
|
-
return importTypeBindings(ts, declaration);
|
|
171
|
-
// ImportEqualsDeclaration, dynamic import(), require(): the whole module is taken.
|
|
172
|
-
return { all: true, pairs: [] };
|
|
173
|
-
}
|
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HFS check 4, dead code (knowledge/hfs/rules.yaml R25, both profiles, errors in every gate):
|
|
3
|
-
* HFS_UNUSED_EXPORT an export of an owner's public entry that no production file outside the owner imports is dead.
|
|
4
|
-
* HFS_UNUSED_FILE a production source file that no root reaches is dead. Roots: the files an app slot requires
|
|
5
|
-
* (main.ts, app.module.ts), every route file (slot tier `route`), the public entry of every package,
|
|
6
|
-
* the declared API app's registered operation contract consumed by app emit,
|
|
7
|
-
* and a source file a framework config of the app names by a relative string literal
|
|
8
|
-
* (next.config.ts pointing next-intl at its request config). Specs and tests are not in the graph,
|
|
9
|
-
* so a file only a spec imports is dead, on purpose. Type-only imports reach a file.
|
|
10
|
-
*
|
|
11
|
-
* Owner = every graph.ownerRoots unit whose tier is not `app` and whose public entry file is in the graph (index.ts or
|
|
12
|
-
* index.tsx, a package's src/index.ts). The `entries` its slot declares (slot data: a second public entry file of the owner) are further
|
|
13
|
-
* public entries of the same owner, judged like the entry, with one addition: a name another declared entry of the owner
|
|
14
|
-
* imports (not re-exports) is used, because it is part of that entry's public signature (an operation file
|
|
15
|
-
* `graphql/queries/query-<x>.ts` typing its request and answer with `graphql/queries/types/<x>.ts`). A barrel or any other
|
|
16
|
-
* non-entry file of the owner still does not count. The exported names of an entry are its named exports, `export { a as b }`,
|
|
17
|
-
* `export { x } from`, declarations with `export`, and `default` for `export default`. `export *` is refused by owners.mjs
|
|
18
|
-
* and contributes no name here.
|
|
19
|
-
*
|
|
20
|
-
* A name is used when a production file outside the owner imports it from the entry (type-only imports count). A
|
|
21
|
-
* namespace import uses its statically accessed members; a computed read or an escape of the namespace uses every name.
|
|
22
|
-
* `import x = require`, a dynamic import()/require(), an `export *` or `export * as` of the entry make every name used.
|
|
23
|
-
* A file outside the owner that re-exports the name (`export { n as m } from`) uses it when `m` is itself
|
|
24
|
-
* used by the importers of that file; a re-exporting file nobody imports is a framework entry (a route file) and uses it.
|
|
25
|
-
* Specs and tests are not in the graph, so an export used only by a spec is dead, on purpose, with these exceptions (unit test
|
|
26
|
-
* standard): the unit spec of every unit role (ruleParams.be.unitRoles: `<name>.service.spec.ts`, `<name>.cli.spec.ts`) and of every logic role
|
|
27
|
-
* (ruleParams.be.logicRoles: `<name>.policy.spec.ts`, `<name>.client.spec.ts`, ...), a
|
|
28
|
-
* unit spec of a webhook, gateway, subscription or SSE controller door (`.webhook.spec.ts`, `.gateway.spec.ts`, `.subscription.spec.ts`, `.controller.spec.ts`: a door has no service of its own), a
|
|
29
|
-
* `*.builder.ts` under src/tests/fixtures/builders, and a file of the test world (slots be.tests.world and be.tests.world.kit, which
|
|
30
|
-
* open and migrate the real databases of the e2e run) read from disk or staged in the git index count as consumers, because a spec can only provide an
|
|
31
|
-
* Inject*() token or a param type the entry exports, and the world wires the same capabilities the apps do. An integration
|
|
32
|
-
* spec (slot be.tests.integration, `src/tests/integration/<capability>/*.integration-spec.ts`) counts as a consumer of exactly one
|
|
33
|
-
* owner: the integration (slot be.integrations, `src/modules/integrations/<provider>/`) whose provider folder is its capability
|
|
34
|
-
* folder, because R112 makes that spec register the integration module and reference its ErrorCode enum. Specs of any other kind,
|
|
35
|
-
* an integration spec importing another owner, and e2e specs still do not count. A consumer inside the owner is
|
|
36
|
-
* skipped like any inside consumer.
|
|
37
|
-
*/
|
|
38
|
-
import fs from 'node:fs';
|
|
39
|
-
import { canonical } from './config.mjs';
|
|
40
|
-
import path from 'node:path';
|
|
41
|
-
import { treeOf } from './required-files.mjs';
|
|
42
|
-
import { globExpression } from '../../lib/glob.mjs';
|
|
43
|
-
import { stagedConsumerSources } from './staged-consumers.mjs';
|
|
44
|
-
import { bindingsOf } from './dead-exports-bindings.mjs';
|
|
45
|
-
import { operationContractRoots } from './operation-contract-roots.mjs';
|
|
46
|
-
export const DEAD_EXPORT_RULE_IDS = ['HFS_UNUSED_EXPORT', 'HFS_UNUSED_FILE'];
|
|
47
|
-
const PACKAGE_ENTRIES = ['src/index.ts', 'src/index.tsx', 'index.ts', 'index.tsx'];
|
|
48
|
-
const OWNER_ENTRIES = ['index.ts', 'index.tsx'];
|
|
49
|
-
const CHAIN_DEPTH = 6;
|
|
50
|
-
/** Tiers whose files are not production source that an app serves: judged by no dead-file rule. */
|
|
51
|
-
const UNJUDGED_TIERS = new Set(['none', 'e2e', 'fixtures']);
|
|
52
|
-
const CONFIG_EXTENSIONS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '/index.ts', '/index.tsx', '/index.js'];
|
|
53
|
-
const CONFIG_STRING = /['"`](\.{1,2}\/[^'"`\s]+)['"`]/g;
|
|
54
|
-
/** The public entry file (repository-relative) of an owner unit, or null when the owner has none in the graph. */
|
|
55
|
-
export function entryOf(graph, config, key, owner) {
|
|
56
|
-
const declared = config.owners?.find(item => item.id === key)?.entry;
|
|
57
|
-
if (declared && graph.files.has(declared))
|
|
58
|
-
return declared;
|
|
59
|
-
for (const candidate of owner.tier === 'package' ? PACKAGE_ENTRIES : OWNER_ENTRIES) {
|
|
60
|
-
const rel = owner.root ? `${owner.root}/${candidate}` : candidate;
|
|
61
|
-
if (graph.files.has(rel))
|
|
62
|
-
return rel;
|
|
63
|
-
}
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
/** The additional public entry files (repository-relative, in the graph) the slot manifest declares for the owner (slot field `entries`, on the slot that holds its entry file). */
|
|
67
|
-
function extraEntries(graph, entry) {
|
|
68
|
-
const found = graph.resolver.classifyPath(entry);
|
|
69
|
-
const names = found.slot ? graph.resolver.slot(found.slot)?.entries ?? [] : [];
|
|
70
|
-
// an entry holding `*` (one file per operation) names every graph file it matches
|
|
71
|
-
const matches = (name) => [...graph.files.keys()].filter(rel => globExpression(`${found.root}/${name}`).test(rel));
|
|
72
|
-
return names.flatMap((name) => (name.includes('*') ? matches(name) : [`${found.root}/${name}`].filter(rel => graph.files.has(rel))));
|
|
73
|
-
}
|
|
74
|
-
const addExportName = (found, sourceFile, name, node) => {
|
|
75
|
-
if (found.has(name))
|
|
76
|
-
return;
|
|
77
|
-
found.set(name, sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1);
|
|
78
|
-
};
|
|
79
|
-
const addBindingNames = (ts, found, sourceFile, name, node) => {
|
|
80
|
-
if (name.kind === ts.SyntaxKind.Identifier)
|
|
81
|
-
addExportName(found, sourceFile, name.text, node);
|
|
82
|
-
else
|
|
83
|
-
for (const element of name.elements)
|
|
84
|
-
if (element.kind === ts.SyntaxKind.BindingElement)
|
|
85
|
-
addBindingNames(ts, found, sourceFile, element.name, node);
|
|
86
|
-
};
|
|
87
|
-
const addExportDeclarationNames = (ts, statement, add) => {
|
|
88
|
-
if (statement.exportClause?.kind === ts.SyntaxKind.NamedExports) {
|
|
89
|
-
for (const element of statement.exportClause.elements)
|
|
90
|
-
add(element.name.text, element);
|
|
91
|
-
}
|
|
92
|
-
else if (statement.exportClause?.kind === ts.SyntaxKind.NamespaceExport)
|
|
93
|
-
add(statement.exportClause.name.text, statement);
|
|
94
|
-
};
|
|
95
|
-
const addStatementNames = (ts, found, sourceFile, statement) => {
|
|
96
|
-
const kind = ts.SyntaxKind;
|
|
97
|
-
const add = (name, node) => addExportName(found, sourceFile, name, node);
|
|
98
|
-
if (ts.isExportDeclaration(statement))
|
|
99
|
-
return addExportDeclarationNames(ts, statement, add);
|
|
100
|
-
if (ts.isExportAssignment(statement))
|
|
101
|
-
return add('default', statement);
|
|
102
|
-
const declaring = statement; // reason: only declaration statements carry modifiers and a name; both are read as optional
|
|
103
|
-
const modifiers = declaring.modifiers ?? [];
|
|
104
|
-
if (!modifiers.some(modifier => modifier.kind === kind.ExportKeyword))
|
|
105
|
-
return undefined;
|
|
106
|
-
if (modifiers.some(modifier => modifier.kind === kind.DefaultKeyword))
|
|
107
|
-
return add('default', statement);
|
|
108
|
-
if (ts.isVariableStatement(statement)) {
|
|
109
|
-
for (const declaration of statement.declarationList.declarations)
|
|
110
|
-
addBindingNames(ts, found, sourceFile, declaration.name, declaration);
|
|
111
|
-
return undefined;
|
|
112
|
-
}
|
|
113
|
-
return declaring.name && ts.isIdentifier(declaring.name) ? add(declaring.name.text, statement) : undefined;
|
|
114
|
-
};
|
|
115
|
-
/** The exported names of an entry source file, each with the line of its declaration. */
|
|
116
|
-
export function exportedNames(ts, sourceFile) {
|
|
117
|
-
const found = new Map();
|
|
118
|
-
for (const statement of sourceFile.statements)
|
|
119
|
-
addStatementNames(ts, found, sourceFile, statement);
|
|
120
|
-
return found;
|
|
121
|
-
}
|
|
122
|
-
const withoutSlash = (value) => value.replace(/\/$/, '');
|
|
123
|
-
const addConfigTextRoots = (graph, directory, text, roots) => {
|
|
124
|
-
for (const match of text.matchAll(CONFIG_STRING)) {
|
|
125
|
-
const target = path.posix.join(directory, match[1] ?? '');
|
|
126
|
-
const hit = CONFIG_EXTENSIONS.map(extension => `${target}${extension}`).find(candidate => graph.files.has(candidate));
|
|
127
|
-
if (hit)
|
|
128
|
-
roots.add(hit);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
const addAppConfigRoots = (graph, config, app, roots) => {
|
|
132
|
-
const directory = `apps/${app.name}`;
|
|
133
|
-
let names = [];
|
|
134
|
-
try {
|
|
135
|
-
names = fs.readdirSync(path.join(config.root, ...directory.split('/')));
|
|
136
|
-
}
|
|
137
|
-
catch {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
for (const name of names) {
|
|
141
|
-
if (graph.resolver.classifyPath(`${directory}/${name}`).slot !== 'fe.app.next' || !/\.[cm]?[jt]sx?$/.test(name))
|
|
142
|
-
continue;
|
|
143
|
-
let text = '';
|
|
144
|
-
try {
|
|
145
|
-
text = fs.readFileSync(path.join(config.root, ...directory.split('/'), name), 'utf8');
|
|
146
|
-
}
|
|
147
|
-
catch {
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
addConfigTextRoots(graph, directory, text, roots);
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
const addFrameworkConfigRoots = (graph, config, roots) => {
|
|
154
|
-
for (const app of config.apps ?? [])
|
|
155
|
-
addAppConfigRoots(graph, config, app, roots);
|
|
156
|
-
};
|
|
157
|
-
/** Roots are required app files, registered operation contracts, routes, package entries and config-named files. */
|
|
158
|
-
function rootFiles(graph, config, context) {
|
|
159
|
-
const roots = new Set();
|
|
160
|
-
for (const [rel, node] of graph.files) {
|
|
161
|
-
if (node.tier === 'route') {
|
|
162
|
-
roots.add(rel);
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (!node.owner || node.tier !== 'app')
|
|
166
|
-
continue;
|
|
167
|
-
const slot = graph.resolver.slot(node.owner.slot);
|
|
168
|
-
const inside = rel.slice(withoutSlash(node.owner.root).length + 1);
|
|
169
|
-
if ((slot?.requires ?? []).includes(inside))
|
|
170
|
-
roots.add(rel);
|
|
171
|
-
}
|
|
172
|
-
for (const [key, owner] of graph.ownerRoots) {
|
|
173
|
-
if (owner.tier !== 'package')
|
|
174
|
-
continue;
|
|
175
|
-
const entry = entryOf(graph, config, key, owner);
|
|
176
|
-
if (entry)
|
|
177
|
-
roots.add(entry);
|
|
178
|
-
}
|
|
179
|
-
// A framework config of an app (slot fe.app.next) names sources by string, never by import.
|
|
180
|
-
addFrameworkConfigRoots(graph, config, roots);
|
|
181
|
-
for (const entry of operationContractRoots(context, graph, config))
|
|
182
|
-
roots.add(entry);
|
|
183
|
-
return roots;
|
|
184
|
-
}
|
|
185
|
-
const unusedFileViolation = (rel, node) => ({
|
|
186
|
-
ruleId: 'HFS_UNUSED_FILE',
|
|
187
|
-
path: rel, line: 1, column: 1,
|
|
188
|
-
slot: node.slot, owner: node.owner?.root ?? null,
|
|
189
|
-
message: `${rel} is not reached from any root (an app main.ts or app.module.ts, a registered operation contract, a route file, a package entry): nothing consumes it. Delete the file, or import it where it is used (a spec alone does not count).`,
|
|
190
|
-
});
|
|
191
|
-
/** HFS_UNUSED_FILE: production files no root reaches through runtime or type-only imports and re-exports. */
|
|
192
|
-
function deadFiles(graph, config, context) {
|
|
193
|
-
const roots = rootFiles(graph, config, context);
|
|
194
|
-
const forward = edgeIndex(graph.edges, edge => edge.from);
|
|
195
|
-
const reached = new Set(roots);
|
|
196
|
-
const queue = [...roots];
|
|
197
|
-
while (queue.length) {
|
|
198
|
-
const current = queue.shift();
|
|
199
|
-
if (current === undefined)
|
|
200
|
-
break;
|
|
201
|
-
for (const { to } of forward.get(current) ?? [])
|
|
202
|
-
if (!reached.has(to)) {
|
|
203
|
-
reached.add(to);
|
|
204
|
-
queue.push(to);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
const judgedFiles = [...graph.files]
|
|
208
|
-
.sort(([a], [b]) => a.localeCompare(b))
|
|
209
|
-
.filter(([, node]) => !(node.tier === null || UNJUDGED_TIERS.has(node.tier) || node.tier === 'route'));
|
|
210
|
-
const violations = judgedFiles.filter(([rel]) => !reached.has(rel)).map(([rel, node]) => unusedFileViolation(rel, node));
|
|
211
|
-
return { violations, judged: judgedFiles.length, roots: roots.size };
|
|
212
|
-
}
|
|
213
|
-
/** The unit spec of a webhook, gateway, subscription or SSE controller door: a door has no service of its own, so its spec is the only unit spec of its kind. */
|
|
214
|
-
const DOOR_SPEC = /^src\/features\/(?:webhooks|realtime)\/.+\.(?:webhook|gateway|subscription|controller)\.spec\.ts$/u;
|
|
215
|
-
const BUILDER_CONSUMER = /^src\/tests\/fixtures\/builders\/.+\.builder\.ts$/u;
|
|
216
|
-
/** The test world slots (knowledge/hfs/slots.yaml): their files wire the real capabilities of the e2e run. */
|
|
217
|
-
const WORLD_SLOTS = new Set(['be.tests.world', 'be.tests.world.kit']);
|
|
218
|
-
/** Whether `rel` is a test file that counts as a consumer of any owner: a unit role spec, a fixture builder, a world file. */
|
|
219
|
-
function testConsumer(graph, rel) {
|
|
220
|
-
const params = graph.resolver.ruleParams() ?? {};
|
|
221
|
-
const specs = [...(params.unitRoles ?? []).map((role) => role.spec), ...(params.logicRoles ?? []).map((role) => `${role}.spec`)];
|
|
222
|
-
if (/^(?:src|apps)\//u.test(rel) && specs.some(spec => rel.endsWith(`.${spec}.ts`)))
|
|
223
|
-
return true;
|
|
224
|
-
if (DOOR_SPEC.test(rel))
|
|
225
|
-
return true;
|
|
226
|
-
if (BUILDER_CONSUMER.test(rel))
|
|
227
|
-
return true;
|
|
228
|
-
const classified = graph.resolver.classifyPath(rel);
|
|
229
|
-
return classified.slot !== undefined && WORLD_SLOTS.has(classified.slot) && classified.status !== 'forbidden' && /\.tsx?$/u.test(rel);
|
|
230
|
-
}
|
|
231
|
-
/** The slot of integration specs and the slot of the integrations they pair with by folder (knowledge/hfs/slots.yaml). */
|
|
232
|
-
const INTEGRATION_SPEC_SLOT = 'be.tests.integration';
|
|
233
|
-
const INTEGRATION_SLOT = 'be.integrations';
|
|
234
|
-
/** The provider an integration spec pairs with (its `<capability>` folder), or null when `rel` is no integration spec. */
|
|
235
|
-
function integrationSpecProvider(graph, rel) {
|
|
236
|
-
const classified = graph.resolver.classifyPath(rel);
|
|
237
|
-
return classified.slot === INTEGRATION_SPEC_SLOT && classified.status !== 'forbidden' ? classified.bindings?.capability ?? null : null;
|
|
238
|
-
}
|
|
239
|
-
/** Whether `to` belongs to the integration of `provider` (slot be.integrations, its `<provider>` binding). */
|
|
240
|
-
function inIntegrationOf(graph, to, provider) {
|
|
241
|
-
const owner = graph.resolver.ownerOf(to);
|
|
242
|
-
return owner?.slot === INTEGRATION_SLOT && owner.bindings?.provider === provider;
|
|
243
|
-
}
|
|
244
|
-
/** The pseudo edges of one test consumer file: its import/export declarations that resolve to graph files it may reach. */
|
|
245
|
-
function consumerFileEdges({ ts, options, graph, config }, rel, provider, indexedText) {
|
|
246
|
-
const abs = path.join(config.root, ...rel.split('/'));
|
|
247
|
-
let text = indexedText;
|
|
248
|
-
if (text === undefined) {
|
|
249
|
-
try {
|
|
250
|
-
text = fs.readFileSync(abs, 'utf8');
|
|
251
|
-
}
|
|
252
|
-
catch {
|
|
253
|
-
return [];
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
const sourceFile = ts.createSourceFile(abs, text, ts.ScriptTarget.Latest, true);
|
|
257
|
-
const edges = [];
|
|
258
|
-
for (const statement of sourceFile.statements) {
|
|
259
|
-
if (!(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) || !statement.moduleSpecifier || !ts.isStringLiteralLike(statement.moduleSpecifier))
|
|
260
|
-
continue;
|
|
261
|
-
const resolved = ts.resolveModuleName(statement.moduleSpecifier.text, abs, options, ts.sys).resolvedModule?.resolvedFileName;
|
|
262
|
-
const to = resolved ? graph.abs(canonical(resolved)) : null;
|
|
263
|
-
if (to && (provider === null || inIntegrationOf(graph, to, provider)))
|
|
264
|
-
edges.push({ from: rel, to, edge: { declaration: statement }, reexport: false });
|
|
265
|
-
}
|
|
266
|
-
return edges;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Pseudo edges (read from disk and staged index sources, outside the production program) to graph files: from the unit role specs, the fixture builders and
|
|
270
|
-
* the test world files to anything, and from an integration spec only to the integration of its own provider folder.
|
|
271
|
-
*/
|
|
272
|
-
function testConsumerEdges({ context, graph, config }) {
|
|
273
|
-
const resolution = { ts: context.ts, options: context.projects?.[0]?.options ?? {}, graph, config };
|
|
274
|
-
// Live lint keeps tracked disk consumers; staged snapshots add the consumers present in the same commit.
|
|
275
|
-
const staged = stagedConsumerSources(config.root, file => testConsumer(graph, file) || integrationSpecProvider(graph, file) !== null);
|
|
276
|
-
const consumers = [...[...treeOf(config.root).files].map((file) => ({ file })), ...staged]
|
|
277
|
-
.map(source => ({ ...source, any: testConsumer(graph, source.file) }))
|
|
278
|
-
.map(({ file, text, any }) => ({ file, text, provider: any ? null : integrationSpecProvider(graph, file), any }))
|
|
279
|
-
.filter(({ any, provider }) => any || provider !== null)
|
|
280
|
-
.sort((a, b) => a.file.localeCompare(b.file));
|
|
281
|
-
return consumers.flatMap(({ file: rel, text, provider }) => consumerFileEdges(resolution, rel, provider, text));
|
|
282
|
-
}
|
|
283
|
-
/** The edges grouped by an end of the edge (`to` unless a selector says otherwise), in edge order. */
|
|
284
|
-
const edgeIndex = (edges, keyOf = edge => edge.to) => {
|
|
285
|
-
const grouped = new Map();
|
|
286
|
-
for (const edge of edges) {
|
|
287
|
-
const key = keyOf(edge);
|
|
288
|
-
let list = grouped.get(key);
|
|
289
|
-
if (!list) {
|
|
290
|
-
list = [];
|
|
291
|
-
grouped.set(key, list);
|
|
292
|
-
}
|
|
293
|
-
list.push(edge);
|
|
294
|
-
}
|
|
295
|
-
return grouped;
|
|
296
|
-
};
|
|
297
|
-
const unitOfFile = (graph, rel) => {
|
|
298
|
-
const known = graph.unit(rel);
|
|
299
|
-
if (known)
|
|
300
|
-
return known;
|
|
301
|
-
const classified = graph.resolver.classifyPath(rel);
|
|
302
|
-
const owner = classified.slot ? graph.resolver.ownerOf(rel) : null;
|
|
303
|
-
return owner ? `${owner.slot}:${owner.root}` : null;
|
|
304
|
-
};
|
|
305
|
-
const consumeReexportedName = (context, result, edge, pair, depth, visiting) => {
|
|
306
|
-
if (pair.exported === null) {
|
|
307
|
-
result.names.add(pair.imported);
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
// A re-export by name: the name is used when the re-exporting file's own export is used further on.
|
|
311
|
-
const reexporter = edge.from;
|
|
312
|
-
if (visiting.has(reexporter) || depth >= CHAIN_DEPTH) {
|
|
313
|
-
result.names.add(pair.imported);
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
if (!(context.edgesTo.get(reexporter)?.length)) {
|
|
317
|
-
// nobody imports the re-exporting file: a framework entry (route file) uses it, an unused owner entry does not
|
|
318
|
-
const holder = context.graph.files.get(reexporter);
|
|
319
|
-
if (!holder?.owner || context.graph.resolver.slot(holder.owner.slot)?.tier === 'app')
|
|
320
|
-
result.names.add(pair.imported);
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
const next = consumedNames(context, reexporter, null, depth + 1, new Set([...visiting, reexporter]));
|
|
324
|
-
if (next.all || next.names.has(pair.exported))
|
|
325
|
-
result.names.add(pair.imported);
|
|
326
|
-
};
|
|
327
|
-
/** What the importers of `file` take from it; edges from inside `ownerKey` are skipped when given. */
|
|
328
|
-
const consumedNames = (context, file, ownerKey, depth, visiting) => {
|
|
329
|
-
const result = { all: false, names: new Set() };
|
|
330
|
-
for (const edge of context.edgesTo.get(file) ?? []) {
|
|
331
|
-
if (ownerKey && unitOfFile(context.graph, edge.from) === ownerKey)
|
|
332
|
-
continue;
|
|
333
|
-
const bindings = bindingsOf(context, edge.edge.declaration);
|
|
334
|
-
if (bindings.all) {
|
|
335
|
-
result.all = true;
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
for (const pair of bindings.pairs)
|
|
339
|
-
consumeReexportedName(context, result, edge, pair, depth, visiting);
|
|
340
|
-
}
|
|
341
|
-
return result;
|
|
342
|
-
};
|
|
343
|
-
/** Adds to `used` the entry exports reached by a consumer outside the owner that imports a file the entry re-exports by name. */
|
|
344
|
-
const addReexportedUses = (context, key, publicEntry, used) => {
|
|
345
|
-
const { graph } = context;
|
|
346
|
-
for (const edge of graph.edges) {
|
|
347
|
-
if (edge.from !== publicEntry || !edge.reexport || graph.unit(edge.to) !== key)
|
|
348
|
-
continue;
|
|
349
|
-
const reexport = bindingsOf(context, edge.edge.declaration);
|
|
350
|
-
const inner = consumedNames(context, edge.to, key, 0, new Set([publicEntry, edge.to]));
|
|
351
|
-
if (reexport.all)
|
|
352
|
-
continue;
|
|
353
|
-
for (const pair of reexport.pairs)
|
|
354
|
-
if ((inner.all || inner.names.has(pair.imported)) && pair.exported !== null)
|
|
355
|
-
used.names.add(pair.exported);
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
const unusedExportViolation = (publicEntry, name, line, owner) => ({
|
|
359
|
-
ruleId: 'HFS_UNUSED_EXPORT',
|
|
360
|
-
path: publicEntry, line, column: 1,
|
|
361
|
-
name, owner: owner.root, slot: owner.slot,
|
|
362
|
-
message: `${publicEntry} exports ${name}, but no production file outside ${owner.root || 'the repository root'} imports it; remove the export (only a unit role spec, a fixture builder, a test world file, or an integration spec of this integration's own provider folder counts besides production files).`,
|
|
363
|
-
});
|
|
364
|
-
/** Adds to `used` what another declared entry of the same owner imports from `publicEntry`: that entry's signature makes it public. */
|
|
365
|
-
const addSiblingEntryUses = (context, publicEntry, declared, used) => {
|
|
366
|
-
for (const edge of context.edgesTo.get(publicEntry) ?? []) {
|
|
367
|
-
if (edge.reexport || edge.from === publicEntry || !declared.has(edge.from))
|
|
368
|
-
continue;
|
|
369
|
-
const bindings = bindingsOf(context, edge.edge.declaration);
|
|
370
|
-
if (bindings.all)
|
|
371
|
-
used.all = true;
|
|
372
|
-
for (const pair of bindings.pairs)
|
|
373
|
-
used.names.add(pair.imported);
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
/** The unused-export violations and the export count of one public entry of an owner (`declared`: its slot's extra entries). */
|
|
377
|
-
const publicEntryExports = (context, key, owner, publicEntry, declared) => {
|
|
378
|
-
const entryFile = context.graph.files.get(publicEntry);
|
|
379
|
-
if (!entryFile)
|
|
380
|
-
return { violations: [], exports: 0 };
|
|
381
|
-
const names = exportedNames(context.ts, entryFile.sourceFile);
|
|
382
|
-
const used = consumedNames(context, publicEntry, key, 0, new Set([publicEntry]));
|
|
383
|
-
addReexportedUses(context, key, publicEntry, used);
|
|
384
|
-
if (declared.has(publicEntry))
|
|
385
|
-
addSiblingEntryUses(context, publicEntry, declared, used);
|
|
386
|
-
const violations = [...names]
|
|
387
|
-
.filter(([name]) => !(used.all || used.names.has(name)))
|
|
388
|
-
.map(([name, line]) => unusedExportViolation(publicEntry, name, line, owner));
|
|
389
|
-
return { violations, exports: names.size };
|
|
390
|
-
};
|
|
391
|
-
const ownerExportFindings = (context, config) => {
|
|
392
|
-
const { graph } = context;
|
|
393
|
-
const violations = [];
|
|
394
|
-
let owners = 0;
|
|
395
|
-
let exports = 0;
|
|
396
|
-
for (const [key, owner] of graph.ownerRoots) {
|
|
397
|
-
if (owner.tier === 'app')
|
|
398
|
-
continue;
|
|
399
|
-
const entry = entryOf(graph, config, key, owner);
|
|
400
|
-
if (!entry)
|
|
401
|
-
continue;
|
|
402
|
-
owners += 1;
|
|
403
|
-
const declared = new Set(extraEntries(graph, entry));
|
|
404
|
-
for (const publicEntry of [entry, ...declared]) {
|
|
405
|
-
const found = publicEntryExports(context, key, owner, publicEntry, declared);
|
|
406
|
-
exports += found.exports;
|
|
407
|
-
violations.push(...found.violations);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return { violations, owners, exports };
|
|
411
|
-
};
|
|
412
|
-
export function checkDeadExports({ context, graph, config }) {
|
|
413
|
-
const edgesTo = edgeIndex([...graph.edges, ...testConsumerEdges({ context, graph, config })]);
|
|
414
|
-
const analysis = { ...context, graph, edgesTo, namespaceCheckers: new WeakMap(), namespaceUses: new WeakMap() };
|
|
415
|
-
const exports = ownerExportFindings(analysis, config);
|
|
416
|
-
const files = deadFiles(graph, config, context);
|
|
417
|
-
const dead = exports.violations.length;
|
|
418
|
-
exports.violations.push(...files.violations);
|
|
419
|
-
return { violations: exports.violations, coverage: { status: 'checked', owners: exports.owners, exports: exports.exports, dead, files: files.judged, roots: files.roots, unusedFiles: files.violations.length } };
|
|
420
|
-
}
|