@tsplc/tmp-compiler 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- 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 +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { type Diagnostic, error } from "@tsplc/compiler/ir";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { plcSources } from "./module-graph";
|
|
4
|
+
import {
|
|
5
|
+
copied,
|
|
6
|
+
type Edit,
|
|
7
|
+
edited,
|
|
8
|
+
from,
|
|
9
|
+
joined,
|
|
10
|
+
type Mapped,
|
|
11
|
+
mapped,
|
|
12
|
+
type Part,
|
|
13
|
+
type Positions,
|
|
14
|
+
} from "./positions";
|
|
15
|
+
import { hasModifier } from "./types";
|
|
16
|
+
|
|
17
|
+
/** An arrow body as a function body: a block as written, or its expression returned (or, for void, evaluated). */
|
|
18
|
+
const functionBody = (
|
|
19
|
+
body: ts.ConciseBody,
|
|
20
|
+
result: ts.TypeNode | undefined,
|
|
21
|
+
): Part => {
|
|
22
|
+
if (ts.isBlock(body)) {
|
|
23
|
+
return copied(body);
|
|
24
|
+
}
|
|
25
|
+
return result?.kind === ts.SyntaxKind.VoidKeyword
|
|
26
|
+
? mapped`{ ${copied(body)}; }`
|
|
27
|
+
: mapped`{ return ${copied(body)}; }`;
|
|
28
|
+
};
|
|
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
|
+
*/
|
|
75
|
+
export const lowerConstFunctions = (
|
|
76
|
+
program: ts.Program,
|
|
77
|
+
positions: Positions,
|
|
78
|
+
) => {
|
|
79
|
+
const checker = program.getTypeChecker();
|
|
80
|
+
const printer = ts.createPrinter();
|
|
81
|
+
const sources = new Map<string, Mapped>();
|
|
82
|
+
const diagnostics: Diagnostic[] = [];
|
|
83
|
+
|
|
84
|
+
for (const source of plcSources(program)) {
|
|
85
|
+
const edits = voidAsyncAnnotations(source, checker);
|
|
86
|
+
for (const statement of source.statements) {
|
|
87
|
+
if (
|
|
88
|
+
!ts.isVariableStatement(statement) ||
|
|
89
|
+
!statement.declarationList.declarations.some(
|
|
90
|
+
(declaration) =>
|
|
91
|
+
declaration.initializer &&
|
|
92
|
+
ts.isArrowFunction(declaration.initializer),
|
|
93
|
+
)
|
|
94
|
+
) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const print = (node: ts.Node) =>
|
|
99
|
+
printer.printNode(ts.EmitHint.Unspecified, node, source);
|
|
100
|
+
const declarations: Mapped[] = [];
|
|
101
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
102
|
+
const arrow = declaration.initializer;
|
|
103
|
+
if (!arrow || !ts.isArrowFunction(arrow)) {
|
|
104
|
+
declarations.push(
|
|
105
|
+
from(
|
|
106
|
+
declaration,
|
|
107
|
+
print(
|
|
108
|
+
ts.factory.updateVariableStatement(
|
|
109
|
+
statement,
|
|
110
|
+
statement.modifiers,
|
|
111
|
+
ts.factory.updateVariableDeclarationList(
|
|
112
|
+
statement.declarationList,
|
|
113
|
+
[declaration],
|
|
114
|
+
),
|
|
115
|
+
),
|
|
116
|
+
),
|
|
117
|
+
),
|
|
118
|
+
);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (
|
|
122
|
+
!(statement.declarationList.flags & ts.NodeFlags.Const) ||
|
|
123
|
+
!ts.isIdentifier(declaration.name)
|
|
124
|
+
) {
|
|
125
|
+
diagnostics.push(
|
|
126
|
+
error(
|
|
127
|
+
"PLC1007",
|
|
128
|
+
"PLC routines require named const bindings; mutable function values are unsupported.",
|
|
129
|
+
positions.node(declaration),
|
|
130
|
+
),
|
|
131
|
+
);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const contextual =
|
|
136
|
+
declaration.type && ts.isFunctionTypeNode(declaration.type)
|
|
137
|
+
? declaration.type
|
|
138
|
+
: undefined;
|
|
139
|
+
const signature = checker
|
|
140
|
+
.getTypeAtLocation(declaration.name)
|
|
141
|
+
.getCallSignatures()[0];
|
|
142
|
+
const returnsVoid =
|
|
143
|
+
signature &&
|
|
144
|
+
Boolean(
|
|
145
|
+
checker.getReturnTypeOfSignature(signature).flags &
|
|
146
|
+
ts.TypeFlags.Void,
|
|
147
|
+
);
|
|
148
|
+
const inferredResult =
|
|
149
|
+
signature &&
|
|
150
|
+
checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
151
|
+
const asyncVoid =
|
|
152
|
+
hasModifier(arrow, ts.SyntaxKind.AsyncKeyword) &&
|
|
153
|
+
inferredResult &&
|
|
154
|
+
inferredResult.flags & ts.TypeFlags.Void;
|
|
155
|
+
const inferredType = asyncVoid
|
|
156
|
+
? ts.factory.createTypeReferenceNode("Promise", [
|
|
157
|
+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword),
|
|
158
|
+
])
|
|
159
|
+
: undefined;
|
|
160
|
+
const result =
|
|
161
|
+
arrow.type ??
|
|
162
|
+
contextual?.type ??
|
|
163
|
+
(returnsVoid
|
|
164
|
+
? ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)
|
|
165
|
+
: inferredType);
|
|
166
|
+
const parameters = arrow.parameters.map((parameter, index) =>
|
|
167
|
+
ts.factory.updateParameterDeclaration(
|
|
168
|
+
parameter,
|
|
169
|
+
parameter.modifiers,
|
|
170
|
+
parameter.dotDotDotToken,
|
|
171
|
+
parameter.name,
|
|
172
|
+
parameter.questionToken,
|
|
173
|
+
parameter.type ?? contextual?.parameters[index]?.type,
|
|
174
|
+
parameter.initializer,
|
|
175
|
+
),
|
|
176
|
+
);
|
|
177
|
+
// The header is printed; the body stays the user's text, so its positions map exactly.
|
|
178
|
+
const head = print(
|
|
179
|
+
ts.factory.createFunctionDeclaration(
|
|
180
|
+
[...(statement.modifiers ?? []), ...(arrow.modifiers ?? [])],
|
|
181
|
+
undefined,
|
|
182
|
+
declaration.name,
|
|
183
|
+
arrow.typeParameters ?? contextual?.typeParameters,
|
|
184
|
+
parameters,
|
|
185
|
+
result,
|
|
186
|
+
ts.factory.createBlock([]),
|
|
187
|
+
),
|
|
188
|
+
).replace(/\{\s*\}$/, "");
|
|
189
|
+
declarations.push(
|
|
190
|
+
from(declaration, mapped`${head}${functionBody(arrow.body, result)}`),
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
edits.push({
|
|
194
|
+
start: statement.getStart(source),
|
|
195
|
+
end: statement.end,
|
|
196
|
+
text: joined(declarations, "\n"),
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (edits.length) {
|
|
200
|
+
sources.set(source.fileName, edited(source, edits));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return { sources, diagnostics };
|
|
204
|
+
};
|