@tsplc/compiler 0.2.16 → 0.2.18

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 (157) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +920 -943
  3. package/dist/cli.js.map +13 -10
  4. package/dist/codesys.js +3893 -3352
  5. package/dist/codesys.js.map +29 -20
  6. package/dist/elaborate-capture.js +33 -17
  7. package/dist/elaborate-capture.js.map +3 -3
  8. package/dist/index.js +8787 -6855
  9. package/dist/index.js.map +40 -20
  10. package/dist/ir.js +1575 -1072
  11. package/dist/ir.js.map +21 -17
  12. package/dist/math.js.map +2 -2
  13. package/dist/project.js +154 -60
  14. package/dist/project.js.map +5 -5
  15. package/dist/telemetry.js +27 -9
  16. package/dist/telemetry.js.map +5 -5
  17. package/package.json +112 -112
  18. package/src/codesys/config.ts +50 -0
  19. package/src/codesys/debug.ts +670 -0
  20. package/src/codesys/emit.ts +142 -0
  21. package/src/codesys/engineering.ts +109 -0
  22. package/src/codesys/expressions.ts +92 -0
  23. package/src/codesys/global-lists.ts +250 -0
  24. package/src/codesys/global-names.ts +68 -0
  25. package/src/codesys/http.ts +890 -0
  26. package/src/codesys/index.ts +30 -0
  27. package/src/codesys/instrumentation.ts +430 -0
  28. package/src/codesys/io-paths.ts +140 -0
  29. package/src/codesys/manifest.ts +643 -0
  30. package/src/codesys/names.ts +81 -0
  31. package/src/codesys/numeric.ts +409 -0
  32. package/src/codesys/operands.ts +62 -0
  33. package/src/codesys/plan.ts +605 -0
  34. package/src/codesys/programs.ts +53 -0
  35. package/src/codesys/publication.ts +171 -0
  36. package/src/codesys/routines.ts +603 -0
  37. package/src/codesys/sources.ts +71 -0
  38. package/src/codesys/st-files.ts +115 -0
  39. package/src/codesys/storage.ts +97 -0
  40. package/src/codesys/symbols.ts +220 -0
  41. package/src/codesys/task-access.ts +320 -0
  42. package/src/codesys/telemetry.ts +732 -0
  43. package/src/codesys/types.ts +250 -0
  44. package/src/codesys/vendor.ts +150 -0
  45. package/src/frontend/analyzer.ts +794 -0
  46. package/src/frontend/async.ts +317 -1137
  47. package/src/frontend/blocks.ts +537 -403
  48. package/src/frontend/configuration.ts +176 -144
  49. package/src/frontend/const-functions.ts +183 -148
  50. package/src/frontend/cooperative.ts +73 -43
  51. package/src/frontend/coroutines.ts +1032 -0
  52. package/src/frontend/elaborate/calls.ts +535 -0
  53. package/src/frontend/elaborate/capture.ts +157 -101
  54. package/src/frontend/elaborate/dialect.ts +257 -0
  55. package/src/frontend/elaborate/expressions.ts +992 -0
  56. package/src/frontend/elaborate/frame.ts +53 -0
  57. package/src/frontend/elaborate/graph.ts +116 -71
  58. package/src/frontend/elaborate/imports.ts +70 -0
  59. package/src/frontend/elaborate/index.ts +435 -331
  60. package/src/frontend/elaborate/initializers.ts +198 -0
  61. package/src/frontend/elaborate/layouts.ts +591 -384
  62. package/src/frontend/elaborate/model.ts +264 -169
  63. package/src/frontend/elaborate/specialize.ts +343 -2586
  64. package/src/frontend/elaborate/statements.ts +606 -0
  65. package/src/frontend/elaborate/storage.ts +546 -0
  66. package/src/frontend/elaborate/values.ts +58 -0
  67. package/src/frontend/elaborate/writes.ts +100 -0
  68. package/src/frontend/exposure.ts +172 -140
  69. package/src/frontend/expressions.ts +1383 -0
  70. package/src/frontend/intrinsics.ts +337 -0
  71. package/src/frontend/module-graph.ts +89 -59
  72. package/src/frontend/positions.ts +271 -200
  73. package/src/frontend/promise-chains.ts +897 -0
  74. package/src/frontend/promises.ts +390 -747
  75. package/src/frontend/routines.ts +181 -0
  76. package/src/frontend/sdk.ts +82 -0
  77. package/src/frontend/services.ts +506 -372
  78. package/src/frontend/statements.ts +855 -0
  79. package/src/frontend/tasks.ts +471 -0
  80. package/src/frontend/telemetry.ts +174 -0
  81. package/src/frontend/types.ts +845 -551
  82. package/src/frontend/typescript.ts +323 -2543
  83. package/src/index.ts +5 -5
  84. package/src/ir/aggregates.ts +275 -0
  85. package/src/ir/allocation.ts +84 -0
  86. package/src/ir/core-model.ts +146 -0
  87. package/src/ir/declarations.ts +295 -0
  88. package/src/ir/diagnostics.ts +61 -0
  89. package/src/ir/domains.ts +152 -0
  90. package/src/ir/fold.ts +342 -0
  91. package/src/ir/index.ts +90 -0
  92. package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
  93. package/src/ir/layouts.ts +181 -0
  94. package/src/ir/link.ts +354 -0
  95. package/src/ir/model.ts +386 -0
  96. package/{ir → src/ir}/normalization-error.ts +4 -4
  97. package/src/ir/normalize.ts +70 -0
  98. package/src/ir/numeric.ts +171 -0
  99. package/src/ir/ranges.ts +256 -0
  100. package/src/ir/routine.ts +1163 -0
  101. package/src/ir/scalars.ts +321 -0
  102. package/src/ir/types.ts +127 -0
  103. package/src/project/artifacts.ts +342 -0
  104. package/src/project/cli.ts +177 -0
  105. package/src/project/debugger.ts +82 -0
  106. package/src/project/index.ts +6 -0
  107. package/src/project/plant.ts +467 -0
  108. package/src/project/project-commands.ts +372 -0
  109. package/src/project/project.ts +457 -0
  110. package/src/project/release.ts +153 -0
  111. package/src/project/server.ts +150 -0
  112. package/src/project/terminal.ts +32 -0
  113. package/stdlib/math.ts +14 -10
  114. package/stdlib/telemetry.ts +71 -47
  115. package/types/async.d.ts +46 -40
  116. package/types/data-types.d.ts +26 -26
  117. package/types/globals.d.ts +41 -38
  118. package/types/index.d.ts +96 -51
  119. package/types/plc.d.ts +3 -4
  120. package/types/telemetry.d.ts +14 -14
  121. package/backends/codesys/config.ts +0 -54
  122. package/backends/codesys/debug.ts +0 -509
  123. package/backends/codesys/emit.ts +0 -1738
  124. package/backends/codesys/engineering.ts +0 -88
  125. package/backends/codesys/global-names.ts +0 -49
  126. package/backends/codesys/http.ts +0 -675
  127. package/backends/codesys/index.ts +0 -13
  128. package/backends/codesys/io-paths.ts +0 -105
  129. package/backends/codesys/manifest.ts +0 -312
  130. package/backends/codesys/names.ts +0 -76
  131. package/backends/codesys/numeric.ts +0 -356
  132. package/backends/codesys/plan.ts +0 -457
  133. package/backends/codesys/publication.ts +0 -59
  134. package/backends/codesys/storage.ts +0 -70
  135. package/backends/codesys/task-access.ts +0 -293
  136. package/backends/codesys/telemetry.ts +0 -593
  137. package/backends/codesys/types.ts +0 -179
  138. package/backends/codesys/vendor.ts +0 -108
  139. package/ir/aggregates.ts +0 -333
  140. package/ir/allocation.ts +0 -74
  141. package/ir/core-model.ts +0 -129
  142. package/ir/core.ts +0 -64
  143. package/ir/declarations.ts +0 -208
  144. package/ir/diagnostics.ts +0 -43
  145. package/ir/domains.ts +0 -96
  146. package/ir/index.ts +0 -8
  147. package/ir/link.ts +0 -185
  148. package/ir/model.ts +0 -346
  149. package/ir/routine.ts +0 -859
  150. package/ir/scalars.ts +0 -675
  151. package/ir/types.ts +0 -76
  152. package/project/artifacts.ts +0 -277
  153. package/project/cli.ts +0 -395
  154. package/project/index.ts +0 -1
  155. package/project/plant.ts +0 -509
  156. package/project/project.ts +0 -358
  157. package/project/server.ts +0 -113
