@starci/eslint-canon-be 3.8.19 → 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.
Files changed (164) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +1 -1
  3. package/runtime/bundle.json +192 -0
  4. package/runtime/engine/by-code-unit.mjs +6 -6
  5. package/runtime/engine/model-config.mjs +98 -76
  6. package/runtime/engine/orca-config.mjs +22 -17
  7. package/runtime/engine/release-config.mjs +22 -18
  8. package/runtime/engine/removed-vocabulary.mjs +21 -20
  9. package/runtime/engine/resources-config.mjs +42 -35
  10. package/runtime/engine/runtime-root.mjs +11 -13
  11. package/runtime/engine/secrets.mjs +170 -102
  12. package/runtime/engine/sonar-config.mjs +16 -13
  13. package/runtime/engine/temp-root.mjs +3 -9
  14. package/runtime/knowledge/hfs/canon-pins.yaml +10 -10
  15. package/runtime/knowledge/hfs/rules.yaml +6 -6
  16. package/runtime/knowledge/hfs/slots.yaml +2 -2
  17. package/runtime/knowledge/sonar-gate.yaml +1 -1
  18. package/runtime/packages/hfs/templates/app/package-scripts/package.json +1 -1
  19. package/runtime/scripts/api/git/cat-file.mjs +0 -1
  20. package/runtime/scripts/api/git/config-get.mjs +1 -2
  21. package/runtime/scripts/api/git/diff.mjs +0 -1
  22. package/runtime/scripts/api/git/lib.mjs +32 -32
  23. package/runtime/scripts/api/git/ls-files.mjs +0 -1
  24. package/runtime/scripts/api/git/ls-tree.mjs +0 -1
  25. package/runtime/scripts/api/git/merge-base.mjs +2 -3
  26. package/runtime/scripts/api/git/remote.mjs +0 -1
  27. package/runtime/scripts/api/git/rev-parse-query.mjs +0 -1
  28. package/runtime/scripts/api/git/show.mjs +0 -1
  29. package/runtime/scripts/hfs/allows.mjs +37 -38
  30. package/runtime/scripts/hfs/architecture/ast-walks.mjs +180 -163
  31. package/runtime/scripts/hfs/architecture/automatic-gates.mjs +80 -73
  32. package/runtime/scripts/hfs/architecture/backend-app-files.mjs +88 -77
  33. package/runtime/scripts/hfs/architecture/backend.mjs +378 -357
  34. package/runtime/scripts/hfs/architecture/background-unowned.mjs +201 -187
  35. package/runtime/scripts/hfs/architecture/canon-clones.mjs +260 -209
  36. package/runtime/scripts/hfs/architecture/canon-jsx-clones.mjs +149 -116
  37. package/runtime/scripts/hfs/architecture/client-reaches-server.mjs +72 -68
  38. package/runtime/scripts/hfs/architecture/clones.mjs +212 -217
  39. package/runtime/scripts/hfs/architecture/config-unread.mjs +7 -9
  40. package/runtime/scripts/hfs/architecture/config.mjs +302 -268
  41. package/runtime/scripts/hfs/architecture/connection-map.mjs +258 -225
  42. package/runtime/scripts/hfs/architecture/constructor-deps.mjs +80 -71
  43. package/runtime/scripts/hfs/architecture/context-coupling.mjs +82 -77
  44. package/runtime/scripts/hfs/architecture/context-map.mjs +267 -234
  45. package/runtime/scripts/hfs/architecture/context-owner.mjs +73 -61
  46. package/runtime/scripts/hfs/architecture/context-platform-tables.mjs +47 -43
  47. package/runtime/scripts/hfs/architecture/context-transaction.mjs +54 -52
  48. package/runtime/scripts/hfs/architecture/contract-fixture-guard.mjs +113 -104
  49. package/runtime/scripts/hfs/architecture/contracts-readonly.mjs +288 -270
  50. package/runtime/scripts/hfs/architecture/contracts.mjs +408 -393
  51. package/runtime/scripts/hfs/architecture/cross-app-duplicate.mjs +50 -51
  52. package/runtime/scripts/hfs/architecture/dead-exports-bindings.mjs +143 -119
  53. package/runtime/scripts/hfs/architecture/dead-exports.mjs +286 -228
  54. package/runtime/scripts/hfs/architecture/default-deny.mjs +129 -110
  55. package/runtime/scripts/hfs/architecture/doc-language.mjs +24 -19
  56. package/runtime/scripts/hfs/architecture/entity-registry.mjs +20 -22
  57. package/runtime/scripts/hfs/architecture/entrypoint.mjs +47 -45
  58. package/runtime/scripts/hfs/architecture/error-codes.mjs +37 -35
  59. package/runtime/scripts/hfs/architecture/error-masked.mjs +55 -59
  60. package/runtime/scripts/hfs/architecture/fe-slot-allows.mjs +24 -23
  61. package/runtime/scripts/hfs/architecture/feature-shape.mjs +40 -38
  62. package/runtime/scripts/hfs/architecture/framework-pinned.mjs +21 -21
  63. package/runtime/scripts/hfs/architecture/frontend-block-render.mjs +106 -98
  64. package/runtime/scripts/hfs/architecture/frontend-grammar.mjs +153 -150
  65. package/runtime/scripts/hfs/architecture/frontend-routing.mjs +161 -149
  66. package/runtime/scripts/hfs/architecture/frontend-world-render.mjs +343 -305
  67. package/runtime/scripts/hfs/architecture/frontend-world.mjs +340 -294
  68. package/runtime/scripts/hfs/architecture/frontend.mjs +174 -184
  69. package/runtime/scripts/hfs/architecture/function-spec-providers.mjs +124 -106
  70. package/runtime/scripts/hfs/architecture/hfs-graph.mjs +52 -48
  71. package/runtime/scripts/hfs/architecture/hooks-are-hooks.mjs +81 -71
  72. package/runtime/scripts/hfs/architecture/i18n-keys.mjs +155 -141
  73. package/runtime/scripts/hfs/architecture/identity.mjs +141 -125
  74. package/runtime/scripts/hfs/architecture/index.mjs +272 -272
  75. package/runtime/scripts/hfs/architecture/injection-token-exported.mjs +45 -43
  76. package/runtime/scripts/hfs/architecture/jwt-ast.mjs +195 -176
  77. package/runtime/scripts/hfs/architecture/machine-ast.mjs +163 -155
  78. package/runtime/scripts/hfs/architecture/managed-scripts.mjs +19 -20
  79. package/runtime/scripts/hfs/architecture/module-per-transport.mjs +177 -172
  80. package/runtime/scripts/hfs/architecture/next-data-contract.mjs +100 -78
  81. package/runtime/scripts/hfs/architecture/next-data-inference.mjs +170 -154
  82. package/runtime/scripts/hfs/architecture/next-data-key.mjs +311 -275
  83. package/runtime/scripts/hfs/architecture/next-data-symbols.mjs +17 -17
  84. package/runtime/scripts/hfs/architecture/next-data.mjs +351 -334
  85. package/runtime/scripts/hfs/architecture/operation-contract-roots.mjs +39 -37
  86. package/runtime/scripts/hfs/architecture/owners.mjs +97 -98
  87. package/runtime/scripts/hfs/architecture/package-shape.mjs +56 -55
  88. package/runtime/scripts/hfs/architecture/presentation.mjs +138 -130
  89. package/runtime/scripts/hfs/architecture/reachability.mjs +258 -241
  90. package/runtime/scripts/hfs/architecture/register-once.mjs +152 -139
  91. package/runtime/scripts/hfs/architecture/registration.mjs +301 -267
  92. package/runtime/scripts/hfs/architecture/required-files.mjs +172 -167
  93. package/runtime/scripts/hfs/architecture/route-files-thin.mjs +85 -77
  94. package/runtime/scripts/hfs/architecture/schema-owner.mjs +206 -181
  95. package/runtime/scripts/hfs/architecture/server-action.mjs +48 -48
  96. package/runtime/scripts/hfs/architecture/side-boundary.mjs +2 -5
  97. package/runtime/scripts/hfs/architecture/source-names-shape.mjs +261 -259
  98. package/runtime/scripts/hfs/architecture/source-names.mjs +304 -278
  99. package/runtime/scripts/hfs/architecture/sql-keys.mjs +277 -194
  100. package/runtime/scripts/hfs/architecture/sql-owner.mjs +236 -194
  101. package/runtime/scripts/hfs/architecture/sql-returning.mjs +46 -45
  102. package/runtime/scripts/hfs/architecture/sql-token-forms.mjs +86 -74
  103. package/runtime/scripts/hfs/architecture/sql-tokens.mjs +456 -341
  104. package/runtime/scripts/hfs/architecture/staged-consumers.mjs +35 -30
  105. package/runtime/scripts/hfs/architecture/surface.mjs +64 -74
  106. package/runtime/scripts/hfs/architecture/symbols.mjs +209 -216
  107. package/runtime/scripts/hfs/architecture/test-world-files.mjs +165 -155
  108. package/runtime/scripts/hfs/architecture/tiers.mjs +161 -154
  109. package/runtime/scripts/hfs/architecture/transport-owner.mjs +155 -149
  110. package/runtime/scripts/hfs/architecture/type-context.mjs +255 -250
  111. package/runtime/scripts/hfs/architecture/typescript.mjs +358 -318
  112. package/runtime/scripts/hfs/architecture/unit-spec-providers.mjs +110 -92
  113. package/runtime/scripts/hfs/declaration-shape.mjs +140 -103
  114. package/runtime/scripts/hfs/declaration-slots.mjs +64 -61
  115. package/runtime/scripts/hfs/edition-slots.mjs +43 -40
  116. package/runtime/scripts/hfs/linear-text.mjs +20 -20
  117. package/runtime/scripts/hfs/param-names.mjs +6 -11
  118. package/runtime/scripts/hfs/path-findings.mjs +120 -100
  119. package/runtime/scripts/hfs/repo-identity.mjs +55 -42
  120. package/runtime/scripts/hfs/rule-catalog.mjs +353 -257
  121. package/runtime/scripts/hfs/rule-params-shape.mjs +60 -47
  122. package/runtime/scripts/hfs/rules/database-migrations.mjs +428 -388
  123. package/runtime/scripts/hfs/rules/fe-no-tests.mjs +37 -36
  124. package/runtime/scripts/hfs/rules/read.mjs +19 -15
  125. package/runtime/scripts/hfs/rules/secrets.mjs +1 -1
  126. package/runtime/scripts/hfs/rules/spec-placement.mjs +12 -13
  127. package/runtime/scripts/hfs/slot-app-view.mjs +69 -74
  128. package/runtime/scripts/hfs/slot-classify.mjs +42 -38
  129. package/runtime/scripts/hfs/slot-errors.mjs +8 -7
  130. package/runtime/scripts/hfs/slot-imports.mjs +70 -63
  131. package/runtime/scripts/hfs/slot-manifest-shape.mjs +74 -52
  132. package/runtime/scripts/hfs/slot-match.mjs +122 -111
  133. package/runtime/scripts/hfs/slot-path.mjs +0 -2
  134. package/runtime/scripts/hfs/slot-required.mjs +65 -59
  135. package/runtime/scripts/hfs/slot-semantic-problems.mjs +138 -112
  136. package/runtime/scripts/hfs/slot-side-problems.mjs +23 -19
  137. package/runtime/scripts/hfs/slots.mjs +175 -152
  138. package/runtime/scripts/hfs/test-world-slot.mjs +0 -1
  139. package/runtime/scripts/hfs/trailing-slashes.mjs +4 -4
  140. package/runtime/scripts/hfs/typescript-programs.mjs +66 -69
  141. package/runtime/scripts/hfs/view.mjs +38 -40
  142. package/runtime/scripts/lib/dockerfile.mjs +131 -114
  143. package/runtime/scripts/lib/env.mjs +3 -6
  144. package/runtime/scripts/lib/event-contract.mjs +73 -78
  145. package/runtime/scripts/lib/git.mjs +15 -16
  146. package/runtime/scripts/lib/glob.mjs +23 -14
  147. package/runtime/scripts/lib/grammar-lint.mjs +33 -37
  148. package/runtime/scripts/lib/i18n.mjs +32 -35
  149. package/runtime/scripts/lib/in-order.mjs +57 -38
  150. package/runtime/scripts/lib/json-clone.mjs +45 -36
  151. package/runtime/scripts/lib/json.mjs +26 -14
  152. package/runtime/scripts/lib/language.mjs +133 -129
  153. package/runtime/scripts/lib/list.mjs +9 -14
  154. package/runtime/scripts/lib/mutation-fence.mjs +10 -5
  155. package/runtime/scripts/lib/path-key.mjs +53 -38
  156. package/runtime/scripts/lib/read-text.mjs +30 -17
  157. package/runtime/scripts/lib/regex.mjs +1 -2
  158. package/runtime/scripts/lib/same-text.mjs +0 -1
  159. package/runtime/scripts/lib/sops-envelope.mjs +148 -120
  160. package/runtime/scripts/lib/stack-services.mjs +128 -129
  161. package/runtime/scripts/lib/ts-analysis.mjs +28 -26
  162. package/runtime/scripts/lib/ts-ast.mjs +36 -42
  163. package/runtime/scripts/lib/walk.mjs +33 -27
  164. package/runtime/scripts/lib/yaml-cached.mjs +4 -4
