@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,659 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import { type Diagnostic, hasErrors, type PlcModule } from "@tsplc/compiler/ir";
|
|
5
|
+
import type { Environment } from "@tsplc/config";
|
|
6
|
+
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
7
|
+
import ts from "typescript";
|
|
8
|
+
import { Analyzer, suppressionDiagnostics } from "./analyzer";
|
|
9
|
+
import { type AsyncLowering, lowerAsync } from "./async";
|
|
10
|
+
import { lowerEnvironment } from "./configuration";
|
|
11
|
+
import { lowerConstEnums } from "./const-enums";
|
|
12
|
+
import { lowerConstFunctions } from "./const-functions";
|
|
13
|
+
import { lowerControlFlow } from "./control-flow";
|
|
14
|
+
import { dialectUses } from "./dialect-names";
|
|
15
|
+
import { lowerElaboration } from "./elaborate/index";
|
|
16
|
+
import { declarationKey, typeKey } from "./lowering-key";
|
|
17
|
+
import { runtimeSources } from "./module-graph";
|
|
18
|
+
import { type Mapped, Positions, sourceSpan } from "./positions";
|
|
19
|
+
import { lowerPromises } from "./promises";
|
|
20
|
+
import { dialectFor, inSdk, sdkFor } from "./sdk";
|
|
21
|
+
import { lowerServices, type ServiceOptions, serviceRecords } from "./services";
|
|
22
|
+
|
|
23
|
+
/** What a compilation writes or adds to help debug the compiler itself. */
|
|
24
|
+
export interface CompileDebug {
|
|
25
|
+
/** Writes every module the compiler generates or rewrites here, as the next step reads it. */
|
|
26
|
+
lowered: string | null;
|
|
27
|
+
/** Adds stack traces to elaboration errors. */
|
|
28
|
+
stackTraces: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The documented switches: `TSPLC_LOWERED=<directory>` and `TSPLC_DEBUG=1`. */
|
|
32
|
+
const compileDebug = (environment: Environment): CompileDebug => ({
|
|
33
|
+
lowered: environment.TSPLC_LOWERED || null,
|
|
34
|
+
stackTraces: Boolean(environment.TSPLC_DEBUG),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export interface FrontendOptions {
|
|
38
|
+
sourceScope?: "project" | "entry";
|
|
39
|
+
services?: ServiceOptions;
|
|
40
|
+
tsconfig: string;
|
|
41
|
+
programs: Readonly<Record<string, { module: string; export: string }>>;
|
|
42
|
+
/** What `env()` reads, and the debug switches; this process's environment by default. */
|
|
43
|
+
environment?: Environment;
|
|
44
|
+
/**
|
|
45
|
+
* The state paths the config's `io` binds. The hardware writes them, so the object each starts
|
|
46
|
+
* from is state even where scan code only reads it.
|
|
47
|
+
*/
|
|
48
|
+
io: readonly BoundPath[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A state path the config's `io` binds, and the input address the hardware writes it from. */
|
|
52
|
+
export interface BoundPath {
|
|
53
|
+
path: string;
|
|
54
|
+
/** The `%I` address of an input, which scan code can't assign; null for an output. */
|
|
55
|
+
input: string | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface FrontendResult {
|
|
59
|
+
module?: PlcModule;
|
|
60
|
+
diagnostics: readonly Diagnostic[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function tsDiagnostic(
|
|
64
|
+
diagnostic: ts.Diagnostic,
|
|
65
|
+
locate: typeof sourceSpan,
|
|
66
|
+
): Diagnostic {
|
|
67
|
+
const result: Diagnostic = {
|
|
68
|
+
origin: "typescript",
|
|
69
|
+
code: `TS${diagnostic.code}`,
|
|
70
|
+
severity:
|
|
71
|
+
diagnostic.category === ts.DiagnosticCategory.Error ? "error" : "warning",
|
|
72
|
+
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
|
|
73
|
+
};
|
|
74
|
+
if (diagnostic.file && diagnostic.start !== undefined) {
|
|
75
|
+
result.span = locate(
|
|
76
|
+
diagnostic.file,
|
|
77
|
+
diagnostic.start,
|
|
78
|
+
diagnostic.start + (diagnostic.length ?? 1),
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Type checks a subset of a tsconfig's files without emitting. A project-reference
|
|
86
|
+
* tsconfig's composite and declaration settings would demand every file be listed
|
|
87
|
+
* (TS6307) or conflict with noEmit, so these replace them.
|
|
88
|
+
*/
|
|
89
|
+
export const typeCheckOnly: ts.CompilerOptions = {
|
|
90
|
+
composite: false,
|
|
91
|
+
declaration: false,
|
|
92
|
+
declarationMap: false,
|
|
93
|
+
emitDeclarationOnly: false,
|
|
94
|
+
incremental: false,
|
|
95
|
+
noEmit: true,
|
|
96
|
+
tsBuildInfoFile: undefined,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Compiles a program's PLC TypeScript into the PLC model: plain TypeScript, never the dialect. */
|
|
100
|
+
export function analyze(options: FrontendOptions): FrontendResult {
|
|
101
|
+
return compileFrontend(options, "rejected");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Compiles source that may be written in the tsPLC dialect, the language plain programs lower
|
|
106
|
+
* into. No command reaches it: the compiler's tests write the dialect to drive its later stages.
|
|
107
|
+
*/
|
|
108
|
+
export function analyzeDialect(options: FrontendOptions): FrontendResult {
|
|
109
|
+
return compileFrontend(options, "accepted");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The frontend. With the dialect `rejected`, every program is plain TypeScript that elaboration
|
|
114
|
+
* compiles from its setup; `accepted`, a program elaborates only when it declares a plain class.
|
|
115
|
+
*/
|
|
116
|
+
function compileFrontend(
|
|
117
|
+
options: FrontendOptions,
|
|
118
|
+
dialect: "rejected" | "accepted",
|
|
119
|
+
): FrontendResult {
|
|
120
|
+
const environment = options.environment ?? { ...process.env };
|
|
121
|
+
const debug = compileDebug(environment);
|
|
122
|
+
const filename = resolve(options.tsconfig);
|
|
123
|
+
const root = dirname(filename);
|
|
124
|
+
const config = ts.readConfigFile(filename, ts.sys.readFile);
|
|
125
|
+
if (config.error) {
|
|
126
|
+
return { diagnostics: [tsDiagnostic(config.error, sourceSpan)] };
|
|
127
|
+
}
|
|
128
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
129
|
+
config.config,
|
|
130
|
+
ts.sys,
|
|
131
|
+
root,
|
|
132
|
+
undefined,
|
|
133
|
+
filename,
|
|
134
|
+
);
|
|
135
|
+
if (parsed.errors.length) {
|
|
136
|
+
return {
|
|
137
|
+
diagnostics: parsed.errors.map((diagnostic) =>
|
|
138
|
+
tsDiagnostic(diagnostic, sourceSpan),
|
|
139
|
+
),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
const programFiles = Object.values(options.programs).map((entry) =>
|
|
143
|
+
resolve(root, entry.module),
|
|
144
|
+
);
|
|
145
|
+
// Full-stack builds follow entry imports; standalone projects retain explicit tsconfig globals.
|
|
146
|
+
const scope = options.sourceScope ?? (options.services ? "entry" : "project");
|
|
147
|
+
// The project's own ambient declarations; a shared base tsconfig's typings are for the host runtime.
|
|
148
|
+
const ambient = parsed.fileNames.filter(
|
|
149
|
+
(file) => /\.d\.[cm]?ts$/.test(file) && file.startsWith(`${root}/`),
|
|
150
|
+
);
|
|
151
|
+
const projectFiles = scope === "project" ? parsed.fileNames : ambient;
|
|
152
|
+
const compilerOptions: ts.CompilerOptions = {
|
|
153
|
+
...parsed.options,
|
|
154
|
+
...typeCheckOnly,
|
|
155
|
+
// Lowered sources import their runtime helpers by .ts path and drop the imports they replace.
|
|
156
|
+
allowImportingTsExtensions: true,
|
|
157
|
+
noUnusedLocals: false,
|
|
158
|
+
noUnusedParameters: false,
|
|
159
|
+
strict: true,
|
|
160
|
+
noImplicitAny: true,
|
|
161
|
+
strictNullChecks: true,
|
|
162
|
+
noCheck: false,
|
|
163
|
+
allowJs: false,
|
|
164
|
+
skipLibCheck: false,
|
|
165
|
+
types: [],
|
|
166
|
+
lib: ["lib.es2025.d.ts"],
|
|
167
|
+
module: ts.ModuleKind.ESNext,
|
|
168
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
169
|
+
};
|
|
170
|
+
// Nothing is emitted, and a service client's router type imports modules outside these (TS6059).
|
|
171
|
+
delete compilerOptions.rootDir;
|
|
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
|
+
};
|
|
179
|
+
const sdk = sdkFor(filename, compilerOptions);
|
|
180
|
+
const rootNames = [
|
|
181
|
+
...new Set([...projectFiles, ...programFiles, sdk, dialectFor(sdk)]),
|
|
182
|
+
];
|
|
183
|
+
const virtualSources = new Map<string, string>();
|
|
184
|
+
// Lowering also adds modules, such as the service client, that exist only here.
|
|
185
|
+
const host = loweringHost(compilerOptions, virtualSources);
|
|
186
|
+
const program = ts.createProgram(rootNames, compilerOptions, host);
|
|
187
|
+
const plcFiles = (program: ts.Program) => {
|
|
188
|
+
const files = new Set(runtimeSources(program));
|
|
189
|
+
for (const source of program.getSourceFiles()) {
|
|
190
|
+
if (inSdk(sdk, source.fileName)) {
|
|
191
|
+
files.add(source);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return files;
|
|
195
|
+
};
|
|
196
|
+
const positions = new Positions();
|
|
197
|
+
const generated = (file: ts.SourceFile) =>
|
|
198
|
+
virtualSources.has(file.fileName) &&
|
|
199
|
+
file.fileName.includes(`/${generatedDirectory}/`);
|
|
200
|
+
// Only PLC files: a type-only import of the server router brings the server's sources along.
|
|
201
|
+
const sourceDiagnostics = (
|
|
202
|
+
program: ts.Program,
|
|
203
|
+
files: ReadonlySet<ts.SourceFile>,
|
|
204
|
+
) =>
|
|
205
|
+
[
|
|
206
|
+
...program.getConfigFileParsingDiagnostics(),
|
|
207
|
+
...program.getOptionsDiagnostics(),
|
|
208
|
+
...program.getGlobalDiagnostics(),
|
|
209
|
+
...[...files].flatMap((file) => [
|
|
210
|
+
...program.getSyntacticDiagnostics(file),
|
|
211
|
+
...program.getSemanticDiagnostics(file),
|
|
212
|
+
]),
|
|
213
|
+
]
|
|
214
|
+
.filter(
|
|
215
|
+
(diagnostic) => !storesGeneratedGlobal(program, diagnostic, generated),
|
|
216
|
+
)
|
|
217
|
+
.map((diagnostic) => tsDiagnostic(diagnostic, positions.span));
|
|
218
|
+
const files = plcFiles(program);
|
|
219
|
+
const diagnostics = sourceDiagnostics(program, files);
|
|
220
|
+
if (
|
|
221
|
+
program
|
|
222
|
+
.getSyntacticDiagnostics()
|
|
223
|
+
.some((item) => !item.file || files.has(item.file))
|
|
224
|
+
) {
|
|
225
|
+
return { diagnostics };
|
|
226
|
+
}
|
|
227
|
+
if (dialect === "rejected") {
|
|
228
|
+
const uses = dialectUses(program, sdk, positions);
|
|
229
|
+
if (uses.length) {
|
|
230
|
+
return { diagnostics: [...diagnostics, ...uses] };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
// Only shown when compiling fails, which one of them may explain.
|
|
234
|
+
const lost: Diagnostic[] = [];
|
|
235
|
+
const lowerings = new LoweringPipeline(program, (program, lowering) => {
|
|
236
|
+
diagnostics.push(...lowering.diagnostics);
|
|
237
|
+
for (const source of runtimeSources(program)) {
|
|
238
|
+
if (lowering.sources.has(source.fileName)) {
|
|
239
|
+
diagnostics.push(...suppressionDiagnostics(source, positions));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (
|
|
243
|
+
lowering.diagnostics.length ||
|
|
244
|
+
(lowering.sources.size && hasErrors(diagnostics))
|
|
245
|
+
) {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
if (!lowering.sources.size) {
|
|
249
|
+
return program;
|
|
250
|
+
}
|
|
251
|
+
positions.record(lowering.sources, program);
|
|
252
|
+
for (const [path, text] of lowering.sources) {
|
|
253
|
+
virtualSources.set(path, text.text);
|
|
254
|
+
}
|
|
255
|
+
if (debug.lowered) {
|
|
256
|
+
for (const [path, text] of lowering.sources) {
|
|
257
|
+
const target = join(debug.lowered, relative(root, path));
|
|
258
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
259
|
+
writeFileSync(target, text.text);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const lowered = ts.createProgram({
|
|
263
|
+
rootNames,
|
|
264
|
+
options: loweredOptions,
|
|
265
|
+
host,
|
|
266
|
+
oldProgram: program,
|
|
267
|
+
});
|
|
268
|
+
const plc = plcFiles(lowered);
|
|
269
|
+
diagnostics.push(...sourceDiagnostics(lowered, plc));
|
|
270
|
+
lost.push(
|
|
271
|
+
...lostImports({
|
|
272
|
+
before: program,
|
|
273
|
+
after: lowered,
|
|
274
|
+
rewritten: lowering.sources,
|
|
275
|
+
plc: new Set(
|
|
276
|
+
[...plc, ...plcFiles(program)].map(({ fileName }) => fileName),
|
|
277
|
+
),
|
|
278
|
+
root,
|
|
279
|
+
positions,
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
return lowered;
|
|
283
|
+
});
|
|
284
|
+
lowerings.run((program) => lowerConstEnums(program, positions));
|
|
285
|
+
// A plain program's setup reads the configuration this resolves. The pass runs again below for
|
|
286
|
+
// the reads that the services pass generates.
|
|
287
|
+
const configuration = lowerings.run((program) =>
|
|
288
|
+
lowerEnvironment(program, sdk, positions, environment),
|
|
289
|
+
);
|
|
290
|
+
const elaboration =
|
|
291
|
+
configuration &&
|
|
292
|
+
lowerings.run((program) =>
|
|
293
|
+
lowerElaboration(program, sdk, options.programs, root, positions, {
|
|
294
|
+
debug,
|
|
295
|
+
configuration: configuration.read,
|
|
296
|
+
always: dialect === "rejected",
|
|
297
|
+
io: options.io,
|
|
298
|
+
}),
|
|
299
|
+
);
|
|
300
|
+
lowerings.run(lowerControlFlow);
|
|
301
|
+
const services = lowerings.run((program) =>
|
|
302
|
+
lowerServices(program, sdk, positions, options.services),
|
|
303
|
+
);
|
|
304
|
+
lowerings.run((program) =>
|
|
305
|
+
lowerEnvironment(program, sdk, positions, environment),
|
|
306
|
+
);
|
|
307
|
+
lowerings.run((program) => lowerConstFunctions(program, positions));
|
|
308
|
+
const promises = lowerings.run((program) =>
|
|
309
|
+
lowerPromises(program, sdk, positions, root),
|
|
310
|
+
);
|
|
311
|
+
const lowering = lowerings.run((program) =>
|
|
312
|
+
lowerAsync(program, sdk, positions, root),
|
|
313
|
+
);
|
|
314
|
+
const lowered = lowerings.program;
|
|
315
|
+
if (!lowered || !elaboration || !services || !promises || !lowering) {
|
|
316
|
+
return { diagnostics: [...diagnostics, ...lost] };
|
|
317
|
+
}
|
|
318
|
+
const analyzer = new Analyzer(
|
|
319
|
+
lowered,
|
|
320
|
+
host,
|
|
321
|
+
root,
|
|
322
|
+
sdk,
|
|
323
|
+
diagnostics,
|
|
324
|
+
positions,
|
|
325
|
+
serviceRecords(lowered, services),
|
|
326
|
+
elaboration.reduced,
|
|
327
|
+
);
|
|
328
|
+
const module = analyzer.run({ ...options.programs, ...lowering.programs });
|
|
329
|
+
attachLowerings(module, root, { elaboration, promises, lowering });
|
|
330
|
+
return hasErrors(diagnostics)
|
|
331
|
+
? { diagnostics: [...diagnostics, ...lost] }
|
|
332
|
+
: { module, diagnostics };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Whether a diagnostic is TypeScript refusing a lowered module's write to a global of a module
|
|
337
|
+
* elaboration generated, through its namespace import. A namespace import is read-only in
|
|
338
|
+
* TypeScript, but each generated module is a global variable list, and the PLC assigns its
|
|
339
|
+
* variables from any program.
|
|
340
|
+
*/
|
|
341
|
+
function storesGeneratedGlobal(
|
|
342
|
+
program: ts.Program,
|
|
343
|
+
diagnostic: ts.Diagnostic,
|
|
344
|
+
generated: (file: ts.SourceFile) => boolean,
|
|
345
|
+
): boolean {
|
|
346
|
+
const { file, start, length } = diagnostic;
|
|
347
|
+
if (
|
|
348
|
+
diagnostic.code !== readOnlyAssignment ||
|
|
349
|
+
!file ||
|
|
350
|
+
start === undefined ||
|
|
351
|
+
length === undefined
|
|
352
|
+
) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
const target = reportedAccess(file, start, start + length);
|
|
356
|
+
if (!target || !ts.isIdentifier(target.expression)) {
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
const checker = program.getTypeChecker();
|
|
360
|
+
const namespace = checker
|
|
361
|
+
.getSymbolAtLocation(target.expression)
|
|
362
|
+
?.declarations?.find(ts.isNamespaceImport);
|
|
363
|
+
const source =
|
|
364
|
+
namespace &&
|
|
365
|
+
checker.getSymbolAtLocation(namespace.parent.parent.moduleSpecifier)
|
|
366
|
+
?.valueDeclaration;
|
|
367
|
+
return Boolean(source && ts.isSourceFile(source) && generated(source));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** TypeScript's errors for an import that names something its module doesn't export. */
|
|
371
|
+
const missingExportCodes: ReadonlySet<number> = new Set([
|
|
372
|
+
2305, 2459, 2460, 2614, 2724,
|
|
373
|
+
]);
|
|
374
|
+
|
|
375
|
+
/** The file an import or re-export's module specifier resolves to, if it resolves to a source file. */
|
|
376
|
+
function importedFile(
|
|
377
|
+
checker: ts.TypeChecker,
|
|
378
|
+
statement: ts.Statement,
|
|
379
|
+
): string | undefined {
|
|
380
|
+
if (
|
|
381
|
+
!(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) ||
|
|
382
|
+
!statement.moduleSpecifier
|
|
383
|
+
) {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
const module = checker.getSymbolAtLocation(
|
|
387
|
+
statement.moduleSpecifier,
|
|
388
|
+
)?.valueDeclaration;
|
|
389
|
+
return module && ts.isSourceFile(module) ? module.fileName : undefined;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Imports a lowering pass broke in files tsPLC doesn't compile, such as a server's router: the
|
|
394
|
+
* pass rewrote a module and dropped a name the file still imports. Only PLC files' TypeScript
|
|
395
|
+
* errors are reported, so without this a server schema reads the lost name as nothing, and the
|
|
396
|
+
* error surfaces far from its cause. The server itself runs the module as written, so a lost name
|
|
397
|
+
* matters only where tsPLC reads that file's types: these are warnings, shown beside the errors
|
|
398
|
+
* they may explain.
|
|
399
|
+
*/
|
|
400
|
+
function lostImports({
|
|
401
|
+
before,
|
|
402
|
+
after,
|
|
403
|
+
rewritten,
|
|
404
|
+
plc,
|
|
405
|
+
root,
|
|
406
|
+
positions,
|
|
407
|
+
}: {
|
|
408
|
+
before: ts.Program;
|
|
409
|
+
after: ts.Program;
|
|
410
|
+
rewritten: ReadonlyMap<string, Mapped>;
|
|
411
|
+
/** The PLC program's files before the pass and after it, whose errors are reported anyway. */
|
|
412
|
+
plc: ReadonlySet<string>;
|
|
413
|
+
root: string;
|
|
414
|
+
positions: Positions;
|
|
415
|
+
}): Diagnostic[] {
|
|
416
|
+
const checker = after.getTypeChecker();
|
|
417
|
+
return after.getSourceFiles().flatMap((file) => {
|
|
418
|
+
const imports = file.statements.flatMap((statement) => {
|
|
419
|
+
const imported = importedFile(checker, statement);
|
|
420
|
+
return imported && rewritten.has(imported)
|
|
421
|
+
? [{ statement, imported }]
|
|
422
|
+
: [];
|
|
423
|
+
});
|
|
424
|
+
const original = before.getSourceFile(file.fileName);
|
|
425
|
+
if (
|
|
426
|
+
!imports.length ||
|
|
427
|
+
!original ||
|
|
428
|
+
file.isDeclarationFile ||
|
|
429
|
+
plc.has(file.fileName) ||
|
|
430
|
+
rewritten.has(file.fileName)
|
|
431
|
+
) {
|
|
432
|
+
return [];
|
|
433
|
+
}
|
|
434
|
+
const missing = (program: ts.Program, source: ts.SourceFile) =>
|
|
435
|
+
program
|
|
436
|
+
.getSemanticDiagnostics(source)
|
|
437
|
+
.filter(
|
|
438
|
+
(diagnostic) =>
|
|
439
|
+
missingExportCodes.has(diagnostic.code) &&
|
|
440
|
+
diagnostic.start !== undefined,
|
|
441
|
+
);
|
|
442
|
+
const known = new Set(
|
|
443
|
+
missing(before, original).map(({ start, code }) => `${start}:${code}`),
|
|
444
|
+
);
|
|
445
|
+
return missing(after, file).flatMap(({ start = 0, length = 1, code }) => {
|
|
446
|
+
const broken = imports.find(
|
|
447
|
+
({ statement }) => statement.pos <= start && start < statement.end,
|
|
448
|
+
);
|
|
449
|
+
if (!broken || known.has(`${start}:${code}`)) {
|
|
450
|
+
return [];
|
|
451
|
+
}
|
|
452
|
+
const name = file.text.slice(start, start + length);
|
|
453
|
+
return [
|
|
454
|
+
{
|
|
455
|
+
origin: "plc" as const,
|
|
456
|
+
code: "PLC1016",
|
|
457
|
+
severity: "warning" as const,
|
|
458
|
+
message: `tsPLC dropped ${name} from ${relative(root, broken.imported)} while compiling the PLC program, but ${relative(root, file.fileName)} imports it, so tsPLC reads that file's types without it. If an error here is in what that file declares, this is its cause, and a tsPLC bug.`,
|
|
459
|
+
span: positions.span(file, start, start + length),
|
|
460
|
+
},
|
|
461
|
+
];
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** TS2540: "Cannot assign to '…' because it is a read-only property." */
|
|
467
|
+
const readOnlyAssignment = 2540;
|
|
468
|
+
|
|
469
|
+
/** The property access whose name a diagnostic spans from `start` to `end`, if it spans one. */
|
|
470
|
+
function reportedAccess(
|
|
471
|
+
file: ts.SourceFile,
|
|
472
|
+
start: number,
|
|
473
|
+
end: number,
|
|
474
|
+
): ts.PropertyAccessExpression | undefined {
|
|
475
|
+
const visit = (node: ts.Node): ts.PropertyAccessExpression | undefined => {
|
|
476
|
+
if (
|
|
477
|
+
ts.isPropertyAccessExpression(node) &&
|
|
478
|
+
node.name.getStart(file) === start &&
|
|
479
|
+
node.name.end === end
|
|
480
|
+
) {
|
|
481
|
+
return node;
|
|
482
|
+
}
|
|
483
|
+
return node.pos <= start && end <= node.end
|
|
484
|
+
? ts.forEachChild(node, visit)
|
|
485
|
+
: undefined;
|
|
486
|
+
};
|
|
487
|
+
return ts.forEachChild(file, visit);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** What every lowering pass returns: the modules it rewrote or generated, and what it rejected. */
|
|
491
|
+
interface Lowering {
|
|
492
|
+
sources: ReadonlyMap<string, Mapped>;
|
|
493
|
+
diagnostics: readonly Diagnostic[];
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Runs the lowering passes in order, each over the program the one before it produced. `next`
|
|
498
|
+
* takes a pass's result to the program the following pass reads, or to undefined when the pass
|
|
499
|
+
* failed; the passes after a failure do not run.
|
|
500
|
+
*/
|
|
501
|
+
class LoweringPipeline {
|
|
502
|
+
private current: ts.Program | undefined;
|
|
503
|
+
|
|
504
|
+
constructor(
|
|
505
|
+
program: ts.Program,
|
|
506
|
+
private readonly next: (
|
|
507
|
+
program: ts.Program,
|
|
508
|
+
lowering: Lowering,
|
|
509
|
+
) => ts.Program | undefined,
|
|
510
|
+
) {
|
|
511
|
+
this.current = program;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** The program the passes produced; undefined once one has failed. */
|
|
515
|
+
get program(): ts.Program | undefined {
|
|
516
|
+
return this.current;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** The result of `pass` over the current program; undefined when it or an earlier pass failed. */
|
|
520
|
+
run<T extends Lowering>(pass: (program: ts.Program) => T): T | undefined {
|
|
521
|
+
if (!this.current) {
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
const lowering = pass(this.current);
|
|
525
|
+
this.current = this.next(this.current, lowering);
|
|
526
|
+
return this.current && lowering;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* The compiler host of every lowering pass: lowered and generated modules are read from
|
|
532
|
+
* `virtualSources`, and everything else from disk. Each pass builds a new program, so the host
|
|
533
|
+
* parses a file again only when its text changed, and the program reuses its predecessor's module
|
|
534
|
+
* structure; a pass then costs its type check rather than reparsing thousands of declarations.
|
|
535
|
+
*/
|
|
536
|
+
function loweringHost(
|
|
537
|
+
options: ts.CompilerOptions,
|
|
538
|
+
virtualSources: ReadonlyMap<string, string>,
|
|
539
|
+
): ts.CompilerHost {
|
|
540
|
+
const host = ts.createCompilerHost(options);
|
|
541
|
+
const read = host.readFile;
|
|
542
|
+
const exists = host.fileExists;
|
|
543
|
+
const directoryExists = host.directoryExists?.bind(host);
|
|
544
|
+
const parse = host.getSourceFile;
|
|
545
|
+
host.readFile = (path) => virtualSources.get(path) ?? read(path);
|
|
546
|
+
host.fileExists = (path) => virtualSources.has(path) || exists(path);
|
|
547
|
+
// Generated modules can live in a directory that only exists in memory.
|
|
548
|
+
host.directoryExists = (path) =>
|
|
549
|
+
[...virtualSources.keys()].some((file) =>
|
|
550
|
+
file.startsWith(`${path.replace(/\/$/, "")}/`),
|
|
551
|
+
) ||
|
|
552
|
+
(directoryExists?.(path) ?? true);
|
|
553
|
+
const parsed = new Map<string, ts.SourceFile>();
|
|
554
|
+
host.getSourceFile = (fileName, version, onError, createNew) => {
|
|
555
|
+
const key = `${fileName}\0${typeof version === "object" ? `${version.languageVersion}:${version.impliedNodeFormat}` : version}`;
|
|
556
|
+
const known = parsed.get(key);
|
|
557
|
+
const lowered = virtualSources.get(fileName);
|
|
558
|
+
// A rewritten module never returns to its file on disk, so a file without lowered text is unchanged.
|
|
559
|
+
if (
|
|
560
|
+
known &&
|
|
561
|
+
!createNew &&
|
|
562
|
+
(lowered === undefined || known.text === lowered)
|
|
563
|
+
) {
|
|
564
|
+
return known;
|
|
565
|
+
}
|
|
566
|
+
const file = parse(fileName, version, onError, createNew);
|
|
567
|
+
if (file) {
|
|
568
|
+
parsed.set(key, file);
|
|
569
|
+
}
|
|
570
|
+
return file;
|
|
571
|
+
};
|
|
572
|
+
return host;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* The lowerings record what they generated by lowering key. The analyzed model's
|
|
577
|
+
* spans point at the user's files, but its ids still start with the lowered module
|
|
578
|
+
* each item was declared in, so the records are matched back by id.
|
|
579
|
+
*/
|
|
580
|
+
function attachLowerings(
|
|
581
|
+
module: PlcModule,
|
|
582
|
+
root: string,
|
|
583
|
+
{
|
|
584
|
+
elaboration,
|
|
585
|
+
promises,
|
|
586
|
+
lowering,
|
|
587
|
+
}: {
|
|
588
|
+
elaboration: ReturnType<typeof lowerElaboration>;
|
|
589
|
+
promises: ReturnType<typeof lowerPromises>;
|
|
590
|
+
lowering: AsyncLowering;
|
|
591
|
+
},
|
|
592
|
+
): void {
|
|
593
|
+
for (const fn of module.functions) {
|
|
594
|
+
const origin = elaboration.origins.get(declarationKey(root, fn));
|
|
595
|
+
if (origin) {
|
|
596
|
+
fn.origin = origin;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
module.types = module.types.map((type) => {
|
|
600
|
+
const literals =
|
|
601
|
+
type.kind === "enum" && elaboration.literals.get(typeKey(root, type.id));
|
|
602
|
+
return type.kind === "enum" && literals
|
|
603
|
+
? {
|
|
604
|
+
...type,
|
|
605
|
+
members: type.members.map((member) => ({
|
|
606
|
+
...member,
|
|
607
|
+
text: literals[member.name],
|
|
608
|
+
})),
|
|
609
|
+
}
|
|
610
|
+
: type;
|
|
611
|
+
});
|
|
612
|
+
for (const block of module.blocks) {
|
|
613
|
+
const key = declarationKey(root, block);
|
|
614
|
+
const runtime = promises.runtimeBlocks.get(key) ?? lowering.blocks.get(key);
|
|
615
|
+
if (runtime) {
|
|
616
|
+
block.runtime = runtime;
|
|
617
|
+
}
|
|
618
|
+
const job = lowering.jobs.get(key);
|
|
619
|
+
const origin = elaboration.origins.get(key);
|
|
620
|
+
if (job) {
|
|
621
|
+
block.job = origin ? { ...job, ...origin } : job;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
if (elaboration.folded.length) {
|
|
625
|
+
module.folded = elaboration.folded;
|
|
626
|
+
}
|
|
627
|
+
if (elaboration.references?.fields.length) {
|
|
628
|
+
module.references = elaboration.references;
|
|
629
|
+
}
|
|
630
|
+
for (const entry of module.programs) {
|
|
631
|
+
const background = lowering.schedules.get(entry.name);
|
|
632
|
+
if (background) {
|
|
633
|
+
entry.background = background;
|
|
634
|
+
}
|
|
635
|
+
const continuation = lowering.continuations.get(entry.name);
|
|
636
|
+
if (continuation) {
|
|
637
|
+
entry.continuation = continuation;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
export function checkHostConfig(file: string): readonly Diagnostic[] {
|
|
643
|
+
const bunTypes = createRequire(import.meta.url).resolve(
|
|
644
|
+
"@types/bun/index.d.ts",
|
|
645
|
+
);
|
|
646
|
+
const program = ts.createProgram([file], {
|
|
647
|
+
strict: true,
|
|
648
|
+
noEmit: true,
|
|
649
|
+
target: ts.ScriptTarget.ES2022,
|
|
650
|
+
module: ts.ModuleKind.ESNext,
|
|
651
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
652
|
+
allowImportingTsExtensions: true,
|
|
653
|
+
skipLibCheck: true,
|
|
654
|
+
types: [bunTypes],
|
|
655
|
+
});
|
|
656
|
+
return ts
|
|
657
|
+
.getPreEmitDiagnostics(program)
|
|
658
|
+
.map((diagnostic) => tsDiagnostic(diagnostic, sourceSpan));
|
|
659
|
+
}
|