@tsplc/compiler 0.2.17 → 0.2.19
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
package/src/frontend/services.ts
CHANGED
|
@@ -1,425 +1,559 @@
|
|
|
1
1
|
import { runtimeSources } from "./module-graph.ts";
|
|
2
|
+
import { namesSdk, symbolAt } from "./sdk.ts";
|
|
2
3
|
import ts from "typescript";
|
|
3
4
|
import { dirname, join, relative } from "node:path";
|
|
4
5
|
import { error, type Diagnostic } from "@tsplc/compiler/ir";
|
|
5
|
-
import { edited, from, mapped, type Edit, type Mapped, type Positions } from "./positions.ts";
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
edited,
|
|
8
|
+
from,
|
|
9
|
+
mapped,
|
|
10
|
+
type Edit,
|
|
11
|
+
type Mapped,
|
|
12
|
+
type Positions,
|
|
13
|
+
} from "./positions.ts";
|
|
14
|
+
import {
|
|
15
|
+
isIntegerType,
|
|
16
|
+
serviceContract,
|
|
17
|
+
servicePath,
|
|
18
|
+
wireLayout,
|
|
19
|
+
wireRecord,
|
|
20
|
+
type ServiceContract,
|
|
21
|
+
type WireField,
|
|
22
|
+
type WireScalar,
|
|
15
23
|
} from "@tsplc/server";
|
|
16
24
|
|
|
17
25
|
/** The server section's transport; each client's type argument names its router. */
|
|
18
26
|
export interface ServiceOptions {
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
transport: "http" | "opcua";
|
|
28
|
+
baseUrlEnv?: string;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
/** A procedure record read from its type, with the PLC enum each enum field names. */
|
|
24
32
|
interface RecordLayout {
|
|
25
|
-
|
|
26
|
-
|
|
33
|
+
fields: Record<string, WireField>;
|
|
34
|
+
enums: Record<string, ts.EnumDeclaration>;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
const property = (checker: ts.TypeChecker, type: ts.Type, name: string) => {
|
|
30
|
-
|
|
31
|
-
|
|
38
|
+
const symbol = type.getProperty(name);
|
|
39
|
+
return symbol && checker.getTypeOfSymbol(symbol);
|
|
32
40
|
};
|
|
33
41
|
|
|
34
42
|
/** An `iec.*` schema's layout, which its type carries as an optional phantom property. */
|
|
35
|
-
const layoutNumber = (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
const layoutNumber = (
|
|
44
|
+
checker: ts.TypeChecker,
|
|
45
|
+
type: ts.Type,
|
|
46
|
+
name: string,
|
|
47
|
+
label: string,
|
|
48
|
+
) => {
|
|
49
|
+
const brand = property(checker, type, wireLayout);
|
|
50
|
+
const value =
|
|
51
|
+
brand && property(checker, checker.getNonNullableType(brand), name);
|
|
52
|
+
if (!value) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
if (!value.isNumberLiteral()) {
|
|
56
|
+
throw new Error(`${label}: ${name} must be a literal number.`);
|
|
57
|
+
}
|
|
58
|
+
return value.value;
|
|
41
59
|
};
|
|
42
60
|
|
|
43
61
|
const scalarLayout = (
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
checker: ts.TypeChecker,
|
|
63
|
+
type: ts.Type,
|
|
64
|
+
label: string,
|
|
47
65
|
): { field: WireScalar; enumeration?: ts.EnumDeclaration } => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
if (type.flags & ts.TypeFlags.Boolean) {
|
|
67
|
+
return { field: { kind: "boolean" } };
|
|
68
|
+
}
|
|
69
|
+
// An enum type is the union of its member literals.
|
|
70
|
+
const first = type.isUnion() ? type.types[0] : type;
|
|
71
|
+
const member = first?.symbol?.valueDeclaration;
|
|
72
|
+
if (
|
|
73
|
+
first &&
|
|
74
|
+
first.flags & ts.TypeFlags.EnumLiteral &&
|
|
75
|
+
member &&
|
|
76
|
+
ts.isEnumMember(member)
|
|
77
|
+
) {
|
|
78
|
+
const members: Record<string, number> = {};
|
|
79
|
+
for (const item of member.parent.members) {
|
|
80
|
+
const value = checker.getConstantValue(item);
|
|
81
|
+
if (typeof value !== "number") {
|
|
82
|
+
throw new Error(`${label}: PLC enums require numeric members.`);
|
|
83
|
+
}
|
|
84
|
+
members[item.name.getText()] = value;
|
|
85
|
+
}
|
|
86
|
+
return { field: { kind: "enum", members }, enumeration: member.parent };
|
|
87
|
+
}
|
|
88
|
+
const capacity = layoutNumber(checker, type, "capacity", label);
|
|
89
|
+
if (capacity !== undefined) {
|
|
90
|
+
return { field: { kind: "string", capacity } };
|
|
91
|
+
}
|
|
92
|
+
const brand = property(checker, type, wireLayout);
|
|
93
|
+
const iec =
|
|
94
|
+
brand && property(checker, checker.getNonNullableType(brand), "iec");
|
|
95
|
+
if (iec?.isStringLiteral() && isIntegerType(iec.value)) {
|
|
96
|
+
const minimum = layoutNumber(checker, type, "minimum", label);
|
|
97
|
+
const maximum = layoutNumber(checker, type, "maximum", label);
|
|
98
|
+
if (minimum !== undefined && maximum !== undefined) {
|
|
99
|
+
return { field: { kind: "integer", iec: iec.value, minimum, maximum } };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
throw new Error(
|
|
103
|
+
`${label}: use z.boolean(), z.enum() of a numeric enum, iec.STRING(), or an integer such as iec.INT().`,
|
|
104
|
+
);
|
|
75
105
|
};
|
|
76
106
|
|
|
77
107
|
const recordLayout = (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
108
|
+
checker: ts.TypeChecker,
|
|
109
|
+
type: ts.Type,
|
|
110
|
+
label: string,
|
|
111
|
+
output: boolean,
|
|
82
112
|
): RecordLayout => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
113
|
+
const fields: Record<string, WireField> = {};
|
|
114
|
+
const enums: Record<string, ts.EnumDeclaration> = {};
|
|
115
|
+
// A procedure without `.input()` takes `void`.
|
|
116
|
+
if (!(type.flags & ts.TypeFlags.Void)) {
|
|
117
|
+
if (
|
|
118
|
+
!(type.flags & ts.TypeFlags.Object) ||
|
|
119
|
+
type.getStringIndexType() ||
|
|
120
|
+
type.getNumberIndexType() ||
|
|
121
|
+
type.getCallSignatures().length
|
|
122
|
+
) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`${label}: PLC procedures exchange one closed z.strictObject() record.`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
for (const field of type.getProperties()) {
|
|
128
|
+
const key = `${label}.${field.name}`;
|
|
129
|
+
if (field.flags & ts.SymbolFlags.Optional) {
|
|
130
|
+
throw new Error(`${key}: optional wire fields are unsupported.`);
|
|
131
|
+
}
|
|
132
|
+
const value = checker.getTypeOfSymbol(field);
|
|
133
|
+
const length = layoutNumber(checker, value, "length", key);
|
|
134
|
+
const element =
|
|
135
|
+
length === undefined ? undefined : value.getNumberIndexType();
|
|
136
|
+
const scalar = scalarLayout(checker, element ?? value, key);
|
|
137
|
+
fields[field.name] =
|
|
138
|
+
length === undefined
|
|
139
|
+
? scalar.field
|
|
140
|
+
: { kind: "array", length, element: scalar.field };
|
|
141
|
+
if (scalar.enumeration) {
|
|
142
|
+
enums[field.name] = scalar.enumeration;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { fields: wireRecord(fields, label, output).fields, enums };
|
|
108
147
|
};
|
|
109
148
|
|
|
110
149
|
/** Identifies a record by layout, so every name for it lowers to one PLC struct. */
|
|
111
150
|
const recordKey = (layout: RecordLayout) =>
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
151
|
+
JSON.stringify([
|
|
152
|
+
layout.fields,
|
|
153
|
+
Object.entries(layout.enums)
|
|
154
|
+
.map(([field, value]) => [
|
|
155
|
+
field,
|
|
156
|
+
value.getSourceFile().fileName,
|
|
157
|
+
value.name.text,
|
|
158
|
+
])
|
|
159
|
+
.sort(),
|
|
160
|
+
]);
|
|
118
161
|
|
|
119
|
-
const procedureAt = (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
162
|
+
const procedureAt = (
|
|
163
|
+
checker: ts.TypeChecker,
|
|
164
|
+
router: ts.Type,
|
|
165
|
+
path: readonly string[],
|
|
166
|
+
) => {
|
|
167
|
+
let type = property(checker, router, "_def");
|
|
168
|
+
type = type && property(checker, type, "record");
|
|
169
|
+
for (const part of path) {
|
|
170
|
+
type = type && property(checker, type, part);
|
|
171
|
+
}
|
|
172
|
+
const definition = type && property(checker, type, "_def");
|
|
173
|
+
if (!definition || !property(checker, definition, "procedure")) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
const kind = property(checker, definition, "type");
|
|
177
|
+
const types = property(checker, definition, "$types");
|
|
178
|
+
const input = types && property(checker, types, "input");
|
|
179
|
+
const output = types && property(checker, types, "output");
|
|
180
|
+
if (!kind?.isStringLiteral() || !input || !output) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
return { kind: kind.value, input, output };
|
|
131
184
|
};
|
|
132
185
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
186
|
+
// Unlike the backend's pascalCase, this keeps underscores: `line_state.get` names `Line_stateGet`.
|
|
187
|
+
const pascalServicePath = (path: string) =>
|
|
188
|
+
path
|
|
189
|
+
.split(".")
|
|
190
|
+
.map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1))
|
|
191
|
+
.join("");
|
|
138
192
|
|
|
139
193
|
/**
|
|
140
194
|
* Replaces each `createServiceClient<Router>()` call with an in-memory module of the called
|
|
141
195
|
* procedures, read from the router's type: the server never runs and no file is written.
|
|
142
196
|
*/
|
|
143
197
|
export const lowerServices = (
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
198
|
+
program: ts.Program,
|
|
199
|
+
sdk: string,
|
|
200
|
+
positions: Positions,
|
|
201
|
+
options: ServiceOptions | undefined,
|
|
148
202
|
) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
203
|
+
const sources = new Map<string, Mapped>();
|
|
204
|
+
const diagnostics: Diagnostic[] = [];
|
|
205
|
+
const records = new Map<string, { name: string; layout: RecordLayout }>();
|
|
206
|
+
const calls = new Map<
|
|
207
|
+
string,
|
|
208
|
+
{ contract: ServiceContract; input: string; output: string }
|
|
209
|
+
>();
|
|
210
|
+
const checker = program.getTypeChecker();
|
|
211
|
+
const fail = (node: ts.Node, code: string, message: string) =>
|
|
212
|
+
diagnostics.push(error(code, message, positions.node(node)));
|
|
213
|
+
const record = (layout: RecordLayout, name: string) => {
|
|
214
|
+
const key = recordKey(layout);
|
|
215
|
+
const existing = records.get(key);
|
|
216
|
+
if (existing) {
|
|
217
|
+
return existing.name;
|
|
218
|
+
}
|
|
219
|
+
records.set(key, { name, layout });
|
|
220
|
+
return name;
|
|
221
|
+
};
|
|
168
222
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
223
|
+
const clients = new Map<ts.Symbol, ts.Type>();
|
|
224
|
+
/** The generated service module, and the first client's creation it stands for. */
|
|
225
|
+
let module: { file: string; client: ts.CallExpression } | undefined;
|
|
226
|
+
const sourceFiles = runtimeSources(program).filter(
|
|
227
|
+
(source) => !source.isDeclarationFile,
|
|
228
|
+
);
|
|
229
|
+
for (const source of sourceFiles) {
|
|
230
|
+
for (const statement of source.statements) {
|
|
231
|
+
if (!ts.isVariableStatement(statement)) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
235
|
+
const call = declaration.initializer;
|
|
236
|
+
if (
|
|
237
|
+
!call ||
|
|
238
|
+
!ts.isCallExpression(call) ||
|
|
239
|
+
!namesSdk(checker, sdk, call.expression, "createServiceClient")
|
|
240
|
+
) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const router = call.typeArguments?.[0];
|
|
244
|
+
const client = symbolAt(checker, declaration.name);
|
|
245
|
+
if (
|
|
246
|
+
!router ||
|
|
247
|
+
!client ||
|
|
248
|
+
!(statement.declarationList.flags & ts.NodeFlags.Const)
|
|
249
|
+
) {
|
|
250
|
+
fail(
|
|
251
|
+
declaration,
|
|
252
|
+
"PLC1013",
|
|
253
|
+
"Create a service client as a module constant: const services = createServiceClient<AppRouter>().",
|
|
254
|
+
);
|
|
255
|
+
} else if (!options) {
|
|
256
|
+
fail(
|
|
257
|
+
call,
|
|
258
|
+
"PLC1013",
|
|
259
|
+
"Configure server in tsplc.config.ts to call the server.",
|
|
260
|
+
);
|
|
261
|
+
} else if (options.transport === "http" && !options.baseUrlEnv) {
|
|
262
|
+
fail(
|
|
263
|
+
call,
|
|
264
|
+
"PLC1013",
|
|
265
|
+
"HTTP service transport requires server.baseUrlEnv.",
|
|
266
|
+
);
|
|
267
|
+
} else {
|
|
268
|
+
clients.set(client, checker.getTypeFromTypeNode(router));
|
|
269
|
+
module ??= {
|
|
270
|
+
file: join(dirname(source.fileName), ".tsplc-services.ts"),
|
|
271
|
+
client: call,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
205
277
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
278
|
+
for (const source of sourceFiles) {
|
|
279
|
+
const edits: Edit[] = [];
|
|
280
|
+
const imports = new Map<string, string>();
|
|
281
|
+
const visit = (node: ts.Node): void => {
|
|
282
|
+
if (
|
|
283
|
+
ts.isImportDeclaration(node) &&
|
|
284
|
+
ts.isStringLiteral(node.moduleSpecifier)
|
|
285
|
+
) {
|
|
286
|
+
const clause = node.importClause;
|
|
287
|
+
const bindings = clause?.namedBindings;
|
|
288
|
+
const typeOnly =
|
|
289
|
+
clause?.isTypeOnly ||
|
|
290
|
+
(!clause?.name &&
|
|
291
|
+
bindings &&
|
|
292
|
+
ts.isNamedImports(bindings) &&
|
|
293
|
+
bindings.elements.every((element) => element.isTypeOnly));
|
|
294
|
+
if (!typeOnly && clause && bindings && ts.isNamedImports(bindings)) {
|
|
295
|
+
const elements = bindings.elements.filter((element) => {
|
|
296
|
+
const imported = symbolAt(checker, element.name);
|
|
297
|
+
return !imported || !clients.has(imported);
|
|
298
|
+
});
|
|
299
|
+
if (elements.length !== bindings.elements.length) {
|
|
300
|
+
edits.push({
|
|
301
|
+
start: node.getStart(source),
|
|
302
|
+
end: node.end,
|
|
303
|
+
text:
|
|
304
|
+
elements.length || clause.name
|
|
305
|
+
? ts
|
|
306
|
+
.createPrinter()
|
|
307
|
+
.printNode(
|
|
308
|
+
ts.EmitHint.Unspecified,
|
|
309
|
+
ts.factory.updateImportDeclaration(
|
|
310
|
+
node,
|
|
311
|
+
node.modifiers,
|
|
312
|
+
ts.factory.updateImportClause(
|
|
313
|
+
clause,
|
|
314
|
+
clause.isTypeOnly,
|
|
315
|
+
clause.name,
|
|
316
|
+
ts.factory.updateNamedImports(bindings, elements),
|
|
317
|
+
),
|
|
318
|
+
node.moduleSpecifier,
|
|
319
|
+
node.attributes,
|
|
320
|
+
),
|
|
321
|
+
source,
|
|
322
|
+
)
|
|
323
|
+
: "",
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (
|
|
328
|
+
!typeOnly &&
|
|
329
|
+
(node.moduleSpecifier.text.startsWith("node:") ||
|
|
330
|
+
[
|
|
331
|
+
"@tsplc/server",
|
|
332
|
+
"@trpc/server",
|
|
333
|
+
"zod",
|
|
334
|
+
"fs",
|
|
335
|
+
"http",
|
|
336
|
+
"https",
|
|
337
|
+
"net",
|
|
338
|
+
"tls",
|
|
339
|
+
].includes(node.moduleSpecifier.text))
|
|
340
|
+
) {
|
|
341
|
+
fail(
|
|
342
|
+
node,
|
|
343
|
+
"PLC1013",
|
|
344
|
+
`Server runtime import ${node.moduleSpecifier.text} cannot execute in PLC code. Expose a PLC procedure on the server router and call it through createServiceClient instead.`,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (ts.isVariableStatement(node)) {
|
|
349
|
+
const clientStatement = node.declarationList.declarations.some(
|
|
350
|
+
(declaration) => {
|
|
351
|
+
const declared = symbolAt(checker, declaration.name);
|
|
352
|
+
return declared && clients.has(declared);
|
|
353
|
+
},
|
|
354
|
+
);
|
|
355
|
+
if (clientStatement) {
|
|
356
|
+
edits.push({ start: node.getStart(source), end: node.end, text: "" });
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (clients.size && ts.isPropertyAccessExpression(node)) {
|
|
361
|
+
const parts: string[] = [];
|
|
362
|
+
let root: ts.Expression = node;
|
|
363
|
+
while (ts.isPropertyAccessExpression(root)) {
|
|
364
|
+
parts.unshift(root.name.text);
|
|
365
|
+
root = root.expression;
|
|
366
|
+
}
|
|
367
|
+
const client = symbolAt(checker, root);
|
|
368
|
+
const router = client && clients.get(client);
|
|
369
|
+
const procedure = router && procedureAt(checker, router, parts);
|
|
370
|
+
if (router) {
|
|
371
|
+
const path = parts.join(".");
|
|
372
|
+
if (!procedure) {
|
|
373
|
+
fail(
|
|
374
|
+
node,
|
|
375
|
+
"PLC1013",
|
|
376
|
+
`${path} is not a procedure of the client's router.`,
|
|
377
|
+
);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
let call = calls.get(path);
|
|
381
|
+
if (!call) {
|
|
382
|
+
try {
|
|
383
|
+
if (procedure.kind !== "query" && procedure.kind !== "mutation") {
|
|
384
|
+
throw new Error(
|
|
385
|
+
`${path}: subscriptions use a different transport; expose a query or mutation to the PLC.`,
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
const input = recordLayout(
|
|
389
|
+
checker,
|
|
390
|
+
procedure.input,
|
|
391
|
+
`${path}.input`,
|
|
392
|
+
false,
|
|
393
|
+
);
|
|
394
|
+
const output = recordLayout(
|
|
395
|
+
checker,
|
|
396
|
+
procedure.output,
|
|
397
|
+
`${path}.output`,
|
|
398
|
+
true,
|
|
399
|
+
);
|
|
400
|
+
call = {
|
|
401
|
+
contract: serviceContract(
|
|
402
|
+
path,
|
|
403
|
+
procedure.kind,
|
|
404
|
+
{ fields: input.fields },
|
|
405
|
+
{ fields: output.fields },
|
|
406
|
+
),
|
|
407
|
+
input: record(input, `${pascalServicePath(path)}Input`),
|
|
408
|
+
output: record(output, `${pascalServicePath(path)}Output`),
|
|
409
|
+
};
|
|
410
|
+
calls.set(path, call);
|
|
411
|
+
} catch (cause) {
|
|
412
|
+
fail(
|
|
413
|
+
node,
|
|
414
|
+
"PLC2001",
|
|
415
|
+
cause instanceof Error ? cause.message : String(cause),
|
|
416
|
+
);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const name = `tsplcService_${path.replaceAll(".", "_")}`;
|
|
421
|
+
const local = imports.get(name) ?? `tsplcRemote${imports.size}`;
|
|
422
|
+
imports.set(name, local);
|
|
423
|
+
edits.push({
|
|
424
|
+
start: node.getStart(source),
|
|
425
|
+
end: node.end,
|
|
426
|
+
text: local,
|
|
427
|
+
});
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
ts.forEachChild(node, visit);
|
|
432
|
+
};
|
|
433
|
+
visit(source);
|
|
434
|
+
if (!edits.length) {
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
let text = edited(source, edits);
|
|
438
|
+
if (imports.size && module) {
|
|
439
|
+
const path = relative(dirname(source.fileName), module.file).replaceAll(
|
|
440
|
+
"\\",
|
|
441
|
+
"/",
|
|
442
|
+
);
|
|
443
|
+
text = mapped`${text}\nimport {${[...imports].map(([name, local]) => `${name} as ${local}`).join(",")}} from ${JSON.stringify(path.startsWith("../") ? path : `./${path}`)};\n`;
|
|
444
|
+
}
|
|
445
|
+
sources.set(source.fileName, text);
|
|
446
|
+
}
|
|
447
|
+
if (module && options) {
|
|
448
|
+
sources.set(
|
|
449
|
+
module.file,
|
|
450
|
+
from(module.client, serviceModule(records, calls, options)),
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
sources,
|
|
455
|
+
diagnostics,
|
|
456
|
+
file: module?.file,
|
|
457
|
+
records: new Map([...records].map(([key, { name }]) => [key, name])),
|
|
458
|
+
};
|
|
341
459
|
};
|
|
342
460
|
|
|
343
461
|
const serviceModule = (
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
462
|
+
records: ReadonlyMap<string, { name: string; layout: RecordLayout }>,
|
|
463
|
+
calls: ReadonlyMap<
|
|
464
|
+
string,
|
|
465
|
+
{ contract: ServiceContract; input: string; output: string }
|
|
466
|
+
>,
|
|
467
|
+
options: ServiceOptions,
|
|
347
468
|
) => {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
469
|
+
const enums = new Map<ts.EnumDeclaration, string>();
|
|
470
|
+
const enumName = (declaration: ts.EnumDeclaration) => {
|
|
471
|
+
const name = enums.get(declaration) ?? `tsplcEnum${enums.size}`;
|
|
472
|
+
enums.set(declaration, name);
|
|
473
|
+
return name;
|
|
474
|
+
};
|
|
475
|
+
const scalar = (
|
|
476
|
+
field: WireScalar,
|
|
477
|
+
enumeration: ts.EnumDeclaration | undefined,
|
|
478
|
+
) => {
|
|
479
|
+
switch (field.kind) {
|
|
480
|
+
case "boolean":
|
|
481
|
+
return "BOOL";
|
|
482
|
+
case "integer":
|
|
483
|
+
return field.iec;
|
|
484
|
+
case "string":
|
|
485
|
+
return `STRING<${field.capacity}>`;
|
|
486
|
+
case "enum":
|
|
487
|
+
return enumName(enumeration!);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
const interfaces = [...records.values()].map(({ name, layout }) => {
|
|
491
|
+
const fields = Object.entries(layout.fields).map(([key, field]) => {
|
|
492
|
+
const enumeration = layout.enums[key];
|
|
493
|
+
const type =
|
|
494
|
+
field.kind === "array"
|
|
495
|
+
? `FixedArray<${scalar(field.element, enumeration)},${field.length}>`
|
|
496
|
+
: scalar(field, enumeration);
|
|
497
|
+
return `${key}:${type}`;
|
|
498
|
+
});
|
|
499
|
+
return `export interface ${name}{${fields.join(";")}}`;
|
|
500
|
+
});
|
|
501
|
+
const functions = [...calls.values()]
|
|
502
|
+
.sort((left, right) =>
|
|
503
|
+
left.contract.path.localeCompare(right.contract.path),
|
|
504
|
+
)
|
|
505
|
+
.map(({ contract, input, output }) => {
|
|
506
|
+
const hasInput = Object.keys(contract.input.fields).length > 0;
|
|
507
|
+
const request = hasInput ? "input" : `defaultValue<${input}>()`;
|
|
508
|
+
const operation =
|
|
509
|
+
options.transport === "http"
|
|
510
|
+
? `tsplcServiceFetch<${output}>(\`\${tsplcServiceBaseUrl}${servicePath(contract)}\`,{method:"POST",json:${request}})`
|
|
511
|
+
: `tsplcServiceCall<${output},${input}>(${JSON.stringify(servicePath(contract))},${request})`;
|
|
512
|
+
return `export const tsplcService_${contract.path.replaceAll(".", "_")}=async(${hasInput ? `input:${input}` : ""}):Promise<${output}>=>{return await ${operation};};`;
|
|
513
|
+
});
|
|
514
|
+
const transport =
|
|
515
|
+
options.transport === "http"
|
|
516
|
+
? `import {fetch as tsplcServiceFetch} from "@tsplc/compiler/async";\nimport {env as tsplcServiceEnv} from "@tsplc/compiler/config";`
|
|
517
|
+
: `import {opcuaCall as tsplcServiceCall} from "@tsplc/compiler/async";`;
|
|
518
|
+
const enumImports = [...enums].map(
|
|
519
|
+
([declaration, name]) =>
|
|
520
|
+
`import type {${declaration.name.text} as ${name}} from ${JSON.stringify(declaration.getSourceFile().fileName)};`,
|
|
521
|
+
);
|
|
522
|
+
const baseUrl =
|
|
523
|
+
options.transport === "http"
|
|
524
|
+
? `const tsplcServiceBaseUrl:STRING<512>=tsplcServiceEnv<STRING<512>>(${JSON.stringify(options.baseUrlEnv)});`
|
|
525
|
+
: "";
|
|
526
|
+
return (
|
|
527
|
+
[transport, ...enumImports, baseUrl, ...interfaces, ...functions].join(
|
|
528
|
+
"\n",
|
|
529
|
+
) + "\n"
|
|
530
|
+
);
|
|
401
531
|
};
|
|
402
532
|
|
|
403
533
|
/** Lowers a type that names a called procedure's record, such as `inferRouterOutputs<AppRouter>["a"]["b"]`. */
|
|
404
534
|
export const serviceRecords = (
|
|
405
|
-
|
|
406
|
-
|
|
535
|
+
program: ts.Program,
|
|
536
|
+
lowering: { file: string | undefined; records: ReadonlyMap<string, string> },
|
|
407
537
|
) => {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
538
|
+
const source = lowering.file && program.getSourceFile(lowering.file);
|
|
539
|
+
if (!source) {
|
|
540
|
+
return undefined;
|
|
541
|
+
}
|
|
542
|
+
const checker = program.getTypeChecker();
|
|
543
|
+
const declarations = new Map(
|
|
544
|
+
source.statements
|
|
545
|
+
.filter(ts.isInterfaceDeclaration)
|
|
546
|
+
.map((value) => [value.name.text, value]),
|
|
547
|
+
);
|
|
548
|
+
return (type: ts.Type): ts.Symbol | undefined => {
|
|
549
|
+
let key: string;
|
|
550
|
+
try {
|
|
551
|
+
key = recordKey(recordLayout(checker, type, "record", false));
|
|
552
|
+
} catch {
|
|
553
|
+
return undefined;
|
|
554
|
+
}
|
|
555
|
+
const name = lowering.records.get(key);
|
|
556
|
+
const declaration = name === undefined ? undefined : declarations.get(name);
|
|
557
|
+
return declaration && checker.getSymbolAtLocation(declaration.name);
|
|
558
|
+
};
|
|
425
559
|
};
|