@@ -1,6 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
2
  import path from 'node:path';
3
-
4
3
  /**
5
4
  * R21 `cross-app-duplicate` (FE_CROSS_APP_DUPLICATE): the slot manifest's `crossApp` law says apps never import each other and
6
5
  * shared code is a `packages/<pkg>` slot. `HFS_DUPLICATE_CODE` sees blocks of a size inside the owner graph; this sees whole
@@ -13,70 +12,70 @@ import path from 'node:path';
13
12
  * Not source, so not judged: declaration files and everything that is not .ts/.tsx (JSON catalogs, CSS).
14
13
  */
15
14
  export const CROSS_APP_DUPLICATE_RULE_IDS = ['FE_CROSS_APP_DUPLICATE'];
16
-
17
15
  const RULE = 'FE_CROSS_APP_DUPLICATE';
18
16
  const SOURCE = /\.[cm]?tsx?$/u;
19
-
20
17
  /** The app a file belongs to: the slot binding when a slot owns it, else the declared app whose directory holds it. */
21
18
  const appOf = (resolver, declaredApps, rel) => resolver.classifyPath(rel).bindings?.app ?? declaredApps.find(name => rel.startsWith(`apps/${name}/`)) ?? null;
22
-
23
19
  const onlyModuleEdges = (ts, file) => file.sourceFile.statements.every(statement => ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)
