@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.
- package/README.md +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -1,71 +1,74 @@
|
|
|
1
1
|
import { type Diagnostic, error } from "@tsplc/compiler/ir";
|
|
2
|
+
import { type Environment, plainValue } from "@tsplc/config";
|
|
2
3
|
import ts from "typescript";
|
|
3
|
-
import {
|
|
4
|
+
import { plcSources } from "./module-graph";
|
|
4
5
|
import { type Edit, edited, type Mapped, type Positions } from "./positions";
|
|
5
|
-
import { namesSdk
|
|
6
|
-
|
|
6
|
+
import { namesSdk } from "./sdk";
|
|
7
|
+
|
|
8
|
+
/** Whether `node` is the initializer of a module-level const, where configuration is read once. */
|
|
9
|
+
const inModuleConst = (node: ts.Node): boolean => {
|
|
10
|
+
const owner = ts.findAncestor(
|
|
11
|
+
node.parent,
|
|
12
|
+
(item) => ts.isVariableDeclaration(item) || ts.isFunctionLike(item),
|
|
13
|
+
);
|
|
14
|
+
return (
|
|
15
|
+
owner !== undefined &&
|
|
16
|
+
ts.isVariableDeclaration(owner) &&
|
|
17
|
+
ts.isVariableDeclarationList(owner.parent) &&
|
|
18
|
+
Boolean(owner.parent.flags & ts.NodeFlags.Const) &&
|
|
19
|
+
ts.isVariableStatement(owner.parent.parent) &&
|
|
20
|
+
ts.isSourceFile(owner.parent.parent.parent)
|
|
21
|
+
);
|
|
22
|
+
};
|
|
7
23
|
|
|
8
24
|
export const lowerEnvironment = (
|
|
9
25
|
program: ts.Program,
|
|
10
26
|
sdk: string,
|
|
11
27
|
positions: Positions,
|
|
12
|
-
environment:
|
|
28
|
+
environment: Environment,
|
|
13
29
|
) => {
|
|
14
30
|
const checker = program.getTypeChecker();
|
|
15
31
|
const sources = new Map<string, Mapped>();
|
|
16
32
|
const diagnostics: Diagnostic[] = [];
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
33
|
+
/** Each environment value an `env()` call reads, by name. */
|
|
34
|
+
const read: Record<string, string> = {};
|
|
35
|
+
for (const source of plcSources(program)) {
|
|
21
36
|
const edits: Edit[] = [];
|
|
22
37
|
const visit = (node: ts.Node): void => {
|
|
23
38
|
if (ts.isCallExpression(node)) {
|
|
24
39
|
if (namesSdk(checker, sdk, node.expression, "env")) {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
owner &&
|
|
29
|
-
!ts.isVariableDeclaration(owner) &&
|
|
30
|
-
!ts.isFunctionLike(owner)
|
|
31
|
-
) {
|
|
32
|
-
owner = owner.parent;
|
|
33
|
-
}
|
|
34
|
-
const moduleConst =
|
|
35
|
-
owner &&
|
|
36
|
-
ts.isVariableDeclaration(owner) &&
|
|
37
|
-
ts.isVariableDeclarationList(owner.parent) &&
|
|
38
|
-
Boolean(owner.parent.flags & ts.NodeFlags.Const) &&
|
|
39
|
-
ts.isVariableStatement(owner.parent.parent) &&
|
|
40
|
-
ts.isSourceFile(owner.parent.parent.parent);
|
|
41
|
-
const value =
|
|
42
|
-
name && ts.isStringLiteral(name)
|
|
43
|
-
? environment[name.text]
|
|
44
|
-
: undefined;
|
|
45
|
-
let message: string | undefined;
|
|
40
|
+
const reject = (message: string) =>
|
|
41
|
+
diagnostics.push(error("PLC1011", message, positions.node(node)));
|
|
42
|
+
const [name, ...rest] = node.arguments;
|
|
46
43
|
if (
|
|
47
|
-
!
|
|
48
|
-
node.arguments.length !== 1 ||
|
|
44
|
+
!inModuleConst(node) ||
|
|
49
45
|
!name ||
|
|
46
|
+
rest.length ||
|
|
50
47
|
!ts.isStringLiteral(name) ||
|
|
51
48
|
node.typeArguments?.length !== 1
|
|
52
49
|
) {
|
|
53
|
-
|
|
54
|
-
"env<STRING<N>> requires a literal environment name in module-level const configuration."
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
} else if (value.includes("\0")) {
|
|
58
|
-
message = `Environment value ${name.text} contains an unsupported NUL byte.`;
|
|
50
|
+
reject(
|
|
51
|
+
"env<STRING<N>> requires a literal environment name in module-level const configuration.",
|
|
52
|
+
);
|
|
53
|
+
return;
|
|
59
54
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
start: node.getStart(source),
|
|
65
|
-
end: node.end,
|
|
66
|
-
text: JSON.stringify(value),
|
|
67
|
-
});
|
|
55
|
+
const value = environment[name.text];
|
|
56
|
+
if (!value) {
|
|
57
|
+
reject(`Required environment value ${name.text} is missing.`);
|
|
58
|
+
return;
|
|
68
59
|
}
|
|
60
|
+
if (!plainValue.safeParse(value).success) {
|
|
61
|
+
reject(
|
|
62
|
+
`Environment value ${name.text} contains an unsupported NUL byte.`,
|
|
63
|
+
);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
read[name.text] = value;
|
|
67
|
+
edits.push({
|
|
68
|
+
start: node.getStart(source),
|
|
69
|
+
end: node.end,
|
|
70
|
+
text: JSON.stringify(value),
|
|
71
|
+
});
|
|
69
72
|
return;
|
|
70
73
|
}
|
|
71
74
|
}
|
|
@@ -76,112 +79,5 @@ export const lowerEnvironment = (
|
|
|
76
79
|
sources.set(source.fileName, edited(source, edits));
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
|
-
return { sources, diagnostics };
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/** URL expressions are compile-time values, never reads from mutable PLC state. */
|
|
83
|
-
export const staticString = (
|
|
84
|
-
checker: ts.TypeChecker,
|
|
85
|
-
root: ts.Expression,
|
|
86
|
-
): string | undefined => {
|
|
87
|
-
const active = new Set<ts.Node>();
|
|
88
|
-
const valueNode = (node: ts.Node): ts.Node | undefined => {
|
|
89
|
-
if (ts.isParenthesizedExpression(node)) {
|
|
90
|
-
return node.expression;
|
|
91
|
-
}
|
|
92
|
-
if (ts.isIdentifier(node)) {
|
|
93
|
-
const declaration = symbolAt(checker, node)?.declarations?.find(
|
|
94
|
-
ts.isVariableDeclaration,
|
|
95
|
-
);
|
|
96
|
-
if (
|
|
97
|
-
declaration &&
|
|
98
|
-
ts.isVariableDeclarationList(declaration.parent) &&
|
|
99
|
-
declaration.parent.flags & ts.NodeFlags.Const
|
|
100
|
-
) {
|
|
101
|
-
return declaration.initializer;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
if (ts.isPropertyAccessExpression(node)) {
|
|
105
|
-
const field = symbolAt(checker, node.name);
|
|
106
|
-
if (
|
|
107
|
-
!field?.declarations?.some(
|
|
108
|
-
(declaration) =>
|
|
109
|
-
ts.canHaveModifiers(declaration) &&
|
|
110
|
-
ts
|
|
111
|
-
.getModifiers(declaration)
|
|
112
|
-
?.some(
|
|
113
|
-
(modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword,
|
|
114
|
-
),
|
|
115
|
-
)
|
|
116
|
-
) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
const object = resolve(node.expression);
|
|
120
|
-
if (object && ts.isObjectLiteralExpression(object)) {
|
|
121
|
-
for (const property of object.properties) {
|
|
122
|
-
if (
|
|
123
|
-
ts.isPropertyAssignment(property) &&
|
|
124
|
-
(ts.isIdentifier(property.name) ||
|
|
125
|
-
ts.isStringLiteral(property.name)) &&
|
|
126
|
-
property.name.text === node.name.text
|
|
127
|
-
) {
|
|
128
|
-
return property.initializer;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
return node;
|
|
135
|
-
};
|
|
136
|
-
const resolve = (node: ts.Node): ts.Node | undefined => {
|
|
137
|
-
if (active.has(node)) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
active.add(node);
|
|
141
|
-
try {
|
|
142
|
-
const next = valueNode(node);
|
|
143
|
-
return next && next !== node ? resolve(next) : next;
|
|
144
|
-
} finally {
|
|
145
|
-
active.delete(node);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
const read = (node: ts.Node): string | undefined => {
|
|
149
|
-
const resolved = resolve(node);
|
|
150
|
-
if (!resolved || active.has(resolved)) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
active.add(resolved);
|
|
154
|
-
try {
|
|
155
|
-
if (
|
|
156
|
-
ts.isStringLiteral(resolved) ||
|
|
157
|
-
ts.isNoSubstitutionTemplateLiteral(resolved)
|
|
158
|
-
) {
|
|
159
|
-
return resolved.text;
|
|
160
|
-
}
|
|
161
|
-
if (ts.isTemplateExpression(resolved)) {
|
|
162
|
-
let value = resolved.head.text;
|
|
163
|
-
for (const part of resolved.templateSpans) {
|
|
164
|
-
const item = read(part.expression);
|
|
165
|
-
if (item === undefined) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
value += item + part.literal.text;
|
|
169
|
-
}
|
|
170
|
-
return value;
|
|
171
|
-
}
|
|
172
|
-
if (
|
|
173
|
-
ts.isBinaryExpression(resolved) &&
|
|
174
|
-
resolved.operatorToken.kind === ts.SyntaxKind.PlusToken
|
|
175
|
-
) {
|
|
176
|
-
const left = read(resolved.left);
|
|
177
|
-
const right = read(resolved.right);
|
|
178
|
-
if (left !== undefined && right !== undefined) {
|
|
179
|
-
return left + right;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
} finally {
|
|
183
|
-
active.delete(resolved);
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
return read(root);
|
|
82
|
+
return { sources, diagnostics, read };
|
|
187
83
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Diagnostic, error } from "@tsplc/compiler/ir";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
-
import {
|
|
3
|
+
import { plcSources } from "./module-graph";
|
|
4
4
|
import {
|
|
5
5
|
copied,
|
|
6
6
|
type Edit,
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
type Part,
|
|
13
13
|
type Positions,
|
|
14
14
|
} from "./positions";
|
|
15
|
+
import { hasModifier } from "./types";
|
|
15
16
|
|
|
16
17
|
/** An arrow body as a function body: a block as written, or its expression returned (or, for void, evaluated). */
|
|
17
18
|
const functionBody = (
|
|
@@ -26,7 +27,51 @@ const functionBody = (
|
|
|
26
27
|
: mapped`{ return ${copied(body)}; }`;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* `: Promise<void>` before the body of each async function and method that returns nothing
|
|
32
|
+
* without saying so, since async lowering requires an explicit `Promise<T>` result.
|
|
33
|
+
*/
|
|
34
|
+
const voidAsyncAnnotations = (
|
|
35
|
+
source: ts.SourceFile,
|
|
36
|
+
checker: ts.TypeChecker,
|
|
37
|
+
): Edit[] =>
|
|
38
|
+
source.statements
|
|
39
|
+
.flatMap((statement): readonly ts.Node[] =>
|
|
40
|
+
ts.isClassDeclaration(statement)
|
|
41
|
+
? statement.members.filter(ts.isMethodDeclaration)
|
|
42
|
+
: [statement],
|
|
43
|
+
)
|
|
44
|
+
.flatMap((routine) => {
|
|
45
|
+
if (
|
|
46
|
+
(!ts.isFunctionDeclaration(routine) &&
|
|
47
|
+
!ts.isMethodDeclaration(routine)) ||
|
|
48
|
+
routine.type ||
|
|
49
|
+
!routine.body ||
|
|
50
|
+
!hasModifier(routine, ts.SyntaxKind.AsyncKeyword)
|
|
51
|
+
) {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
const signature = checker.getSignatureFromDeclaration(routine);
|
|
55
|
+
const result =
|
|
56
|
+
signature &&
|
|
57
|
+
checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
58
|
+
if (!result || !(result.flags & ts.TypeFlags.Void)) {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
const position = routine.body.getStart(source);
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
start: position,
|
|
65
|
+
end: position,
|
|
66
|
+
text: from(routine, ":Promise<void> "),
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Normalizes static arrow bindings into function declarations, so all routine forms use the same
|
|
73
|
+
* PLC analysis, and annotates async routines that return nothing (see `voidAsyncAnnotations`).
|
|
74
|
+
*/
|
|
30
75
|
export const lowerConstFunctions = (
|
|
31
76
|
program: ts.Program,
|
|
32
77
|
positions: Positions,
|
|
@@ -36,41 +81,8 @@ export const lowerConstFunctions = (
|
|
|
36
81
|
const sources = new Map<string, Mapped>();
|
|
37
82
|
const diagnostics: Diagnostic[] = [];
|
|
38
83
|
|
|
39
|
-
for (const source of
|
|
40
|
-
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
const edits: Edit[] = [];
|
|
44
|
-
for (const statement of source.statements) {
|
|
45
|
-
const routines = ts.isClassDeclaration(statement)
|
|
46
|
-
? statement.members.filter(ts.isMethodDeclaration)
|
|
47
|
-
: [statement];
|
|
48
|
-
for (const routine of routines) {
|
|
49
|
-
if (
|
|
50
|
-
(!ts.isFunctionDeclaration(routine) &&
|
|
51
|
-
!ts.isMethodDeclaration(routine)) ||
|
|
52
|
-
routine.type ||
|
|
53
|
-
!routine.body ||
|
|
54
|
-
!routine.modifiers?.some(
|
|
55
|
-
(item) => item.kind === ts.SyntaxKind.AsyncKeyword,
|
|
56
|
-
)
|
|
57
|
-
) {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
const signature = checker.getSignatureFromDeclaration(routine);
|
|
61
|
-
const result =
|
|
62
|
-
signature &&
|
|
63
|
-
checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
64
|
-
if (result && result.flags & ts.TypeFlags.Void) {
|
|
65
|
-
const position = routine.body.getStart(source);
|
|
66
|
-
edits.push({
|
|
67
|
-
start: position,
|
|
68
|
-
end: position,
|
|
69
|
-
text: from(routine, ":Promise<void> "),
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
84
|
+
for (const source of plcSources(program)) {
|
|
85
|
+
const edits = voidAsyncAnnotations(source, checker);
|
|
74
86
|
for (const statement of source.statements) {
|
|
75
87
|
if (
|
|
76
88
|
!ts.isVariableStatement(statement) ||
|
|
@@ -137,9 +149,7 @@ export const lowerConstFunctions = (
|
|
|
137
149
|
signature &&
|
|
138
150
|
checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
139
151
|
const asyncVoid =
|
|
140
|
-
arrow.
|
|
141
|
-
(item) => item.kind === ts.SyntaxKind.AsyncKeyword,
|
|
142
|
-
) &&
|
|
152
|
+
hasModifier(arrow, ts.SyntaxKind.AsyncKeyword) &&
|
|
143
153
|
inferredResult &&
|
|
144
154
|
inferredResult.flags & ts.TypeFlags.Void;
|
|
145
155
|
const inferredType = asyncVoid
|