@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,455 @@
1
+ import type { BuildConfiguration, ProjectConfiguration } from "../config.ts";
2
+ import { LANGUAGE_ROWS } from "../contract/contract.generated.ts";
3
+ import { filesBelow, filesIn } from "../fs/tree.ts";
4
+ import type { Host } from "../host.ts";
5
+ import { packageScope } from "../inventory.ts";
6
+ import { dirnamePath, joinPath, relativePath, resolvePath } from "../paths.ts";
7
+ import { LANGUAGE, message } from "../product.ts";
8
+ import type { Module, Scope } from "../scope.ts";
9
+ import type { Engine } from "../session.ts";
10
+ import { globExpression } from "../text/glob.ts";
11
+ import { failureExit, type FailureExit } from "../failure.ts";
12
+ import { MANIFEST } from "../fs/manifest.ts";
13
+
14
+ /** The names of the files a program of the language is read from. */
15
+ export const MARKERS = LANGUAGE_ROWS[LANGUAGE].markers;
16
+ const MARKER_EXPRESSIONS = MARKERS.map(globExpression);
17
+
18
+ /** Whether one file name is a compiler configuration's: one of the language row's markers. */
19
+ function isConfigFile(name: string): boolean {
20
+ return MARKER_EXPRESSIONS.some((marker) => marker.test(name));
21
+ }
22
+
23
+ /**
24
+ * Where one configuration of a run comes from, which decides what a failure to build it
25
+ * does: a guess discovery derived is dropped, any other ends the run.
26
+ */
27
+ export type Origin = "declared" | "scope" | "derived" | "consumer";
28
+
29
+ /** One consumer the run loads. */
30
+ export interface LoadedConsumer {
31
+ /** The name the consumer publishes itself under, which every finding names it by. */
32
+ readonly id: string;
33
+ /** The consumer's directory, absolute. */
34
+ readonly path: string;
35
+ }
36
+
37
+ /** One compiler configuration a run opens. */
38
+ export interface Configuration {
39
+ /**
40
+ * The name a run gives the project: the identifier the build matrix declares for it, or,
41
+ * for a derived one, its configuration file's path below the target root, else its
42
+ * absolute path. A consumer's is its configuration file's absolute path.
43
+ */
44
+ readonly id: string;
45
+ /** The compiler configuration file, absolute. */
46
+ readonly configFile: string;
47
+ readonly origin: Origin;
48
+ /** The compiler's parse of the configuration, which discovery could read and found not circular. */
49
+ readonly parsed: Parsed;
50
+ /** The consumer the configuration loads, for one whose origin is `consumer`. */
51
+ readonly consumer?: LoadedConsumer | undefined;
52
+ /**
53
+ * The configuration of a program the run forms for JavaScript files, which no file holds,
54
+ * so `configFile` is a free path the snapshot alone holds it at.
55
+ */
56
+ readonly written?: Readonly<Record<string, unknown>> | undefined;
57
+ }
58
+
59
+ /** Whether the run analyzes one configuration as the target's. */
60
+ export function isTargetConfiguration(configuration: Configuration): boolean {
61
+ return configuration.origin !== "consumer";
62
+ }
63
+
64
+ /**
65
+ * One project discovery derived and the run could not build, with the first line of the
66
+ * error that dropped it.
67
+ */
68
+ export interface NotBuilt {
69
+ /** The name the project would have carried, as {@link Configuration.id} spells it. */
70
+ readonly id: string;
71
+ /** The compiler configuration file, absolute. */
72
+ readonly configFile: string;
73
+ /** The error's first line, every path below the target root spelled relative to it. */
74
+ readonly error: string;
75
+ }
76
+
77
+ /**
78
+ * One entry of the target's matrix: a configuration discovery read, or a tree the plan forms
79
+ * programs over for the JavaScript files no configuration lists.
80
+ */
81
+ type Target =
82
+ | { readonly kind: "configuration"; readonly configuration: Configuration }
83
+ /** The target tree of a derived matrix. */
84
+ | { readonly kind: "derived" }
85
+ /** The directory of a matrix entry naming its `package.json`, at the absolute `manifest`. */
86
+ | { readonly kind: "declared"; readonly id: string; readonly manifest: string };
87
+
88
+ /** Every configuration file a run opens, the target's in discovery order, then each consumer's. */
89
+ export interface Discovery {
90
+ /** Whether the matrix is the configuration document's or one discovery derived. */
91
+ readonly mode: "declared" | "derived";
92
+ /** The target's entries, in the order a declared matrix lists them, a derived tree last. */
93
+ readonly targets: readonly Target[];
94
+ /** Every configuration of {@link targets}, in its order, then each consumer's. */
95
+ readonly configurations: readonly Configuration[];
96
+ /** The derived configurations the compiler could not read, in discovery order. */
97
+ readonly notBuilt: readonly NotBuilt[];
98
+ /** Every declared consumer, in the scope's order. */
99
+ readonly consumers: readonly LoadedConsumer[];
100
+ /** The consumers holding no configuration, which the plan forms programs over. */
101
+ readonly unconfigured: readonly LoadedConsumer[];
102
+ }
103
+
104
+ /**
105
+ * The first line of one error about the target, every mention of the target root spelled
106
+ * relative to it: a path below the root loses the root and its separator, and the root
107
+ * itself is `.`.
108
+ */
109
+ export function targetRelative(text: string, targetRoot: string): string {
110
+ const root = targetRoot.endsWith("/") ? targetRoot.slice(0, -1) : targetRoot;
111
+ const [first = ""] = text.split(/\r?\n/u);
112
+ const spelled = first.split(`${root}/`).join("").split(root).join(".").trim();
113
+ return spelled === "" ? "the configuration could not be built" : spelled;
114
+ }
115
+
116
+ /**
117
+ * Whether a derived matrix leaves one configuration file unopened: a `jsconfig.json` beside a
118
+ * `tsconfig.json`, because the compiler's editor host opens the directory's `tsconfig.json`
119
+ * first and so never its `jsconfig.json`.
120
+ */
121
+ function shadowed(host: Host, configFile: string): boolean {
122
+ return (
123
+ configFile.endsWith("/jsconfig.json") &&
124
+ host.kindOf(joinPath(dirnamePath(configFile), "tsconfig.json")) === "file"
125
+ );
126
+ }
127
+
128
+ /** The configuration files directly in `dir` a derived matrix opens, by name. */
129
+ export function configFilesIn(host: Host, dir: string): readonly string[] {
130
+ return filesIn(host, dir, isConfigFile).filter((name) => !shadowed(host, joinPath(dir, name)));
131
+ }
132
+
133
+ /** The configuration files below `root` a derived matrix opens, by absolute path in ascending order. */
134
+ function configFilesUnder(host: Host, root: string): readonly string[] {
135
+ return filesBelow(host, root, isConfigFile)
136
+ .map((path) => joinPath(root, path))
137
+ .filter((path) => !shadowed(host, path));
138
+ }
139
+
140
+ export type Parsed = ReturnType<Engine["parseConfigFile"]>;
141
+
142
+ /** The configuration files one parsed configuration's `references` name. */
143
+ function referencesOf(parsed: Parsed): readonly string[] {
144
+ // The parse answers a directory reference as the directory; the compiler reads a
145
+ // path not ending `.json` as its `tsconfig.json`:
146
+ // https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/core/projectreference.go#L18
147
+ return (parsed.projectReferences ?? []).map(({ path }) =>
148
+ path.endsWith(".json") ? path : joinPath(path, "tsconfig.json"),
149
+ );
150
+ }
151
+
152
+ /**
153
+ * The code of a configuration whose `extends` chain returns to a configuration it already
154
+ * passed. A snapshot that opens two configurations whose `extends` chains reach one such
155
+ * cycle panics (`OwnerCache.AddOwner: entry not found`, typescript 7.1.0-dev.20261014.1),
156
+ * so the analysis refuses one before any snapshot is asked to open it.
157
+ */
158
+ const CIRCULAR_EXTENDS = 18000;
159
+
160
+ /** One configuration file's parse, or why it cannot be read. */
161
+ function readConfiguration(
162
+ engine: Engine,
163
+ configFile: string,
164
+ ): { readonly parsed: Parsed } | { readonly reason: string } {
165
+ try {
166
+ const parsed = engine.parseConfigFile(configFile);
167
+ const circular = parsed.errors.find(({ code }) => code === CIRCULAR_EXTENDS);
168
+ // The compiler leaves the message's `{0}` unfilled:
169
+ // https://github.com/microsoft/TypeScript/pull/64755
170
+ return circular === undefined
171
+ ? { parsed }
172
+ : { reason: `TS${String(circular.code)}: ${circular.text.replace("{0}", () => configFile)}` };
173
+ } catch (error: unknown) {
174
+ return { reason: message(error) };
175
+ }
176
+ }
177
+
178
+ /** One configuration file the compiler parsed with no circular `extends`, and its parse. */
179
+ interface Read {
180
+ readonly configFile: string;
181
+ readonly parsed: Parsed;
182
+ }
183
+
184
+ /**
185
+ * Every configuration file reached from `pending`, each once, each followed by the
186
+ * configurations it references that `follows` admits, transitively. `unreadable` is told
187
+ * each configuration the compiler cannot read, which the walk then goes on without.
188
+ */
189
+ function derivedFrom(
190
+ engine: Engine,
191
+ host: Host,
192
+ pending: string[],
193
+ follows: (reference: string) => boolean,
194
+ unreadable: (configFile: string, reason: string) => void,
195
+ ): Read[] {
196
+ const seen = new Set<string>();
197
+ const found: Read[] = [];
198
+ for (let configFile = pending.shift(); configFile !== undefined; configFile = pending.shift()) {
199
+ const path = resolvePath(host.workingDirectory(), configFile);
200
+ if (seen.has(path)) {
201
+ continue;
202
+ }
203
+ seen.add(path);
204
+ const read = readConfiguration(engine, path);
205
+ if ("reason" in read) {
206
+ unreadable(path, read.reason);
207
+ continue;
208
+ }
209
+ found.push({ configFile: path, parsed: read.parsed });
210
+ pending.push(...referencesOf(read.parsed).filter(follows));
211
+ }
212
+ return found;
213
+ }
214
+
215
+ /** The name one module publishes itself under: the scope's, else its manifest's. */
216
+ export function moduleIdentity(host: Host, module: Module): string {
217
+ return module.id === "" ? packageScope(host, module.path)("index.ts").package : module.id;
218
+ }
219
+
220
+ /**
221
+ * The compiler configurations one consumer is loaded through: every one under its root and
222
+ * each one those reference, transitively. A consumer that is absent, no directory or holds a
223
+ * configuration the compiler cannot read ends the run naming it, because a finding computed
224
+ * without a declared consumer would claim a reference set the run never read.
225
+ */
226
+ function consumerProjects(engine: Engine, host: Host, root: string): readonly Configuration[] {
227
+ const kind = host.kindOf(root);
228
+ if (kind === "absent") {
229
+ throw failureExit(
230
+ "missing-consumer",
231
+ `the scope declares the consumer ${root}, and nothing is at that path: check the consumer out at ${root} and install its dependencies`,
232
+ );
233
+ }
234
+ if (kind !== "directory") {
235
+ throw failureExit(
236
+ "missing-consumer",
237
+ `the scope declares the consumer ${root}, which is not a directory: check the consumer out at ${root}`,
238
+ );
239
+ }
240
+ const found = derivedFrom(
241
+ engine,
242
+ host,
243
+ [...configFilesUnder(host, root)],
244
+ () => true,
245
+ (configFile, reason) => {
246
+ throw failureExit(
247
+ "missing-consumer",
248
+ `the consumer ${root} does not load, ${configFile} cannot be read as a compiler configuration: ${reason}`,
249
+ );
250
+ },
251
+ );
252
+ return found.map(({ configFile, parsed }) => ({
253
+ id: configFile,
254
+ configFile,
255
+ origin: "consumer" as const,
256
+ parsed,
257
+ }));
258
+ }
259
+
260
+ /** The failure of a consumer that holds no configuration and no JavaScript file a program is formed over. */
261
+ export function consumerWithoutSources(root: string): FailureExit {
262
+ return failureExit(
263
+ "missing-consumer",
264
+ `the consumer ${root} holds no compiler configuration and no JavaScript file, so none of its references can be read: check the consumer out at ${root} with its ${MARKERS.join(", ")} or its JavaScript sources`,
265
+ );
266
+ }
267
+
268
+ /** The failure of a target that holds no configuration and no JavaScript file a program is formed over. */
269
+ export function targetWithoutSources(target: string): FailureExit {
270
+ return failureExit(
271
+ "no-program",
272
+ `${target} holds no compiler configuration, no file named ${MARKERS.join(", ")}, and no JavaScript file: run from the directory that holds one, or name the languages to analyze`,
273
+ );
274
+ }
275
+
276
+ /** A target configuration a matrix or the invocation names that cannot be built, ending the run. */
277
+ function unbuilt(named: string, reason: string): never {
278
+ throw failureExit(
279
+ "no-program",
280
+ `${named} ${reason}: correct the configuration, or name the configurations to analyze`,
281
+ );
282
+ }
283
+
284
+ /**
285
+ * The projects a build matrix declares, in its order, each a file below the target root, a
286
+ * `package.json` entry a tree for the plan to form in its place. An entry naming an absent file,
287
+ * a directory or a file the compiler cannot read ends the run naming it; one naming an earlier
288
+ * entry's file adds nothing. A reference below the root the matrix does not name ends the run
289
+ * naming both, because the compiler builds a referenced project's sources into the referencing
290
+ * program without reporting their diagnostics. A reference outside the root is an outside project.
291
+ */
292
+ function declaredProjects(
293
+ engine: Engine,
294
+ host: Host,
295
+ targetRoot: string,
296
+ entries: readonly ProjectConfiguration[],
297
+ ): { readonly targets: readonly Target[]; readonly notBuilt: readonly NotBuilt[] } {
298
+ const targets: Target[] = [];
299
+ const projects: Configuration[] = [];
300
+ const references = new Map<string, readonly string[]>();
301
+ const manifests = new Set<string>();
302
+ for (const entry of entries) {
303
+ const configFile = joinPath(targetRoot, entry.project);
304
+ const named = `the project ${JSON.stringify(entry.id)} (${configFile})`;
305
+ const kind = host.kindOf(configFile);
306
+ if (kind !== "file") {
307
+ unbuilt(
308
+ named,
309
+ kind === "absent"
310
+ ? "does not exist"
311
+ : "is a directory, and a project names its compiler configuration file",
312
+ );
313
+ }
314
+ if (references.has(configFile) || manifests.has(configFile)) {
315
+ continue;
316
+ }
317
+ if (configFile.endsWith(`/${MANIFEST}`)) {
318
+ manifests.add(configFile);
319
+ targets.push({ kind: "declared", id: entry.id, manifest: configFile });
320
+ continue;
321
+ }
322
+ const read = readConfiguration(engine, configFile);
323
+ if ("reason" in read) {
324
+ unbuilt(named, `cannot be read as a compiler configuration: ${read.reason}`);
325
+ }
326
+ references.set(configFile, referencesOf(read.parsed));
327
+ const configuration: Configuration = {
328
+ id: entry.id,
329
+ configFile,
330
+ origin: "declared",
331
+ parsed: read.parsed,
332
+ };
333
+ projects.push(configuration);
334
+ targets.push({ kind: "configuration", configuration });
335
+ }
336
+ for (const project of projects) {
337
+ const unnamed = references
338
+ .get(project.configFile)
339
+ ?.find((path) => !references.has(path) && relativePath(targetRoot, path) !== undefined);
340
+ if (unnamed !== undefined) {
341
+ unbuilt(
342
+ `the project ${JSON.stringify(project.id)} (${project.configFile})`,
343
+ `references ${unnamed}, which the build matrix does not name. A referenced project is built into the program that references it, so the matrix names it too`,
344
+ );
345
+ }
346
+ }
347
+ return { targets, notBuilt: [] };
348
+ }
349
+
350
+ /**
351
+ * The projects a run derives where the matrix declares none: the configuration the scope
352
+ * names for the target where its path names one, every compiler configuration under the
353
+ * target root outside a skipped directory, then each configuration below the root those
354
+ * reference, transitively, each once. A configuration the scope names ends the run where it
355
+ * cannot be read; one the walk found is dropped and named. Where the scope names none, the
356
+ * plan forms programs over the target's JavaScript files no configuration lists.
357
+ */
358
+ function derivedProjects(
359
+ engine: Engine,
360
+ host: Host,
361
+ scope: Scope,
362
+ ): { readonly targets: readonly Target[]; readonly notBuilt: readonly NotBuilt[] } {
363
+ const targetRoot = scope.target.path;
364
+ const fromScope = scope.target.path.endsWith(".json")
365
+ ? [resolvePath(host.workingDirectory(), scope.target.path)]
366
+ : [];
367
+ const absoluteRoot = resolvePath(host.workingDirectory(), targetRoot);
368
+ const rootDir = resolvePath(
369
+ host.workingDirectory(),
370
+ fromScope.length > 0 ? dirnamePath(targetRoot) : targetRoot,
371
+ );
372
+ const idOf = (configFile: string): string => relativePath(targetRoot, configFile) ?? configFile;
373
+ const notBuilt: NotBuilt[] = [];
374
+ const found = derivedFrom(
375
+ engine,
376
+ host,
377
+ [...fromScope, ...configFilesUnder(host, targetRoot)],
378
+ (reference) => relativePath(rootDir, reference) !== undefined,
379
+ (configFile, reason) => {
380
+ if (fromScope.includes(configFile)) {
381
+ unbuilt(configFile, `cannot be read as a compiler configuration: ${reason}`);
382
+ }
383
+ notBuilt.push({
384
+ id: idOf(configFile),
385
+ configFile,
386
+ error: targetRelative(reason, absoluteRoot),
387
+ });
388
+ },
389
+ );
390
+ const configurations = found.map(({ configFile, parsed }): Configuration => ({
391
+ id: idOf(configFile),
392
+ configFile,
393
+ origin: fromScope.includes(configFile) ? "scope" : "derived",
394
+ parsed,
395
+ }));
396
+ return {
397
+ targets: [
398
+ ...configurations.map((configuration) => ({ kind: "configuration" as const, configuration })),
399
+ ...(fromScope.length > 0 ? [] : [{ kind: "derived" as const }]),
400
+ ],
401
+ notBuilt,
402
+ };
403
+ }
404
+
405
+ /**
406
+ * Every configuration file one run opens: the build matrix's project entries in its order,
407
+ * or, where it holds none, those derived from the scope and the target tree, then each
408
+ * declared consumer's, a configuration the target or an earlier consumer opens being theirs
409
+ * alone. The consumers are read first, so one that cannot be read ends the run before any
410
+ * failure of the target: the Contract's failure-class table lists `missing-consumer` before
411
+ * `no-program`.
412
+ */
413
+ export function discoverProjects(
414
+ engine: Engine,
415
+ host: Host,
416
+ scope: Scope,
417
+ configurations: readonly BuildConfiguration[] = [],
418
+ ): Discovery {
419
+ const consumers = scope.consumers.map((module) => {
420
+ const projects = consumerProjects(engine, host, module.path);
421
+ return { consumer: { id: moduleIdentity(host, module), path: module.path }, projects };
422
+ });
423
+ const declared = configurations.filter(
424
+ (entry): entry is ProjectConfiguration => "project" in entry,
425
+ );
426
+ const target =
427
+ declared.length > 0
428
+ ? {
429
+ mode: "declared" as const,
430
+ ...declaredProjects(engine, host, scope.target.path, declared),
431
+ }
432
+ : { mode: "derived" as const, ...derivedProjects(engine, host, scope) };
433
+
434
+ const read = target.targets.flatMap((one) =>
435
+ one.kind === "configuration" ? [one.configuration] : [],
436
+ );
437
+ const opened = new Set(read.map((one) => one.configFile));
438
+ const consumed: Configuration[] = [];
439
+ for (const { consumer, projects } of consumers) {
440
+ for (const one of projects.filter(({ configFile }) => !opened.has(configFile))) {
441
+ opened.add(one.configFile);
442
+ consumed.push({ ...one, consumer });
443
+ }
444
+ }
445
+ return {
446
+ mode: target.mode,
447
+ targets: target.targets,
448
+ configurations: [...read, ...consumed],
449
+ notBuilt: target.notBuilt,
450
+ consumers: consumers.map(({ consumer }) => consumer),
451
+ unconfigured: consumers
452
+ .filter(({ projects }) => projects.length === 0)
453
+ .map(({ consumer }) => consumer),
454
+ };
455
+ }
@@ -5,7 +5,7 @@
5
5
  * whether the path is the target's own or a workspace package's.