24
- || (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression)));
25
-
20
+ || (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression)));
26
21
  const leafText = (SyntaxKind, node, sourceFile) => (node.kind === SyntaxKind.JsxText ? node.getText(sourceFile).replace(/\s+/gu, ' ').trim() : node.getText(sourceFile));
27
-
28
22
  function collectTokens(SyntaxKind, sourceFile, node, parts) {
29
- if (node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode) return;
30
- if (node.kind === SyntaxKind.JsxText && node.containsOnlyTriviaWhiteSpaces) return;
31
- const children = node.getChildren(sourceFile);
32
- if (!children.length) {
33
- if (node.kind !== SyntaxKind.EndOfFileToken) parts.push(leafText(SyntaxKind, node, sourceFile));
34
- return;
35
- }
36
- for (const child of children) collectTokens(SyntaxKind, sourceFile, child, parts);
23
+ if (node.kind >= SyntaxKind.FirstJSDocNode && node.kind <= SyntaxKind.LastJSDocNode)
24
+ return;
25
+ if (node.kind === SyntaxKind.JsxText && node.containsOnlyTriviaWhiteSpaces)
26
+ return;
27
+ const children = node.getChildren(sourceFile);
28
+ if (!children.length) {
29
+ if (node.kind !== SyntaxKind.EndOfFileToken)
30
+ parts.push(leafText(SyntaxKind, node, sourceFile));
31
+ return;
32
+ }
33
+ for (const child of children)
34
+ collectTokens(SyntaxKind, sourceFile, child, parts);
37
35
  }
