@cplieger/deadset-ts 8.0.0-dev.14 → 8.0.0-dev.16

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 (149) hide show
  1. package/dist/src/alias-chain.js +2 -2
  2. package/dist/src/analysis.js +43 -38
  3. package/dist/src/commands/read.js +6 -7
  4. package/dist/src/conformance-record.js +1 -1
  5. package/dist/src/consumers.js +4 -2
  6. package/dist/src/contract/config.generated.js +1 -1
  7. package/dist/src/contract/contract.generated.js +120 -2
  8. package/dist/src/dependencies.js +81 -49
  9. package/dist/src/entries/build-scripts.js +222 -0
  10. package/dist/src/{configuration-files.js → entries/configuration-files.js} +20 -58
  11. package/dist/src/{convention-rows.js → entries/convention-rows.js} +120 -27
  12. package/dist/src/entries/conventions.js +739 -0
  13. package/dist/src/{html-entries.js → entries/html-entries.js} +3 -3
  14. package/dist/src/{mdx-entries.js → entries/mdx-entries.js} +2 -2
  15. package/dist/src/entries/run-directories.js +77 -0
  16. package/dist/src/entries/sources.js +144 -0
  17. package/dist/src/entries/static-module.js +669 -0
  18. package/dist/src/entries/test-runners.js +684 -0
  19. package/dist/src/{workflow-steps.js → entries/workflow-steps.js} +18 -8
  20. package/dist/src/extensions.js +37 -0
  21. package/dist/src/facts.js +38 -0
  22. package/dist/src/file-facts.js +1 -1
  23. package/dist/src/findings/emitters.js +5 -8
  24. package/dist/src/findings/self-check.js +36 -20
  25. package/dist/src/findings-pass.js +5 -6
  26. package/dist/src/framework-lifecycle.js +1 -1
  27. package/dist/src/fs/manifest.js +15 -20
  28. package/dist/src/generated-file.js +172 -29
  29. package/dist/src/graph.js +2 -5
  30. package/dist/src/index.js +1 -1
  31. package/dist/src/injection-container.js +1 -1
  32. package/dist/src/inventory/commonjs.js +507 -0
  33. package/dist/src/inventory/enumerate.js +167 -0
  34. package/dist/src/inventory/syntax.js +236 -0
  35. package/dist/src/inventory.js +305 -705
  36. package/dist/src/javascript-file.js +26 -11
  37. package/dist/src/load/classify.js +28 -9
  38. package/dist/src/load/diagnostics.js +8 -1
  39. package/dist/src/load/discover.js +16 -8
  40. package/dist/src/load/package-scope.js +56 -0
  41. package/dist/src/load/plan.js +2 -2
  42. package/dist/src/load/synthesize.js +5 -5
  43. package/dist/src/load/workspace-manifests.js +1 -1
  44. package/dist/src/matrix.js +6 -13
  45. package/dist/src/outside-tests.js +1 -1
  46. package/dist/src/paths.js +16 -0
  47. package/dist/src/platform-calls.js +78 -0
  48. package/dist/src/platform-symbols.js +140 -0
  49. package/dist/src/position.js +41 -0
  50. package/dist/src/ref.js +11 -20
  51. package/dist/src/references.js +281 -50
  52. package/dist/src/resolve.js +1 -1
  53. package/dist/src/{configured-declarations.js → roots/configured-declarations.js} +4 -4
  54. package/dist/src/{entry-point-gaps.js → roots/entry-point-gaps.js} +5 -8
  55. package/dist/src/{manifest.js → roots/manifest.js} +4 -4
  56. package/dist/src/roots/published-reach.js +412 -0
  57. package/dist/src/roots/root-set.js +489 -0
  58. package/dist/src/roots/unseen-loads.js +208 -0
  59. package/dist/src/serialization-contract.js +1 -1
  60. package/dist/src/session.js +3 -0
  61. package/dist/src/source-files.js +5 -9
  62. package/dist/src/suppress-inline.js +33 -81
  63. package/dist/src/suppress.js +30 -32
  64. package/dist/src/test-support.js +4 -2
  65. package/dist/src/verbs/analyze.js +13 -1
  66. package/dist/src/verbs/describe.js +5 -3
  67. package/jsr.json +1 -1
  68. package/package.json +3 -3
  69. package/src/alias-chain.ts +2 -2
  70. package/src/analysis.ts +77 -53
  71. package/src/automatic-types.ts +1 -1
  72. package/src/commands/read.ts +6 -8
  73. package/src/conformance-record.ts +1 -1
  74. package/src/consumers.ts +5 -1
  75. package/src/contract/config.generated.ts +1 -1
  76. package/src/contract/contract.generated.ts +139 -2
  77. package/src/dependencies.ts +98 -50
  78. package/src/entries/build-scripts.ts +292 -0
  79. package/src/{configuration-files.ts → entries/configuration-files.ts} +23 -75
  80. package/src/{convention-rows.ts → entries/convention-rows.ts} +167 -28
  81. package/src/{conventions.ts → entries/conventions.ts} +347 -522
  82. package/src/{html-entries.ts → entries/html-entries.ts} +5 -5
  83. package/src/{mdx-entries.ts → entries/mdx-entries.ts} +4 -4
  84. package/src/entries/run-directories.ts +108 -0
  85. package/src/entries/sources.ts +266 -0
  86. package/src/entries/static-module.ts +857 -0
  87. package/src/entries/test-runners.ts +868 -0
  88. package/src/{workflow-steps.ts → entries/workflow-steps.ts} +29 -11
  89. package/src/exempt.ts +3 -2
  90. package/src/extensions.ts +45 -0
  91. package/src/facts.ts +44 -0
  92. package/src/file-facts.ts +2 -2
  93. package/src/findings/emitters.ts +14 -10
  94. package/src/findings/self-check.ts +41 -22
  95. package/src/findings/visibility-narrowing.ts +1 -1
  96. package/src/findings-pass.ts +5 -12
  97. package/src/framework-lifecycle.ts +1 -1
  98. package/src/fs/manifest.ts +25 -21
  99. package/src/generated-file.ts +240 -36
  100. package/src/graph.ts +3 -6
  101. package/src/index.ts +1 -1
  102. package/src/injection-container.ts +1 -1
  103. package/src/inventory/commonjs.ts +696 -0
  104. package/src/inventory/enumerate.ts +203 -0
  105. package/src/inventory/syntax.ts +311 -0
  106. package/src/inventory.ts +424 -878
  107. package/src/javascript-file.ts +29 -12
  108. package/src/load/classify.ts +49 -13
  109. package/src/load/diagnostics.ts +11 -1
  110. package/src/load/discover.ts +28 -12
  111. package/src/load/package-scope.ts +74 -0
  112. package/src/load/plan.ts +11 -2
  113. package/src/load/synthesize.ts +6 -6
  114. package/src/load/test-globals.ts +1 -1
  115. package/src/load/workspace-manifests.ts +1 -1
  116. package/src/load/workspace-run.ts +1 -1
  117. package/src/matrix.ts +7 -14
  118. package/src/outside-tests.ts +1 -1
  119. package/src/paths.ts +18 -0
  120. package/src/platform-calls.ts +119 -0
  121. package/src/platform-symbols.ts +205 -0
  122. package/src/position.ts +42 -0
  123. package/src/ref.ts +11 -20
  124. package/src/references.ts +383 -73
  125. package/src/report.ts +1 -1
  126. package/src/resolve.ts +1 -1
  127. package/src/{configured-declarations.ts → roots/configured-declarations.ts} +7 -7
  128. package/src/{entry-point-gaps.ts → roots/entry-point-gaps.ts} +5 -8
  129. package/src/{manifest.ts → roots/manifest.ts} +5 -5
  130. package/src/roots/published-reach.ts +481 -0
  131. package/src/{roots.ts → roots/root-set.ts} +203 -630
  132. package/src/roots/unseen-loads.ts +307 -0
  133. package/src/serialization-contract.ts +1 -1
  134. package/src/session.ts +17 -1
  135. package/src/source-files.ts +5 -10
  136. package/src/suppress-inline.ts +53 -89
  137. package/src/suppress.ts +48 -33
  138. package/src/test-support.ts +8 -3
  139. package/src/verbs/analyze.ts +17 -4
  140. package/src/verbs/describe.ts +5 -2
  141. package/src/verbs/explain.ts +1 -1
  142. package/dist/src/build-scripts.js +0 -418
  143. package/dist/src/conventions.js +0 -907
  144. package/dist/src/entry-points.js +0 -513
  145. package/dist/src/roots.js +0 -865
  146. package/dist/src/unseen-loads.js +0 -100
  147. package/src/build-scripts.ts +0 -536
  148. package/src/entry-points.ts +0 -661
  149. package/src/unseen-loads.ts +0 -134
