@cplieger/deadset-ts 8.0.0-dev.1 → 8.0.0-dev.11

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 (276) hide show
  1. package/README.md +1 -1
  2. package/dist/bin/mapper-connection.js +2 -1
  3. package/dist/bin/node-host.js +24 -5
  4. package/dist/src/analysis.js +181 -341
  5. package/dist/src/augmentations.js +106 -13
  6. package/dist/src/automatic-types.js +10 -29
  7. package/dist/src/build-scripts.js +425 -0
  8. package/dist/src/component-layer.js +4 -2
  9. package/dist/src/components.js +2 -3
  10. package/dist/src/config.js +6 -82
  11. package/dist/src/configuration-files.js +28 -176
  12. package/dist/src/configured-declarations.js +15 -20
  13. package/dist/src/conformance-record.js +1 -1
  14. package/dist/src/consumers.js +3 -30
  15. package/dist/src/contextual-uses.js +56 -9
  16. package/dist/src/contract/config.generated.js +558 -0
  17. package/dist/src/contract/contract.generated.js +1244 -0
  18. package/dist/src/contract/report.generated.js +862 -0
  19. package/dist/src/convention-rows.js +73 -3
  20. package/dist/src/conventions.js +36 -66
  21. package/dist/src/dependencies.js +103 -40
  22. package/dist/src/edges.js +38 -122
  23. package/dist/src/element-access.js +4 -6
  24. package/dist/src/entry-point-gaps.js +2 -8
  25. package/dist/src/entry-points.js +11 -27
  26. package/dist/src/enum-group.js +3 -10
  27. package/dist/src/erased-stores.js +4 -6
  28. package/dist/src/exempt-classes.js +1 -63
  29. package/dist/src/exempt.js +21 -40
  30. package/dist/src/failure.js +36 -0
  31. package/dist/src/file-facts.js +2 -25
  32. package/dist/src/findings/completion.js +139 -72
  33. package/dist/src/findings/dependencies-and-module-machinery.js +4 -5
  34. package/dist/src/findings/emitters.js +5 -5
  35. package/dist/src/findings/interfaces.js +4 -5
  36. package/dist/src/findings/intra-function.js +13 -10
  37. package/dist/src/findings/non-code-artifacts.js +7 -8
  38. package/dist/src/findings/reads-and-writes.js +16 -13
  39. package/dist/src/findings/self-check.js +10 -12
  40. package/dist/src/findings/unused-declarations.js +41 -32
  41. package/dist/src/findings/visibility-narrowing.js +19 -16
  42. package/dist/src/findings-pass.js +8 -15
  43. package/dist/src/framework-lifecycle.js +7 -7
  44. package/dist/src/fs/document.js +52 -0
  45. package/dist/src/fs/manifest.js +72 -0
  46. package/dist/src/fs/tree.js +88 -0
  47. package/dist/src/generated-file.js +7 -4
  48. package/dist/src/git-ignore.js +39 -26
  49. package/dist/src/graph.js +5 -3
  50. package/dist/src/html-entries.js +6 -33
  51. package/dist/src/in-operands.js +2 -1
  52. package/dist/src/index.js +1 -1
  53. package/dist/src/injection-container.js +1 -1
  54. package/dist/src/intra-function-parts.js +63 -43
  55. package/dist/src/inventory.js +2 -21
  56. package/dist/src/javascript-file.js +33 -0
  57. package/dist/src/json-document.js +198 -67
  58. package/dist/src/keyof-comparisons.js +4 -6
  59. package/dist/src/kinds.js +3 -366
  60. package/dist/src/load/classify.js +270 -0
  61. package/dist/src/load/diagnostics.js +80 -0
  62. package/dist/src/load/discover.js +277 -0
  63. package/dist/src/{emit-map.js → load/emit-map.js} +1 -1
  64. package/dist/src/load/expected-module.js +136 -0
  65. package/dist/src/load/load.js +27 -0
  66. package/dist/src/load/module-lookup.js +37 -0
  67. package/dist/src/load/plan.js +313 -0
  68. package/dist/src/load/specifiers.js +115 -0
  69. package/dist/src/load/synthesize.js +140 -0
  70. package/dist/src/load/test-globals.js +207 -0
  71. package/dist/src/load/test-joins.js +82 -0
  72. package/dist/src/{workspace-manifests.js → load/workspace-manifests.js} +4 -4
  73. package/dist/src/{workspace-programs.js → load/workspace-programs.js} +89 -66
  74. package/dist/src/{workspace-resolution.js → load/workspace-resolution.js} +23 -65
  75. package/dist/src/{workspace-run.js → load/workspace-run.js} +7 -7
  76. package/dist/src/{workspace.js → load/workspace.js} +62 -124
  77. package/dist/src/manifest.js +49 -54
  78. package/dist/src/matrix.js +76 -4
  79. package/dist/src/mdx-entries.js +3 -26
  80. package/dist/src/outside-tests.js +1 -1
  81. package/dist/src/partial.js +2 -1
  82. package/dist/src/paths.js +26 -15
  83. package/dist/src/position.js +2 -1
  84. package/dist/src/print.js +11 -79
  85. package/dist/src/product.js +13 -0
  86. package/dist/src/query.js +1 -0
  87. package/dist/src/ref.js +1 -0
  88. package/dist/src/references.js +48 -30
  89. package/dist/src/report-schema.js +51 -0
  90. package/dist/src/report.js +57 -41
  91. package/dist/src/reporters/annotations.js +15 -8
  92. package/dist/src/reporters/sarif.js +16 -10
  93. package/dist/src/reporters/template.js +4 -19
  94. package/dist/src/resolve.js +245 -729
  95. package/dist/src/roots.js +273 -87
  96. package/dist/src/run.js +66 -77
  97. package/dist/src/schema.js +320 -343
  98. package/dist/src/scope.js +38 -115
  99. package/dist/src/serialization-contract.js +595 -21
  100. package/dist/src/session.js +34 -29
  101. package/dist/src/source-files.js +1 -1
  102. package/dist/src/suppress-file.js +25 -28
  103. package/dist/src/suppress-inline.js +3 -2
  104. package/dist/src/suppress.js +33 -51
  105. package/dist/src/sweep.js +7 -9
  106. package/dist/src/symbol-keys.js +3 -5
  107. package/dist/src/template-field.js +9 -19
  108. package/dist/src/test-support.js +8 -11
  109. package/dist/src/text/compare.js +26 -0
  110. package/dist/src/text/glob.js +194 -0
  111. package/dist/src/text/named-files.js +54 -0
  112. package/dist/src/text/shell.js +159 -0
  113. package/dist/src/text/yaml.js +393 -0
  114. package/dist/src/type-errors.js +11 -232
  115. package/dist/src/unseen-loads.js +100 -0
  116. package/dist/src/unwrap.js +23 -0
  117. package/dist/src/usage.js +10 -0
  118. package/dist/src/verbs/analyze.js +166 -126
  119. package/dist/src/verbs/describe.js +9 -16
  120. package/dist/src/verbs/explain.js +10 -7
  121. package/dist/src/verbs/print-config.js +2 -1
  122. package/dist/src/verbs/print-projects.js +6 -4
  123. package/dist/src/verbs/print-retained.js +4 -2
  124. package/dist/src/verbs/print-roots.js +4 -2
  125. package/dist/src/verbs/verb.js +1 -5
  126. package/dist/src/verbs/version.js +14 -3
  127. package/dist/src/workflow-steps.js +62 -64
  128. package/jsr.json +1 -1
  129. package/package.json +5 -4
  130. package/src/analysis.ts +245 -502
  131. package/src/augmentations.ts +135 -17
  132. package/src/automatic-types.ts +11 -29
  133. package/src/build-scripts.ts +543 -0
  134. package/src/component-layer.ts +4 -2
  135. package/src/components.ts +2 -4
  136. package/src/config.ts +31 -302
  137. package/src/configuration-files.ts +34 -179
  138. package/src/configured-declarations.ts +18 -23
  139. package/src/conformance-record.ts +1 -1
  140. package/src/consumers.ts +6 -57
  141. package/src/contextual-uses.ts +64 -12
  142. package/src/contract/config.generated.ts +818 -0
  143. package/src/contract/contract.generated.ts +1308 -0
  144. package/src/contract/report.generated.ts +1237 -0
  145. package/src/convention-rows.ts +85 -3
  146. package/src/conventions.ts +46 -84
  147. package/src/dependencies.ts +118 -46
  148. package/src/edges.ts +41 -140
  149. package/src/element-access.ts +4 -7
  150. package/src/entry-point-gaps.ts +3 -9
  151. package/src/entry-points.ts +12 -38
  152. package/src/enum-group.ts +2 -11
  153. package/src/erased-stores.ts +3 -7
  154. package/src/exempt-classes.ts +1 -75
  155. package/src/exempt.ts +36 -50
  156. package/src/failure.ts +63 -0
  157. package/src/file-facts.ts +9 -24
  158. package/src/finding.ts +14 -9
  159. package/src/findings/boundary.ts +2 -0
  160. package/src/findings/completion.ts +181 -84
  161. package/src/findings/dependencies-and-module-machinery.ts +4 -7
  162. package/src/findings/emitter.ts +8 -9
  163. package/src/findings/emitters.ts +7 -7
  164. package/src/findings/interfaces.ts +4 -6
  165. package/src/findings/intra-function.ts +13 -11
  166. package/src/findings/non-code-artifacts.ts +9 -11
  167. package/src/findings/reads-and-writes.ts +20 -16
  168. package/src/findings/self-check.ts +11 -17
  169. package/src/findings/unused-declarations.ts +45 -32
  170. package/src/findings/visibility-narrowing.ts +20 -17
  171. package/src/findings-pass.ts +10 -19
  172. package/src/framework-lifecycle.ts +7 -7
  173. package/src/fs/document.ts +70 -0
  174. package/src/fs/manifest.ts +92 -0
  175. package/src/fs/tree.ts +110 -0
  176. package/src/generated-file.ts +7 -4
  177. package/src/git-ignore.ts +50 -26
  178. package/src/graph.ts +5 -3
  179. package/src/host.ts +12 -5
  180. package/src/html-entries.ts +6 -33
  181. package/src/in-operands.ts +2 -1
  182. package/src/index.ts +1 -1
  183. package/src/injection-container.ts +1 -1
  184. package/src/intra-function-parts.ts +73 -44
  185. package/src/inventory.ts +2 -20
  186. package/src/javascript-file.ts +37 -0
  187. package/src/json-document.ts +241 -76
  188. package/src/keyof-comparisons.ts +3 -7
  189. package/src/kinds.ts +5 -413
  190. package/src/load/classify.ts +397 -0
  191. package/src/load/diagnostics.ts +144 -0
  192. package/src/load/discover.ts +455 -0
  193. package/src/{emit-map.ts → load/emit-map.ts} +1 -1
  194. package/src/load/expected-module.ts +188 -0
  195. package/src/load/load.ts +73 -0
  196. package/src/load/module-lookup.ts +50 -0
  197. package/src/load/plan.ts +504 -0
  198. package/src/load/specifiers.ts +168 -0
  199. package/src/load/synthesize.ts +211 -0
  200. package/src/load/test-globals.ts +251 -0
  201. package/src/load/test-joins.ts +98 -0
  202. package/src/{workspace-manifests.ts → load/workspace-manifests.ts} +5 -5
  203. package/src/{workspace-programs.ts → load/workspace-programs.ts} +128 -85
  204. package/src/{workspace-resolution.ts → load/workspace-resolution.ts} +33 -76
  205. package/src/{workspace-run.ts → load/workspace-run.ts} +18 -16
  206. package/src/{workspace.ts → load/workspace.ts} +90 -134
  207. package/src/manifest.ts +81 -75
  208. package/src/matrix.ts +122 -4
  209. package/src/mdx-entries.ts +3 -26
  210. package/src/outside-tests.ts +1 -1
  211. package/src/partial.ts +3 -2
  212. package/src/paths.ts +27 -15
  213. package/src/position.ts +2 -1
  214. package/src/print.ts +12 -92
  215. package/src/product.ts +18 -0
  216. package/src/query.ts +11 -0
  217. package/src/ref.ts +1 -0
  218. package/src/references.ts +52 -31
  219. package/src/report-schema.ts +68 -0
  220. package/src/report.ts +101 -224
  221. package/src/reporters/annotations.ts +22 -11
  222. package/src/reporters/json.ts +1 -1
  223. package/src/reporters/reporter.ts +2 -4
  224. package/src/reporters/reporters.ts +1 -1
  225. package/src/reporters/sarif.ts +29 -16
  226. package/src/reporters/template.ts +4 -20
  227. package/src/reporters/text.ts +3 -3
  228. package/src/resolve.ts +283 -1114
  229. package/src/roots.ts +339 -104
  230. package/src/run.ts +75 -91
  231. package/src/schema.ts +391 -388
  232. package/src/scope.ts +46 -138
  233. package/src/serialization-contract.ts +763 -22
  234. package/src/session.ts +70 -54
  235. package/src/source-files.ts +1 -1
  236. package/src/suppress-file.ts +24 -33
  237. package/src/suppress-inline.ts +2 -2
  238. package/src/suppress.ts +43 -58
  239. package/src/sweep.ts +23 -9
  240. package/src/symbol-keys.ts +2 -5
  241. package/src/template-field.ts +15 -25
  242. package/src/test-support.ts +17 -13
  243. package/src/text/compare.ts +27 -0
  244. package/src/text/glob.ts +210 -0
  245. package/src/text/named-files.inputs.json +1522 -0
  246. package/src/text/named-files.ts +68 -0
  247. package/src/text/shell.ts +161 -0
  248. package/src/text/yaml.ts +423 -0
  249. package/src/type-errors.ts +18 -288
  250. package/src/unseen-loads.ts +134 -0
  251. package/src/unwrap.ts +34 -0
  252. package/src/usage.ts +10 -0
  253. package/src/verbs/analyze.ts +224 -162
  254. package/src/verbs/describe.ts +14 -15
  255. package/src/verbs/explain.ts +10 -11
  256. package/src/verbs/print-config.ts +2 -1
  257. package/src/verbs/print-projects.ts +6 -4
  258. package/src/verbs/print-retained.ts +4 -2
  259. package/src/verbs/print-roots.ts +4 -2
  260. package/src/verbs/verb.ts +2 -8
  261. package/src/verbs/version.ts +15 -3
  262. package/src/workflow-steps.ts +74 -64
  263. package/dist/src/base-url.js +0 -18
  264. package/dist/src/discover.js +0 -468
  265. package/dist/src/glob.js +0 -65
  266. package/dist/src/pnpm-workspace.js +0 -120
  267. package/dist/src/setup-failure.js +0 -23
  268. package/dist/src/test-joins.js +0 -101
  269. package/dist/src/version.js +0 -7
  270. package/src/base-url.ts +0 -24
  271. package/src/discover.ts +0 -653
  272. package/src/glob.ts +0 -67
  273. package/src/pnpm-workspace.ts +0 -135
  274. package/src/setup-failure.ts +0 -39
  275. package/src/test-joins.ts +0 -120
  276. package/src/version.ts +0 -7