38
-
39
36
  const tokenText = (SyntaxKind, sourceFile) => {
40
- const parts = [];
41
- collectTokens(SyntaxKind, sourceFile, sourceFile, parts);
42
- return parts.join('\u0000');
37
+ const parts = [];
38
+ collectTokens(SyntaxKind, sourceFile, sourceFile, parts);
39
+ return parts.join('\u0000');
43
40
  };
44
-
45
41
  /** Groups the comparable files by the hash of their token text; returns the groups and the number of files compared. */
46
42
  function groupByContent(graph, ts, declaredApps) {
47
- const groups = new Map();
48
- let files = 0;
49
- for (const file of graph.files.values()) {
50
- if (!SOURCE.test(file.rel) || /\.d\.[cm]?tsx?$/u.test(file.rel)) continue;
51
- const app = appOf(graph.resolver, declaredApps, file.rel);
52
- if (!app || onlyModuleEdges(ts, file) || !file.sourceFile.statements.length) continue;
53
- files += 1;
54
- const key = createHash('sha256').update(tokenText(ts.SyntaxKind, file.sourceFile)).digest('hex');
55
- if (!groups.has(key)) groups.set(key, []);
56
- groups.get(key).push({ rel: file.rel, app });
57
- }
58
- return { groups, files };
43
+ const groups = new Map();
44
+ let files = 0;
45
+ for (const file of graph.files.values()) {
46
+ if (!SOURCE.test(file.rel) || /\.d\.[cm]?tsx?$/u.test(file.rel))
47
+ continue;
48
+ const app = appOf(graph.resolver, declaredApps, file.rel);
49
+ if (!app || onlyModuleEdges(ts, file) || !file.sourceFile.statements.length)
50
+ continue;
51
+ files += 1;
52
+ const key = createHash('sha256').update(tokenText(ts.SyntaxKind, file.sourceFile)).digest('hex');
53
+ if (!groups.has(key))
54
+ groups.set(key, []);
55
+ groups.get(key)?.push({ rel: file.rel, app });
56
+ }
57
+ return { groups, files };
59
58
  }
