@tsplc/tmp-compiler 0.6.2 → 0.6.3
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/dist/codesys.js +25 -20
- package/dist/codesys.js.map +3 -3
- package/dist/index.js +814 -692
- package/dist/index.js.map +8 -7
- package/package.json +9 -9
- package/src/codesys/http.ts +37 -25
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/coroutines.ts +11 -3
- package/src/frontend/elaborate/expressions.ts +6 -0
- package/src/frontend/services.ts +15 -12
- package/src/frontend/typescript.ts +9 -1
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "git+https://github.com/rip-technologies/tsplc.git",
|
|
18
18
|
"directory": "packages/compiler"
|
|
19
19
|
},
|
|
20
|
-
"version": "0.6.
|
|
20
|
+
"version": "0.6.3",
|
|
21
21
|
"type": "module",
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
@@ -93,17 +93,17 @@
|
|
|
93
93
|
"bun": ">=1.4.3"
|
|
94
94
|
},
|
|
95
95
|
"dependencies": {
|
|
96
|
-
"@tsplc/agent": "npm:@tsplc/tmp-agent@0.6.
|
|
97
|
-
"@tsplc/build": "npm:@tsplc/tmp-build@0.6.
|
|
98
|
-
"@tsplc/config": "npm:@tsplc/tmp-config@0.6.
|
|
99
|
-
"@tsplc/debug": "npm:@tsplc/tmp-debug@0.6.
|
|
100
|
-
"@tsplc/opcua": "npm:@tsplc/tmp-opcua@0.6.
|
|
101
|
-
"@tsplc/typegen": "npm:@tsplc/tmp-typegen@0.6.
|
|
96
|
+
"@tsplc/agent": "npm:@tsplc/tmp-agent@0.6.3",
|
|
97
|
+
"@tsplc/build": "npm:@tsplc/tmp-build@0.6.3",
|
|
98
|
+
"@tsplc/config": "npm:@tsplc/tmp-config@0.6.3",
|
|
99
|
+
"@tsplc/debug": "npm:@tsplc/tmp-debug@0.6.3",
|
|
100
|
+
"@tsplc/opcua": "npm:@tsplc/tmp-opcua@0.6.3",
|
|
101
|
+
"@tsplc/typegen": "npm:@tsplc/tmp-typegen@0.6.3",
|
|
102
102
|
"typescript": "6.0.3",
|
|
103
103
|
"zod": "4.3.6",
|
|
104
104
|
"@types/bun": "1.4.2",
|
|
105
|
-
"@tsplc/server": "npm:@tsplc/tmp-server@0.6.
|
|
106
|
-
"@tsplc/iec": "npm:@tsplc/tmp-iec@0.6.
|
|
105
|
+
"@tsplc/server": "npm:@tsplc/tmp-server@0.6.3",
|
|
106
|
+
"@tsplc/iec": "npm:@tsplc/tmp-iec@0.6.3"
|
|
107
107
|
},
|
|
108
108
|
"bin": {
|
|
109
109
|
"tsplc": "./dist/cli.js"
|
package/src/codesys/http.ts
CHANGED
|
@@ -149,12 +149,11 @@ const serviceScalar = (type: PlcType): Required<JsonScalar> | undefined => {
|
|
|
149
149
|
|
|
150
150
|
type RecordLayout = Extract<PlcType, { kind: "record" }>;
|
|
151
151
|
|
|
152
|
-
/** A service block's request and response ports and their records. */
|
|
152
|
+
/** A service block's request and response ports and their records; no response when the call answers nothing. */
|
|
153
153
|
interface HttpLayout {
|
|
154
154
|
request: Variable;
|
|
155
|
-
response: Variable;
|
|
156
155
|
requestLayout: RecordLayout;
|
|
157
|
-
|
|
156
|
+
response: { port: Variable; layout: RecordLayout } | null;
|
|
158
157
|
}
|
|
159
158
|
|
|
160
159
|
/**
|
|
@@ -166,17 +165,23 @@ export const httpLayout = (
|
|
|
166
165
|
types: ReadonlyMap<string, PlcType>,
|
|
167
166
|
): HttpLayout | { error: string } => {
|
|
168
167
|
const request = block.inputs.find((v) => v.name === "request");
|
|
169
|
-
|
|
170
|
-
|
|
168
|
+
// A call that answers nothing has no result port.
|
|
169
|
+
const result = block.outputs.find((v) => v.name === "result");
|
|
170
|
+
if (!request) {
|
|
171
171
|
return { error: "Missing HTTP record ports." };
|
|
172
172
|
}
|
|
173
173
|
const requestLayout = types.get(request.type);
|
|
174
|
-
const
|
|
174
|
+
const resultLayout = result && types.get(result.type);
|
|
175
|
+
const response =
|
|
176
|
+
result && resultLayout?.kind === "record"
|
|
177
|
+
? { port: result, layout: resultLayout }
|
|
178
|
+
: null;
|
|
175
179
|
if (
|
|
176
180
|
requestLayout?.kind !== "record" ||
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
181
|
+
(result &&
|
|
182
|
+
(!response ||
|
|
183
|
+
response.layout.fields.length === 0 ||
|
|
184
|
+
response.layout.fields.length > maxRecordFields))
|
|
180
185
|
) {
|
|
181
186
|
return {
|
|
182
187
|
error: `HTTP requires closed request/response records with 1..${maxRecordFields} response fields.`,
|
|
@@ -197,11 +202,12 @@ export const httpLayout = (
|
|
|
197
202
|
const element = scalarSize(type.element);
|
|
198
203
|
return element === undefined ? undefined : 2 + type.length * (element + 1);
|
|
199
204
|
};
|
|
200
|
-
|
|
205
|
+
const records = [
|
|
201
206
|
[requestLayout, false],
|
|
202
|
-
[
|
|
203
|
-
] as const
|
|
204
|
-
|
|
207
|
+
...(response ? [[response.layout, true] as const] : []),
|
|
208
|
+
] as const;
|
|
209
|
+
for (const [record, answer] of records) {
|
|
210
|
+
const sizes = record.fields.map((field) => fieldSize(field, answer));
|
|
205
211
|
if (!sizes.every((size): size is number => size !== undefined)) {
|
|
206
212
|
return {
|
|
207
213
|
error:
|
|
@@ -218,7 +224,7 @@ export const httpLayout = (
|
|
|
218
224
|
};
|
|
219
225
|
}
|
|
220
226
|
}
|
|
221
|
-
return { request,
|
|
227
|
+
return { request, requestLayout, response };
|
|
222
228
|
};
|
|
223
229
|
|
|
224
230
|
/** A service block's typed JSON codecs and ports; no application protocol lives here. */
|
|
@@ -251,7 +257,7 @@ const serviceCodecs = (
|
|
|
251
257
|
if ("error" in layout) {
|
|
252
258
|
throw new Error(layout.error);
|
|
253
259
|
}
|
|
254
|
-
const { request,
|
|
260
|
+
const { request, requestLayout, response } = layout;
|
|
255
261
|
const typeOf = (id: string) => required(types.get(id));
|
|
256
262
|
const scalarOf = (type: PlcType) => required(serviceScalar(type));
|
|
257
263
|
const encode = (type: PlcType, value: string): string =>
|
|
@@ -269,21 +275,22 @@ const serviceCodecs = (
|
|
|
269
275
|
return `Append(${stStringLiteral(`${i ? "," : ""}"${field.name}":`)});${type.kind === "array" ? "Append('[');" : ""}${encode(type, value)}${type.kind === "array" ? "Append(']');" : ""}`;
|
|
270
276
|
})
|
|
271
277
|
.join("\n");
|
|
272
|
-
// `seen` has a bit for each response field, so a record has at most `maxRecordFields`.
|
|
273
|
-
const responseCode = responseLayout.fields
|
|
274
|
-
.map((field, i) => {
|
|
275
|
-
const value = `${port("result")}.${target.field(response.type, field.name)}`;
|
|
276
|
-
const decode = scalarOf(typeOf(field.type)).decode(value, target);
|
|
277
|
-
return `${i === 0 ? "IF" : "ELSIF"} part = ${stStringLiteral(field.name)} THEN IF (seen AND SHL(DWORD#1,${i})) <> 0 THEN malformed:=TRUE; END_IF; seen:=seen OR SHL(DWORD#1,${i}); ${decode}`;
|
|
278
|
-
})
|
|
279
|
-
.join("\n");
|
|
280
278
|
const declarations = (variables: readonly Variable[]) =>
|
|
281
279
|
variables
|
|
282
280
|
.map((v) => `${required(fields.get(v.id))} : ${target.name(v.type)};`)
|
|
283
281
|
.join("\n");
|
|
284
282
|
const inputs = declarations(block.inputs);
|
|
285
283
|
const outputs = declarations(block.outputs);
|
|
286
|
-
const
|
|
284
|
+
const decodeRecord = (result: Variable, record: RecordLayout) => {
|
|
285
|
+
// `seen` has a bit for each response field, so a record has at most `maxRecordFields`.
|
|
286
|
+
const responseCode = record.fields
|
|
287
|
+
.map((field, i) => {
|
|
288
|
+
const value = `${port("result")}.${target.field(result.type, field.name)}`;
|
|
289
|
+
const decode = scalarOf(typeOf(field.type)).decode(value, target);
|
|
290
|
+
return `${i === 0 ? "IF" : "ELSIF"} part = ${stStringLiteral(field.name)} THEN IF (seen AND SHL(DWORD#1,${i})) <> 0 THEN malformed:=TRUE; END_IF; seen:=seen OR SHL(DWORD#1,${i}); ${decode}`;
|
|
291
|
+
})
|
|
292
|
+
.join("\n");
|
|
293
|
+
return `cursor:=1;malformed:=FALSE;seen:=0;
|
|
287
294
|
SkipWhitespace();Expect(123);SkipWhitespace();
|
|
288
295
|
WHILE cursor<=TO_UINT(ByteLength(payload)) AND NOT malformed DO
|
|
289
296
|
IF payload[cursor-1]=125 THEN cursor:=cursor+1;EXIT;END_IF;
|
|
@@ -294,7 +301,12 @@ WHILE cursor<=TO_UINT(ByteLength(payload)) AND NOT malformed DO
|
|
|
294
301
|
ELSIF payload[cursor-1]<>125 THEN malformed:=TRUE;END_IF;
|
|
295
302
|
END_WHILE;
|
|
296
303
|
SkipWhitespace();
|
|
297
|
-
IF malformed OR cursor<>TO_UINT(ByteLength(payload))+1 OR seen<>DWORD#${2 **
|
|
304
|
+
IF malformed OR cursor<>TO_UINT(ByteLength(payload))+1 OR seen<>DWORD#${2 ** record.fields.length - 1} THEN ${port("error")}:=${jobError.malformedResponse};END_IF;`;
|
|
305
|
+
};
|
|
306
|
+
// A call that answers nothing succeeds on its reply's status alone, so its body is never read.
|
|
307
|
+
const decodeResponse = response
|
|
308
|
+
? decodeRecord(response.port, response.layout)
|
|
309
|
+
: "";
|
|
298
310
|
return { port, requestCode, decodeResponse, inputs, outputs };
|
|
299
311
|
};
|
|
300
312
|
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { type Diagnostic, error } from "@tsplc/compiler/ir";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { plcSources } from "./module-graph";
|
|
4
|
+
import { type Edit, edited, type Mapped, type Positions } from "./positions";
|
|
5
|
+
import { hasModifier } from "./types";
|
|
6
|
+
|
|
7
|
+
const withoutParentheses = (type: ts.TypeNode): ts.TypeNode =>
|
|
8
|
+
ts.isParenthesizedTypeNode(type) ? withoutParentheses(type.type) : type;
|
|
9
|
+
|
|
10
|
+
/** `Name` when `type` is `(typeof Name)[keyof typeof Name]`, the type of the values of the object `Name`. */
|
|
11
|
+
const valuesOf = (type: ts.TypeNode): string | undefined => {
|
|
12
|
+
const access = withoutParentheses(type);
|
|
13
|
+
if (!ts.isIndexedAccessTypeNode(access)) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const object = withoutParentheses(access.objectType);
|
|
17
|
+
const index = withoutParentheses(access.indexType);
|
|
18
|
+
if (
|
|
19
|
+
!ts.isTypeQueryNode(object) ||
|
|
20
|
+
!ts.isIdentifier(object.exprName) ||
|
|
21
|
+
!ts.isTypeOperatorNode(index) ||
|
|
22
|
+
index.operator !== ts.SyntaxKind.KeyOfKeyword
|
|
23
|
+
) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const keys = withoutParentheses(index.type);
|
|
27
|
+
return ts.isTypeQueryNode(keys) &&
|
|
28
|
+
ts.isIdentifier(keys.exprName) &&
|
|
29
|
+
keys.exprName.text === object.exprName.text
|
|
30
|
+
? object.exprName.text
|
|
31
|
+
: undefined;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const unwrapped = (node: ts.Expression): ts.Expression =>
|
|
35
|
+
ts.isParenthesizedExpression(node) || ts.isSatisfiesExpression(node)
|
|
36
|
+
? unwrapped(node.expression)
|
|
37
|
+
: node;
|
|
38
|
+
|
|
39
|
+
/** The object literal an initializer writes, and whether it is `as const`; undefined when it writes none. */
|
|
40
|
+
const objectLiteral = (
|
|
41
|
+
initializer: ts.Expression,
|
|
42
|
+
): { object: ts.ObjectLiteralExpression; asConst: boolean } | undefined => {
|
|
43
|
+
const outer = unwrapped(initializer);
|
|
44
|
+
const asConst =
|
|
45
|
+
ts.isAsExpression(outer) &&
|
|
46
|
+
ts.isTypeReferenceNode(outer.type) &&
|
|
47
|
+
ts.isIdentifier(outer.type.typeName) &&
|
|
48
|
+
outer.type.typeName.text === "const";
|
|
49
|
+
const object = unwrapped(asConst ? outer.expression : outer);
|
|
50
|
+
return ts.isObjectLiteralExpression(object) ? { object, asConst } : undefined;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** A member's number, `1` or `-1`, as an enum member's initializer. */
|
|
54
|
+
const isNumber = (value: ts.Expression): boolean =>
|
|
55
|
+
ts.isNumericLiteral(value) ||
|
|
56
|
+
(ts.isPrefixUnaryExpression(value) &&
|
|
57
|
+
value.operator === ts.SyntaxKind.MinusToken &&
|
|
58
|
+
ts.isNumericLiteral(value.operand));
|
|
59
|
+
|
|
60
|
+
type Member = ts.PropertyAssignment & {
|
|
61
|
+
readonly name: ts.Identifier | ts.StringLiteral;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const isMember = (property: ts.ObjectLiteralElementLike): property is Member =>
|
|
65
|
+
ts.isPropertyAssignment(property) &&
|
|
66
|
+
(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) &&
|
|
67
|
+
isNumber(property.initializer);
|
|
68
|
+
|
|
69
|
+
/** `text` as whitespace of its length and lines. */
|
|
70
|
+
const blank = (text: string) => text.replace(/[^\r\n]/g, " ");
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The edits that write a const object and its type alias as the enum they spell, or why it isn't
|
|
74
|
+
* one. Every offset and line stays where it was: setup runs the module as written, and finds what
|
|
75
|
+
* it made by those offsets.
|
|
76
|
+
*/
|
|
77
|
+
const enumEdits = (
|
|
78
|
+
source: ts.SourceFile,
|
|
79
|
+
statement: ts.VariableStatement,
|
|
80
|
+
declaration: ts.VariableDeclaration,
|
|
81
|
+
alias: ts.TypeAliasDeclaration,
|
|
82
|
+
): { edits: Edit[] } | { refusal: string; at: ts.Node } | undefined => {
|
|
83
|
+
const written =
|
|
84
|
+
declaration.initializer &&
|
|
85
|
+
!declaration.type &&
|
|
86
|
+
objectLiteral(declaration.initializer);
|
|
87
|
+
// Only an object literal is an attempt at an enum; anything else stays as it compiled before.
|
|
88
|
+
if (!written) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const { object, asConst } = written;
|
|
92
|
+
if (statement.declarationList.declarations.length > 1) {
|
|
93
|
+
return { refusal: "declare it in a const of its own.", at: declaration };
|
|
94
|
+
}
|
|
95
|
+
if (!(statement.declarationList.flags & ts.NodeFlags.Const)) {
|
|
96
|
+
return { refusal: "declare it with const.", at: declaration };
|
|
97
|
+
}
|
|
98
|
+
if (!asConst) {
|
|
99
|
+
return {
|
|
100
|
+
refusal: `declare it as const: const ${alias.name.text} = { ... } as const.`,
|
|
101
|
+
at: declaration,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const odd = object.properties.find((property) => !isMember(property));
|
|
105
|
+
if (odd) {
|
|
106
|
+
return {
|
|
107
|
+
refusal: "each member is a name and a number, such as Idle: 0.",
|
|
108
|
+
at: odd,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (
|
|
112
|
+
hasModifier(alias, ts.SyntaxKind.ExportKeyword) &&
|
|
113
|
+
!hasModifier(statement, ts.SyntaxKind.ExportKeyword)
|
|
114
|
+
) {
|
|
115
|
+
return { refusal: "export it as its type alias is.", at: declaration };
|
|
116
|
+
}
|
|
117
|
+
const blanked = (start: number, end: number): Edit => ({
|
|
118
|
+
start,
|
|
119
|
+
end,
|
|
120
|
+
text: blank(source.text.slice(start, end)),
|
|
121
|
+
});
|
|
122
|
+
const keyword = statement.declarationList.getStart(source);
|
|
123
|
+
return {
|
|
124
|
+
edits: [
|
|
125
|
+
// `const` and `enum ` are alike in length.
|
|
126
|
+
{ start: keyword, end: keyword + "const".length, text: "enum " },
|
|
127
|
+
blanked(declaration.name.end, object.getStart(source)),
|
|
128
|
+
...object.properties.filter(isMember).map((member) => {
|
|
129
|
+
const colon = member
|
|
130
|
+
.getChildren(source)
|
|
131
|
+
.find((child) => child.kind === ts.SyntaxKind.ColonToken);
|
|
132
|
+
const at = colon?.getStart(source) ?? member.name.end;
|
|
133
|
+
return { start: at, end: at + 1, text: "=" };
|
|
134
|
+
}),
|
|
135
|
+
blanked(object.end, statement.end),
|
|
136
|
+
blanked(alias.getStart(source), alias.end),
|
|
137
|
+
],
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Writes each const object that a type alias of its name reads the values of, as in
|
|
143
|
+
* `const Mode = { Idle: 0, Run: 1 } as const; type Mode = (typeof Mode)[keyof typeof Mode];`, as the
|
|
144
|
+
* numeric enum it spells, so every later pass compiles it as that enum. A server's schema may read
|
|
145
|
+
* it too, so every module is rewritten, but only the PLC program's own are refused.
|
|
146
|
+
*/
|
|
147
|
+
export const lowerConstEnums = (program: ts.Program, positions: Positions) => {
|
|
148
|
+
const sources = new Map<string, Mapped>();
|
|
149
|
+
const diagnostics: Diagnostic[] = [];
|
|
150
|
+
const plc = new Set(plcSources(program));
|
|
151
|
+
for (const source of program.getSourceFiles()) {
|
|
152
|
+
if (source.isDeclarationFile) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const aliases = new Map(
|
|
156
|
+
source.statements
|
|
157
|
+
.filter(ts.isTypeAliasDeclaration)
|
|
158
|
+
.filter(
|
|
159
|
+
(alias) =>
|
|
160
|
+
!alias.typeParameters && valuesOf(alias.type) === alias.name.text,
|
|
161
|
+
)
|
|
162
|
+
.map((alias) => [alias.name.text, alias]),
|
|
163
|
+
);
|
|
164
|
+
if (!aliases.size) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const edits: Edit[] = [];
|
|
168
|
+
for (const statement of source.statements.filter(ts.isVariableStatement)) {
|
|
169
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
170
|
+
const alias =
|
|
171
|
+
ts.isIdentifier(declaration.name) &&
|
|
172
|
+
aliases.get(declaration.name.text);
|
|
173
|
+
const result =
|
|
174
|
+
alias && enumEdits(source, statement, declaration, alias);
|
|
175
|
+
if (!alias || !result) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if ("edits" in result) {
|
|
179
|
+
edits.push(...result.edits);
|
|
180
|
+
} else if (plc.has(source)) {
|
|
181
|
+
diagnostics.push(
|
|
182
|
+
error(
|
|
183
|
+
"PLC2001",
|
|
184
|
+
`${alias.name.text} is an enum by its type alias, so ${result.refusal}`,
|
|
185
|
+
positions.node(result.at),
|
|
186
|
+
),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (edits.length) {
|
|
192
|
+
sources.set(source.fileName, edited(source, edits));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return { sources, diagnostics };
|
|
196
|
+
};
|
|
@@ -687,7 +687,15 @@ class Coroutine {
|
|
|
687
687
|
if (method === "GET" && json) {
|
|
688
688
|
this.context.fail(call, "GET requests cannot contain json.");
|
|
689
689
|
}
|
|
690
|
-
|
|
690
|
+
// A response of `void` is the request's success alone; its body is never read.
|
|
691
|
+
const responseType =
|
|
692
|
+
response.kind === ts.SyntaxKind.VoidKeyword ? null : this.text(response);
|
|
693
|
+
if (!responseType && target) {
|
|
694
|
+
this.context.fail(
|
|
695
|
+
call,
|
|
696
|
+
"A request that answers void has no value to store.",
|
|
697
|
+
);
|
|
698
|
+
}
|
|
691
699
|
const httpName = this.context.names.name(
|
|
692
700
|
`Tsplc${rpc ? "Rpc" : "Http"}`,
|
|
693
701
|
call,
|
|
@@ -702,8 +710,8 @@ class Coroutine {
|
|
|
702
710
|
from(
|
|
703
711
|
call,
|
|
704
712
|
`interface ${httpName}Input {start:BOOL;cancel:BOOL;request:${requestType}}
|
|
705
|
-
interface ${httpName}Output {busy:BOOL;done:BOOL;error:UDINT
|
|
706
|
-
class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { protected execute(input:${httpName}Input):${httpName}Output {return {busy:false,done:false,error:0
|
|
713
|
+
interface ${httpName}Output {busy:BOOL;done:BOOL;error:UDINT${responseType ? `;result:${responseType}` : ""}}
|
|
714
|
+
class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { protected execute(input:${httpName}Input):${httpName}Output {return {busy:false,done:false,error:0${responseType ? `,result:defaultValue<${responseType}>()` : ""}};} }`,
|
|
707
715
|
),
|
|
708
716
|
);
|
|
709
717
|
blocks.set(
|
|
@@ -1867,6 +1867,12 @@ export class ExpressionSpecializer {
|
|
|
1867
1867
|
const width = iec ?? (bound.kind === "code" ? bound.iec : undefined);
|
|
1868
1868
|
const layout = bound.kind === "code" ? bound.layout : undefined;
|
|
1869
1869
|
const length = bound.kind === "code" ? bound.length : undefined;
|
|
1870
|
+
if (declared.flags & ts.TypeFlags.Void) {
|
|
1871
|
+
throw new SpecializeError(
|
|
1872
|
+
name,
|
|
1873
|
+
`${name.text} holds what a call that answers nothing returns, which is no value; make the call a statement of its own.`,
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1870
1876
|
const type = width ?? layout?.text ?? this.localType(declared);
|
|
1871
1877
|
// The async lowering suspends only at a bare await, so an awaited value cannot be converted in place.
|
|
1872
1878
|
if (
|
package/src/frontend/services.ts
CHANGED
|
@@ -230,9 +230,14 @@ const serviceExportName = (path: string) =>
|
|
|
230
230
|
interface ServiceCall {
|
|
231
231
|
contract: ServiceContract;
|
|
232
232
|
input: string;
|
|
233
|
-
|
|
233
|
+
/** Null when the procedure answers nothing. */
|
|
234
|
+
output: string | null;
|
|
234
235
|
}
|
|
235
236
|
|
|
237
|
+
/** tRPC infers `void` for a procedure without `.output()` or with `z.void()`, and `undefined` with `z.undefined()`. */
|
|
238
|
+
const answersNothing = (type: ts.Type) =>
|
|
239
|
+
Boolean(type.flags & (ts.TypeFlags.Void | ts.TypeFlags.Undefined));
|
|
240
|
+
|
|
236
241
|
/** A called procedure's contract with the record names it exchanges, or why the PLC cannot call it. */
|
|
237
242
|
const serviceCall = (
|
|
238
243
|
checker: ts.TypeChecker,
|
|
@@ -248,24 +253,21 @@ const serviceCall = (
|
|
|
248
253
|
`${path}.input`,
|
|
249
254
|
false,
|
|
250
255
|
);
|
|
251
|
-
const output =
|
|
252
|
-
|
|
253
|
-
procedure.output,
|
|
254
|
-
`${path}.output`,
|
|
255
|
-
true,
|
|
256
|
-
);
|
|
256
|
+
const output = answersNothing(procedure.output)
|
|
257
|
+
? null
|
|
258
|
+
: recordLayout(checker, procedure.output, `${path}.output`, true);
|
|
257
259
|
const contract = contractCheck(() =>
|
|
258
260
|
serviceContract(
|
|
259
261
|
path,
|
|
260
262
|
kind,
|
|
261
263
|
{ fields: input.fields },
|
|
262
|
-
{ fields: output.fields },
|
|
264
|
+
output && { fields: output.fields },
|
|
263
265
|
),
|
|
264
266
|
);
|
|
265
267
|
return {
|
|
266
268
|
contract,
|
|
267
269
|
input: record(input, `${pascalServicePath(path)}Input`),
|
|
268
|
-
output: record(output, `${pascalServicePath(path)}Output`),
|
|
270
|
+
output: output && record(output, `${pascalServicePath(path)}Output`),
|
|
269
271
|
};
|
|
270
272
|
});
|
|
271
273
|
|
|
@@ -553,11 +555,12 @@ const serviceModule = (
|
|
|
553
555
|
.map(({ contract, input, output }) => {
|
|
554
556
|
const hasInput = Object.keys(contract.input.fields).length > 0;
|
|
555
557
|
const request = hasInput ? "input" : `defaultValue<${input}>()`;
|
|
558
|
+
const answer = output ?? "void";
|
|
556
559
|
const operation =
|
|
557
560
|
options.transport === "http"
|
|
558
|
-
? `tsplcServiceFetch<${
|
|
559
|
-
: `tsplcServiceCall<${
|
|
560
|
-
return `export const ${serviceExportName(contract.path)}=async(${hasInput ? `input:${input}` : ""}):Promise<${
|
|
561
|
+
? `tsplcServiceFetch<${answer}>(\`\${tsplcServiceBaseUrl}${servicePath(contract)}\`,{method:"POST",json:${request}})`
|
|
562
|
+
: `tsplcServiceCall<${answer},${input}>(${JSON.stringify(servicePath(contract))},${request})`;
|
|
563
|
+
return `export const ${serviceExportName(contract.path)}=async(${hasInput ? `input:${input}` : ""}):Promise<${answer}>=>{${output ? "return " : ""}await ${operation};};`;
|
|
561
564
|
});
|
|
562
565
|
const transport =
|
|
563
566
|
options.transport === "http"
|
|
@@ -8,6 +8,7 @@ import ts from "typescript";
|
|
|
8
8
|
import { Analyzer, suppressionDiagnostics } from "./analyzer";
|
|
9
9
|
import { type AsyncLowering, lowerAsync } from "./async";
|
|
10
10
|
import { lowerEnvironment } from "./configuration";
|
|
11
|
+
import { lowerConstEnums } from "./const-enums";
|
|
11
12
|
import { lowerConstFunctions } from "./const-functions";
|
|
12
13
|
import { lowerControlFlow } from "./control-flow";
|
|
13
14
|
import { dialectUses } from "./dialect-names";
|
|
@@ -169,6 +170,12 @@ function compileFrontend(
|
|
|
169
170
|
// Nothing is emitted, and a service client's router type imports modules outside these (TS6059).
|
|
170
171
|
delete compilerOptions.rootDir;
|
|
171
172
|
delete compilerOptions.outDir;
|
|
173
|
+
// Lowering writes enums, such as for a const object a type alias reads as one, which a project
|
|
174
|
+
// that allows only erasable syntax refuses in its own source.
|
|
175
|
+
const loweredOptions: ts.CompilerOptions = {
|
|
176
|
+
...compilerOptions,
|
|
177
|
+
erasableSyntaxOnly: false,
|
|
178
|
+
};
|
|
172
179
|
const sdk = sdkFor(filename, compilerOptions);
|
|
173
180
|
const rootNames = [
|
|
174
181
|
...new Set([...projectFiles, ...programFiles, sdk, dialectFor(sdk)]),
|
|
@@ -254,7 +261,7 @@ function compileFrontend(
|
|
|
254
261
|
}
|
|
255
262
|
const lowered = ts.createProgram({
|
|
256
263
|
rootNames,
|
|
257
|
-
options:
|
|
264
|
+
options: loweredOptions,
|
|
258
265
|
host,
|
|
259
266
|
oldProgram: program,
|
|
260
267
|
});
|
|
@@ -274,6 +281,7 @@ function compileFrontend(
|
|
|
274
281
|
);
|
|
275
282
|
return lowered;
|
|
276
283
|
});
|
|
284
|
+
lowerings.run((program) => lowerConstEnums(program, positions));
|
|
277
285
|
// A plain program's setup reads the configuration this resolves. The pass runs again below for
|
|
278
286
|
// the reads that the services pass generates.
|
|
279
287
|
const configuration = lowerings.run((program) =>
|