6
6
  */
7
7
 
8
- import { dirnamePath, joinPath, relativePath } from "./paths.ts";
8
+ import { dirnamePath, joinPath, relativePath } from "../paths.ts";
9
9
 
10
10
  /** The source extensions the compiler emits each output extension from. */
11
11
  const EMITTED: readonly { readonly emitted: string; readonly sources: readonly string[] }[] = [
@@ -0,0 +1,188 @@
1
+ import type { SourceFile } from "@typescript/native/ast";
2
+ import {
3
+ isNoSubstitutionTemplateLiteral,
4
+ isStringLiteral,
5
+ type Node,
6
+ } from "@typescript/native/ast";
7
+ import { packageOfSpecifier } from "../dependencies.ts";
8
+ import { MANIFEST } from "../fs/manifest.ts";
9
+ import { entriesOf } from "../fs/tree.ts";
10
+ import type { Host } from "../host.ts";
11
+ import { dirnamePath, isAbsolutePath, joinPath, relativePath, resolvePath } from "../paths.ts";
12
+ import { dependencySpecifiers, installedPackage, manifestsAbove } from "./module-lookup.ts";
13
+ import type { CompilerOptions } from "./plan.ts";
14
+
15
+ /** Where one project reads what its imports may name. */
16
+ export interface ModuleContext {
17
+ readonly host: Host;
18
+ /** The root of the module the project is the target's or a consumer's, absolute. */
19
+ readonly targetRoot: string;
20
+ /** The project configuration's `paths` mapping. */
21
+ readonly paths: PathsMapping;
22
+ /** Whether one manifest is a workspace member's, whose dependencies the workspace installs. */
23
+ readonly member: (manifest: string) => boolean;
24
+ }
25
+
26
+ /** One configuration's `paths` mapping, its targets read against `base`. */
27
+ interface PathsMapping {
28
+ readonly base: string;
29
+ readonly entries: readonly { readonly key: string; readonly targets: readonly string[] }[];
30
+ }
31
+
32
+ /** The string literal holding one offset, innermost first, a doc comment's included. */
33
+ function literalAt(file: SourceFile, offset: number): string | undefined {
34
+ let found: string | undefined;
35
+ const visit = (node: Node): void => {
36
+ if (offset < node.pos || offset >= node.end) {
37
+ return;
38
+ }
39
+ if (isStringLiteral(node) || isNoSubstitutionTemplateLiteral(node)) {
40
+ found = node.text;
41
+ }
42
+ // `forEachChild` does not enter a doc comment, where a JavaScript file imports types.
43
+ for (const doc of (node as { readonly jsDoc?: readonly Node[] }).jsDoc ?? []) {
44
+ visit(doc);
45
+ }
46
+ node.forEachChild(visit);
47
+ };
48
+ file.forEachChild(visit);
49
+ return found;
50
+ }
51
+
52
+ /**
53
+ * What one `paths` key's `*` matches in one specifier, `""` for a key with no `*` that
54
+ * is the specifier, or `undefined` where the key does not match it.
55
+ */
56
+ function mappedBy(key: string, specifier: string): string | undefined {
57
+ const star = key.indexOf("*");
58
+ if (star < 0) {
59
+ return key === specifier ? "" : undefined;
60
+ }
61
+ const prefix = key.slice(0, star);
62
+ const suffix = key.slice(star + 1);
63
+ return specifier.length >= prefix.length + suffix.length &&
64
+ specifier.startsWith(prefix) &&
65
+ specifier.endsWith(suffix)
66
+ ? specifier.slice(prefix.length, specifier.length - suffix.length)
67
+ : undefined;
68
+ }
69
+
70
+ /**
71
+ * One `paths` target, its first `*` replaced by `star` taken literally, as the compiler does:
72
+ * https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/module/resolver.go#L1282
73
+ */
74
+ function substituted(target: string, star: string): string {
75
+ const at = target.indexOf("*");
76
+ return at < 0 ? target : target.slice(0, at) + star + target.slice(at + 1);
77
+ }
78
+
79
+ /**
80
+ * Whether a path names something on disk, as written or with any extension; under
81
+ * `fileOnly`, only a file counts.
82
+ */
83
+ function provided(host: Host, path: string, fileOnly: boolean): boolean {
84
+ const counts = (at: string): boolean =>
85
+ fileOnly ? host.kindOf(at) === "file" : host.kindOf(at) !== "absent";
86
+ if (counts(path)) {
87
+ return true;
88
+ }
89
+ const dir = dirnamePath(path);
90
+ const stem = `${path.slice(dir.length + 1)}.`;
91
+ return entriesOf(host, dir).some(
92
+ (entry) => entry.name.startsWith(stem) && counts(joinPath(dir, entry.name)),
93
+ );
94
+ }
95
+
96
+ /** The manifest at or above `dir`, no higher than the target root, that declares `name`. */
97
+ function declaringManifest(
98
+ host: Host,
99
+ dir: string,
100
+ targetRoot: string,
101
+ name: string,
102
+ ): string | undefined {
103
+ for (const { dir: at, manifest } of manifestsAbove(host, dir, targetRoot)) {
104
+ if (dependencySpecifiers(manifest, name).length > 0) {
105
+ return joinPath(at, MANIFEST);
106
+ }
107
+ }
108
+ return undefined;
109
+ }
110
+
111
+ /**
112
+ * Why one import that the compiler cannot resolve names a module the project expects,
113
+ * with the fix, or undefined where the module is one the project does not expect.
114
+ */
115
+ function expectedModule(
116
+ context: ModuleContext,
117
+ importer: string,
118
+ specifier: string,
119
+ ): string | undefined {
120
+ const { host, targetRoot } = context;
121
+ const fix = "run the generator or the build that writes it";
122
+ if (specifier.startsWith(".") || isAbsolutePath(specifier)) {
123
+ return provided(host, resolvePath(dirnamePath(importer), specifier), false)
124
+ ? undefined
125
+ : `which names no file of the target: ${fix}`;
126
+ }
127
+ if (specifier.startsWith("#")) {
128
+ return `which the manifest's imports map to no file: ${fix}`;
129
+ }
130
+ const mapped = context.paths.entries.flatMap(({ key, targets }) => {
131
+ const star = mappedBy(key, specifier);
132
+ return star === undefined
133
+ ? []
134
+ : targets.map((target) => resolvePath(context.paths.base, substituted(target, star)));
135
+ });
136
+ const name = packageOfSpecifier(specifier);
137
+ // The compiler looks up in `node_modules` what `paths` does not resolve, so the error
138
+ // of an installed package's import is the import's own, a subpath its exports omit:
139
+ // https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/module/resolver.go#L548-L575
140
+ if (name !== undefined && installedPackage(host, dirnamePath(importer), name) !== undefined) {
141
+ return undefined;
142
+ }
143
+ if (mapped.length > 0) {
144
+ return mapped.some((path) => provided(host, path, true))
145
+ ? undefined
146
+ : `which the compiler configuration's paths map to no file: ${fix}`;
147
+ }
148
+ const manifest =
149
+ name === undefined
150
+ ? undefined
151
+ : declaringManifest(host, dirnamePath(importer), targetRoot, name);
152
+ // No run requires a nested package that is no workspace member to be installed.
153
+ if (
154
+ manifest === undefined ||
155
+ (dirnamePath(manifest) !== targetRoot && !context.member(manifest))
156
+ ) {
157
+ return undefined;
158
+ }
159
+ const where = relativePath(targetRoot, manifest) ?? manifest;
160
+ return `a dependency ${where} declares that is not installed: run the package manager's install`;
161
+ }
162
+
163
+ /** The `paths` mapping of the configuration at `configFile` whose parse answered `options`. */
164
+ export function pathsOf(configFile: string, options: CompilerOptions): PathsMapping {
165
+ // The parse answers `pathsBasePath`, the directory of the configuration that writes
166
+ // `paths` (an extended one included), though the client's option type omits it:
167
+ // https://github.com/microsoft/typescript-go/blob/89d5d5b2849a0db0957065889ca58536fa6d2e4a/internal/tsoptions/tsconfigparsing.go#L1120
168
+ const declaredAt = (options as { readonly pathsBasePath?: unknown }).pathsBasePath;
169
+ return {
170
+ base: typeof declaredAt === "string" ? declaredAt : dirnamePath(configFile),
171
+ entries: Object.entries(options.paths ?? {}).map(([key, targets]) => ({ key, targets })),
172
+ };
173
+ }
174
+
175
+ /**
176
+ * Why one import the compiler cannot resolve, at `offset` of `file`, names a module the project
177
+ * expects and nothing provides, with the fix; undefined where the project does not expect it.
178
+ */
179
+ export function missingModule(
180
+ context: ModuleContext,
181
+ file: SourceFile,
182
+ offset: number,
183
+ ): { readonly specifier: string; readonly reason: string } | undefined {
184
+ const specifier = literalAt(file, offset);
185
+ const reason =
186
+ specifier === undefined ? undefined : expectedModule(context, file.fileName, specifier);
187
+ return specifier === undefined || reason === undefined ? undefined : { specifier, reason };
188
+ }