60
-
61
59
  const duplicateViolation = (group, item) => {
62
- const twins = group.filter(other => other.app !== item.app).map(other => other.rel);
63
- return {
64
- ruleId: RULE, path: item.rel, line: 1, column: 1, twins,
65
- message: `${item.rel} is token-for-token the same file as ${twins.join(', ')} in another app (comments and whitespace ignored); shared code is a packages/<pkg> slot. Move it to a package (packages/<pkg>) and let each app re-export it (${path.posix.basename(item.rel)} keeps only an import or export statement).`,
66
- };
60
+ const twins = group.filter(other => other.app !== item.app).map(other => other.rel);
61
+ return {
62
+ ruleId: RULE, path: item.rel, line: 1, column: 1, twins,
63
+ message: `${item.rel} is token-for-token the same file as ${twins.join(', ')} in another app (comments and whitespace ignored); shared code is a packages/<pkg> slot. Move it to a package (packages/<pkg>) and let each app re-export it (${path.posix.basename(item.rel)} keeps only an import or export statement).`,
64
+ };
67
65
  };
68
-
69
66
  export function checkCrossAppDuplicate({ graph, context }) {
70
- const ts = context.ts;
71
- const declaredApps = graph.resolver.repo.apps.map(app => app.name);
72
- const { groups, files } = groupByContent(graph, ts, declaredApps);
73
- const violations = [];
74
- let duplicated = 0;
75
- for (const group of groups.values()) {
76
- if (new Set(group.map(item => item.app)).size < 2) continue;
77
- duplicated += 1;
78
- group.sort((a, b) => a.rel.localeCompare(b.rel));
79
- for (const item of group) violations.push(duplicateViolation(group, item));
80
- }
81
- return { violations, coverage: { status: 'checked', files, duplicatedGroups: duplicated } };
67
+ const ts = context.ts;
68
+ const declaredApps = graph.resolver.repo.apps.map((app) => app.name);
69
+ const { groups, files } = groupByContent(graph, ts, declaredApps);
70
+ const violations = [];
71
+ let duplicated = 0;
72
+ for (const group of groups.values()) {
73
+ if (new Set(group.map(item => item.app)).size < 2)
74
+ continue;
75
+ duplicated += 1;
76
+ group.sort((a, b) => a.rel.localeCompare(b.rel));
77
+ for (const item of group)
78
+ violations.push(duplicateViolation(group, item));
79
+ }
80
+ return { violations, coverage: { status: 'checked', files, duplicatedGroups: duplicated } };
82
81
  }
@@ -1,149 +1,173 @@
1
1
  // Import and namespace consumption analysis shared by dead-export edges.
2
2
  import path from 'node:path';
3
-
4
3
  /** Disk/staged test consumers have no production checker; bind their local names without resolving dependencies. */