@@ -2,189 +2,284 @@ import ts from "typescript";
2
2
  import type { CapturedGraph, CapturedNode, CapturedValue } from "./graph.ts";
3
3
 
4
4
  /** One step from an owner to the object it holds. */
5
- export type Step = { kind: "field"; name: string } | { kind: "index"; index: number };
5
+ export type Step =
6
+ { kind: "field"; name: string } | { kind: "index"; index: number };
6
7
 
7
8
  /** Where a captured object is stored: a root binding and the steps to it. */
8
9
  export interface Owner {
9
- root: Root;
10
- steps: readonly Step[];
10
+ root: Root;
11
+ steps: readonly Step[];
11
12
  }
12
13
 
13
14
  /** The TypeScript path of an owned object or field, as the program names it: `program.diverters.binTrackers[10]`. */
14
15
  export const tsPath = (owner: Owner): string =>
15
- owner.root.name +
16
- owner.steps
17
- .map((step) =>
18
- step.kind === "index"
19
- ? `[${step.index}]`
20
- : /^\d+$/.test(step.name)
21
- ? `[${step.name}]`
22
- : `.${step.name}`,
23
- )
24
- .join("");
16
+ owner.root.name + owner.steps.map(stepPath).join("");
17
+
18
+ /** One step of a TypeScript path: an index, a numeric key, or a property. */
19
+ const stepPath = (step: Step): string => {
20
+ if (step.kind === "index") {
21
+ return `[${step.index}]`;
22
+ }
23
+ return /^\d+$/.test(step.name) ? `[${step.name}]` : `.${step.name}`;
24
+ };
25
+
26
+ /** Each step from a captured object to what it holds, with the value there. */
27
+ export const children = (captured: CapturedNode): [Step, CapturedValue][] => {
28
+ if (captured.kind === "array") {
29
+ return captured.items.map((item, index) => [
30
+ { kind: "index", index },
31
+ item,
32
+ ]);
33
+ }
34
+ if (captured.kind === "object" || captured.kind === "instance") {
35
+ return Object.entries(captured.fields).map(([name, field]) => [
36
+ { kind: "field", name },
37
+ field,
38
+ ]);
39
+ }
40
+ return [];
41
+ };
42
+
43
+ /** The value one step down from a captured object, when it has one there. */
44
+ const child = (
45
+ captured: CapturedNode,
46
+ step: Step,
47
+ ): CapturedValue | undefined => {
48
+ if (step.kind === "index") {
49
+ return captured.kind === "array" ? captured.items[step.index] : undefined;
50
+ }
51
+ return captured.kind === "object" || captured.kind === "instance"
52
+ ? captured.fields[step.name]
53
+ : undefined;
54
+ };
55
+
56
+ /** A field a class declares: a property, or a constructor parameter property. */
57
+ type ClassField = ts.PropertyDeclaration | ts.ParameterPropertyDeclaration;
25
58
 
