@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,248 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { machineKit } from './machine-ast.mjs';
|
|
3
|
-
import { propertyValue } from './function-spec-providers.mjs';
|
|
4
|
-
/**
|
|
5
|
-
* R50 `transport-owner` (FE_TRANSPORT_OWNER), the machine half of the eslint transport rules. ESLint judges one file at a time by
|
|
6
|
-
* the spelling of a call; the machine judges the repository by what the checker resolves. The browser calls the back end through
|
|
7
|
-
* Apollo GraphQL and axios (owner ruling 2026-10-13, both editions). A repository has exactly ONE transport client folder (the auth
|
|
8
|
-
* and no-auth Apollo clients with their link chain) and ONE GraphQLResponse union, both named by slot (never by a path spelled here):
|
|
9
|
-
* the api package's (`fe.package.api.client`, `fe.package.api.outcome`) when several apps call the back end, or the app's own
|
|
10
|
-
* (`fe.transport.client`, `fe.transport.outcome`) when exactly one app does. An app calls the back end when it holds a file of the
|
|
11
|
-
* api owner (modules/api, its client and union) or imports the api package; an app that holds neither (a marketing landing) shares
|
|
12
|
-
* no transport and is not counted. The union homes are the slots the manifest marks `outcomeHome`, not a list kept here.
|
|
13
|
-
*
|
|
14
|
-
* - two or more client folders, or two or more unions, in the repository (two apps each keeping one, or a package one plus an
|
|
15
|
-
* app one) are findings on every copy;
|
|
16
|
-
* - an app client or app union in a repository where more than one app calls the back end is a finding (it belongs to the package);
|
|
17
|
-
* - no file outside the client folder and the stream reader folder (`fe.transport.stream`, `modules/api/sse/clients/`) references the global `fetch`: a call, `globalThis.fetch`, `window.fetch`, or a
|
|
18
|
-
* reference passed as a value (`useSWR(key, fetch)`, `const send = fetch`), all of which a spelling-based rule misses; with no
|
|
19
|
-
* client at all every such reference is a finding that says the repository has no client;
|
|
20
|
-
* - axios is imported only by the api owner (modules/api, the api package) for its rest calls; every other HTTP library is a
|
|
21
|
-
* second transport wherever it is imported, by static import, `import()` or `require()`;
|
|
22
|
-
* - an `ApolloClient` is constructed only inside the client folder;
|
|
23
|
-
* - every GraphQL operation (`graphql/queries/query-*.ts`, `graphql/mutations/mutation-*.ts`, `graphql/subscriptions/subscription-*.ts`) imports the client (or the api
|
|
24
|
-
* package that holds it), so an operation is a caller of the one transport and not a second one.
|
|
25
|
-
* - every stream operation (`sse/<area>/stream-*.ts`) imports the stream reader, so a server-sent-events stream is read by the
|
|
26
|
-
* transport client layer and not by a fetch of its own.
|
|
27
|
-
*/
|
|
28
|
-
export const TRANSPORT_OWNER_RULE_IDS = ['FE_TRANSPORT_OWNER'];
|
|
29
|
-
const RULE = 'FE_TRANSPORT_OWNER';
|
|
30
|
-
/** Packages that send HTTP requests: a second transport when a file imports one (axios only outside the api owner). */
|
|
31
|
-
const HTTP_LIBRARIES = new Set(['axios', 'ky', 'ky-universal', 'got', 'node-fetch', 'undici', 'cross-fetch', 'isomorphic-fetch', 'superagent', 'ofetch', 'whatwg-fetch']);
|
|
32
|
-
const REST_LIBRARY = 'axios';
|
|
33
|
-
const GLOBAL_OBJECTS = new Set(['globalThis', 'window', 'self', 'global']);
|
|
34
|
-
const APP_CLIENT_SLOT = 'fe.transport.client';
|
|
35
|
-
const APP_OUTCOME_SLOT = 'fe.transport.outcome';
|
|
36
|
-
const PACKAGE_API_SLOT = 'fe.package.api';
|
|
37
|
-
const CLIENT_SLOTS = new Set([APP_CLIENT_SLOT, 'fe.package.api.client']);
|
|
38
|
-
/** The stream reader (server-sent events) is the second half of the transport client layer: it may call `fetch`, and it is no second Apollo client folder. */
|
|
39
|
-
const STREAM_SLOT = 'fe.transport.stream';
|
|
40
|
-
/** The api owner: the slots whose files may import axios (the rest calls and the client's own links). */
|
|
41
|
-
const API_OWNER_SLOTS = new Set(['fe.modules.api', PACKAGE_API_SLOT, ...CLIENT_SLOTS]);
|
|
42
|
-
/** The slots that hold the GraphQL operations of an app or of the api package. */
|
|
43
|
-
const OPERATION_SLOTS = new Set(['fe.modules.api', PACKAGE_API_SLOT]);
|
|
44
|
-
const OPERATION_FILE = /^(?:query|mutation|subscription)-[^/]+\.ts$/u;
|
|
45
|
-
const STREAM_OPERATION_FILE = /^stream-[^/]+\.ts$/u;
|
|
46
|
-
const NO_CLIENT = "the repository has no transport client. Create the one Apollo client folder (packages/<family>-api/src/graphql/clients/, or the only app's modules/api/graphql/clients/) and call the back end through it.";
|
|
47
|
-
/** True when `node` names the global fetch: no import, and no declaration outside a lib declaration file. */
|
|
48
|
-
function isGlobalObjectFetch(ts, node) {
|
|
49
|
-
return ts.isPropertyAccessExpression(node) && node.name.text === 'fetch' && ts.isIdentifier(node.expression) && GLOBAL_OBJECTS.has(node.expression.text);
|
|
50
|
-
}
|
|
51
|
-
function isDeclarationName(ts, parent, node) {
|
|
52
|
-
return (ts.isPropertyAssignment(parent) || ts.isPropertyDeclaration(parent) || ts.isMethodDeclaration(parent) || ts.isBindingElement(parent) || ts.isParameter(parent) || ts.isVariableDeclaration(parent)) && parent.name === node;
|
|
53
|
-
}
|
|
54
|
-
function isTypeOrImportPosition(ts, parent) {
|
|
55
|
-
return ts.isTypeQueryNode(parent) || ts.isQualifiedName(parent) || ts.isImportSpecifier(parent) || ts.isExportSpecifier(parent) || ts.isPropertySignature(parent);
|
|
56
|
-
}
|
|
57
|
-
function isGlobalFetch(ts, kit, checker, node) {
|
|
58
|
-
if (!ts.isIdentifier(node))
|
|
59
|
-
return isGlobalObjectFetch(ts, node);
|
|
60
|
-
if (node.text !== 'fetch')
|
|
61
|
-
return false;
|
|
62
|
-
if (kit.importBinding(checker, node))
|
|
63
|
-
return false;
|
|
64
|
-
const parent = node.parent;
|
|
65
|
-
if (ts.isPropertyAccessExpression(parent) && parent.name === node)
|
|
66
|
-
return false;
|
|
67
|
-
// A type position (`typeof fetch`, `typeof globalThis.fetch`) names the type of fetch and sends nothing.
|
|
68
|
-
if (isTypeOrImportPosition(ts, parent))
|
|
69
|
-
return false;
|
|
70
|
-
if (isDeclarationName(ts, parent, node))
|
|
71
|
-
return false;
|
|
72
|
-
const declarations = kit.declarationsOf(checker, node);
|
|
73
|
-
return declarations.every((declaration) => declaration.getSourceFile().isDeclarationFile);
|
|
74
|
-
}
|
|
75
|
-
/** Findings on every copy when there are several, and on an app's own copy in a repository where several apps call the back end. */
|
|
76
|
-
function reportMultiplicity(items, noun, callingApps, report) {
|
|
77
|
-
const list = items.map((item) => item.root).join(', ');
|
|
78
|
-
for (const item of items) {
|
|
79
|
-
if (items.length > 1) {
|
|
80
|
-
report(item, item.sourceFile, `The repository has ${items.length} ${noun}s (${list}); it has exactly one: the api package's (packages/<family>-api/src/graphql/) when the apps share it, or the only app's. Keep one and delete the others.`);
|
|
81
|
-
}
|
|
82
|
-
else if ((item.slot === APP_CLIENT_SLOT || item.slot === APP_OUTCOME_SLOT) && callingApps.length > 1) {
|
|
83
|
-
report(item, item.sourceFile, `${item.root} is an app's ${noun} in a repository of ${callingApps.length} apps that call the back end (${callingApps.join(', ')}); a repository where several apps call the back end keeps its one ${noun} in the api package (packages/<family>-api/src/graphql/).`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function globalFetchUse(ts, kit, checker, node) {
|
|
88
|
-
if (ts.isPropertyAccessExpression(node) && isGlobalFetch(ts, kit, checker, node))
|
|
89
|
-
return 'property';
|
|
90
|
-
if (ts.isIdentifier(node) && isGlobalFetch(ts, kit, checker, node))
|
|
91
|
-
return 'identifier';
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
function moduleSpecifierOf(ts, node) {
|
|
95
|
-
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
|
|
96
|
-
return node.moduleSpecifier;
|
|
97
|
-
if (ts.isCallExpression(node) && (node.expression.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(node.expression) && node.expression.text === 'require')))
|
|
98
|
-
return node.arguments[0];
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
function isApolloClientConstruction(ts, kit, checker, node) {
|
|
102
|
-
if (!ts.isNewExpression(node))
|
|
103
|
-
return false;
|
|
104
|
-
const binding = kit.importBinding(checker, node.expression);
|
|
105
|
-
return Boolean(binding?.name === 'ApolloClient' && binding.module.startsWith('@apollo/client'));
|
|
106
|
-
}
|
|
107
|
-
function inspectLibraryImport({ ts, file, node, transport, report }) {
|
|
108
|
-
const specifier = moduleSpecifierOf(ts, node);
|
|
109
|
-
if (!specifier || !ts.isStringLiteralLike(specifier))
|
|
110
|
-
return;
|
|
111
|
-
const library = specifier.text.split('/')[0];
|
|
112
|
-
if (!HTTP_LIBRARIES.has(library) || (library === REST_LIBRARY && (API_OWNER_SLOTS.has(file.slot))))
|
|
113
|
-
return;
|
|
114
|
-
const where = library === REST_LIBRARY
|
|
115
|
-
? 'axios belongs to the api owner (modules/api/rest, the api package) only: call their functions'
|
|
116
|
-
: `the transport is Apollo GraphQL (${transport}) with axios for the rest calls of modules/api/rest`;
|
|
117
|
-
report(file, node, `${specifier.text} is a second HTTP transport in ${file.rel}; ${where}.`, { library: specifier.text });
|
|
118
|
-
}
|
|
119
|
-
/** True when a call of fetch declares, in its init object literal, the request header accept: "text/event-stream". */
|
|
120
|
-
function asksForEventStream(ts, node) {
|
|
121
|
-
const call = node.parent && ts.isCallExpression(node.parent) && node.parent.expression === node ? node.parent : null;
|
|
122
|
-
const init = call?.arguments[1];
|
|
123
|
-
if (!init || !ts.isObjectLiteralExpression(init))
|
|
124
|
-
return false;
|
|
125
|
-
const headers = propertyValue(ts, init, 'headers');
|
|
126
|
-
const accept = headers && ts.isObjectLiteralExpression(headers) ? propertyValue(ts, headers, 'accept') : null;
|
|
127
|
-
return Boolean(accept && ts.isStringLiteralLike(accept) && accept.text === 'text/event-stream');
|
|
128
|
-
}
|
|
129
|
-
function inspectTransportNode(scope, node) {
|
|
130
|
-
const { ts, kit, checker, file, isClient, owned, report } = scope;
|
|
131
|
-
const fetchUse = globalFetchUse(ts, kit, checker, node);
|
|
132
|
-
if (fetchUse !== null) {
|
|
133
|
-
if (isClient)
|
|
134
|
-
return false;
|
|
135
|
-
if (file.slot === STREAM_SLOT) {
|
|
136
|
-
if (!asksForEventStream(ts, node))
|
|
137
|
-
report(file, node, `${file.rel} fetches something other than an event stream; the stream reader folder reads server-sent events only (accept: "text/event-stream"), every other request is an operation of modules/api run by Apollo or axios.`);
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
if (fetchUse === 'property')
|
|
141
|
-
report(file, node, `${node.getText(file.sourceFile)} reaches the global fetch outside the transport client; ${owned}`);
|
|
142
|
-
else
|
|
143
|
-
report(file, node, `fetch is used outside the transport client (called, aliased or passed as a value); ${owned}`);
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
if (!isClient && isApolloClientConstruction(ts, kit, checker, node))
|
|
147
|
-
report(file, node, `${file.rel} constructs an ApolloClient outside the transport client folder; ${owned}`);
|
|
148
|
-
inspectLibraryImport({ ...scope, node });
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
/** True when the operation imports the client folder, or (from an app) the api package that holds it. */
|
|
152
|
-
function reachesClient(graph, file, clientRels) {
|
|
153
|
-
return graph.edges.some((edge) => {
|
|
154
|
-
if (edge.from !== file.rel || !edge.runtime)
|
|
155
|
-
return false;
|
|
156
|
-
if (clientRels.has(edge.to))
|
|
157
|
-
return true;
|
|
158
|
-
return file.slot !== PACKAGE_API_SLOT && (graph.files.get(edge.to)?.slot ?? '').startsWith(PACKAGE_API_SLOT);
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
/** True for a stream operation of the app's api layer: `modules/api/sse/<area>/stream-<channel>.ts`. */
|
|
162
|
-
function isStreamOperation(file) {
|
|
163
|
-
return file.slot === 'fe.modules.api' && STREAM_OPERATION_FILE.test(path.posix.basename(file.rel)) && path.posix.basename(path.posix.dirname(path.posix.dirname(file.rel))) === 'sse';
|
|
164
|
-
}
|
|
165
|
-
/** True when the stream operation imports a file of the stream reader folder. */
|
|
166
|
-
function reachesStreamReader(graph, file) {
|
|
167
|
-
return graph.edges.some((edge) => edge.from === file.rel && edge.runtime && graph.files.get(edge.to)?.slot === STREAM_SLOT);
|
|
168
|
-
}
|
|
169
|
-
/** Only a stream operation (or the reader folder itself) imports the stream reader: a REST call, a GraphQL operation or a hook never does. */
|
|
170
|
-
function inspectReaderImporters({ files, graph, report }) {
|
|
171
|
-
const byRel = new Map(files.map((file) => [file.rel, file]));
|
|
172
|
-
for (const edge of graph.edges) {
|
|
173
|
-
if (!edge.runtime || graph.files.get(edge.to)?.slot !== STREAM_SLOT)
|
|
174
|
-
continue;
|
|
175
|
-
const from = byRel.get(edge.from);
|
|
176
|
-
if (!from || from.slot === STREAM_SLOT || isStreamOperation(from))
|
|
177
|
-
continue;
|
|
178
|
-
report(from, from.sourceFile, `${from.rel} imports the stream reader (modules/api/sse/clients); only a stream operation (sse/<area>/stream-<channel>.ts) reads a server-sent-events stream.`);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
function inspectTransportFiles({ files, graph, kit, ts, clientRels, transport, owned, report }) {
|
|
182
|
-
const appsWithFiles = new Set();
|
|
183
|
-
let operations = 0;
|
|
184
|
-
for (const file of files) {
|
|
185
|
-
if (file.bindings?.app)
|
|
186
|
-
appsWithFiles.add(file.bindings.app);
|
|
187
|
-
const scope = { ts, kit, checker: kit.checkerOf(file.sourceFile), file, isClient: clientRels.has(file.rel), transport, owned, report };
|
|
188
|
-
kit.walk(file.sourceFile, (node) => inspectTransportNode(scope, node));
|
|
189
|
-
if (OPERATION_SLOTS.has(file.slot) && OPERATION_FILE.test(path.posix.basename(file.rel))) {
|
|
190
|
-
operations += 1;
|
|
191
|
-
if (!reachesClient(graph, file, clientRels))
|
|
192
|
-
report(file, file.sourceFile, `${file.rel} is a GraphQL operation that does not import the transport client; an operation runs through the repository's one Apollo client, never through its own transport.`);
|
|
193
|
-
}
|
|
194
|
-
if (isStreamOperation(file)) {
|
|
195
|
-
operations += 1;
|
|
196
|
-
if (!reachesStreamReader(graph, file))
|
|
197
|
-
report(file, file.sourceFile, `${file.rel} is a stream operation that does not import the stream reader (modules/api/sse/clients); a stream operation runs on the transport client layer's reader, never on a fetch of its own.`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
inspectReaderImporters({ files, graph, report });
|
|
201
|
-
return { appsWithFiles, operations };
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* The declared Next apps that call the back end: those holding a file of an api owner slot (modules/api, the app client, the app
|
|
205
|
-
* union) or importing a file of the api package. An app with neither shares no transport.
|
|
206
|
-
*/
|
|
207
|
-
function transportApps(nextApps, files, graph) {
|
|
208
|
-
const byRel = new Map(files.map((file) => [file.rel, file]));
|
|
209
|
-
const calling = new Set(files.filter((file) => API_OWNER_SLOTS.has(file.slot) && file.slot !== PACKAGE_API_SLOT).map((file) => file.bindings?.app));
|
|
210
|
-
for (const edge of graph.edges) {
|
|
211
|
-
const app = byRel.get(edge.from)?.bindings?.app;
|
|
212
|
-
if (app && (byRel.get(edge.to)?.slot ?? '').startsWith(PACKAGE_API_SLOT))
|
|
213
|
-
calling.add(app);
|
|
214
|
-
}
|
|
215
|
-
return nextApps.map((item) => item.name).filter((name) => calling.has(name));
|
|
216
|
-
}
|
|
217
|
-
/** One instance per client folder (its slot root), carried by the folder's first file. */
|
|
218
|
-
function clientInstances(clients) {
|
|
219
|
-
const byRoot = new Map();
|
|
220
|
-
for (const file of [...clients].sort((a, b) => a.rel.localeCompare(b.rel)))
|
|
221
|
-
if (!byRoot.has(file.root))
|
|
222
|
-
byRoot.set(file.root, file);
|
|
223
|
-
return [...byRoot.values()];
|
|
224
|
-
}
|
|
225
|
-
export function checkTransportOwner(input) {
|
|
226
|
-
const { graph } = input;
|
|
227
|
-
const kit = machineKit(input);
|
|
228
|
-
const { ts, resolver } = kit;
|
|
229
|
-
const violations = [];
|
|
230
|
-
const report = (file, node, message, extra = {}) => violations.push({ ruleId: RULE, ...kit.at(file.rel, file.sourceFile, node), message, ...extra });
|
|
231
|
-
const nextApps = resolver.repo.apps.filter((item) => item.kind === 'next');
|
|
232
|
-
const files = [...graph.files.values()].filter((file) => Boolean(file.slot) && file.tier !== 'e2e').map((file) => {
|
|
233
|
-
const classified = resolver.classifyPath(file.rel);
|
|
234
|
-
return { ...file, root: classified.root ?? file.rel, bindings: classified.bindings };
|
|
235
|
-
});
|
|
236
|
-
const clients = files.filter((file) => CLIENT_SLOTS.has(file.slot));
|
|
237
|
-
const instances = clientInstances(clients);
|
|
238
|
-
const outcomes = files.filter((file) => resolver.slot(file.slot)?.outcomeHome === true).map((file) => ({ ...file, root: file.rel }));
|
|
239
|
-
const clientRels = new Set(clients.map((file) => file.rel));
|
|
240
|
-
const transport = instances.length ? instances.map((item) => item.root).join(', ') : 'no client exists yet';
|
|
241
|
-
const owned = instances.length ? `the repository has one transport (${transport}). Call the back end through a modules/api operation and take its GraphQLResponse.` : NO_CLIENT;
|
|
242
|
-
// Exactly one client folder and one GraphQLResponse union per repository.
|
|
243
|
-
const calling = transportApps(nextApps, files, graph);
|
|
244
|
-
reportMultiplicity(instances, 'transport client', calling, report);
|
|
245
|
-
reportMultiplicity(outcomes, 'GraphQLResponse union', calling, report);
|
|
246
|
-
const { appsWithFiles, operations } = inspectTransportFiles({ files, graph, kit, ts, clientRels, transport, owned, report });
|
|
247
|
-
return { violations, coverage: { status: 'checked', apps: appsWithFiles.size, operations, clients: instances.length, outcomes: outcomes.length } };
|
|
248
|
-
}
|
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
function appendProjectReferences(relative, parsed, config, ts, helpers, queue, errors) {
|
|
4
|
-
for (const reference of parsed.projectReferences ?? []) {
|
|
5
|
-
const referencedFile = helpers.typeScriptProjectReferencePath(ts, reference);
|
|
6
|
-
const absoluteReference = path.resolve(referencedFile);
|
|
7
|
-
if (!helpers.isInside(config.root, absoluteReference)) {
|
|
8
|
-
errors.push({ ruleId: 'ARCH_TSCONFIG_REFERENCE_OUTSIDE', project: relative, message: `Project reference leaves the repository: ${helpers.slash(path.relative(config.root, absoluteReference))}.` });
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
queue.push(helpers.slash(path.relative(config.root, absoluteReference)));
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function readProject(relative, config, ts, helpers, state) {
|
|
16
|
-
const configFile = path.join(config.root, ...relative.split('/'));
|
|
17
|
-
if (!fs.existsSync(configFile)) {
|
|
18
|
-
state.errors.push({ ruleId: 'ARCH_TSCONFIG_MISSING', project: relative, message: `${relative} does not exist.` });
|
|
19
|
-
state.invalidProjects.add(relative);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
const { read, parsed } = helpers.readTypeScriptProject(ts, configFile);
|
|
23
|
-
if (parsed === null) {
|
|
24
|
-
state.errors.push(helpers.compilerError(ts, config.root, read.error, relative));
|
|
25
|
-
state.invalidProjects.add(relative);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
if (parsed.errors.length) {
|
|
29
|
-
state.errors.push(...parsed.errors.map(item => helpers.compilerError(ts, config.root, item, relative)));
|
|
30
|
-
state.invalidProjects.add(relative);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
appendProjectReferences(relative, parsed, config, ts, helpers, state.queue, state.errors);
|
|
34
|
-
state.parsedProjects.set(relative, parsed);
|
|
35
|
-
}
|
|
36
|
-
function loadProjectConfigurations(config, ts, helpers, errors) {
|
|
37
|
-
const state = { errors, parsedProjects: new Map(), invalidProjects: new Set(), queue: [...config.projects] };
|
|
38
|
-
const seenProjects = new Set();
|
|
39
|
-
while (state.queue.length) {
|
|
40
|
-
const relative = state.queue.shift(); // length checked by the loop condition
|
|
41
|
-
if (seenProjects.has(relative))
|
|
42
|
-
continue;
|
|
43
|
-
seenProjects.add(relative);
|
|
44
|
-
readProject(relative, config, ts, helpers, state);
|
|
45
|
-
}
|
|
46
|
-
return state;
|
|
47
|
-
}
|
|
48
|
-
function pathMatches(config, paths, file, helpers) {
|
|
49
|
-
const relative = helpers.slash(path.relative(config.root, file));
|
|
50
|
-
const normalized = helpers.slash(paths).replace(/\/$/, '');
|
|
51
|
-
return helpers.sameOrUnder(relative, normalized);
|
|
52
|
-
}
|
|
53
|
-
function visitProjectReferences(relative, selected, parsedProjects, config, ts, helpers) {
|
|
54
|
-
if (selected.has(relative))
|
|
55
|
-
return;
|
|
56
|
-
selected.add(relative);
|
|
57
|
-
for (const reference of parsedProjects.get(relative)?.projectReferences ?? []) {
|
|
58
|
-
const referencedFile = helpers.typeScriptProjectReferencePath(ts, reference);
|
|
59
|
-
const referenced = helpers.slash(path.relative(config.root, path.resolve(referencedFile)));
|
|
60
|
-
if (parsedProjects.has(referenced))
|
|
61
|
-
visitProjectReferences(referenced, selected, parsedProjects, config, ts, helpers);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
function invalidProjectMatches(config, paths, relative, helpers) {
|
|
65
|
-
const directory = path.dirname(path.join(config.root, ...relative.split('/')));
|
|
66
|
-
return paths.some(prefix => {
|
|
67
|
-
const absolute = path.join(config.root, ...helpers.slash(prefix).split('/'));
|
|
68
|
-
return helpers.isInside(directory, absolute);
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
function selectProjects(config, paths, ts, state, helpers) {
|
|
72
|
-
const selected = new Set();
|
|
73
|
-
const direct = new Set();
|
|
74
|
-
if (!paths.length)
|
|
75
|
-
return { selected, direct };
|
|
76
|
-
for (const [relative, parsed] of state.parsedProjects) {
|
|
77
|
-
if (parsed.fileNames.some(file => paths.some(prefix => pathMatches(config, prefix, file, helpers)))) {
|
|
78
|
-
direct.add(relative);
|
|
79
|
-
visitProjectReferences(relative, selected, state.parsedProjects, config, ts, helpers);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
for (const relative of state.invalidProjects) {
|
|
83
|
-
if (invalidProjectMatches(config, paths, relative, helpers))
|
|
84
|
-
selected.add(relative);
|
|
85
|
-
}
|
|
86
|
-
state.errors.splice(0, state.errors.length, ...state.errors.filter(error => !error.project || selected.has(error.project)));
|
|
87
|
-
return { selected, direct };
|
|
88
|
-
}
|
|
89
|
-
function createProjects(config, paths, state, selection, ts, helpers) {
|
|
90
|
-
const projects = [];
|
|
91
|
-
for (const [relative, parsed] of state.parsedProjects) {
|
|
92
|
-
if (paths.length && !selection.selected.has(relative))
|
|
93
|
-
continue;
|
|
94
|
-
const rootNames = paths.length && selection.direct.has(relative)
|
|
95
|
-
? parsed.fileNames.filter(file => paths.some(prefix => pathMatches(config, prefix, file, helpers)))
|
|
96
|
-
: parsed.fileNames;
|
|
97
|
-
const program = helpers.createTypeScriptProgram(ts, { rootNames, options: parsed.options, projectReferences: parsed.projectReferences });
|
|
98
|
-
state.errors.push(...program.getSyntacticDiagnostics().filter(item => !item.file || helpers.isInside(config.root, item.file.fileName))
|
|
99
|
-
.map(item => helpers.compilerError(ts, config.root, item, relative, 'ARCH_SYNTAX_INVALID')));
|
|
100
|
-
projects.push({ relative, program, options: parsed.options });
|
|
101
|
-
}
|
|
102
|
-
return projects;
|
|
103
|
-
}
|
|
104
|
-
function ownershipRank(config, project, file, helpers) {
|
|
105
|
-
return helpers.isInside(path.dirname(path.join(config.root, ...project.relative.split('/'))), file)
|
|
106
|
-
? project.relative.split('/').length : -1;
|
|
107
|
-
}
|
|
108
|
-
function collectProjectFiles(config, projects, helpers) {
|
|
109
|
-
const fileMap = new Map();
|
|
110
|
-
const ownerByFile = new Map();
|
|
111
|
-
// Retain only the winning source occurrence while visiting each project. Equal ranks keep the first project,
|
|
112
|
-
// as does a file outside every project directory; the deepest enclosing tsconfig otherwise owns its checker.
|
|
113
|
-
// Within that project the last realpath alias supplies the source, matching the compiler's occurrence order.
|
|
114
|
-
for (const project of projects) {
|
|
115
|
-
for (const sourceFile of project.program.getSourceFiles()) {
|
|
116
|
-
if (!helpers.isProductionSource(config.root, sourceFile))
|
|
117
|
-
continue;
|
|
118
|
-
const name = helpers.canonical(sourceFile.fileName);
|
|
119
|
-
const owner = ownerByFile.get(name);
|
|
120
|
-
if (owner === project) {
|
|
121
|
-
fileMap.set(name, sourceFile);
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
if (owner && ownershipRank(config, owner, name, helpers) >= ownershipRank(config, project, name, helpers))
|
|
125
|
-
continue;
|
|
126
|
-
ownerByFile.set(name, project);
|
|
127
|
-
fileMap.set(name, sourceFile);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return { fileMap, ownerByFile };
|
|
131
|
-
}
|
|
132
|
-
function makeFileGraph(config, projects, helpers) {
|
|
133
|
-
const { fileMap, ownerByFile } = collectProjectFiles(config, projects, helpers);
|
|
134
|
-
const checkerByFile = new Map();
|
|
135
|
-
for (const [name, owner] of ownerByFile)
|
|
136
|
-
checkerByFile.set(name, owner.program.getTypeChecker());
|
|
137
|
-
const files = [...fileMap.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([, file]) => file);
|
|
138
|
-
const edges = new Map([...fileMap.keys()].map((file) => [file, []]));
|
|
139
|
-
for (const [file, sourceFile] of fileMap) {
|
|
140
|
-
const sourceName = path.resolve(sourceFile.fileName);
|
|
141
|
-
if (sourceName !== file)
|
|
142
|
-
edges.set(sourceName, edges.get(file) ?? []);
|
|
143
|
-
}
|
|
144
|
-
return { fileMap, checkerByFile, ownerByFile, files, edges };
|
|
145
|
-
}
|
|
146
|
-
// Resolution entries contain failed lookup paths as well as hits. Keep only one bounded batch for the current project;
|
|
147
|
-
// graph edges retain the resolved targets, so rotating this scratch cache changes neither coverage nor findings.
|
|
148
|
-
const RESOLUTION_BATCH_SIZE = 1024;
|
|
149
|
-
function resolutionCache(state, project) {
|
|
150
|
-
if (state.resolutionProject !== project || state.resolutionCount === RESOLUTION_BATCH_SIZE) {
|
|
151
|
-
state.resolutionProject = project;
|
|
152
|
-
state.resolutionCount = 0;
|
|
153
|
-
state.resolutionCache = state.ts.createModuleResolutionCache(state.config.root, file => state.ts.sys.useCaseSensitiveFileNames ? file : file.toLowerCase(), project.options);
|
|
154
|
-
}
|
|
155
|
-
state.resolutionCount += 1;
|
|
156
|
-
return state.resolutionCache;
|
|
157
|
-
}
|
|
158
|
-
function addUnprovenFindings(sourceFile, project, references, errors, config, helpers) {
|
|
159
|
-
for (const item of references.unproven)
|
|
160
|
-
errors.push({
|
|
161
|
-
ruleId: 'ARCH_DYNAMIC_DEPENDENCY_UNPROVEN',
|
|
162
|
-
project: project.relative,
|
|
163
|
-
path: helpers.relativePath(config.root, sourceFile.fileName),
|
|
164
|
-
...helpers.sourceLocation(sourceFile, item.node),
|
|
165
|
-
message: `${item.kind} must use a string-literal module name so architecture coverage can resolve its dependency.`,
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
function unresolvedReferenceFinding(reference, sourceFile, project, config, workspaceNames, errors, helpers) {
|
|
169
|
-
const codeLike = !helpers.assetExtension.test(reference.specifier);
|
|
170
|
-
const workspaceImport = [...workspaceNames].some(name => helpers.sameOrUnder(reference.specifier, name));
|
|
171
|
-
const internal = reference.specifier.startsWith('.') || helpers.pathAliasMatches(reference.specifier, project.options.paths) || workspaceImport;
|
|
172
|
-
// A __generated__/ folder is the gitignored output of the app's codegen step (the GraphQL wire types, fe-stack codegen): it
|
|
173
|
-
// exists after `npm run codegen`, which lint, typecheck and build run first, and the compiler proves those imports there.
|
|
174
|
-
const generated = /(?:^|\/)__generated__(?:\/|$)/.test(reference.specifier);
|
|
175
|
-
if (codeLike && internal && !generated)
|
|
176
|
-
errors.push({
|
|
177
|
-
ruleId: 'ARCH_INTERNAL_IMPORT_UNRESOLVED',
|
|
178
|
-
project: project.relative,
|
|
179
|
-
path: helpers.relativePath(config.root, sourceFile.fileName),
|
|
180
|
-
...helpers.sourceLocation(sourceFile, reference.node),
|
|
181
|
-
specifier: reference.specifier,
|
|
182
|
-
message: `Internal import ${reference.specifier} is not resolvable with ${project.relative}.`,
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
function internalReference(reference, project, workspaceNames, helpers) {
|
|
186
|
-
const workspaceImport = [...workspaceNames].some(name => reference.specifier === name || reference.specifier.startsWith(`${name}/`));
|
|
187
|
-
return reference.specifier.startsWith('.') || helpers.pathAliasMatches(reference.specifier, project.options.paths) || workspaceImport;
|
|
188
|
-
}
|
|
189
|
-
function appendBoundaryFinding(state, reference, sourceFile, project, actualTarget, { internal, reviewable }) {
|
|
190
|
-
const { config, errors, helpers } = state;
|
|
191
|
-
if (internal && helpers.crossesSide(config, actualTarget)) {
|
|
192
|
-
errors.push({
|
|
193
|
-
ruleId: 'ARCH_INTERNAL_IMPORT_OUTSIDE',
|
|
194
|
-
project: project.relative,
|
|
195
|
-
path: helpers.relativePath(config.root, sourceFile.fileName),
|
|
196
|
-
...helpers.sourceLocation(sourceFile, reference.node),
|
|
197
|
-
specifier: reference.specifier,
|
|
198
|
-
message: `Internal import ${reference.specifier} leaves the ${path.basename(config.root)} side for ${helpers.slash(path.relative(config.packageRoot, actualTarget))}; nothing crosses the sides of an app.`,
|
|
199
|
-
});
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
if (internal && !reviewable) {
|
|
203
|
-
errors.push({
|
|
204
|
-
ruleId: 'ARCH_INTERNAL_IMPORT_OUTSIDE',
|
|
205
|
-
project: project.relative,
|
|
206
|
-
path: helpers.relativePath(config.root, sourceFile.fileName),
|
|
207
|
-
...helpers.sourceLocation(sourceFile, reference.node),
|
|
208
|
-
specifier: reference.specifier,
|
|
209
|
-
message: `Internal import ${reference.specifier} resolves outside the checked repository.`,
|
|
210
|
-
});
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
function appendEdge(from, sourceFile, project, owningWorkspace, reference, actualTarget, state) {
|
|
216
|
-
if (!state.fileMap.has(actualTarget))
|
|
217
|
-
return;
|
|
218
|
-
const { config, ts, edges, edgeKeys, errors, helpers, workspaces } = state;
|
|
219
|
-
const edge = {
|
|
220
|
-
from,
|
|
221
|
-
to: actualTarget,
|
|
222
|
-
runtime: reference.runtime,
|
|
223
|
-
specifier: reference.specifier,
|
|
224
|
-
node: reference.node,
|
|
225
|
-
declaration: reference.declaration,
|
|
226
|
-
reexport: ts.isExportDeclaration(reference.declaration),
|
|
227
|
-
sourceFile,
|
|
228
|
-
project: project.relative,
|
|
229
|
-
...helpers.sourceLocation(sourceFile, reference.node),
|
|
230
|
-
};
|
|
231
|
-
const key = `${from}\0${actualTarget}\0${reference.node.getStart(sourceFile)}\0${reference.specifier}`;
|
|
232
|
-
if (!edgeKeys.has(key)) {
|
|
233
|
-
edges.get(from)?.push(edge);
|
|
234
|
-
edgeKeys.add(key);
|
|
235
|
-
}
|
|
236
|
-
const targetWorkspace = helpers.workspaceOf(workspaces, actualTarget);
|
|
237
|
-
if (owningWorkspace && targetWorkspace && owningWorkspace !== targetWorkspace) {
|
|
238
|
-
if (!owningWorkspace.app && targetWorkspace.app)
|
|
239
|
-
errors.push(helpers.boundaryViolation(config.root, edge, owningWorkspace, targetWorkspace, 'ARCH_PACKAGE_IMPORTS_APP', 'A reusable workspace package cannot depend on an application workspace.'));
|
|
240
|
-
if (!helpers.packageExported(ts, targetWorkspace, reference.specifier, actualTarget))
|
|
241
|
-
errors.push(helpers.boundaryViolation(config.root, edge, owningWorkspace, targetWorkspace, 'ARCH_PACKAGE_EXPORT_BYPASS', 'Cross-package imports must use the target package name and a declared package export.'));
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* An import of one of the app's root workspace packages (`@scope/agent-sdk`, `@scope/agent-sdk/workbench`). The package lies outside the side
|
|
246
|
-
* folder, yet it is first-party source both sides may import: through its package entry points (the `exports` of its manifest) and never past
|
|
247
|
-
* them. An entry point is no import that leaves the side and adds no edge (the package is not part of this side's graph; the package's own
|
|
248
|
-
* lint judges its files); a path into the package's source is refused. Returns true when the reference was settled here.
|
|
249
|
-
*/
|
|
250
|
-
function rootPackageReference(sourceFile, project, reference, state) {
|
|
251
|
-
const { config, errors, helpers, host, ts } = state;
|
|
252
|
-
if (!config.rootPackages?.length)
|
|
253
|
-
return false;
|
|
254
|
-
const item = helpers.rootPackageOf(config, reference.specifier);
|
|
255
|
-
if (item === null)
|
|
256
|
-
return false;
|
|
257
|
-
if (!helpers.rootPackageEntryDeclared(item, reference.specifier)) {
|
|
258
|
-
errors.push({
|
|
259
|
-
ruleId: 'ARCH_INTERNAL_IMPORT_OUTSIDE',
|
|
260
|
-
project: project.relative,
|
|
261
|
-
path: helpers.relativePath(config.root, sourceFile.fileName),
|
|
262
|
-
...helpers.sourceLocation(sourceFile, reference.node),
|
|
263
|
-
specifier: reference.specifier,
|
|
264
|
-
message: `Internal import ${reference.specifier} reaches into the package ${item.name} past its package entry points; import ${helpers.rootPackageEntries(item).join(', ')} instead.`,
|
|
265
|
-
});
|
|
266
|
-
return true;
|
|
267
|
-
}
|
|
268
|
-
// An entry point that a path alias sends outside the package is no entry point: the ordinary boundary judges it.
|
|
269
|
-
const resolved = helpers.resolveTypeScriptModule(ts, reference.specifier, sourceFile.fileName, project.options, host, resolutionCache(state, project));
|
|
270
|
-
return resolved === undefined || helpers.isInside(item.root, helpers.canonical(resolved));
|
|
271
|
-
}
|
|
272
|
-
function processReference(from, sourceFile, project, owningWorkspace, reference, state) {
|
|
273
|
-
const { config, ts, workspaceNames, workspaces, host, errors, helpers } = state;
|
|
274
|
-
if (rootPackageReference(sourceFile, project, reference, state))
|
|
275
|
-
return;
|
|
276
|
-
// A workspace package is read at its source, never at its build: the same import resolves whether or not dist exists.
|
|
277
|
-
const workspaceTarget = workspaces.filter(item => item.root !== helpers.canonical(config.root)).map(item => helpers.workspaceSourceEntry(ts, item, reference.specifier)).find(Boolean);
|
|
278
|
-
const resolvedName = workspaceTarget ?? helpers.resolveTypeScriptModule(ts, reference.specifier, sourceFile.fileName, project.options, host, resolutionCache(state, project));
|
|
279
|
-
if (!resolvedName) {
|
|
280
|
-
unresolvedReferenceFinding(reference, sourceFile, project, config, workspaceNames, errors, helpers);
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
const actualTarget = helpers.canonical(resolvedName);
|
|
284
|
-
const internal = internalReference(reference, project, workspaceNames, helpers);
|
|
285
|
-
// The boundary is the checkout, not the checked project: a path alias that lands in a sibling
|
|
286
|
-
// package of the same repository (`@fe-kit/*` -> packages/fe-kit) is still source a reviewer can
|
|
287
|
-
// open, while anything past the repository, or anything inside an installed dependency tree, is
|
|
288
|
-
// not. `config.repository` is null when there is no git checkout around the project, and the
|
|
289
|
-
// project is then the boundary it always was.
|
|
290
|
-
const reviewable = helpers.isInside(config.root, actualTarget)
|
|
291
|
-
|| Boolean(config.repository && helpers.isInside(config.repository, actualTarget) && !helpers.slash(actualTarget).includes('/node_modules/'));
|
|
292
|
-
// A side of an app (config.root is be/ or fe/) imports nothing of the app outside itself: the root holds no source, the other
|
|
293
|
-
// side is another program, and a declared read (sides.fe.reads, be/contracts/) is codegen input, never an import.
|
|
294
|
-
if (appendBoundaryFinding(state, reference, sourceFile, project, actualTarget, { internal, reviewable }))
|
|
295
|
-
return;
|
|
296
|
-
appendEdge(from, sourceFile, project, owningWorkspace, reference, actualTarget, state);
|
|
297
|
-
}
|
|
298
|
-
function processSourceFile(from, sourceFile, state) {
|
|
299
|
-
const { config, ts, workspaces, ownerByFile, helpers } = state;
|
|
300
|
-
const owningWorkspace = helpers.workspaceOf(workspaces, from);
|
|
301
|
-
const project = ownerByFile.get(from);
|
|
302
|
-
if (!project)
|
|
303
|
-
return;
|
|
304
|
-
const references = helpers.moduleReferences(ts, sourceFile, project.program.getTypeChecker());
|
|
305
|
-
addUnprovenFindings(sourceFile, project, references, state.errors, config, helpers);
|
|
306
|
-
for (const reference of references.found)
|
|
307
|
-
processReference(from, sourceFile, project, owningWorkspace, reference, state);
|
|
308
|
-
}
|
|
309
|
-
function dependencyGraph(config, loaded, fileGraph, ownerByFile, errors, helpers) {
|
|
310
|
-
const { ts } = loaded;
|
|
311
|
-
const edgeKeys = new Set();
|
|
312
|
-
const host = { ...ts.sys, fileExists: ts.sys.fileExists, readFile: ts.sys.readFile, realpath: ts.sys.realpath };
|
|
313
|
-
const workspaces = helpers.workspaceMetadata(config);
|
|
314
|
-
const workspaceNames = new Set(workspaces.map(item => item.name).filter((name) => Boolean(name)));
|
|
315
|
-
const state = { config, ts, fileMap: fileGraph.fileMap, edges: fileGraph.edges, edgeKeys, errors, workspaces, workspaceNames, host, ownerByFile, helpers, resolutionCount: 0 };
|
|
316
|
-
for (const [from, sourceFile] of fileGraph.fileMap)
|
|
317
|
-
processSourceFile(from, sourceFile, state);
|
|
318
|
-
return { edges: fileGraph.edges, workspaces };
|
|
319
|
-
}
|
|
320
|
-
export function contextFromProjects(config, loaded, paths, helpers) {
|
|
321
|
-
const { ts } = loaded;
|
|
322
|
-
const errors = [];
|
|
323
|
-
const projectState = loadProjectConfigurations(config, ts, helpers, errors);
|
|
324
|
-
const selection = selectProjects(config, paths, ts, projectState, helpers);
|
|
325
|
-
const projects = createProjects(config, paths, projectState, selection, ts, helpers);
|
|
326
|
-
const fileGraph = makeFileGraph(config, projects, helpers);
|
|
327
|
-
const { edges, workspaces } = dependencyGraph(config, loaded, fileGraph, fileGraph.ownerByFile, errors, helpers);
|
|
328
|
-
if (projects.length && fileGraph.files.length === 0)
|
|
329
|
-
errors.push({ ruleId: 'ARCH_NO_SOURCE', message: 'The configured TypeScript projects contain no production TypeScript or JavaScript source.' });
|
|
330
|
-
const { checkerByFile } = fileGraph;
|
|
331
|
-
return { loaded, errors, files: fileGraph.files, edges, programs: projects.map(item => item.program), program: projects[0]?.program ?? null, projects, ts, workspaces,
|
|
332
|
-
// reason: every file the checkers walk belongs to a program; an unknown file still answers null, as before.
|
|
333
|
-
checkerFor: (file) => (checkerByFile.get(helpers.canonical(file)) ?? null),
|
|
334
|
-
workspaceOf: (file) => helpers.workspaceOf(workspaces, helpers.canonical(file)) };
|
|
335
|
-
}
|