5
4
  const namespaceChecker = (context, sourceFile, namespace) => {
6
- if (context.namespaceCheckers.has(sourceFile)) return context.namespaceCheckers.get(sourceFile);
7
- const { ts } = context;
8
- let checker = context.checkerFor?.(sourceFile.fileName);
9
- if (!checker?.getSymbolAtLocation(namespace.name)) {
10
- const options = { noResolve: true, noLib: true, types: [], allowJs: true, target: ts.ScriptTarget.Latest };
11
- const host = ts.createCompilerHost(options);
12
- const filename = path.resolve(sourceFile.fileName);
13
- host.getSourceFile = file => path.resolve(file) === filename ? sourceFile : undefined;
14
- host.fileExists = file => path.resolve(file) === filename;
15
- host.readFile = file => path.resolve(file) === filename ? sourceFile.text : undefined;
16
- checker = ts.createProgram([filename], options, host).getTypeChecker();
17
- }
18
- context.namespaceCheckers.set(sourceFile, checker);
19
- return checker;
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;
20
21
  };
21
-
22
- const transparentExpression = (ts, node) => node && (ts.isParenthesizedExpression(node) || ts.isAsExpression(node)
23
- || ts.isTypeAssertionExpression(node) || ts.isNonNullExpression(node) || ts.isSatisfiesExpression(node));
24
-
25
- const literalMember = (ts, node) => {
26
- while (transparentExpression(ts, node)) node = node.expression;
27
- return node && (ts.isStringLiteralLike(node) || ts.isNumericLiteral(node)) ? node.text : null;
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;
28
29
  };
29
-
30
30
  const destructuredMember = (ts, name) => {
31
- if (ts.isIdentifier(name)) return name.text;
32
- if (ts.isComputedPropertyName(name)) return literalMember(ts, name.expression);
33
- return literalMember(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);
34
36
  };
35
-
36
37
  /** A destructuring alias consumes the property's name, while a rest/computed binding may consume every export. */
37
38
  const destructuredBindings = (ts, pattern) => {
38
- const names = [];
39
- const elements = ts.isObjectBindingPattern(pattern) ? pattern.elements : pattern.properties;
40
- for (const element of elements) {
41
- if (element.dotDotDotToken || ts.isSpreadAssignment(element)) return { all: true, pairs: [] };
42
- const name = destructuredMember(ts, element.propertyName ?? element.name);
43
- if (name === null) return { all: true, pairs: [] };
44
- names.push(name);
45
- }
46
- return { all: false, pairs: names.map(imported => ({ imported, exported: null })) };
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 })) };
47
50
  };
48
-
49
51
  const namespaceReference = (ts, checker, symbol, node) => {
50
- if (checker.getSymbolAtLocation(node) === symbol) return true;
51
- const parent = node.parent;
52
- if (ts.isShorthandPropertyAssignment(parent) && parent.name === node) return checker.getShorthandAssignmentValueSymbol(parent) === symbol;
53
- return ts.isExportSpecifier(parent) && node === (parent.propertyName ?? parent.name)
54
- && checker.getExportSpecifierLocalTargetSymbol(parent) === symbol;
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;
55
59
  };
56
-
57
- const unwrappedNamespace = (ts, node) => {
58
- while (transparentExpression(ts, node.parent)) node = node.parent;
59
- return node;
60
+ const unwrappedNamespace = (ts, input) => {
61
+ let node = input;
62
+ while (transparentExpression(ts, node.parent))
63
+ node = node.parent;
64
+ return node;
60
65
  };
61
-
62
66
  const indexedNamespaceMember = (ts, node) => {
63
- if (!ts.isTypeQueryNode(node.parent) || node.parent.exprName !== node) return null;
64
- let query = node.parent;
65
- while (ts.isParenthesizedTypeNode(query.parent)) query = query.parent;
66
- const indexed = query.parent;
67
- return ts.isIndexedAccessTypeNode(indexed) && indexed.objectType === query && ts.isLiteralTypeNode(indexed.indexType)
68
- ? literalMember(ts, indexed.indexType.literal) : null;
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;
69
75
  };
70
-
71
76
  /** The first member is the entry export even in a chain such as namespace.Enum.Member or namespace.Type.Inner. */
72
77
  const namespaceUse = (ts, reference) => {
73
- const node = unwrappedNamespace(ts, reference);
74
- const parent = node.parent;
75
- let name = null;
76
- if (ts.isPropertyAccessExpression(parent) && parent.expression === node) name = parent.name.text;
77
- else if (ts.isQualifiedName(parent) && parent.left === node) name = parent.right.text;
78
- else if (ts.isElementAccessExpression(parent) && parent.expression === node) name = literalMember(ts, parent.argumentExpression);
79
- else if (ts.isVariableDeclaration(parent) && parent.initializer === node && ts.isObjectBindingPattern(parent.name)) return destructuredBindings(ts, parent.name);
80
- else if (ts.isBinaryExpression(parent) && parent.right === node && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken
81
- && ts.isObjectLiteralExpression(parent.left)) return destructuredBindings(ts, parent.left);
82
- else name = indexedNamespaceMember(ts, node);
83
- return name === null ? { all: true, pairs: [] } : { all: false, pairs: [{ imported: name, exported: null }] };
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 }] };
84
95
  };
