@tsplc/compiler 0.5.4 → 0.5.5

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/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.5.4",
20
+ "version": "0.5.5",
21
21
  "type": "module",
22
22
  "exports": {
23
23
  ".": {
@@ -101,17 +101,17 @@
101
101
  "bun": ">=1.4.3"
102
102
  },
103
103
  "dependencies": {
104
- "@tsplc/agent": "0.5.4",
105
- "@tsplc/build": "0.5.4",
106
- "@tsplc/config": "0.5.4",
107
- "@tsplc/debug": "0.5.4",
108
- "@tsplc/opcua": "0.5.4",
109
- "@tsplc/typegen": "0.5.4",
104
+ "@tsplc/agent": "0.5.5",
105
+ "@tsplc/build": "0.5.5",
106
+ "@tsplc/config": "0.5.5",
107
+ "@tsplc/debug": "0.5.5",
108
+ "@tsplc/opcua": "0.5.5",
109
+ "@tsplc/typegen": "0.5.5",
110
110
  "typescript": "6.0.3",
111
111
  "zod": "4.3.6",
112
112
  "@types/bun": "1.4.2",
113
- "@tsplc/server": "0.5.4",
114
- "@tsplc/iec": "0.5.4"
113
+ "@tsplc/server": "0.5.5",
114
+ "@tsplc/iec": "0.5.5"
115
115
  },
116
116
  "bin": {
117
117
  "tsplc": "./dist/cli.js"
@@ -8,7 +8,7 @@ import {
8
8
  mapped,
9
9
  type Part,
10
10
  } from "../positions";
11
- import { declaredBySdk, inSdk, symbolAt } from "../sdk";
11
+ import { declaredBySdk, inSdk, sdkModule, symbolAt } from "../sdk";
12
12
  import type { DialectText } from "./dialect";
13
13
  import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
14
14
  import type { Frame } from "./frame";
@@ -146,8 +146,9 @@ export class CallSpecializer {
146
146
  expected,
147
147
  );
148
148
  }
149
- if (ts.isIdentifier(callee) && this.sdkCall(callee)) {
150
- return this.sdk(callee.text, node, frame);
149
+ const sdkFile = ts.isIdentifier(callee) && this.sdkDeclaration(callee);
150
+ if (ts.isIdentifier(callee) && sdkFile) {
151
+ return this.sdk(callee.text, sdkModule(sdkFile), node, frame);
151
152
  }
152
153
  return this.expressions.invoke(
153
154
  target,
@@ -205,9 +206,17 @@ export class CallSpecializer {
205
206
  }
206
207
 
207
208
  /** SDK functions scan code calls, such as the PLC clock. */
208
- private sdkCall(callee: ts.Identifier): boolean {
209
+ /** The SDK declaration file of the function `callee` names, or null when the SDK doesn't declare it. */
210
+ private sdkDeclaration(callee: ts.Identifier): string | null {
209
211
  const symbol = symbolAt(this.checker, callee);
210
- return symbol !== undefined && declaredBySdk(this.sdkFile, symbol);
212
+ if (symbol === undefined || !declaredBySdk(this.sdkFile, symbol)) {
213
+ return null;
214
+ }
215
+ return (
216
+ symbol.declarations
217
+ ?.map((declaration) => declaration.getSourceFile().fileName)
218
+ .find((file) => inSdk(this.sdkFile, file)) ?? null
219
+ );
211
220
  }
212
221
 
213
222
  /** A method of `@tsplc/compiler/telemetry`'s `telemetry`. */
@@ -269,14 +278,19 @@ export class CallSpecializer {
269
278
  );
270
279
  }
271
280
 
272
- private sdk(name: string, node: ts.CallExpression, frame: Frame): Value {
281
+ private sdk(
282
+ name: string,
283
+ module: string,
284
+ node: ts.CallExpression,
285
+ frame: Frame,
286
+ ): Value {
273
287
  const args = node.arguments.map((argument) =>
274
288
  this.dialect.code(
275
289
  this.expressions.expression(argument, frame),
276
290
  undefined,
277
291
  ),
278
292
  );
279
- this.imports.clock.add(name);
293
+ this.imports.addSdk(module, name);
280
294
  return {
281
295
  kind: "code",
282
296
  text: mapped`${name}(${joined(args, ", ")})`,
@@ -76,7 +76,8 @@ plugin({
76
76
  build.module("@tsplc/compiler/async", () => ({
77
77
  contents: `export const createServiceClient = () => globalThis.__tsplcService([]);
78
78
  const unsupported = (name) => () => { throw new Error(name + " cannot run during PLC setup."); };
79
- export const sleep = unsupported("sleep"), fetch = unsupported("fetch"), cancelAsync = unsupported("cancelAsync");`,
79
+ export const sleep = unsupported("sleep"), fetch = unsupported("fetch"), cancelAsync = unsupported("cancelAsync");
80
+ export const errorCode = unsupported("errorCode"), asyncDiagnostics = unsupported("asyncDiagnostics"), opcuaCall = unsupported("opcuaCall");`,
80
81
  loader: "js",
81
82
  }));
82
83
  },
@@ -152,7 +152,7 @@ export class DialectText {
152
152
  needsConversion(value.iec ?? "DINT", target)
153
153
  ) {
154
154
  // A TypeScript number holds any of these; in the PLC a value converts as an IEC assignment does.
155
- this.imports.conversions.add("wrap");
155
+ this.imports.addSdk("@tsplc/compiler/types", "wrap");
156
156
  return mapped`wrap<${target}>(${this.code(value, undefined)})`;
157
157
  }
158
158
  if (declared && this.layouts.isOptionalRecord(declared)) {
@@ -237,7 +237,7 @@ export class DialectText {
237
237
  if (!needsConversion(from, to) || !needsConversion(to, from)) {
238
238
  return [leftText, rightText];
239
239
  }
240
- this.imports.conversions.add("wrap");
240
+ this.imports.addSdk("@tsplc/compiler/types", "wrap");
241
241
  return [mapped`wrap<LINT>(${leftText})`, mapped`wrap<LINT>(${rightText})`];
242
242
  }
243
243
  }
@@ -31,20 +31,25 @@ export class ModuleImports {
31
31
 
32
32
  /**
33
33
  * What the generated entry module imports besides the generated types: the storage modules,
34
- * kept modules' bindings and the SDK functions scan code calls.
34
+ * kept modules' bindings, and the SDK functions scan code and generated conversions call.
35
35
  */
36
36
  export class EntryImports {
37
37
  /** Storage modules generated code reads through a namespace import, by import name. */
38
38
  readonly namespaces = new Map<string, string>();
39
39
  /** Bindings of kept modules, read by their own names. */
40
40
  readonly kept = new ModuleImports();
41
- /** `@tsplc/compiler/clock` functions scan code calls. */
42
- readonly clock = new Set<string>();
43
- /** Conversions generated code calls from `@tsplc/compiler/types`. */
44
- readonly conversions = new Set<string>();
41
+ /** SDK functions by the module that declares them, such as `@tsplc/compiler/clock`. */
42
+ private readonly sdk = new Map<string, Set<string>>();
45
43
  /** Whether scan code calls `telemetry`. */
46
44
  telemetry = false;
47
45
 
46
+ /** Records a call of `name`, which `module` of the SDK declares. */
47
+ addSdk(module: string, name: string): void {
48
+ const names = this.sdk.get(module) ?? new Set<string>();
49
+ names.add(name);
50
+ this.sdk.set(module, names);
51
+ }
52
+
48
53
  lines(entryFile: string): string[] {
49
54
  return [
50
55
  ...[...this.namespaces].map(
@@ -52,19 +57,13 @@ export class EntryImports {
52
57
  `import * as ${name} from "${importPath(entryFile, file)}";`,
53
58
  ),
54
59
  ...this.kept.lines(entryFile, "import"),
55
- ...(this.clock.size
56
- ? [
57
- `import { ${[...this.clock].join(", ")} } from "@tsplc/compiler/clock";`,
58
- ]
59
- : []),
60
+ ...[...this.sdk].map(
61
+ ([module, names]) =>
62
+ `import { ${[...names].join(", ")} } from "${module}";`,
63
+ ),
60
64
  ...(this.telemetry
61
65
  ? [`import { telemetry } from "@tsplc/compiler/telemetry";`]
62
66
  : []),
63
- ...(this.conversions.size
64
- ? [
65
- `import { ${[...this.conversions].join(", ")} } from "@tsplc/compiler/types";`,
66
- ]
67
- : []),
68
67
  ];
69
68
  }
70
69
  }
@@ -1,6 +1,6 @@
1
1
  import { realpathSync } from "node:fs";
2
2
  import { createRequire } from "node:module";
3
- import { dirname } from "node:path";
3
+ import { basename, dirname } from "node:path";
4
4
  import ts from "typescript";
5
5
 
6
6
  /** The `@tsplc/compiler/globals` declaration file a project compiles against. */
@@ -44,6 +44,12 @@ export const sdkFor = (
44
44
  export const inSdk = (sdk: string, fileName: string): boolean =>
45
45
  fileName.startsWith(`${dirname(sdk)}/`);
46
46
 
47
+ /** The path a program imports an SDK declaration file by: `index.d.ts` is `@tsplc/compiler/types`, any other its own name. */
48
+ export const sdkModule = (fileName: string): string => {
49
+ const name = basename(fileName, ".d.ts");
50
+ return `@tsplc/compiler/${name === "index" ? "types" : name}`;
51
+ };
52
+
47
53
  /** A symbol followed through import and export aliases to what it names. */
48
54
  export const unaliased = (
49
55
  checker: ts.TypeChecker,