@@ -0,0 +1,543 @@
1
+ /**
2
+ * The files a bundler's JavaScript API builds from, read from the build scripts a
3
+ * manifest's scripts run: the strings an options object passes as the bundler's entries.
4
+ * A script is read without running it, so a value that is not written out, a computed
5
+ * path or an options object built at run time among them, names nothing.
6
+ */
7
+
8
+ import {
9
+ isArrayBindingPattern,
10
+ isArrayLiteralExpression,
11
+ isArrowFunction,
12
+ isCallExpression,
13
+ isConstructorDeclaration,
14
+ isFunctionDeclaration,
15
+ isFunctionExpression,
16
+ isGetAccessorDeclaration,
17
+ isIdentifier,
18
+ isImportDeclaration,
19
+ isMetaProperty,
20
+ isMethodDeclaration,
21
+ isNamedImports,
22
+ isNamespaceImport,
23
+ isNewExpression,
24
+ isNoSubstitutionTemplateLiteral,
25
+ isObjectBindingPattern,
26
+ isObjectLiteralExpression,
27
+ isPropertyAccessExpression,
28
+ isPropertyAssignment,
29
+ isSetAccessorDeclaration,
30
+ isStringLiteral,
31
+ isVariableDeclaration,
32
+ isVariableStatement,
33
+ NodeFlags,
34
+ type CallExpression,
35
+ type Node,
36
+ type SourceFile,
37
+ } from "@typescript/native/ast";
38
+ import { filesNamedBy, type ParseFile } from "./configuration-files.ts";
39
+ import type { Host } from "./host.ts";
40
+ import { ancestors, dirnamePath, relativePath, resolvePath } from "./paths.ts";
41
+ import type { SourceFiles } from "./source-files.ts";
42
+ import { bytewise } from "./text/compare.ts";
43
+ import { isRecord } from "./json-document.ts";
44
+ import { unwrapped } from "./unwrap.ts";
45
+ import { holdsManifest, inInstalledPackages, manifestAt } from "./fs/manifest.ts";
46
+ import { globExpression } from "./text/glob.ts";
47
+ import { scriptTokens } from "./text/shell.ts";
48
+
49
+ /** One bundler API: the module, the functions it exports that build, and the option naming entries. */
50
+ interface BundlerAPI {
51
+ readonly module: string;
52
+ /** The exported names of the building functions; `default` is the module's own value called. */
53
+ readonly calls: readonly string[];
54
+ /** The option whose strings name the entries, read as `entries` states. */
55
+ readonly option: string;
56
+ /** The option that moves the directory the entries are read against. */
57
+ readonly base: string;
58
+ /** Whether an entry string is a path, glob characters matching, or a module request, only a relative one naming a file. */
59
+ readonly paths: boolean;
60
+ }
61
+
62
+ /**
63
+ * The bundlers whose build call names its entries: esbuild resolves `entryPoints` against
64
+ * `absWorkingDir` (https://esbuild.github.io/api/#working-directory), webpack `entry`
65
+ * against `context` (https://webpack.js.org/configuration/entry-context/), and Rollup
66
+ * `input` against the working directory, each the manifest's directory when its script runs.
67
+ */
68
+ const BUNDLERS: readonly BundlerAPI[] = [
69
+ {
70
+ module: "esbuild",
71
+ calls: ["build", "buildSync", "context"],
72
+ option: "entryPoints",
73
+ base: "absWorkingDir",
74
+ paths: true,
75
+ },
76
+ { module: "webpack", calls: ["default"], option: "entry", base: "context", paths: false },
77
+ { module: "rollup", calls: ["rollup", "watch"], option: "input", base: "", paths: true },
78
+ ];
79
+
80
+ const MODULE_EXTENSION = /\.(?:js|mjs|cjs|ts|mts|cts)$/u;
81
+
82
+ /** One own file a build script's bundler call names as an entry. */
83
+ interface BuildEntry {
84
+ readonly file: SourceFile;
85
+ /** The string that named it. */
86
+ readonly source: string;
87
+ }
88
+
89
+ function literalOf(node: Node): string | undefined {
90
+ const at = unwrapped(node);
91
+ return isStringLiteral(at) || isNoSubstitutionTemplateLiteral(at) ? at.text : undefined;
92
+ }
93
+
94
+ /** Whether one call is `require(<module>)`. */
95
+ function requires(node: Node, module: string): boolean {
96
+ const at = unwrapped(node);
97
+ return (
98
+ isCallExpression(at) &&
99
+ isIdentifier(at.expression) &&
100
+ at.expression.text === "require" &&
101
+ at.arguments.length === 1 &&
102
+ at.arguments[0] !== undefined &&
103
+ literalOf(at.arguments[0]) === module
104
+ );
105
+ }
106
+
107
+ /** The local names one file binds to a bundler: each building function, and the module's own value. */
108
+ interface Bindings {
109
+ readonly functions: ReadonlySet<string>;
110
+ readonly modules: ReadonlySet<string>;
111
+ }
112
+
113
+ function bindingsOf(file: SourceFile, api: BundlerAPI): Bindings {
114
+ const functions = new Set<string>();
115
+ const modules = new Set<string>();
116
+ const building = (name: string): boolean => api.calls.includes(name);
117
+ for (const statement of file.statements) {
118
+ if (isImportDeclaration(statement)) {
119
+ if (literalOf(statement.moduleSpecifier) !== api.module) {
120
+ continue;
121
+ }
122
+ const clause = statement.importClause;
123
+ if (clause?.name !== undefined) {
124
+ (building("default") ? functions : modules).add(clause.name.text);
125
+ }
126
+ const named = clause?.namedBindings;
127
+ if (named !== undefined && isNamespaceImport(named)) {
128
+ modules.add(named.name.text);
129
+ } else if (named !== undefined && isNamedImports(named)) {
130
+ for (const element of named.elements) {
131
+ if (building((element.propertyName ?? element.name).text)) {
132
+ functions.add(element.name.text);
133
+ }
134
+ }
135
+ }
136
+ } else if (isVariableStatement(statement)) {
137
+ for (const declaration of statement.declarationList.declarations) {
138
+ const { name, initializer } = declaration;
139
+ if (initializer === undefined || !requires(initializer, api.module)) {
140
+ continue;
141
+ }
142
+ if (isIdentifier(name)) {
143
+ (building("default") ? functions : modules).add(name.text);
144
+ } else if (isObjectBindingPattern(name)) {
145
+ for (const element of name.elements) {
146
+ const local = element.name;
147
+ const key = element.propertyName ?? local;
148
+ if (
149
+ key !== undefined &&
150
+ local !== undefined &&
151
+ isIdentifier(key) &&
152
+ isIdentifier(local) &&
153
+ building(key.text)
154
+ ) {
155
+ functions.add(local.text);
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+ }
162
+ return { functions, modules };
163
+ }
164
+
165
+ /** Whether one call's callee is a building function of a bundler the file binds, `shadowed` naming none. */
166
+ function builds(
167
+ callee: Node,
168
+ api: BundlerAPI,
169
+ bindings: Bindings,
170
+ shadowed: ReadonlySet<string>,
171
+ ): boolean {
172
+ const at = unwrapped(callee);
173
+ if (isIdentifier(at)) {
174
+ return bindings.functions.has(at.text) && !shadowed.has(at.text);
175
+ }
176
+ if (
177
+ !isPropertyAccessExpression(at) ||
178
+ !isIdentifier(at.name) ||
179
+ !api.calls.includes(at.name.text)
180
+ ) {
181
+ return false;
182
+ }
183
+ const holder = unwrapped(at.expression);
184
+ return (
185
+ (isIdentifier(holder) && bindings.modules.has(holder.text) && !shadowed.has(holder.text)) ||
186
+ requires(holder, api.module)
187
+ );
188
+ }
189
+
190
+ /** The top-level `const` initializers one file declares, by name. */
191
+ function constants(file: SourceFile): ReadonlyMap<string, Node> {
192
+ const found = new Map<string, Node>();
193
+ for (const statement of file.statements) {
194
+ if (
195
+ isVariableStatement(statement) &&
196
+ (statement.declarationList.flags & NodeFlags.Const) !== 0
197
+ ) {
198
+ for (const { name, initializer } of statement.declarationList.declarations) {
199
+ if (isIdentifier(name) && initializer !== undefined) {
200
+ found.set(name.text, initializer);
201
+ }
202
+ }
203
+ }
204
+ }
205
+ return found;
206
+ }
207
+
208
+ /** The value one object literal writes for one member as `name: value`. */
209
+ function member(object: Node, name: string): Node | undefined {
210
+ if (!isObjectLiteralExpression(object)) {
211
+ return undefined;
212
+ }
213
+ for (const property of object.properties) {
214
+ if (isPropertyAssignment(property) && literalOrName(property.name) === name) {
215
+ return property.initializer;
216
+ }
217
+ }
218
+ return undefined;
219
+ }
220
+
221
+ function literalOrName(node: Node): string | undefined {
222
+ return isIdentifier(node) ? node.text : literalOf(node);
223
+ }
224
+
225
+ /** Whether one expression is the script's own directory: `__dirname` or `import.meta.dirname`. */
226
+ function isOwnDirectory(node: Node): boolean {
227
+ const at = unwrapped(node);
228
+ if (isIdentifier(at)) {
229
+ return at.text === "__dirname";
230
+ }
231
+ return (
232
+ isPropertyAccessExpression(at) &&
233
+ isMetaProperty(at.expression) &&
234
+ isIdentifier(at.name) &&
235
+ at.name.text === "dirname"
236
+ );
237
+ }
238
+
239
+ /** One path an entry value writes. */
240
+ interface EntryPath {
241
+ readonly path: string;
242
+ /** What the script writes for it. */
243
+ readonly text: string;
244
+ /** Whether it is written as neither a relative nor an absolute path. */
245
+ readonly request: boolean;
246
+ }
247
+
248
+ /**
249
+ * The path one entry value writes, read against `dir`: a literal, or a `resolve` or
250
+ * `join` call, of `path` or bound from it, whose first argument is the script's own
251
+ * directory and whose others are literals.
252
+ */
253
+ function pathOf(node: Node, dir: string, scriptDir: string): EntryPath | undefined {
254
+ const at = unwrapped(node);
255
+ const text = literalOf(at);
256
+ if (text !== undefined) {
257
+ const request = !text.startsWith("./") && !text.startsWith("../") && !text.startsWith("/");
258
+ return { path: resolvePath(dir, text), text, request };
259
+ }
260
+ if (!isCallExpression(at)) {
261
+ return undefined;
262
+ }
263
+ const callee = unwrapped(at.expression);
264
+ const name = isPropertyAccessExpression(callee) ? callee.name : callee;
265
+ if (!isIdentifier(name) || (name.text !== "resolve" && name.text !== "join")) {
266
+ return undefined;
267
+ }
268
+ const [first, ...rest] = at.arguments;
269
+ const parts = rest.map(literalOf);
270
+ if (first === undefined || !isOwnDirectory(first) || parts.some((part) => part === undefined)) {
271
+ return undefined;
272
+ }
273
+ const joined = parts.join("/");
274
+ return { path: resolvePath(scriptDir, joined), text: joined, request: false };
275
+ }
276
+
277
+ /** The entry values one entries option writes: a value, an array of them, an object of them. */
278
+ function entryValues(node: Node): readonly Node[] {
279
+ const at = unwrapped(node);
280
+ if (isArrayLiteralExpression(at)) {
281
+ return at.elements.flatMap(entryValues);
282
+ }
283
+ if (isObjectLiteralExpression(at)) {
284
+ const imported = member(at, "import") ?? member(at, "in");
285
+ if (imported !== undefined) {
286
+ return entryValues(imported);
287
+ }
288
+ return at.properties.flatMap((property) =>
289
+ isPropertyAssignment(property) ? entryValues(property.initializer) : [],
290
+ );
291
+ }
292
+ return [at];
293
+ }
294
+
295
+ /** The own files one entry string names, read as `api` reads it. */
296
+ function entriesNamed(
297
+ files: SourceFiles,
298
+ api: BundlerAPI,
299
+ entry: EntryPath,
300
+ ): readonly SourceFile[] {
301
+ if (!api.paths && entry.request) {
302
+ return [];
303
+ }
304
+ if (api.paths && /[*?[{]/u.test(entry.text)) {
305
+ const expression = globExpression(entry.path);
306
+ return [...files.byName.values()].filter((file) => expression.test(file.fileName));
307
+ }
308
+ const dir = dirnamePath(entry.path);
309
+ return filesNamedBy(files, dir, entry.path.slice(dir.length + 1));
310
+ }
311
+
312
+ /** Whether one node is a function, whose body runs only where something calls it. */
313
+ function isFunction(node: Node): boolean {
314
+ return (
315
+ isFunctionDeclaration(node) ||
316
+ isFunctionExpression(node) ||
317
+ isArrowFunction(node) ||
318
+ isMethodDeclaration(node) ||
319
+ isConstructorDeclaration(node) ||
320
+ isGetAccessorDeclaration(node) ||
321
+ isSetAccessorDeclaration(node)
322
+ );
323
+ }
324
+
325
+ /** The names one binding name binds: an identifier, or each name a pattern holds. */
326
+ function boundNames(name: Node, into: Set<string>): void {
327
+ if (isIdentifier(name)) {
328
+ into.add(name.text);
329
+ } else if (isObjectBindingPattern(name) || isArrayBindingPattern(name)) {
330
+ name.forEachChild((element) => {
331
+ const bound = (element as { readonly name?: Node }).name;
332
+ if (bound !== undefined) {
333
+ boundNames(bound, into);
334
+ }
335
+ });
336
+ }
337
+ }
338
+
339
+ /** The names one function binds of its own: its parameters and the declarations its body holds outside nested functions. */
340
+ function ownBindings(fn: Node): ReadonlySet<string> {
341
+ const found = new Set<string>();
342
+ const visit = (node: Node): void => {
343
+ if (node !== fn && isFunction(node)) {
344
+ if (isFunctionDeclaration(node) && node.name !== undefined) {
345
+ found.add(node.name.text);
346
+ }
347
+ return;
348
+ }
349
+ if (isVariableDeclaration(node)) {
350
+ boundNames(node.name, found);
351
+ }
352
+ node.forEachChild(visit);
353
+ };
354
+ for (const parameter of (fn as { readonly parameters?: readonly { readonly name: Node }[] })
355
+ .parameters ?? []) {
356
+ boundNames(parameter.name, found);
357
+ }
358
+ fn.forEachChild(visit);
359
+ return found;
360
+ }
361
+
362
+ /** The functions one file declares under a name: a function declaration, or a variable bound to a function. */
363
+ function namedFunctions(file: SourceFile): ReadonlyMap<string, readonly Node[]> {
364
+ const found = new Map<string, Node[]>();
365
+ const add = (name: string, fn: Node): void => {
366
+ found.set(name, [...(found.get(name) ?? []), fn]);
367
+ };
368
+ const visit = (node: Node): void => {
369
+ if (isFunctionDeclaration(node) && node.name !== undefined) {
370
+ add(node.name.text, node);
371
+ } else if (
372
+ isVariableDeclaration(node) &&
373
+ isIdentifier(node.name) &&
374
+ node.initializer !== undefined &&
375
+ isFunction(unwrapped(node.initializer))
376
+ ) {
377
+ add(node.name.text, unwrapped(node.initializer));
378
+ }
379
+ node.forEachChild(visit);
380
+ };
381
+ file.forEachChild(visit);
382
+ return found;
383
+ }
384
+
385
+ /**
386
+ * The calls one build script runs: those of its top level, and those of each function
387
+ * a call it runs calls or passes on, as a callee, an argument or a function called where
388
+ * it is written. `shadowed` is the names a function running them binds of its own, which
389
+ * hide the file's bindings of the same names.
390
+ */
391
+ function runCalls(
392
+ file: SourceFile,
393
+ each: (call: CallExpression, shadowed: ReadonlySet<string>) => void,
394
+ ): void {
395
+ const named = namedFunctions(file);
396
+ const ran = new Set<Node>();
397
+ const run = (fn: Node, outer: ReadonlySet<string>): void => {
398
+ if (ran.has(fn)) {
399
+ return;
400
+ }
401
+ ran.add(fn);
402
+ const shadowed = new Set([...outer, ...ownBindings(fn)]);
403
+ fn.forEachChild((child) => {
404
+ visit(child, shadowed);
405
+ });
406
+ };
407
+ const runs = (node: Node, shadowed: ReadonlySet<string>): void => {
408
+ const at = unwrapped(node);
409
+ if (isFunction(at)) {
410
+ run(at, shadowed);
411
+ } else if (isIdentifier(at) && !shadowed.has(at.text)) {
412
+ for (const fn of named.get(at.text) ?? []) {
413
+ run(fn, new Set());
414
+ }
415
+ }
416
+ };
417
+ const visit = (node: Node, shadowed: ReadonlySet<string>): void => {
418
+ if (isFunction(node)) {
419
+ return;
420
+ }
421
+ if (isCallExpression(node) || isNewExpression(node)) {
422
+ if (isCallExpression(node)) {
423
+ each(node, shadowed);
424
+ }
425
+ runs(node.expression, shadowed);
426
+ for (const argument of node.arguments ?? []) {
427
+ runs(argument, shadowed);
428
+ }
429
+ }
430
+ node.forEachChild((child) => {
431
+ visit(child, shadowed);
432
+ });
433
+ };
434
+ file.forEachChild((child) => {
435
+ visit(child, new Set());
436
+ });
437
+ }
438
+
439
+ /** The entries every bundler call one build script runs names, read against `dir`. */
440
+ function scriptEntries(file: SourceFile, dir: string, files: SourceFiles): readonly BuildEntry[] {
441
+ const found: BuildEntry[] = [];
442
+ const declared = constants(file);
443
+ const scriptDir = dirnamePath(file.fileName);
444
+ const bound = BUNDLERS.map((api) => ({ api, bindings: bindingsOf(file, api) }));
445
+ runCalls(file, (call, shadowed) => {
446
+ for (const { api, bindings } of bound) {
447
+ if (!builds(call.expression, api, bindings, shadowed)) {
448
+ continue;
449
+ }
450
+ const [argument] = call.arguments;
451
+ let options: Node | undefined = argument === undefined ? undefined : unwrapped(argument);
452
+ if (options !== undefined && isIdentifier(options)) {
453
+ options = shadowed.has(options.text) ? undefined : declared.get(options.text);
454
+ options = options === undefined ? undefined : unwrapped(options);
455
+ }
456
+ const entries = options === undefined ? undefined : member(options, api.option);
457
+ const base = options === undefined || api.base === "" ? undefined : member(options, api.base);
458
+ const against = base === undefined ? { path: dir } : pathOf(base, dir, scriptDir);
459
+ if (entries === undefined || against === undefined) {
460
+ continue;
461
+ }
462
+ for (const value of entryValues(entries)) {
463
+ const named = pathOf(value, against.path, scriptDir);
464
+ if (named === undefined) {
465
+ continue;
466
+ }
467
+ for (const entry of entriesNamed(files, api, named)) {
468
+ found.push({ file: entry, source: named.text });
469
+ }
470
+ }
471
+ }
472
+ });
473
+ return found;
474
+ }
475
+
476
+ /** The modules one manifest's scripts run, by absolute path, each below the target root. */
477
+ function scriptModules(host: Host, root: string, dir: string): readonly string[] {
478
+ const scripts = manifestAt(host, dir)?.["scripts"];
479
+ if (!isRecord(scripts)) {
480
+ return [];
481
+ }
482
+ const found = new Set<string>();
483
+ for (const command of Object.values(scripts)) {
484
+ if (typeof command !== "string") {
485
+ continue;
486
+ }
487
+ for (const token of scriptTokens(command)) {
488
+ const path = resolvePath(dir, token);
489
+ if (
490
+ MODULE_EXTENSION.test(path) &&
491
+ relativePath(root, path) !== undefined &&
492
+ !inInstalledPackages(path) &&
493
+ host.kindOf(path) === "file"
494
+ ) {
495
+ found.add(path);
496
+ }
497
+ }
498
+ }
499
+ return [...found].sort(bytewise);
500
+ }
501
+
502
+ /**
503
+ * Every own file of one project a build script names as a bundler's entry: a module a
504
+ * script of a manifest at or above an own file runs, below the target root, read from
505
+ * the program where it holds the module and parsed alone otherwise, where a parser is given.
506
+ */
507
+ export function buildScriptEntries(
508
+ host: Host,
509
+ targetRoot: string,
510
+ files: SourceFiles,
511
+ parse: ParseFile | undefined,
512
+ ): readonly BuildEntry[] {
513
+ const root = resolvePath(host.workingDirectory(), targetRoot);
514
+ const dirs = new Set<string>([root]);
515
+ for (const file of files.byName.values()) {
516
+ for (const dir of ancestors(dirnamePath(file.fileName), root)) {
517
+ if (dirs.has(dir)) {
518
+ break;
519
+ }
520
+ dirs.add(dir);
521
+ }
522
+ }
523
+ const found: BuildEntry[] = [];
524
+ for (const dir of [...dirs].sort(bytewise)) {
525
+ if (!holdsManifest(host, dir)) {
526
+ continue;
527
+ }
528
+ for (const path of scriptModules(host, root, dir)) {
529
+ let script = files.byName.get(path);
530
+ if (script === undefined && parse !== undefined) {
531
+ try {
532
+ script = parse(path, host.readFile(path));
533
+ } catch {
534
+ continue;
535
+ }
536
+ }
537
+ if (script !== undefined) {
538
+ found.push(...scriptEntries(script, dir, files));
539
+ }
540
+ }
541
+ }
542
+ return found;
543
+ }
@@ -8,10 +8,12 @@
8
8
 
9
9
  import type { CreateSnapshotParams } from "@typescript/native/sync";
10
10
  import type { Host } from "./host.ts";
11
+ import { INSTALLED_PACKAGES } from "./fs/manifest.ts";
11
12
  import { dirnamePath, joinPath } from "./paths.ts";
13
+ import { PRODUCT } from "./product.ts";
12
14
 
13
15
  /** The name the mapper's manifest declares and every configuration's entry names. */
14
- const MAPPER_PACKAGE = "deadset-ts-component-files";
16
+ const MAPPER_PACKAGE = `${PRODUCT}-component-files`;
15
17
 
16
18
  /** The file system one snapshot is created with. */
17
19
  type SnapshotFileSystem = NonNullable<CreateSnapshotParams["fileSystem"]>;
@@ -150,7 +152,7 @@ export function componentLayer(
150
152
  mappers,
151
153
  );
152
154
  if (mapper !== undefined) {
153
- symlinks[joinPath(dirnamePath(configFile), "node_modules", MAPPER_PACKAGE)] = {
155
+ symlinks[joinPath(dirnamePath(configFile), INSTALLED_PACKAGES, MAPPER_PACKAGE)] = {
154
156
  target: mapper.packageDirectory,
155
157
  host: true,
156
158
  };
package/src/components.ts CHANGED
@@ -7,16 +7,14 @@
7
7
  import type { Cascade } from "./config.ts";
8
8
  import { OUTSIDE, type Graph } from "./graph.ts";
9
9
  import type { InventorySymbol } from "./inventory.ts";
10
-
11
- /** The name every component identifier this analyzer mints starts with. */
12
- const ANALYZER = "deadset-ts";
10
+ import { PRODUCT } from "./product.ts";
13
11
 
14
12
  /** The fewest digits a component identifier's number is written with. */
15
13
  const ID_DIGITS = 4;
16
14
 
17
15
  /** The identifier of the component at one place of the run's order, counted from one. */
18
16
  export function componentId(place: number): string {
19
- return `${ANALYZER}/c-${String(place).padStart(ID_DIGITS, "0")}`;
17
+ return `${PRODUCT}/c-${String(place).padStart(ID_DIGITS, "0")}`;
20
18
  }
21
19
 
22
20
  /** The index of a declaration the component walk has not reached. */