85
-
86
96
  const namespaceBindings = (context, declaration, namespace) => {
87
- if (context.namespaceUses.has(namespace)) return context.namespaceUses.get(namespace);
88
- const { ts } = context;
89
- const sourceFile = declaration.getSourceFile();
90
- const checker = namespaceChecker(context, sourceFile, namespace);
91
- const symbol = checker.getSymbolAtLocation(namespace.name);
92
- const result = { all: false, pairs: [] };
93
- const visit = node => {
94
- if (ts.isJSDocSeeTag(node) || ts.isJSDocLink(node) || ts.isJSDocLinkCode(node) || ts.isJSDocLinkPlain(node)) return;
95
- if (ts.isIdentifier(node) && node !== namespace.name && namespaceReference(ts, checker, symbol, node)) {
96
- const used = namespaceUse(ts, node);
97
- result.all ||= used.all;
98
- result.pairs.push(...used.pairs);
99
- }
100
- if (!result.all) {
101
- ts.forEachChild(node, visit);
102
- for (const doc of node.jsDoc ?? []) visit(doc);
103
- }
104
- };
105
- // An unavailable binding must never turn an unknown consumer into a false unused-export finding.
106
- if (symbol) visit(sourceFile);
107
- else result.all = true;
108
- context.namespaceUses.set(namespace, result);
109
- return result;
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;
110
127
  };
111
-
112
128
  const importBindings = (context, declaration) => {
113
- const { ts } = context;
114
- const clause = declaration.importClause;
115
- if (!clause) return { all: false, pairs: [] }; // side-effect import: uses nothing
116
- const pairs = [];
117
- if (clause.name) pairs.push({ imported: 'default', exported: null });
118
- const named = clause.namedBindings;
119
- if (named && named.kind === ts.SyntaxKind.NamespaceImport) {
120
- const namespace = namespaceBindings(context, declaration, named);
121
- return { all: namespace.all, pairs: [...pairs, ...namespace.pairs] };
122
- }
123
- if (named) for (const element of named.elements) pairs.push({ imported: (element.propertyName ?? element.name).text, exported: null });
124
- return { all: false, pairs };
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 };
125
145
  };
126
-
127
146
  const exportBindings = (ts, declaration) => {
128
- const clause = declaration.exportClause;
129
- if (!clause || clause.kind === ts.SyntaxKind.NamespaceExport) return { all: true, pairs: [] };
130
- return { all: false, pairs: clause.elements.map(element => ({ imported: (element.propertyName ?? element.name).text, exported: element.name.text })) };
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 })) };
131
151
  };
132
-
133
152
  const importTypeBindings = (ts, declaration) => {
134
- let qualifier = declaration.qualifier;
135
- if (!qualifier) return { all: true, pairs: [] };
136
- while (qualifier.kind === ts.SyntaxKind.QualifiedName) qualifier = qualifier.left;
137
- return { all: false, pairs: [{ imported: qualifier.text, exported: null }] };
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 }] };
138
159
  };
139
-
140
160
  /** Names an import/export declaration takes from its target: {all} or {pairs: [{imported, exported}]}. */
141
161
  export function bindingsOf(context, declaration) {
142
- const { ts } = context;
143
- if (!declaration) return { all: true, pairs: [] };
144
- if (declaration.kind === ts.SyntaxKind.ImportDeclaration) return importBindings(context, declaration);
145
- if (declaration.kind === ts.SyntaxKind.ExportDeclaration) return exportBindings(ts, declaration);
146
- if (declaration.kind === ts.SyntaxKind.ImportTypeNode) return importTypeBindings(ts, declaration);
147
- // ImportEqualsDeclaration, dynamic import(), require(): the whole module is taken.
148
- return { all: true, pairs: [] };
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: [] };
149
173
  }