@tsplc/compiler 0.5.13 → 0.5.15

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 (263) hide show
  1. package/README.md +33 -8
  2. package/dist/cli.js +794 -621
  3. package/dist/cli.js.map +19 -16
  4. package/dist/clock.js +5 -2
  5. package/dist/clock.js.map +5 -4
  6. package/dist/codesys.js +4162 -4157
  7. package/dist/codesys.js.map +43 -29
  8. package/dist/index.js +7852 -6549
  9. package/dist/index.js.map +53 -50
  10. package/dist/ir.js +2482 -2277
  11. package/dist/ir.js.map +28 -25
  12. package/dist/retain.js +3 -1
  13. package/dist/retain.js.map +3 -3
  14. package/dist/signals.js +115 -0
  15. package/dist/signals.js.map +10 -0
  16. package/dist/telemetry.js +25 -20
  17. package/dist/telemetry.js.map +6 -6
  18. package/package.json +19 -39
  19. package/src/codesys/config.ts +3 -3
  20. package/src/codesys/debug.ts +92 -108
  21. package/src/codesys/emit.ts +100 -88
  22. package/src/codesys/endpoints.ts +111 -0
  23. package/src/codesys/engineering.ts +30 -54
  24. package/src/codesys/explain/expressions.ts +445 -0
  25. package/src/codesys/explain/flags.ts +134 -0
  26. package/src/codesys/explain/parameters.ts +53 -0
  27. package/src/codesys/explain/rows.ts +182 -0
  28. package/src/codesys/explain/tables.ts +79 -0
  29. package/src/codesys/explain/tasks.ts +68 -0
  30. package/src/codesys/explain/writes.ts +497 -0
  31. package/src/codesys/explain.ts +85 -1401
  32. package/src/codesys/expressions.ts +41 -33
  33. package/src/codesys/global-lists.ts +114 -94
  34. package/src/codesys/global-names.ts +12 -14
  35. package/src/codesys/http.ts +428 -388
  36. package/src/codesys/instrumentation.ts +142 -148
  37. package/src/codesys/io-paths.ts +31 -23
  38. package/src/codesys/loops.ts +101 -0
  39. package/src/codesys/manifest.ts +269 -269
  40. package/src/codesys/names.ts +10 -33
  41. package/src/codesys/numeric.ts +89 -42
  42. package/src/codesys/plan.ts +202 -190
  43. package/src/codesys/programs.ts +23 -16
  44. package/src/codesys/publication.ts +13 -86
  45. package/src/codesys/required.ts +8 -0
  46. package/src/codesys/routines.ts +103 -489
  47. package/src/codesys/scheduling.ts +96 -0
  48. package/src/codesys/service-channels.ts +23 -0
  49. package/src/codesys/sources.ts +34 -28
  50. package/src/codesys/st-files.ts +112 -105
  51. package/src/codesys/statements.ts +409 -0
  52. package/src/codesys/storage.ts +41 -23
  53. package/src/codesys/symbols.ts +130 -119
  54. package/src/codesys/task-access.ts +65 -221
  55. package/src/codesys/telemetry-settings.ts +19 -0
  56. package/src/codesys/telemetry.ts +185 -148
  57. package/src/codesys/types.ts +69 -109
  58. package/src/codesys/vendor.ts +13 -50
  59. package/src/frontend/analyzer.ts +66 -111
  60. package/src/frontend/async.ts +316 -128
  61. package/src/frontend/blocks.ts +178 -154
  62. package/src/frontend/configuration.ts +49 -153
  63. package/src/frontend/const-functions.ts +50 -40
  64. package/src/frontend/control-flow.ts +164 -129
  65. package/src/frontend/cooperative.ts +17 -9
  66. package/src/frontend/coroutines.ts +539 -441
  67. package/src/frontend/dialect-names.ts +104 -0
  68. package/src/frontend/elaborate/array-searches.ts +77 -69
  69. package/src/frontend/elaborate/assignments.ts +68 -77
  70. package/src/frontend/elaborate/calls.ts +878 -512
  71. package/src/frontend/elaborate/capture-protocol.ts +118 -21
  72. package/src/frontend/elaborate/capture.ts +196 -109
  73. package/src/frontend/elaborate/declarations.ts +124 -0
  74. package/src/frontend/elaborate/dialect.ts +119 -46
  75. package/src/frontend/elaborate/expressions.ts +763 -442
  76. package/src/frontend/elaborate/filtered-arrays.ts +127 -91
  77. package/src/frontend/elaborate/frame.ts +50 -2
  78. package/src/frontend/elaborate/generics.ts +20 -34
  79. package/src/frontend/elaborate/graph.ts +13 -87
  80. package/src/frontend/elaborate/imports.ts +0 -5
  81. package/src/frontend/elaborate/index.ts +416 -169
  82. package/src/frontend/elaborate/initializers.ts +64 -42
  83. package/src/frontend/elaborate/iterators.ts +228 -364
  84. package/src/frontend/elaborate/layouts.ts +477 -116
  85. package/src/frontend/elaborate/model.ts +113 -55
  86. package/src/frontend/elaborate/references.ts +116 -138
  87. package/src/frontend/elaborate/retention.ts +137 -59
  88. package/src/frontend/elaborate/specialize.ts +617 -308
  89. package/src/frontend/elaborate/statements.ts +436 -247
  90. package/src/frontend/elaborate/storage.ts +280 -213
  91. package/src/frontend/elaborate/time-constructors.ts +34 -0
  92. package/src/frontend/elaborate/values.ts +77 -7
  93. package/src/frontend/elaborate/writes.ts +98 -84
  94. package/src/frontend/expressions.ts +149 -151
  95. package/src/frontend/generated-names.ts +3 -3
  96. package/src/frontend/intrinsics.ts +31 -17
  97. package/src/frontend/lowering-key.ts +23 -0
  98. package/src/frontend/module-graph.ts +105 -43
  99. package/src/frontend/positions.ts +43 -35
  100. package/src/frontend/promise-chains.ts +226 -266
  101. package/src/frontend/promises.ts +88 -67
  102. package/src/frontend/routines.ts +122 -65
  103. package/src/frontend/sdk.ts +104 -5
  104. package/src/frontend/services.ts +149 -102
  105. package/src/frontend/statements.ts +60 -64
  106. package/src/frontend/tasks.ts +151 -96
  107. package/src/frontend/telemetry.ts +1 -1
  108. package/src/frontend/types.ts +366 -181
  109. package/src/frontend/typescript.ts +239 -112
  110. package/src/index.ts +2 -1
  111. package/src/ir/aggregates.ts +90 -75
  112. package/src/ir/allocation.ts +29 -18
  113. package/src/ir/conditions.ts +38 -78
  114. package/src/ir/core-model.ts +128 -11
  115. package/src/ir/declarations.ts +18 -32
  116. package/src/ir/diagnostics.ts +25 -18
  117. package/src/ir/domains.ts +26 -23
  118. package/src/ir/fold.ts +55 -50
  119. package/src/ir/identity.ts +10 -0
  120. package/src/ir/index.ts +31 -7
  121. package/src/ir/job-errors.ts +30 -8
  122. package/src/ir/layouts.ts +63 -34
  123. package/src/ir/link.ts +11 -46
  124. package/src/ir/model.ts +25 -6
  125. package/src/ir/normalize.ts +3 -28
  126. package/src/ir/numeric.ts +45 -18
  127. package/src/ir/operands.ts +102 -0
  128. package/src/ir/ranges.ts +242 -67
  129. package/src/ir/routine.ts +126 -183
  130. package/src/ir/scalars.ts +29 -25
  131. package/src/ir/share.ts +323 -343
  132. package/src/ir/short-circuit.ts +140 -0
  133. package/src/ir/tables.ts +111 -0
  134. package/src/ir/temporaries.ts +15 -113
  135. package/src/ir/types.ts +32 -8
  136. package/src/project/artifacts.ts +143 -71
  137. package/src/project/cli.ts +104 -87
  138. package/src/project/debugger.ts +48 -34
  139. package/src/project/deployments.ts +12 -68
  140. package/src/project/flags.ts +14 -0
  141. package/src/project/plant.ts +293 -225
  142. package/src/project/project-commands.ts +254 -192
  143. package/src/project/project.ts +184 -141
  144. package/src/project/release.ts +22 -28
  145. package/src/project/server.ts +22 -26
  146. package/src/project/terminal.ts +13 -2
  147. package/src/runtime/async.ts +17 -13
  148. package/src/runtime/launch.ts +5 -2
  149. package/src/runtime/main.ts +8 -3
  150. package/src/runtime/missing-file.ts +3 -0
  151. package/src/runtime/process.ts +24 -19
  152. package/src/runtime/program.ts +20 -53
  153. package/src/runtime/retain.ts +18 -10
  154. package/src/runtime/runtime.ts +47 -60
  155. package/src/runtime/settings.ts +27 -0
  156. package/src/runtime/storage.ts +49 -74
  157. package/src/runtime/symbols.ts +19 -75
  158. package/src/runtime/telemetry.ts +37 -14
  159. package/src/runtime/transport.ts +26 -30
  160. package/stdlib/clock.ts +4 -2
  161. package/stdlib/plc-time.ts +2 -0
  162. package/stdlib/retain.ts +2 -0
  163. package/stdlib/signals.ts +142 -0
  164. package/stdlib/telemetry.ts +5 -12
  165. package/types/dialect.d.ts +49 -0
  166. package/types/globals.d.ts +2 -42
  167. package/types/index.d.ts +3 -1
  168. package/types/retain.d.ts +8 -2
  169. package/types/telemetry.d.ts +3 -0
  170. package/dist/bun-runtime.js +0 -1472
  171. package/dist/bun-runtime.js.map +0 -26
  172. package/dist/elaborate-capture.js +0 -304
  173. package/dist/elaborate-capture.js.map +0 -11
  174. package/dist/math.js +0 -24
  175. package/dist/math.js.map +0 -10
  176. package/dist/project.js +0 -626
  177. package/dist/project.js.map +0 -12
  178. package/dist/server-check.js +0 -59
  179. package/dist/server-check.js.map +0 -11
  180. package/src/frontend/exposure.ts +0 -181
  181. package/src/project/index.ts +0 -6
  182. package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
  183. package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
  184. package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
  185. package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
  186. package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
  187. package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
  188. package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
  189. package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
  190. package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
  191. package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
  192. package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
  193. package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
  194. package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
  195. package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
  196. package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
  197. package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
  198. package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
  199. package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
  200. package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
  201. package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
  202. package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
  203. package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
  204. package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
  205. package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
  206. package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
  207. package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
  208. package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
  209. package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
  210. package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
  211. package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
  212. package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
  213. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
  214. package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
  215. package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
  216. package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
  217. package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
  218. package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
  219. package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
  220. package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
  221. package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
  222. package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
  223. package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
  224. package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
  225. package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
  226. package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
  227. package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
  228. package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
  229. package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
  230. package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
  231. package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
  232. package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
  233. package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
  234. package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
  235. package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
  236. package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
  237. package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
  238. package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
  239. package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
  240. package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
  241. package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
  242. package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
  243. package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
  244. package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
  245. package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
  246. package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
  247. package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
  248. package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
  249. package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
  250. package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
  251. package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
  252. package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
  253. package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
  254. package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
  255. package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
  256. package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
  257. package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
  258. package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
  259. package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
  260. package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
  261. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
  262. package/stdlib/math.ts +0 -23
  263. package/types/plc.d.ts +0 -4