26
59
  export interface Root {
27
- file: string;
28
- name: string;
29
- declaration: ts.VariableDeclaration;
30
- exported: boolean;
60
+ file: string;
61
+ name: string;
62
+ declaration: ts.VariableDeclaration;
63
+ exported: boolean;
64
+ /** What setup bound it to. */
65
+ value: CapturedValue;
31
66
  }
32
67
 
33
68
  export interface ModelNode {
34
- id: number;
35
- captured: CapturedNode;
36
- /** The declared type where the object is stored; undefined for functions and services. */
37
- type?: ts.Type;
38
- /** The first place setup reached it. Every other place holding it refers to this one. */
39
- owner?: Owner;
69
+ id: number;
70
+ captured: CapturedNode;
71
+ /** The declared type where the object is stored; undefined for functions and services. */
72
+ type?: ts.Type;
73
+ /** The first place setup reached it. Every other place holding it refers to this one. */
74
+ owner?: Owner;
40
75
  }
41
76
 
42
77
  /** The captured setup graph with each object's owner and declared type. */
43
78
  export class Model {
44
- readonly nodes: ModelNode[];
45
- readonly roots: Root[] = [];
46
- readonly rootValues = new Map<Root, CapturedValue>();
47
-
48
- constructor(
49
- readonly graph: CapturedGraph,
50
- readonly checker: ts.TypeChecker,
51
- readonly program: ts.Program,
52
- entry: string,
53
- ) {
54
- this.nodes = graph.nodes.map((captured, id) => ({ id, captured }));
55
- // Dependencies own what they build before the entry module's instances refer to it.
56
- const ordered = [
57
- ...graph.bindings.filter((binding) => binding.file !== entry),
58
- ...graph.bindings.filter((binding) => binding.file === entry),
59
- ];
60
- for (const binding of ordered) {
61
- const declaration = this.binding(binding.file, binding.name);
62
- const root: Root = {
63
- file: binding.file,
64
- name: binding.name,
65
- declaration,
66
- exported: Boolean(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export),
67
- };
68
- this.roots.push(root);
69
- this.rootValues.set(root, binding.value);
70
- this.own(binding.value, { root, steps: [] }, checker.getTypeAtLocation(declaration.name));
71
- }
72
- }
73
-
74
- node(value: CapturedValue): ModelNode | undefined {
75
- return value.kind === "node" ? this.nodes[value.id] : undefined;
76
- }
77
-
78
- private binding(file: string, name: string): ts.VariableDeclaration {
79
- const source = this.program.getSourceFile(file);
80
- for (const statement of source?.statements ?? [])
81
- if (ts.isVariableStatement(statement))
82
- for (const declaration of statement.declarationList.declarations)
83
- if (ts.isIdentifier(declaration.name) && declaration.name.text === name)
84
- return declaration;
85
- throw new Error(`${file}: no module binding ${name}.`);
86
- }
87
-
88
- /** The class a captured instance belongs to. */
89
- classOf(node: ModelNode): ts.ClassDeclaration {
90
- const captured = node.captured;
91
- if (captured.kind !== "instance") throw new Error("Not a class instance.");
92
- const source = this.program.getSourceFile(captured.file);
93
- const found = source?.statements.find(
94
- (statement): statement is ts.ClassDeclaration =>
95
- ts.isClassDeclaration(statement) && statement.name?.text === captured.name,
96
- );
97
- if (!found) throw new Error(`${captured.file}: no class ${captured.name}.`);
98
- return found;
99
- }
100
-
101
- /** A class field's declaration: a property, or a constructor parameter property. */
102
- member(
103
- declaration: ts.ClassDeclaration,
104
- name: string,
105
- ): ts.PropertyDeclaration | ts.ParameterDeclaration | undefined {
106
- for (const member of declaration.members) {
107
- if (ts.isPropertyDeclaration(member) && member.name.getText() === name) return member;
108
- if (ts.isConstructorDeclaration(member))
109
- for (const parameter of member.parameters)
110
- if (
111
- ts.isParameterPropertyDeclaration(parameter, member) &&
112
- parameter.name.getText() === name
113
- )
114
- return parameter;
115
- }
116
- return undefined;
117
- }
118
-
119
- /** The declared type of a field of `type` holding `value`, choosing the union member it matches. */
120
- fieldType(type: ts.Type, name: string, holder: CapturedNode): ts.Type | undefined {
121
- const variant = this.variant(type, holder);
122
- const property = variant.getProperty(name) ?? this.checker.getPropertyOfType(variant, name);
123
- return property && this.checker.getTypeOfSymbol(property);
124
- }
125
-
126
- /** The member of a union of object types that a captured object is, by its literal fields. */
127
- variant(type: ts.Type, value: CapturedNode): ts.Type {
128
- const candidates = (type.isUnion() ? type.types : [type]).filter(
129
- (member) => !(member.flags & ts.TypeFlags.Undefined),
130
- );
131
- if (candidates.length === 1 || value.kind !== "object") return candidates[0] ?? type;
132
- return (
133
- candidates.find((candidate) =>
134
- Object.entries(value.fields).every(([name, field]) => {
135
- const property = candidate.getProperty(name);
136
- if (!property) return false;
137
- const fieldType = this.checker.getTypeOfSymbol(property);
138
- return (
139
- !fieldType.isLiteral() || field.kind !== "string" || fieldType.value === field.value
140
- );
141
- }),
142
- ) ?? candidates[0]!
143
- );
144
- }
145
-
146
- elementType(type: ts.Type): ts.Type | undefined {
147
- const target = this.checker.getNonNullableType(type);
148
- return (
149
- this.checker.getIndexTypeOfType(target, ts.IndexKind.Number) ??
150
- (this.checker.isTupleType(target)
151
- ? this.checker.getTypeArguments(target as ts.TypeReference)[0]
152
- : undefined)
153
- );
154
- }
155
-
156
- private own(value: CapturedValue, owner: Owner, type: ts.Type | undefined): void {
157
- const node = this.node(value);
158
- if (!node || node.owner) return;
159
- node.owner = owner;
160
- node.type = type;
161
- const captured = node.captured;
162
- if (captured.kind === "array") {
163
- const element = type && this.elementType(type);
164
- captured.items.forEach((item, index) =>
165
- this.own(
166
- item,
167
- { root: owner.root, steps: [...owner.steps, { kind: "index", index }] },
168
- element,
169
- ),
170
- );
171
- } else if (captured.kind === "object") {
172
- for (const [name, field] of Object.entries(captured.fields))
173
- this.own(
174
- field,
175
- { root: owner.root, steps: [...owner.steps, { kind: "field", name }] },
176
- type && this.fieldType(type, name, captured),
177
- );
178
- } else if (captured.kind === "instance") {
179
- const declaration = this.classOf(node);
180
- for (const [name, field] of Object.entries(captured.fields)) {
181
- const member = this.member(declaration, name);
182
- this.own(
183
- field,
184
- { root: owner.root, steps: [...owner.steps, { kind: "field", name }] },
185
- member && this.checker.getTypeAtLocation(member.name),
186
- );
187
- }
188
- }
189
- }
79
+ readonly nodes: ModelNode[];
80
+ readonly roots: Root[] = [];
81
+
82
+ constructor(
83
+ readonly graph: CapturedGraph,
84
+ readonly checker: ts.TypeChecker,
85
+ readonly program: ts.Program,
86
+ entry: string,
87
+ ) {
88
+ this.nodes = graph.nodes.map((captured, id) => ({ id, captured }));
89
+ // Dependencies own what they build before the entry module's instances refer to it.
90
+ const ordered = [
91
+ ...graph.bindings.filter((binding) => binding.file !== entry),
92
+ ...graph.bindings.filter((binding) => binding.file === entry),
93
+ ];
94
+ for (const binding of ordered) {
95
+ const declaration = this.binding(binding.file, binding.name);
96
+ const root: Root = {
97
+ file: binding.file,
98
+ name: binding.name,
99
+ declaration,
100
+ exported: Boolean(
101
+ ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export,
102
+ ),
103
+ value: binding.value,
104
+ };
105
+ this.roots.push(root);
106
+ this.own(
107
+ binding.value,
108
+ { root, steps: [] },
109
+ checker.getTypeAtLocation(declaration.name),
110
+ );
111
+ }
112
+ }
113
+
114
+ /** The object a captured value refers to; undefined for a primitive. */
115
+ node(value: Extract<CapturedValue, { kind: "node" }>): ModelNode;
116
+ node(value: CapturedValue): ModelNode | undefined;
117
+ node(value: CapturedValue): ModelNode | undefined {
118
+ if (value.kind !== "node") {
119
+ return undefined;
120
+ }
121
+ const node = this.nodes[value.id];
122
+ if (!node) {
123
+ throw new Error(`The setup graph has no object ${value.id}.`);
124
+ }
125
+ return node;
126
+ }
127
+
128
+ private binding(file: string, name: string): ts.VariableDeclaration {
129
+ const source = this.program.getSourceFile(file);
130
+ for (const statement of source?.statements ?? []) {
131
+ if (ts.isVariableStatement(statement)) {
132
+ for (const declaration of statement.declarationList.declarations) {
133
+ if (
134
+ ts.isIdentifier(declaration.name) &&
135
+ declaration.name.text === name
136
+ ) {
137
+ return declaration;
138
+ }
139
+ }
140
+ }
141
+ }
142
+ throw new Error(`${file}: no module binding ${name}.`);
143
+ }
144
+
145
+ /** The node at a path from a root, following captured values. */
146
+ at(root: Root, steps: readonly Step[]): ModelNode | undefined {
147
+ const value = steps.reduce<CapturedValue | undefined>((current, step) => {
148
+ const node = current && this.node(current);
149
+ return node && child(node.captured, step);
150
+ }, root.value);
151
+ return value && this.node(value);
152
+ }
153
+
154
+ /** The class a captured instance belongs to. */
155
+ classOf(node: ModelNode): ts.ClassDeclaration {
156
+ const captured = node.captured;
157
+ if (captured.kind !== "instance") {
158
+ throw new Error("Not a class instance.");
159
+ }
160
+ const source = this.program.getSourceFile(captured.file);
161
+ const found = source?.statements.find(
162
+ (statement): statement is ts.ClassDeclaration =>
163
+ ts.isClassDeclaration(statement) &&
164
+ statement.name?.text === captured.name,
165
+ );
166
+ if (!found) {
167
+ throw new Error(`${captured.file}: no class ${captured.name}.`);
168
+ }
169
+ return found;
170
+ }
171
+
172
+ /** A class field's declaration: a property, or a constructor parameter property. */
173
+ member(
174
+ declaration: ts.ClassDeclaration,
175
+ name: string,
176
+ ): ClassField | undefined {
177
+ return this.fields(declaration).find(
178
+ (field) => field.name.getText() === name,
179
+ );
180
+ }
181
+
182
+ /** A class's fields in declaration order: properties and constructor parameter properties. */
183
+ fields(declaration: ts.ClassDeclaration): ClassField[] {
184
+ return declaration.members.flatMap((member): ClassField[] => {
185
+ if (ts.isPropertyDeclaration(member)) {
186
+ return [member];
187
+ }
188
+ if (ts.isConstructorDeclaration(member)) {
189
+ return member.parameters.filter(
190
+ (parameter): parameter is ts.ParameterPropertyDeclaration =>
191
+ ts.isParameterPropertyDeclaration(parameter, member),
192
+ );
193
+ }
194
+ return [];
195
+ });
196
+ }
197
+
198
+ /** The declared type of a field of a captured instance or record. */
199
+ capturedFieldType(holder: ModelNode, name: string): ts.Type | undefined {
200
+ if (holder.captured.kind === "instance") {
201
+ const member = this.member(this.classOf(holder), name);
202
+ return member && this.checker.getTypeAtLocation(member.name);
203
+ }
204
+ return holder.type && this.fieldType(holder.type, name, holder.captured);
205
+ }
206
+
207
+ /** The declared type of a field of `type` holding `value`, choosing the union member it matches. */
208
+ fieldType(
209
+ type: ts.Type,
210
+ name: string,
211
+ holder: CapturedNode,
212
+ ): ts.Type | undefined {
213
+ const variant = this.variant(type, holder);
214
+ const property =
215
+ variant.getProperty(name) ??
216
+ this.checker.getPropertyOfType(variant, name);
217
+ return property && this.checker.getTypeOfSymbol(property);
218
+ }
219
+
220
+ /** The member of a union of object types that a captured object is, by its literal fields. */
221
+ variant(type: ts.Type, value: CapturedNode): ts.Type {
222
+ const candidates = (type.isUnion() ? type.types : [type]).filter(
223
+ (member) => !(member.flags & ts.TypeFlags.Undefined),
224
+ );
225
+ const [first] = candidates;
226
+ if (!first) {
227
+ return type;
228
+ }
229
+ if (candidates.length === 1 || value.kind !== "object") {
230
+ return first;
231
+ }
232
+ return (
233
+ candidates.find((candidate) =>
234
+ Object.entries(value.fields).every(([name, field]) => {
235
+ const property = candidate.getProperty(name);
236
+ if (!property) {
237
+ return false;
238
+ }
239
+ const fieldType = this.checker.getTypeOfSymbol(property);
240
+ return (
241
+ !fieldType.isLiteral() ||
242
+ field.kind !== "string" ||
243
+ fieldType.value === field.value
244
+ );
245
+ }),
246
+ ) ?? first
247
+ );
248
+ }
249
+
250
+ elementType(type: ts.Type): ts.Type | undefined {
251
+ const target = this.checker.getNonNullableType(type);
252
+ return (
253
+ this.checker.getIndexTypeOfType(target, ts.IndexKind.Number) ??
254
+ (this.checker.isTupleType(target)
255
+ ? this.checker.getTypeArguments(target as ts.TypeReference)[0]
256
+ : undefined)
257
+ );
258
+ }
259
+
260
+ private own(
261
+ value: CapturedValue,
262
+ owner: Owner,
263
+ type: ts.Type | undefined,
264
+ ): void {
265
+ const node = this.node(value);
266
+ if (!node || node.owner) {
267
+ return;
268
+ }
269
+ node.owner = owner;
270
+ node.type = type;
271
+ const element =
272
+ node.captured.kind === "array" && type
273
+ ? this.elementType(type)
274
+ : undefined;
275
+ for (const [step, held] of children(node.captured)) {
276
+ this.own(
277
+ held,
278
+ { root: owner.root, steps: [...owner.steps, step] },
279
+ step.kind === "index"
280
+ ? element
281
+ : this.capturedFieldType(node, step.name),
282
+ );
283
+ }
284
+ }
190
285
  }