@@ -1,100 +0,0 @@
1
- /**
2
- * The loads of a project's production files whose target the analysis cannot resolve: an
3
- * `import()` or `require()` call whose path is computed and starts with no relative literal
4
- * naming a directory below the calling file's. The files such a load runs are roots the
5
- * analysis does not see, so the configuration's roots are never complete.
6
- */
7
- import { isBinaryExpression, isCallExpression, isIdentifier, isNoSubstitutionTemplateLiteral, isStringLiteral, isTemplateExpression, SyntaxKind, } from "@typescript/native/ast";
8
- import { dirnamePath, relativePath, resolvePath } from "./paths.js";
9
- import { renderPosition } from "./position.js";
10
- import { UNANSWERED } from "./query.js";
11
- import { sourceFilesOf } from "./source-files.js";
12
- import { unwrapped } from "./unwrap.js";
13
- /** The literal text a computed path starts with: a template's head, or a concatenation's first literal. */
14
- export function leadingLiteral(node) {
15
- const at = unwrapped(node);
16
- if (isTemplateExpression(at)) {
17
- return at.head.text;
18
- }
19
- if (isBinaryExpression(at) && at.operatorToken.kind === SyntaxKind.PlusToken) {
20
- const left = unwrapped(at.left);
21
- return isStringLiteral(left) || isNoSubstitutionTemplateLiteral(left)
22
- ? left.text
23
- : leadingLiteral(at.left);
24
- }
25
- return undefined;
26
- }
27
- /**
28
- * The directory below `dir` a computed load's leading literal names, which the entry
29
- * rules root every file of, or none where it names a package, `dir` itself or a
30
- * directory holding it, each of which would name files no rule roots.
31
- */
32
- export function computedLoadDirectory(dir, prefix) {
33
- if (!(prefix.startsWith("./") || prefix.startsWith("../")) || !prefix.endsWith("/")) {
34
- return undefined;
35
- }
36
- const target = resolvePath(dir, prefix);
37
- return target === dir || relativePath(target, dir) !== undefined ? undefined : target;
38
- }
39
- /** Whether a computed path's leading literal names a package, whose files are none of the target's. */
40
- function namesPackage(prefix) {
41
- return (prefix !== undefined &&
42
- prefix !== "" &&
43
- !prefix.startsWith(".") &&
44
- !prefix.startsWith("/") &&
45
- !/^[A-Za-z][A-Za-z\d+.-]*:/u.test(prefix));
46
- }
47
- /**
48
- * Every unresolved load the project's production files write, in file then source order.
49
- * `testFiles` holds the test files by their path below the target root. A `require` call
50
- * loads a module only where the name resolves to no declaration of the project's own
51
- * files: one the project declares is its own function.
52
- */
53
- export function unseenLoadsOf(project, configuration, targetRoot, testFiles) {
54
- const tests = new Set(testFiles);
55
- const sites = [];
56
- for (const [path, file] of sourceFilesOf(project, targetRoot).byPath) {
57
- if (tests.has(path) || file.isDeclarationFile) {
58
- continue;
59
- }
60
- const dir = dirnamePath(file.fileName);
61
- const visit = (node) => {
62
- if (isCallExpression(node)) {
63
- const callee = node.expression;
64
- const required = isIdentifier(callee) && callee.text === "require";
65
- const [first] = node.arguments;
66
- const argument = first === undefined ? undefined : unwrapped(first);
67
- if ((callee.kind === SyntaxKind.ImportKeyword || required) &&
68
- argument !== undefined &&
69
- !isStringLiteral(argument) &&
70
- !isNoSubstitutionTemplateLiteral(argument)) {
71
- const prefix = leadingLiteral(argument);
72
- const seen = namesPackage(prefix) ||
73
- (prefix !== undefined && computedLoadDirectory(dir, prefix) !== undefined);
74
- if (!seen) {
75
- sites.push({
76
- position: renderPosition(file, targetRoot, node.getStart()),
77
- require: required ? callee : undefined,
78
- });
79
- }
80
- }
81
- }
82
- node.forEachChild(visit);
83
- };
84
- file.forEachChild(visit);
85
- }
86
- const requires = sites.flatMap((site) => (site.require === undefined ? [] : [site.require]));
87
- const own = project.ownPaths();
88
- const local = new Set();
89
- project.symbolsAt(requires.map((node) => project.handle(node))).forEach((symbol, at) => {
90
- const node = requires[at];
91
- if (node !== undefined &&
92
- symbol !== UNANSWERED &&
93
- symbol?.declarations.some((handle) => own.has(handle.path)) === true) {
94
- local.add(node);
95
- }
96
- });
97
- return sites
98
- .filter((site) => site.require === undefined || !local.has(site.require))
99
- .map((site) => ({ configuration, position: site.position }));
100
- }
@@ -1,536 +0,0 @@
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 { unwrapped } from "./unwrap.ts";
44
- import { holdsManifest, inInstalledPackages, manifestAt } from "./fs/manifest.ts";
45
- import { globExpression } from "./text/glob.ts";
46
- import { pathsNamed, readCommands } from "./commands/read.ts";
47
- import { manifestCommands } from "./manifest.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 command strings run or name, by absolute path, each below the target root. */
477
- function scriptModules(host: Host, root: string, dir: string): readonly string[] {
478
- const manifest = manifestAt(host, dir);
479
- const found = new Set<string>();
480
- for (const { command } of manifest === undefined ? [] : manifestCommands(host, dir, manifest)) {
481
- for (const { path } of pathsNamed(readCommands(command, dir))) {
482
- if (
483
- MODULE_EXTENSION.test(path) &&
484
- relativePath(root, path) !== undefined &&
485
- !inInstalledPackages(path) &&
486
- host.kindOf(path) === "file"
487
- ) {
488
- found.add(path);
489
- }
490
- }
491
- }
492
- return [...found].sort(bytewise);
493
- }
494
-
495
- /**
496
- * Every own file of one project a build script names as a bundler's entry: a module a
497
- * script of a manifest at or above an own file runs, below the target root, read from
498
- * the program where it holds the module and parsed alone otherwise, where a parser is given.
499
- */
500
- export function buildScriptEntries(
501
- host: Host,
502
- targetRoot: string,
503
- files: SourceFiles,
504
- parse: ParseFile | undefined,
505
- ): readonly BuildEntry[] {
506
- const root = resolvePath(host.workingDirectory(), targetRoot);
507
- const dirs = new Set<string>([root]);
508
- for (const file of files.byName.values()) {
509
- for (const dir of ancestors(dirnamePath(file.fileName), root)) {
510
- if (dirs.has(dir)) {
511
- break;
512
- }
513
- dirs.add(dir);
514
- }
515
- }
516
- const found: BuildEntry[] = [];
517
- for (const dir of [...dirs].sort(bytewise)) {
518
- if (!holdsManifest(host, dir)) {
519
- continue;
520
- }
521
- for (const path of scriptModules(host, root, dir)) {
522
- let script = files.byName.get(path);
523
- if (script === undefined && parse !== undefined) {
524
- try {
525
- script = parse(path, host.readFile(path));
526
- } catch {
527
- continue;
528
- }
529
- }
530
- if (script !== undefined) {
531
- found.push(...scriptEntries(script, dir, files));
532
- }
533
- }
534
- }
535
- return found;
536
- }