@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.
- package/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
calendarLiteral,
|
|
4
|
+
defaultValue,
|
|
5
|
+
durationLiteral,
|
|
6
|
+
parseInteger,
|
|
7
|
+
type Constant,
|
|
8
|
+
type Expression,
|
|
9
|
+
} from "@tsplc/compiler/ir";
|
|
10
|
+
import type { ExpressionLowering } from "./expressions.ts";
|
|
11
|
+
import { numericLiteral } from "./types.ts";
|
|
12
|
+
|
|
13
|
+
type Conversion = Extract<Expression, { kind: "convert" }>["policy"];
|
|
14
|
+
type Arithmetic = Pick<
|
|
15
|
+
Extract<Expression, { kind: "binary" }>,
|
|
16
|
+
"operator" | "policy"
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
// TIME constructor scales in nanoseconds, and calendar constructor result types.
|
|
20
|
+
const durationScales = new Map<string, bigint>([
|
|
21
|
+
["ms", 1_000_000n],
|
|
22
|
+
["seconds", 1_000_000_000n],
|
|
23
|
+
["minutes", 60_000_000_000n],
|
|
24
|
+
]);
|
|
25
|
+
const calendarTypes = new Map<string, string>([
|
|
26
|
+
["date", "DATE"],
|
|
27
|
+
["timeOfDay", "TIME_OF_DAY"],
|
|
28
|
+
["dateAndTime", "DATE_AND_TIME"],
|
|
29
|
+
]);
|
|
30
|
+
const conversions = new Map<string, Conversion>([
|
|
31
|
+
["exact", "exact"],
|
|
32
|
+
["wrap", "wrap"],
|
|
33
|
+
["saturate", "saturate"],
|
|
34
|
+
]);
|
|
35
|
+
const arithmetic = new Map<string, Arithmetic>([
|
|
36
|
+
["div", { operator: "divide" }],
|
|
37
|
+
["wrapAdd", { operator: "add", policy: "wrap" }],
|
|
38
|
+
["wrapSubtract", { operator: "subtract", policy: "wrap" }],
|
|
39
|
+
["wrapMultiply", { operator: "multiply", policy: "wrap" }],
|
|
40
|
+
["saturatingAdd", { operator: "add", policy: "saturate" }],
|
|
41
|
+
["saturatingSubtract", { operator: "subtract", policy: "saturate" }],
|
|
42
|
+
["saturatingMultiply", { operator: "multiply", policy: "saturate" }],
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
/** A call to an SDK intrinsic such as `ms`, `wrap<T>` or `defaultValue<T>`; undefined for any other call. */
|
|
46
|
+
export function intrinsicExpression(
|
|
47
|
+
lowering: ExpressionLowering,
|
|
48
|
+
node: ts.CallExpression,
|
|
49
|
+
expected?: string,
|
|
50
|
+
): Expression | undefined {
|
|
51
|
+
const name = lowering.sdkCall(node);
|
|
52
|
+
if (name === undefined) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const scale = durationScales.get(name);
|
|
56
|
+
if (scale !== undefined) {
|
|
57
|
+
return timeConstant(lowering, node, "TIME", (argument) =>
|
|
58
|
+
durationConstant(lowering, argument, scale),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const calendar = calendarTypes.get(name);
|
|
62
|
+
if (calendar !== undefined) {
|
|
63
|
+
return timeConstant(lowering, node, calendar, (argument) =>
|
|
64
|
+
calendarConstant(lowering, argument, calendar),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const conversion = conversions.get(name);
|
|
68
|
+
if (conversion !== undefined) {
|
|
69
|
+
return converted(lowering, node, conversion);
|
|
70
|
+
}
|
|
71
|
+
const operation = arithmetic.get(name);
|
|
72
|
+
if (operation !== undefined) {
|
|
73
|
+
return arithmeticExpression(lowering, node, operation);
|
|
74
|
+
}
|
|
75
|
+
switch (name) {
|
|
76
|
+
case "errorCode":
|
|
77
|
+
return errorCode(lowering, node);
|
|
78
|
+
case "now":
|
|
79
|
+
return now(lowering, node);
|
|
80
|
+
case "timeToMilliseconds":
|
|
81
|
+
return timeToMilliseconds(lowering, node);
|
|
82
|
+
case "defaultValue":
|
|
83
|
+
return defaultValueExpression(lowering, node);
|
|
84
|
+
case "copy":
|
|
85
|
+
return copyExpression(lowering, node, expected);
|
|
86
|
+
default:
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function errorCode(
|
|
92
|
+
lowering: ExpressionLowering,
|
|
93
|
+
node: ts.CallExpression,
|
|
94
|
+
): Expression {
|
|
95
|
+
const [reason, ...rest] = node.arguments;
|
|
96
|
+
if (!reason || rest.length) {
|
|
97
|
+
lowering.fail(node, "PLC1012", "errorCode requires one rejection reason.");
|
|
98
|
+
}
|
|
99
|
+
return lowering.expression(reason, "UDINT");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A TIME or calendar value from its one compile-time argument. */
|
|
103
|
+
function timeConstant(
|
|
104
|
+
lowering: ExpressionLowering,
|
|
105
|
+
node: ts.CallExpression,
|
|
106
|
+
type: string,
|
|
107
|
+
constant: (argument: ts.Expression) => Constant | undefined,
|
|
108
|
+
): Expression {
|
|
109
|
+
const [argument, ...rest] = node.arguments;
|
|
110
|
+
if (!argument || rest.length || node.typeArguments?.length) {
|
|
111
|
+
lowering.fail(
|
|
112
|
+
node,
|
|
113
|
+
"PLC2001",
|
|
114
|
+
"Time constructors require one compile-time literal argument.",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const value = constant(argument);
|
|
118
|
+
if (!value) {
|
|
119
|
+
lowering.fail(
|
|
120
|
+
argument,
|
|
121
|
+
"PLC2002",
|
|
122
|
+
"Invalid time literal: require an exact supported domain value and millisecond TIME precision.",
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return lowering.checked({
|
|
126
|
+
span: lowering.positions.node(node),
|
|
127
|
+
type,
|
|
128
|
+
kind: "constant",
|
|
129
|
+
value,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function durationConstant(
|
|
134
|
+
lowering: ExpressionLowering,
|
|
135
|
+
argument: ts.Expression,
|
|
136
|
+
scale: bigint,
|
|
137
|
+
): Constant | undefined {
|
|
138
|
+
const text = durationText(lowering, argument);
|
|
139
|
+
if (text === undefined) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
const decimal = /^-?0[xob]/i.test(text) ? String(parseInteger(text)) : text;
|
|
143
|
+
return durationLiteral(decimal.replace(/n$/, ""), scale);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The literal as written, so its decimal spelling survives, or a folded integer constant. */
|
|
147
|
+
function durationText(
|
|
148
|
+
lowering: ExpressionLowering,
|
|
149
|
+
argument: ts.Expression,
|
|
150
|
+
): string | undefined {
|
|
151
|
+
const literal = numericLiteral(argument);
|
|
152
|
+
if (literal !== undefined) {
|
|
153
|
+
return literal;
|
|
154
|
+
}
|
|
155
|
+
const folded = lowering.constant(lowering.expression(argument));
|
|
156
|
+
return folded?.kind === "integer" ? folded.value : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function calendarConstant(
|
|
160
|
+
lowering: ExpressionLowering,
|
|
161
|
+
argument: ts.Expression,
|
|
162
|
+
type: string,
|
|
163
|
+
): Constant | undefined {
|
|
164
|
+
const layout = lowering.resolver.types.get(type);
|
|
165
|
+
return layout?.kind === "calendar" &&
|
|
166
|
+
(ts.isStringLiteral(argument) ||
|
|
167
|
+
ts.isNoSubstitutionTemplateLiteral(argument))
|
|
168
|
+
? calendarLiteral(layout.domain, argument.text)
|
|
169
|
+
: undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function now(
|
|
173
|
+
lowering: ExpressionLowering,
|
|
174
|
+
node: ts.CallExpression,
|
|
175
|
+
): Expression {
|
|
176
|
+
if (node.arguments.length || node.typeArguments?.length) {
|
|
177
|
+
lowering.fail(node, "PLC2001", "now() takes no arguments.");
|
|
178
|
+
}
|
|
179
|
+
const span = lowering.positions.node(node);
|
|
180
|
+
return lowering.checked({
|
|
181
|
+
span,
|
|
182
|
+
type: "DINT",
|
|
183
|
+
kind: "unary",
|
|
184
|
+
operator: "clock",
|
|
185
|
+
operand: {
|
|
186
|
+
span,
|
|
187
|
+
type: "DINT",
|
|
188
|
+
kind: "constant",
|
|
189
|
+
value: { kind: "integer", value: "0" },
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function timeToMilliseconds(
|
|
195
|
+
lowering: ExpressionLowering,
|
|
196
|
+
node: ts.CallExpression,
|
|
197
|
+
): Expression {
|
|
198
|
+
const [time, ...rest] = node.arguments;
|
|
199
|
+
if (!time || rest.length || node.typeArguments?.length) {
|
|
200
|
+
lowering.fail(
|
|
201
|
+
node,
|
|
202
|
+
"PLC2001",
|
|
203
|
+
"timeToMilliseconds requires one TIME argument and no type arguments.",
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return lowering.checked({
|
|
207
|
+
span: lowering.positions.node(node),
|
|
208
|
+
type: "UDINT",
|
|
209
|
+
kind: "unary",
|
|
210
|
+
operator: "timeToMilliseconds",
|
|
211
|
+
operand: lowering.expression(time),
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function defaultValueExpression(
|
|
216
|
+
lowering: ExpressionLowering,
|
|
217
|
+
node: ts.CallExpression,
|
|
218
|
+
): Expression {
|
|
219
|
+
const [layout, ...rest] = node.typeArguments ?? [];
|
|
220
|
+
if (node.arguments.length || !layout || rest.length) {
|
|
221
|
+
lowering.fail(
|
|
222
|
+
node,
|
|
223
|
+
"PLC2001",
|
|
224
|
+
"defaultValue requires exactly one concrete type and no value arguments.",
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
const type = lowering.type(layout);
|
|
228
|
+
if (!lowering.resolver.isValue(type)) {
|
|
229
|
+
lowering.fail(
|
|
230
|
+
node,
|
|
231
|
+
"PLC2001",
|
|
232
|
+
"defaultValue requires a fixed value layout, not an instance or borrow schema.",
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (type.io) {
|
|
236
|
+
lowering.fail(node, "PLC4002", "Default values cannot create logical I/O.");
|
|
237
|
+
}
|
|
238
|
+
return lowering.checked({
|
|
239
|
+
span: lowering.positions.node(node),
|
|
240
|
+
type: type.type,
|
|
241
|
+
kind: "constant",
|
|
242
|
+
value: defaultValue(type.type, lowering.resolver.types),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function copyExpression(
|
|
247
|
+
lowering: ExpressionLowering,
|
|
248
|
+
node: ts.CallExpression,
|
|
249
|
+
expected: string | undefined,
|
|
250
|
+
): Expression {
|
|
251
|
+
const [value, ...rest] = node.arguments;
|
|
252
|
+
if (
|
|
253
|
+
!value ||
|
|
254
|
+
rest.length ||
|
|
255
|
+
(node.typeArguments && node.typeArguments.length !== 1)
|
|
256
|
+
) {
|
|
257
|
+
lowering.fail(node, "PLC2001", "copy requires one fixed aggregate value.");
|
|
258
|
+
}
|
|
259
|
+
const layout = node.typeArguments?.[0];
|
|
260
|
+
const operand = lowering.expression(
|
|
261
|
+
value,
|
|
262
|
+
layout ? lowering.type(layout).type : expected,
|
|
263
|
+
);
|
|
264
|
+
return lowering.checked({
|
|
265
|
+
span: lowering.positions.node(node),
|
|
266
|
+
type: operand.type,
|
|
267
|
+
kind: "copy",
|
|
268
|
+
operand,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The explicit PLC type argument a numeric conversion or arithmetic operation works in. */
|
|
273
|
+
function operationType(
|
|
274
|
+
lowering: ExpressionLowering,
|
|
275
|
+
node: ts.CallExpression,
|
|
276
|
+
): string {
|
|
277
|
+
const [argument, ...rest] = node.typeArguments ?? [];
|
|
278
|
+
if (!argument || rest.length) {
|
|
279
|
+
lowering.fail(
|
|
280
|
+
node,
|
|
281
|
+
"PLC2001",
|
|
282
|
+
"Numeric SDK operations require one explicit PLC type argument.",
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
const target = lowering.type(argument);
|
|
286
|
+
if (target.io) {
|
|
287
|
+
lowering.fail(
|
|
288
|
+
argument,
|
|
289
|
+
"PLC2001",
|
|
290
|
+
"I/O wrappers cannot select an operation type.",
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
return target.type;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function converted(
|
|
297
|
+
lowering: ExpressionLowering,
|
|
298
|
+
node: ts.CallExpression,
|
|
299
|
+
policy: Conversion,
|
|
300
|
+
): Expression {
|
|
301
|
+
const type = operationType(lowering, node);
|
|
302
|
+
const [operand, ...rest] = node.arguments;
|
|
303
|
+
if (!operand || rest.length) {
|
|
304
|
+
lowering.fail(node, "PLC2001", "Conversion needs exactly one operand.");
|
|
305
|
+
}
|
|
306
|
+
return lowering.checked({
|
|
307
|
+
span: lowering.positions.node(node),
|
|
308
|
+
type,
|
|
309
|
+
kind: "convert",
|
|
310
|
+
policy,
|
|
311
|
+
operand: lowering.expression(operand),
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function arithmeticExpression(
|
|
316
|
+
lowering: ExpressionLowering,
|
|
317
|
+
node: ts.CallExpression,
|
|
318
|
+
operation: Arithmetic,
|
|
319
|
+
): Expression {
|
|
320
|
+
const type = operationType(lowering, node);
|
|
321
|
+
const [left, right, ...rest] = node.arguments;
|
|
322
|
+
if (!left || !right || rest.length) {
|
|
323
|
+
lowering.fail(
|
|
324
|
+
node,
|
|
325
|
+
"PLC2001",
|
|
326
|
+
"Arithmetic SDK operation needs two operands.",
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
return lowering.checked({
|
|
330
|
+
span: lowering.positions.node(node),
|
|
331
|
+
type,
|
|
332
|
+
kind: "binary",
|
|
333
|
+
...operation,
|
|
334
|
+
left: lowering.expression(left, type),
|
|
335
|
+
right: lowering.expression(right, type),
|
|
336
|
+
});
|
|
337
|
+
}
|
|
@@ -1,63 +1,93 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
import { dirname, relative } from "node:path";
|
|
3
|
+
import { symbolAt } from "./sdk.ts";
|
|
4
|
+
|
|
5
|
+
/** The quoted specifier an import generated into `importer` uses to reach `target`. */
|
|
6
|
+
export const importSpecifier = (importer: string, target: string): string => {
|
|
7
|
+
const path = relative(dirname(importer), target).replaceAll("\\", "/");
|
|
8
|
+
return JSON.stringify(path.startsWith(".") ? path : `./${path}`);
|
|
9
|
+
};
|
|
2
10
|
|
|
3
11
|
/** Runtime imports define execution; type-only imports provide layouts without allocating state. */
|
|
4
|
-
export const runtimeSources = (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
12
|
+
export const runtimeSources = (
|
|
13
|
+
program: ts.Program,
|
|
14
|
+
): readonly ts.SourceFile[] => {
|
|
15
|
+
const checker = program.getTypeChecker();
|
|
16
|
+
const visited = new Set<ts.SourceFile>();
|
|
17
|
+
// A block array allocates through its type, so its block's module runs even when type-imported.
|
|
18
|
+
const visitBlockArrays = (node: ts.Node): void => {
|
|
19
|
+
if (
|
|
20
|
+
ts.isTypeReferenceNode(node) &&
|
|
21
|
+
node.typeName.getText() === "FixedArray" &&
|
|
22
|
+
node.typeArguments?.[0] &&
|
|
23
|
+
ts.isTypeReferenceNode(node.typeArguments[0])
|
|
24
|
+
) {
|
|
25
|
+
const block = symbolAt(
|
|
26
|
+
checker,
|
|
27
|
+
node.typeArguments[0].typeName,
|
|
28
|
+
)?.declarations?.find(ts.isClassDeclaration);
|
|
29
|
+
if (block) {
|
|
30
|
+
visit(block.getSourceFile());
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
ts.forEachChild(node, visitBlockArrays);
|
|
34
|
+
};
|
|
35
|
+
const visit = (source: ts.SourceFile): void => {
|
|
36
|
+
if (visited.has(source)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
visited.add(source);
|
|
40
|
+
if (source.isDeclarationFile) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
visitBlockArrays(source);
|
|
44
|
+
for (const statement of source.statements) {
|
|
45
|
+
if (ts.isImportDeclaration(statement)) {
|
|
46
|
+
if (statement.importClause?.isTypeOnly) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const bindings = statement.importClause?.namedBindings;
|
|
50
|
+
if (
|
|
51
|
+
!statement.importClause?.name &&
|
|
52
|
+
bindings &&
|
|
53
|
+
ts.isNamedImports(bindings) &&
|
|
54
|
+
bindings.elements.every((value) => value.isTypeOnly)
|
|
55
|
+
) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const target = checker
|
|
59
|
+
.getSymbolAtLocation(statement.moduleSpecifier)
|
|
60
|
+
?.declarations?.find(ts.isSourceFile);
|
|
61
|
+
if (target) {
|
|
62
|
+
visit(target);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
ts.isExportDeclaration(statement) &&
|
|
67
|
+
statement.moduleSpecifier &&
|
|
68
|
+
!statement.isTypeOnly
|
|
69
|
+
) {
|
|
70
|
+
if (
|
|
71
|
+
statement.exportClause &&
|
|
72
|
+
ts.isNamedExports(statement.exportClause) &&
|
|
73
|
+
statement.exportClause.elements.every((value) => value.isTypeOnly)
|
|
74
|
+
) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const target = checker
|
|
78
|
+
.getSymbolAtLocation(statement.moduleSpecifier)
|
|
79
|
+
?.declarations?.find(ts.isSourceFile);
|
|
80
|
+
if (target) {
|
|
81
|
+
visit(target);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
for (const file of program.getRootFileNames()) {
|
|
87
|
+
const source = program.getSourceFile(file);
|
|
88
|
+
if (source) {
|
|
89
|
+
visit(source);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return program.getSourceFiles().filter((source) => visited.has(source));
|
|
63
93
|
};
|