@starci/eslint-canon-be 3.8.20 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/runtime/bundle.json +192 -0
- package/runtime/engine/by-code-unit.mjs +6 -6
- package/runtime/engine/model-config.mjs +98 -76
- package/runtime/engine/orca-config.mjs +22 -17
- package/runtime/engine/release-config.mjs +22 -18
- package/runtime/engine/removed-vocabulary.mjs +21 -20
- package/runtime/engine/resources-config.mjs +42 -35
- package/runtime/engine/runtime-root.mjs +11 -13
- package/runtime/engine/secrets.mjs +170 -102
- package/runtime/engine/sonar-config.mjs +16 -13
- package/runtime/engine/temp-root.mjs +3 -9
- package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
- package/runtime/knowledge/hfs/rules.yaml +6 -6
- package/runtime/knowledge/hfs/slots.yaml +2 -2
- package/runtime/knowledge/sonar-gate.yaml +1 -1
- package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
- package/runtime/scripts/api/git/cat-file.mjs +0 -1
- package/runtime/scripts/api/git/config-get.mjs +1 -2
- package/runtime/scripts/api/git/diff.mjs +0 -1
- package/runtime/scripts/api/git/lib.mjs +32 -32
- package/runtime/scripts/api/git/ls-files.mjs +0 -1
- package/runtime/scripts/api/git/ls-tree.mjs +0 -1
- package/runtime/scripts/api/git/merge-base.mjs +2 -3
- package/runtime/scripts/api/git/remote.mjs +0 -1
- package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
- package/runtime/scripts/api/git/show.mjs +0 -1
- package/runtime/scripts/hfs/allows.mjs +37 -38
- package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
- package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
- package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
- package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
- package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
- package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
- package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
- package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
- package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
- package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
- package/runtime/scripts/hfs/architecture/config.mjs +302 -268
- package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
- package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
- package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
- package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
- package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
- package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
- package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
- package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
- package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
- package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
- package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
- package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
- package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
- package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
- package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
- package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
- package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
- package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
- package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
- package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
- package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
- package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
- package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
- package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
- package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
- package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
- package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
- package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
- package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
- package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
- package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
- package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
- package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
- package/runtime/scripts/hfs/architecture/index.mjs +272 -272
- package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
- package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
- package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
- package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
- package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
- package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
- package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
- package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
- package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
- package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
- package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
- package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
- package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
- package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
- package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
- package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
- package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
- package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
- package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
- package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
- package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
- package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
- package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
- package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
- package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
- package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
- package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
- package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
- package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
- package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
- package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
- package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
- package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
- package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
- package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
- package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
- package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
- package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
- package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
- package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
- package/runtime/scripts/hfs/edition-slots.mjs +43 -40
- package/runtime/scripts/hfs/linear-text.mjs +20 -20
- package/runtime/scripts/hfs/param-names.mjs +6 -11
- package/runtime/scripts/hfs/path-findings.mjs +120 -100
- package/runtime/scripts/hfs/repo-identity.mjs +55 -42
- package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
- package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
- package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
- package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
- package/runtime/scripts/hfs/rules/read.mjs +19 -15
- package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
- package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
- package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
- package/runtime/scripts/hfs/slot-classify.mjs +42 -38
- package/runtime/scripts/hfs/slot-errors.mjs +8 -7
- package/runtime/scripts/hfs/slot-imports.mjs +70 -63
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
- package/runtime/scripts/hfs/slot-match.mjs +122 -111
- package/runtime/scripts/hfs/slot-path.mjs +0 -2
- package/runtime/scripts/hfs/slot-required.mjs +65 -59
- package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
- package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
- package/runtime/scripts/hfs/slots.mjs +175 -152
- package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
- package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
- package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
- package/runtime/scripts/hfs/view.mjs +38 -40
- package/runtime/scripts/lib/dockerfile.mjs +131 -114
- package/runtime/scripts/lib/env.mjs +3 -6
- package/runtime/scripts/lib/event-contract.mjs +73 -78
- package/runtime/scripts/lib/git.mjs +15 -16
- package/runtime/scripts/lib/glob.mjs +23 -14
- package/runtime/scripts/lib/grammar-lint.mjs +33 -37
- package/runtime/scripts/lib/i18n.mjs +32 -35
- package/runtime/scripts/lib/in-order.mjs +57 -38
- package/runtime/scripts/lib/json-clone.mjs +45 -36
- package/runtime/scripts/lib/json.mjs +26 -14
- package/runtime/scripts/lib/language.mjs +133 -129
- package/runtime/scripts/lib/list.mjs +9 -14
- package/runtime/scripts/lib/mutation-fence.mjs +10 -5
- package/runtime/scripts/lib/path-key.mjs +53 -38
- package/runtime/scripts/lib/read-text.mjs +30 -17
- package/runtime/scripts/lib/regex.mjs +1 -2
- package/runtime/scripts/lib/same-text.mjs +0 -1
- package/runtime/scripts/lib/sops-envelope.mjs +148 -120
- package/runtime/scripts/lib/stack-services.mjs +128 -129
- package/runtime/scripts/lib/ts-analysis.mjs +28 -26
- package/runtime/scripts/lib/ts-ast.mjs +36 -42
- package/runtime/scripts/lib/walk.mjs +33 -27
- package/runtime/scripts/lib/yaml-cached.mjs +4 -4
|
@@ -1,280 +1,273 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HFS check 5, one name one declaration (knowledge/hfs/rules.yaml R21 and R30, both profiles, errors in every gate):
|
|
3
|
-
* HFS_DUPLICATE_SYMBOL a name an owner's public entry exports, resolved to the file that declares it, that another
|
|
4
|
-
* production file of the repository also declares and exports (function, class, const, enum,
|
|
5
|
-
* type, interface). Two `InjectPrimaryEntityManager` declarations were this. A re-export of one
|
|
6
|
-
* declaration is not a second declaration. There is no allowlist of names.
|
|
7
|
-
* HFS_ALIAS_REEXPORT `export { X as Y }`, `export { default as Y }` and `export * as ns` in production source, and an
|
|
8
|
-
* exported `const Y = X` / `const Y = X.y` (a bare identifier or member that resolves to a function,
|
|
9
|
-
* class, const or enum of the repository), `type Y = X` and `interface Y extends X {}` (no body, no
|
|
10
|
-
* type arguments, no type parameters): a second name for one declaration. Rename the declaration,
|
|
11
|
-
* or import it by its name. A route file (slot fe.route) binding a declaration to a name Next.js
|
|
12
|
-
* requires of a route segment (generateMetadata, generateStaticParams, ...) is not a second name: the
|
|
13
|
-
* framework fixes that name, so no rename can remove it.
|
|
14
|
-
* Specs and tests are not in the graph. The public entry of an owner is found the way dead-exports.mjs finds it.
|
|
15
|
-
*/
|
|
16
1
|
import { canonical } from './config.mjs';
|
|
17
2
|
import { hasAnyFlag } from '../../lib/ts-ast.mjs';
|
|
18
3
|
import { entryOf } from './dead-exports.mjs';
|
|
19
|
-
|
|
20
4
|
export const SYMBOL_RULE_IDS = ['HFS_DUPLICATE_SYMBOL', 'HFS_ALIAS_REEXPORT'];
|
|
21
|
-
|
|
22
5
|
const lineOf = (sourceFile, node) => sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
23
|
-
|
|
6
|
+
/** The modifiers a statement carries (none for a statement that cannot have any). */
|
|
7
|
+
const modifiersOf = (ts, statement) => (ts.canHaveModifiers(statement) ? statement.modifiers : undefined) ?? [];
|
|
24
8
|
/** The names a source file declares at top level and exports: Map<name, line>. Re-exports from another module declare nothing. */
|
|
25
9
|
function addBindingNames(ts, sourceFile, name, into, node) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
10
|
+
if (ts.isIdentifier(name)) {
|
|
11
|
+
into.set(name.text, lineOf(sourceFile, node));
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
for (const element of name.elements) {
|
|
15
|
+
if (ts.isBindingElement(element))
|
|
16
|
+
addBindingNames(ts, sourceFile, element.name, into, node);
|
|
17
|
+
}
|
|
33
18
|
}
|
|
34
|
-
|
|
35
19
|
function recordStatementDeclarations(ts, sourceFile, statement, declared, exported) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
20
|
+
const kind = ts.SyntaxKind;
|
|
21
|
+
const modifiers = modifiersOf(ts, statement);
|
|
22
|
+
const publish = modifiers.some(modifier => modifier.kind === kind.ExportKeyword) && !modifiers.some(modifier => modifier.kind === kind.DefaultKeyword);
|
|
23
|
+
const into = publish ? exported : declared;
|
|
24
|
+
if (ts.isVariableStatement(statement)) {
|
|
25
|
+
for (const declaration of statement.declarationList.declarations)
|
|
26
|
+
addBindingNames(ts, sourceFile, declaration.name, into, declaration);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const declares = [kind.FunctionDeclaration, kind.ClassDeclaration, kind.EnumDeclaration, kind.TypeAliasDeclaration, kind.InterfaceDeclaration];
|
|
30
|
+
const name = statement.name;
|
|
31
|
+
if (name && ts.isIdentifier(name) && declares.includes(statement.kind)) {
|
|
32
|
+
into.set(name.text, lineOf(sourceFile, statement));
|
|
33
|
+
}
|
|
48
34
|
}
|
|
49
|
-
|
|
50
35
|
function exposeLocalExports(localLists, declared, exported) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
36
|
+
for (const element of localLists) {
|
|
37
|
+
const local = (element.propertyName ?? element.name).text;
|
|
38
|
+
if (declared.has(local))
|
|
39
|
+
exported.set(element.name.text, declared.get(local));
|
|
40
|
+
}
|
|
55
41
|
}
|
|
56
|
-
|
|
57
42
|
function declaredExports(ts, sourceFile) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
43
|
+
const declared = new Map();
|
|
44
|
+
const exported = new Map();
|
|
45
|
+
const localLists = [];
|
|
46
|
+
for (const statement of sourceFile.statements) {
|
|
47
|
+
if (ts.isExportDeclaration(statement)) {
|
|
48
|
+
if (!statement.moduleSpecifier && statement.exportClause && ts.isNamedExports(statement.exportClause))
|
|
49
|
+
localLists.push(...statement.exportClause.elements);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
recordStatementDeclarations(ts, sourceFile, statement, declared, exported);
|
|
66
53
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
exposeLocalExports(localLists, declared, exported);
|
|
71
|
-
return exported;
|
|
54
|
+
// `export { local }` of a name declared in this file makes that declaration exported.
|
|
55
|
+
exposeLocalExports(localLists, declared, exported);
|
|
56
|
+
return exported;
|
|
72
57
|
}
|
|
73
|
-
|
|
74
58
|
/** The declaration an export of an entry resolves to: its repository-relative file and its own name, or null. */
|
|
75
59
|
function declaringFile(ts, checker, graph, symbol) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
60
|
+
const resolved = hasAnyFlag(symbol.flags, ts.SymbolFlags.Alias) ? checker.getAliasedSymbol(symbol) : symbol;
|
|
61
|
+
for (const declaration of resolved?.declarations ?? []) {
|
|
62
|
+
const rel = graph.abs(canonical(declaration.getSourceFile().fileName));
|
|
63
|
+
if (rel)
|
|
64
|
+
return { rel, name: resolved.getName() };
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
82
67
|
}
|
|
83
|
-
|
|
84
68
|
function declarationIndex(ts, graph) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
69
|
+
const declarations = new Map(); // name -> Map<rel, line>
|
|
70
|
+
for (const [rel, node] of graph.files) {
|
|
71
|
+
for (const [name, line] of declaredExports(ts, node.sourceFile)) {
|
|
72
|
+
if (name === 'default')
|
|
73
|
+
continue;
|
|
74
|
+
let byFile = declarations.get(name);
|
|
75
|
+
if (!byFile) {
|
|
76
|
+
byFile = new Map();
|
|
77
|
+
declarations.set(name, byFile);
|
|
78
|
+
}
|
|
79
|
+
byFile.set(rel, line);
|
|
80
|
+
}
|
|
91
81
|
}
|
|
92
|
-
|
|
93
|
-
return declarations;
|
|
82
|
+
return declarations;
|
|
94
83
|
}
|
|
95
|
-
|
|
96
84
|
function duplicateViolation(scan, symbol, owner, entry) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
85
|
+
const { ts, checker, graph, declarations, seen } = scan;
|
|
86
|
+
const declaration = declaringFile(ts, checker, graph, symbol);
|
|
87
|
+
if (!declaration || declaration.name === 'default')
|
|
88
|
+
return null;
|
|
89
|
+
const { rel: home, name } = declaration;
|
|
90
|
+
const others = [...(declarations.get(name) ?? [])].filter(([rel]) => rel !== home);
|
|
91
|
+
if (!others.length || seen.has(`${home}\0${name}`))
|
|
92
|
+
return null;
|
|
93
|
+
seen.add(`${home}\0${name}`);
|
|
94
|
+
const where = others.map(([rel, at]) => `${rel}:${at}`);
|
|
95
|
+
return {
|
|
96
|
+
ruleId: 'HFS_DUPLICATE_SYMBOL',
|
|
97
|
+
path: home, line: declarations.get(name)?.get(home) ?? 1, column: 1, name, owner: owner.root, entry,
|
|
98
|
+
otherDeclarations: where,
|
|
99
|
+
message: `${name} is exported through the public entry ${entry} (declared in ${home}) and also declared and exported in ${where.join(', ')}; one name, one declaration: rename or delete the copy.`,
|
|
100
|
+
};
|
|
111
101
|
}
|
|
112
|
-
|
|
113
102
|
function inspectPublicOwner(ts, context, graph, config, declarations, seen, [key, owner]) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
103
|
+
if (owner.tier === 'app')
|
|
104
|
+
return { violations: [], surface: 0 };
|
|
105
|
+
const entry = entryOf(graph, config, key, owner);
|
|
106
|
+
if (!entry)
|
|
107
|
+
return { violations: [], surface: 0 };
|
|
108
|
+
const entryFile = graph.files.get(entry);
|
|
109
|
+
const checker = context.checkerFor(entryFile.abs);
|
|
110
|
+
const moduleSymbol = checker?.getSymbolAtLocation(entryFile.sourceFile);
|
|
111
|
+
if (!moduleSymbol)
|
|
112
|
+
return { violations: [], surface: 0 };
|
|
113
|
+
const violations = [];
|
|
114
|
+
const scan = { ts, checker, graph, declarations, seen };
|
|
115
|
+
let surface = 0;
|
|
116
|
+
for (const symbol of checker.getExportsOfModule(moduleSymbol)) {
|
|
117
|
+
surface += 1;
|
|
118
|
+
const violation = duplicateViolation(scan, symbol, owner, entry);
|
|
119
|
+
if (violation)
|
|
120
|
+
violations.push(violation);
|
|
121
|
+
}
|
|
122
|
+
return { violations, surface };
|
|
130
123
|
}
|
|
131
|
-
|
|
132
124
|
function duplicateSymbols({ context, graph, config }) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
125
|
+
const declarations = declarationIndex(context.ts, graph);
|
|
126
|
+
const violations = [];
|
|
127
|
+
const seen = new Set();
|
|
128
|
+
let surface = 0;
|
|
129
|
+
for (const item of graph.ownerRoots) {
|
|
130
|
+
const result = inspectPublicOwner(context.ts, context, graph, config, declarations, seen, item);
|
|
131
|
+
violations.push(...result.violations);
|
|
132
|
+
surface += result.surface;
|
|
133
|
+
}
|
|
134
|
+
return { violations, surface };
|
|
143
135
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
&& !(statement.modifiers ?? []).some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword);
|
|
136
|
+
const isExported = (ts, statement) => modifiersOf(ts, statement).some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword)
|
|
137
|
+
&& !modifiersOf(ts, statement).some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword);
|
|
147
138
|
const bareAliasExpression = (ts, expression) => ts.isIdentifier(expression)
|
|
148
|
-
|
|
149
|
-
|
|
139
|
+
|| (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.name) && (ts.isIdentifier(expression.expression) || ts.isPropertyAccessExpression(expression.expression)));
|
|
150
140
|
/** The declaration `node` (an identifier, a member or a qualified name) resolves to when a repository file declares it as one of `kinds`, else null. */
|
|
151
141
|
function repositoryDeclaration(ts, checker, graph, node, kinds) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
142
|
+
let target = node;
|
|
143
|
+
if (ts.isPropertyAccessExpression(node))
|
|
144
|
+
target = node.name;
|
|
145
|
+
else if (ts.isQualifiedName(node))
|
|
146
|
+
target = node.right;
|
|
147
|
+
const symbol = checker?.getSymbolAtLocation(target);
|
|
148
|
+
if (!symbol)
|
|
149
|
+
return null;
|
|
150
|
+
const resolved = hasAnyFlag(symbol.flags, ts.SymbolFlags.Alias) ? checker.getAliasedSymbol(symbol) : symbol;
|
|
151
|
+
const home = (resolved?.declarations ?? []).some(declaration => kinds.includes(declaration.kind) && graph.abs(canonical(declaration.getSourceFile().fileName)));
|
|
152
|
+
return home ? resolved.getName() : null;
|
|
160
153
|
}
|
|
161
|
-
|
|
162
154
|
/**
|
|
163
155
|
* The exported declarations of one top-level statement that only rename another declaration of the repository:
|
|
164
156
|
* `export const Y = X` and `export const Y = X.y` (X or y a function, class, const or enum), `export type Y = X` and
|
|
165
157
|
* `export interface Y extends X {}` with no body, no type arguments and no type parameters.
|
|
166
158
|
*/
|
|
167
159
|
function variableAliases(ts, checker, graph, statement, kind) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
160
|
+
const found = [];
|
|
161
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
162
|
+
if (!ts.isIdentifier(declaration.name) || !declaration.initializer || !bareAliasExpression(ts, declaration.initializer))
|
|
163
|
+
continue;
|
|
164
|
+
const of = repositoryDeclaration(ts, checker, graph, declaration.initializer, [kind.FunctionDeclaration, kind.ClassDeclaration, kind.VariableDeclaration, kind.EnumDeclaration]);
|
|
165
|
+
if (of && of !== declaration.name.text)
|
|
166
|
+
found.push({ node: declaration, name: declaration.name.text, of, form: `export const ${declaration.name.text} = ${declaration.initializer.getText()}` });
|
|
167
|
+
}
|
|
168
|
+
return found;
|
|
175
169
|
}
|
|
176
|
-
|
|
177
170
|
function typeAliasOf(ts, checker, graph, statement, kind) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
171
|
+
if (statement.typeParameters?.length || !ts.isTypeReferenceNode(statement.type) || statement.type.typeArguments?.length)
|
|
172
|
+
return [];
|
|
173
|
+
const of = repositoryDeclaration(ts, checker, graph, statement.type.typeName, [kind.InterfaceDeclaration, kind.TypeAliasDeclaration, kind.ClassDeclaration, kind.EnumDeclaration]);
|
|
174
|
+
return of && of !== statement.name.text
|
|
175
|
+
? [{ node: statement, name: statement.name.text, of, form: `export type ${statement.name.text} = ${statement.type.getText()}` }]
|
|
176
|
+
: [];
|
|
183
177
|
}
|
|
184
|
-
|
|
185
178
|
function interfaceAliasOf(ts, checker, graph, statement, kind) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
179
|
+
if (statement.members.length || statement.typeParameters?.length)
|
|
180
|
+
return [];
|
|
181
|
+
const heritage = (statement.heritageClauses ?? []).flatMap((clause) => clause.types);
|
|
182
|
+
const only = heritage[0];
|
|
183
|
+
if (heritage.length !== 1 || !only || only.typeArguments?.length || !bareAliasExpression(ts, only.expression))
|
|
184
|
+
return [];
|
|
185
|
+
const of = repositoryDeclaration(ts, checker, graph, only.expression, [kind.InterfaceDeclaration, kind.TypeAliasDeclaration, kind.ClassDeclaration]);
|
|
186
|
+
return of && of !== statement.name.text
|
|
187
|
+
? [{ node: statement, name: statement.name.text, of, form: `export interface ${statement.name.text} extends ${only.expression.getText()} {}` }]
|
|
188
|
+
: [];
|
|
194
189
|
}
|
|
195
|
-
|
|
196
190
|
function declarationAliases(ts, checker, graph, statement) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
191
|
+
const kind = ts.SyntaxKind;
|
|
192
|
+
if (!checker || !isExported(ts, statement))
|
|
193
|
+
return [];
|
|
194
|
+
if (ts.isVariableStatement(statement))
|
|
195
|
+
return variableAliases(ts, checker, graph, statement, kind);
|
|
196
|
+
if (ts.isTypeAliasDeclaration(statement))
|
|
197
|
+
return typeAliasOf(ts, checker, graph, statement, kind);
|
|
198
|
+
if (ts.isInterfaceDeclaration(statement))
|
|
199
|
+
return interfaceAliasOf(ts, checker, graph, statement, kind);
|
|
200
|
+
return [];
|
|
203
201
|
}
|
|
204
|
-
|
|
205
202
|
/** The names Next.js reads from a route segment file (layout, page, route, ...): the framework fixes them. */
|
|
206
203
|
const NEXT_SEGMENT_EXPORTS = new Set([
|
|
207
|
-
|
|
208
|
-
|
|
204
|
+
'generateMetadata', 'metadata', 'generateViewport', 'viewport', 'generateStaticParams', 'generateImageMetadata', 'generateSitemaps',
|
|
205
|
+
'dynamic', 'dynamicParams', 'revalidate', 'fetchCache', 'runtime', 'preferredRegion', 'maxDuration',
|
|
209
206
|
]);
|
|
210
207
|
const ROUTE_SLOT = 'fe.route';
|
|
211
|
-
|
|
212
208
|
function isFrameworkName(node, name) {
|
|
213
|
-
|
|
209
|
+
return node.slot === ROUTE_SLOT && NEXT_SEGMENT_EXPORTS.has(name);
|
|
214
210
|
}
|
|
215
|
-
|
|
216
211
|
function addDeclarationAliasViolations(env, rel, node, statement, point, violations) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
212
|
+
const { ts, checker, graph } = env;
|
|
213
|
+
for (const alias of declarationAliases(ts, checker, graph, statement)) {
|
|
214
|
+
if (isFrameworkName(node, alias.name))
|
|
215
|
+
continue;
|
|
216
|
+
const at = point(alias.node);
|
|
217
|
+
violations.push({
|
|
218
|
+
ruleId: 'HFS_ALIAS_REEXPORT', path: rel, line: at.line + 1, column: at.character + 1, name: alias.name, aliasOf: alias.of,
|
|
219
|
+
message: `${alias.form} in ${rel} gives the declaration ${alias.of} a second name: use ${alias.of} where ${alias.name} is used and delete ${alias.name}.`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
226
222
|
}
|
|
227
|
-
|
|
228
223
|
function addNamespaceAliasViolation(rel, statement, clause, point, violations) {
|
|
229
|
-
|
|
230
|
-
violations.push({
|
|
231
|
-
ruleId: 'HFS_ALIAS_REEXPORT', path: rel, line: at.line + 1, column: at.character + 1, name: clause.name.text,
|
|
232
|
-
message: `export * as ${clause.name.text} in ${rel} publishes a whole module under a second name: export the declarations it holds by their own names.`,
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function addNamedAliasViolations(rel, node, clause, point, violations) {
|
|
237
|
-
for (const element of clause.elements) {
|
|
238
|
-
if (!element.propertyName || element.propertyName.text === element.name.text || isFrameworkName(node, element.name.text)) continue;
|
|
239
|
-
const at = point(element);
|
|
224
|
+
const at = point(statement);
|
|
240
225
|
violations.push({
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
message: `export { ${element.propertyName.text} as ${element.name.text} } in ${rel} gives one declaration a second name: rename the declaration to ${element.name.text}, or import and export it as ${element.propertyName.text}.`,
|
|
226
|
+
ruleId: 'HFS_ALIAS_REEXPORT', path: rel, line: at.line + 1, column: at.character + 1, name: clause.name.text,
|
|
227
|
+
message: `export * as ${clause.name.text} in ${rel} publishes a whole module under a second name: export the declarations it holds by their own names.`,
|
|
244
228
|
});
|
|
245
|
-
}
|
|
246
229
|
}
|
|
247
|
-
|
|
230
|
+
function addNamedAliasViolations(rel, node, clause, point, violations) {
|
|
231
|
+
for (const element of clause.elements) {
|
|
232
|
+
if (!element.propertyName || element.propertyName.text === element.name.text || isFrameworkName(node, element.name.text))
|
|
233
|
+
continue;
|
|
234
|
+
const at = point(element);
|
|
235
|
+
violations.push({
|
|
236
|
+
ruleId: 'HFS_ALIAS_REEXPORT', path: rel, line: at.line + 1, column: at.character + 1,
|
|
237
|
+
name: element.name.text, aliasOf: element.propertyName.text,
|
|
238
|
+
message: `export { ${element.propertyName.text} as ${element.name.text} } in ${rel} gives one declaration a second name: rename the declaration to ${element.name.text}, or import and export it as ${element.propertyName.text}.`,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
248
242
|
function addExportAliasViolations(ts, rel, node, statement, point, violations) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
243
|
+
if (!ts.isExportDeclaration(statement) || !statement.exportClause)
|
|
244
|
+
return;
|
|
245
|
+
const clause = statement.exportClause;
|
|
246
|
+
if (ts.isNamespaceExport(clause)) {
|
|
247
|
+
addNamespaceAliasViolation(rel, statement, clause, point, violations);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
addNamedAliasViolations(rel, node, clause, point, violations);
|
|
256
251
|
}
|
|
257
|
-
|
|
258
252
|
function addStatementAliasViolations(context, graph, rel, node, statement, checker, violations) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
253
|
+
const point = (target) => node.sourceFile.getLineAndCharacterOfPosition(target.getStart(node.sourceFile));
|
|
254
|
+
addDeclarationAliasViolations({ ts: context.ts, checker, graph }, rel, node, statement, point, violations);
|
|
255
|
+
addExportAliasViolations(context.ts, rel, node, statement, point, violations);
|
|
262
256
|
}
|
|
263
|
-
|
|
264
257
|
function aliasReexports({ context, graph }) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
258
|
+
const violations = [];
|
|
259
|
+
for (const [rel, node] of graph.files) {
|
|
260
|
+
const checker = context.checkerFor(node.abs);
|
|
261
|
+
for (const statement of node.sourceFile.statements)
|
|
262
|
+
addStatementAliasViolations(context, graph, rel, node, statement, checker, violations);
|
|
263
|
+
}
|
|
264
|
+
return { violations };
|
|
271
265
|
}
|
|
272
|
-
|
|
273
266
|
export function checkSymbols(input) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
267
|
+
const duplicates = duplicateSymbols(input);
|
|
268
|
+
const aliases = aliasReexports(input);
|
|
269
|
+
return {
|
|
270
|
+
violations: [...duplicates.violations, ...aliases.violations],
|
|
271
|
+
coverage: { status: 'checked', publicNames: duplicates.surface, duplicates: duplicates.violations.length, aliases: aliases.violations.length },
|
|
272
|
+
};
|
|
280
273
|
}
|