@@ -1,10 +1,11 @@
1
- import { relative } from "node:path";
2
1
  import { identityDigest } from "@tsplc/compiler/ir";
3
2
  import ts from "typescript";
4
- import type {
5
- CapturedGraph,
6
- CapturedNode,
7
- CapturedValue,
3
+ import { modulePath } from "../lowering-key";
4
+ import {
5
+ boundNames,
6
+ type CapturedGraph,
7
+ type CapturedNode,
8
+ type CapturedValue,
8
9
  } from "./capture-protocol";
9
10
 
10
11
  /** One step from an owner to the object it holds. */
@@ -20,16 +21,34 @@ export interface Owner {
20
21
 
21
22
  /** The TypeScript path of an owned object or field, as the program names it: `program.diverters.sampleCounters[10]`. */
22
23
  export const tsPath = (owner: Owner): string =>
23
- owner.root.name + owner.steps.map(stepPath).join("");
24
+ owner.root.name + owner.steps.map((step) => stepPath(step, String)).join("");
24
25
 
25
- /** One step of a TypeScript path: an index, a numeric key, or a property. */
26
- const stepPath = (step: Step): string => {
26
+ /** One step of a path: an index, a numeric key in brackets as `key` spells it, or a field. */
27
+ export const stepPath = (step: Step, key: (name: string) => string): string => {
27
28
  if (step.kind === "index") {
28
29
  return `[${step.index}]`;
29
30
  }
30
- return /^\d+$/.test(step.name) ? `[${step.name}]` : `.${step.name}`;
31
+ return /^\d+$/.test(step.name) ? `[${key(step.name)}]` : `.${step.name}`;
31
32
  };
32
33
 
34
+ /** Where `holder`, owned at `owner`, puts what it holds at `step`. */
35
+ export const ownerAt = (owner: Owner, step: Step): Owner => ({
36
+ root: owner.root,
37
+ steps: [...owner.steps, step],
38
+ });
39
+
40
+ const sameStep = (left: Step, right: Step): boolean =>
41
+ left.kind === "index"
42
+ ? right.kind === "index" && left.index === right.index
43
+ : right.kind === "field" && left.name === right.name;
44
+
45
+ /** The members of a union, or the type itself. */
46
+ export const unionMembers = (type: ts.Type): readonly ts.Type[] =>
47
+ type.isUnion() ? type.types : [type];
48
+
49
+ export const isExported = (declaration: ts.Declaration): boolean =>
50
+ Boolean(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export);
51
+
33
52
  /** Each step from a captured object to what it holds, with the value there. */
34
53
  export const children = (captured: CapturedNode): [Step, CapturedValue][] => {
35
54
  if (captured.kind === "array") {
@@ -68,7 +87,13 @@ export interface Root {
68
87
  name: string;
69
88
  /** The variable, or the element of a destructuring declaration, that names the root. */
70
89
  declaration: ts.VariableDeclaration | ts.BindingElement;
90
+ /**
91
+ * Whether something outside the program addresses it: an export the hardware, HMI and server
92
+ * read, or a binding the config's `io` reaches. Every field of such a root is stored.
93
+ */
71
94
  exported: boolean;
95
+ /** Whether the config's `io` reaches it, so the hardware may write any field of it. */
96
+ bound: boolean;
72
97
  /** What setup bound it to. */
73
98
  value: CapturedValue;
74
99
  }
@@ -91,6 +116,8 @@ export interface ModelNode {
91
116
  export class Model {
92
117
  readonly nodes: ModelNode[];
93
118
  readonly roots: Root[] = [];
119
+ /** The paths the hardware writes every scan, with the input address of each. */
120
+ readonly inputs: ReadonlyMap<string, string>;
94
121
  private readonly projectRoot: string;
95
122
 
96
123
  constructor(
@@ -99,7 +126,17 @@ export class Model {
99
126
  readonly program: ts.Program,
100
127
  entry: string,
101
128
  projectRoot: string,
129
+ {
130
+ bound,
131
+ inputs,
132
+ }: {
133
+ /** The bindings the config's `io` reaches state through. */
134
+ bound: ReadonlySet<string>;
135
+ /** The paths the config's `io` binds to inputs, with each input's address. */
136
+ inputs: ReadonlyMap<string, string>;
137
+ },
102
138
  ) {
139
+ this.inputs = inputs;
103
140
  this.nodes = graph.nodes.map((captured, id) => ({
104
141
  id,
105
142
  reference: id,
@@ -116,9 +153,8 @@ export class Model {
116
153
  file: binding.file,
117
154
  name: binding.name,
118
155
  declaration,
119
- exported: Boolean(
120
- ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export,
121
- ),
156
+ exported: isExported(declaration) || bound.has(binding.name),
157
+ bound: bound.has(binding.name),
122
158
  value: binding.value,
123
159
  };
124
160
  this.roots.push(root);
@@ -137,29 +173,52 @@ export class Model {
137
173
  * module and its path from there. Undefined for an object no root owns.
138
174
  */
139
175
  identity(node: ModelNode): string | undefined {
176
+ return node.owner && this.ownerIdentity(node.owner);
177
+ }
178
+
179
+ private ownerIdentity(owner: Owner): string {
180
+ return `${modulePath(this.projectRoot, owner.root.file)}:${tsPath(owner)}`;
181
+ }
182
+
183
+ /** Whether `node` is owned exactly at `owner`, rather than only held there. */
184
+ ownsAt(node: ModelNode, owner: Owner): boolean {
185
+ const own = node.owner;
186
+ return (
187
+ own !== undefined &&
188
+ own.root === owner.root &&
189
+ own.steps.length === owner.steps.length &&
190
+ own.steps.every((step, index) => {
191
+ const other = owner.steps[index];
192
+ return other !== undefined && sameStep(step, other);
193
+ })
194
+ );
195
+ }
196
+
197
+ /** What setup left in a record's or instance's field: undefined when it set none. */
198
+ field(holder: ModelNode, name: string): CapturedValue {
140
199
  return (
141
- node.owner &&
142
- `${relative(this.projectRoot, node.owner.root.file).replace(/\\/g, "/")}:${tsPath(node.owner)}`
200
+ child(holder.captured, { kind: "field", name }) ?? { kind: "undefined" }
143
201
  );
144
202
  }
145
203
 
146
204
  /** Gives each object setup put somewhere its reference id, from its identity. */
147
205
  private assignReferences(): void {
148
- const taken = new Map<number, ModelNode>();
206
+ const taken = new Map<number, Owner>();
149
207
  for (const node of this.nodes) {
150
- const identity = this.identity(node);
151
- if (!identity || !node.owner) {
208
+ const owner = node.owner;
209
+ if (!owner) {
152
210
  continue;
153
211
  }
154
212
  const reference =
155
- Number.parseInt(identityDigest(identity), 16) & 0x7fffffff;
213
+ Number.parseInt(identityDigest(this.ownerIdentity(owner)), 16) &
214
+ 0x7fffffff;
156
215
  const other = taken.get(reference);
157
- if (other?.owner) {
216
+ if (other) {
158
217
  throw new Error(
159
- `${tsPath(other.owner)} and ${tsPath(node.owner)} would hold the same reference id; rename one of them.`,
218
+ `${tsPath(other)} and ${tsPath(owner)} would hold the same reference id; rename one of them.`,
160
219
  );
161
220
  }
162
- taken.set(reference, node);
221
+ taken.set(reference, owner);
163
222
  node.reference = reference;
164
223
  }
165
224
  }
@@ -182,29 +241,21 @@ export class Model {
182
241
  file: string,
183
242
  name: string,
184
243
  ): ts.VariableDeclaration | ts.BindingElement {
185
- const source = this.program.getSourceFile(file);
186
- const named = (
187
- declaration: ts.VariableDeclaration | ts.BindingElement,
188
- ): ts.VariableDeclaration | ts.BindingElement | undefined => {
189
- if (ts.isIdentifier(declaration.name)) {
190
- return declaration.name.text === name ? declaration : undefined;
191
- }
192
- for (const element of declaration.name.elements) {
193
- const found = ts.isBindingElement(element) && named(element);
194
- if (found) {
195
- return found;
196
- }
197
- }
198
- return undefined;
199
- };
200
- for (const statement of source?.statements ?? []) {
201
- if (ts.isVariableStatement(statement)) {
202
- for (const declaration of statement.declarationList.declarations) {
203
- const found = named(declaration);
204
- if (found) {
205
- return found;
206
- }
207
- }
244
+ const declarations = this.sourceFile(file).statements.flatMap(
245
+ (statement) =>
246
+ ts.isVariableStatement(statement)
247
+ ? [...statement.declarationList.declarations]
248
+ : [],
249
+ );
250
+ for (const declaration of declarations) {
251
+ const named = boundNames(declaration.name).find(
252
+ (bound) => bound.text === name,
253
+ )?.parent;
254
+ if (
255
+ named &&
256
+ (ts.isVariableDeclaration(named) || ts.isBindingElement(named))
257
+ ) {
258
+ return named;
208
259
  }
209
260
  }
210
261
  throw new Error(`${file}: no module binding ${name}.`);
@@ -246,6 +297,17 @@ export class Model {
246
297
  return found;
247
298
  }
248
299
 
300
+ /** The nearest member of a class or the classes it extends that is `name` and passes `is`. */
301
+ memberNamed<T extends ts.ClassElement>(
302
+ declaration: ts.ClassDeclaration,
303
+ name: string,
304
+ is: (member: ts.ClassElement) => member is T,
305
+ ): T | undefined {
306
+ return this.members(declaration).find(
307
+ (member): member is T => is(member) && member.name?.getText() === name,
308
+ );
309
+ }
310
+
249
311
  /** A class field's declaration: a property, or a constructor parameter property. */
250
312
  member(
251
313
  declaration: ts.ClassDeclaration,
@@ -319,15 +381,13 @@ export class Model {
319
381
  holder: CapturedNode,
320
382
  ): ts.Type | undefined {
321
383
  const variant = this.variant(type, holder);
322
- const property =
323
- variant.getProperty(name) ??
324
- this.checker.getPropertyOfType(variant, name);
384
+ const property = variant.getProperty(name);
325
385
  return property && this.checker.getTypeOfSymbol(property);
326
386
  }
327
387
 
328
388
  /** The member of a union of object types that a captured object is, by its literal fields. */
329
389
  variant(type: ts.Type, value: CapturedNode): ts.Type {
330
- const candidates = (type.isUnion() ? type.types : [type]).filter(
390
+ const candidates = unionMembers(type).filter(
331
391
  (member) => !(member.flags & ts.TypeFlags.Undefined),
332
392
  );
333
393
  const [first] = candidates;
@@ -355,13 +415,11 @@ export class Model {
355
415
  );
356
416
  }
357
417
 
418
+ /** The type of an array's or tuple's elements. */
358
419
  elementType(type: ts.Type): ts.Type | undefined {
359
- const target = this.checker.getNonNullableType(type);
360
- return (
361
- this.checker.getIndexTypeOfType(target, ts.IndexKind.Number) ??
362
- (this.checker.isTupleType(target)
363
- ? this.checker.getTypeArguments(target as ts.TypeReference)[0]
364
- : undefined)
420
+ return this.checker.getIndexTypeOfType(
421
+ this.checker.getNonNullableType(type),
422
+ ts.IndexKind.Number,
365
423
  );
366
424
  }
367
425
 
@@ -388,7 +446,7 @@ export class Model {
388
446
  : undefined;
389
447
  for (const [step, held] of children(node.captured)) {
390
448
  queue.push({
391
- owner: { root: next.owner.root, steps: [...next.owner.steps, step] },
449
+ owner: ownerAt(next.owner, step),
392
450
  type:
393
451
  step.kind === "index"
394
452
  ? element