@tsplc/compiler 0.6.0 → 0.6.2

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.6.0",
20
+ "version": "0.6.2",
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": "0.6.0",
97
- "@tsplc/build": "0.6.0",
98
- "@tsplc/config": "0.6.0",
99
- "@tsplc/debug": "0.6.0",
100
- "@tsplc/opcua": "0.6.0",
101
- "@tsplc/typegen": "0.6.0",
96
+ "@tsplc/agent": "0.6.2",
97
+ "@tsplc/build": "0.6.2",
98
+ "@tsplc/config": "0.6.2",
99
+ "@tsplc/debug": "0.6.2",
100
+ "@tsplc/opcua": "0.6.2",
101
+ "@tsplc/typegen": "0.6.2",
102
102
  "typescript": "6.0.3",
103
103
  "zod": "4.3.6",
104
104
  "@types/bun": "1.4.2",
105
- "@tsplc/server": "0.6.0",
106
- "@tsplc/iec": "0.6.0"
105
+ "@tsplc/server": "0.6.2",
106
+ "@tsplc/iec": "0.6.2"
107
107
  },
108
108
  "bin": {
109
109
  "tsplc": "./dist/cli.js"
@@ -10,6 +10,7 @@ import {
10
10
  } from "@tsplc/compiler/ir";
11
11
  import ts from "typescript";
12
12
  import { BlockResolver, storageInitializer } from "./blocks";
13
+ import type { Uncompiled } from "./elaborate/reduce";
13
14
  import {
14
15
  type AnalysisContext,
15
16
  ExpressionLowering,
@@ -99,6 +100,8 @@ export class Analyzer {
99
100
  private readonly diagnostics: Diagnostic[],
100
101
  private readonly positions: Positions,
101
102
  serviceRecord: ServiceRecord | null,
103
+ /** What of each module elaboration rewrote is PLC code, where that is not all of it. */
104
+ private readonly reduced: ReadonlyMap<string, Uncompiled>,
102
105
  ) {
103
106
  this.checker = program.getTypeChecker();
104
107
  this.resolver = new TypeResolver(
@@ -184,9 +187,35 @@ export class Analyzer {
184
187
  };
185
188
  }
186
189
 
190
+ /**
191
+ * A module's statements that are PLC code. A module elaboration cut down is there for
192
+ * TypeScript, but for the constants a module of constants reads; the entry module it wrote also
193
+ * declares, for other modules to import, names that are no code of its own.
194
+ */
195
+ private statements(source: ts.SourceFile): readonly ts.Statement[] {
196
+ const part = this.reduced.get(source.fileName);
197
+ if (!part) {
198
+ return source.statements;
199
+ }
200
+ const names = (statement: ts.Statement): string[] => {
201
+ if (ts.isVariableStatement(statement)) {
202
+ return statement.declarationList.declarations.map((declaration) =>
203
+ declaration.name.getText(),
204
+ );
205
+ }
206
+ return ts.isEnumDeclaration(statement) ? [statement.name.text] : [];
207
+ };
208
+ return source.statements.filter((statement) =>
209
+ "compiled" in part
210
+ ? ts.isVariableStatement(statement) &&
211
+ names(statement).some((name) => part.compiled.has(name))
212
+ : !names(statement).some((name) => part.declared.has(name)),
213
+ );
214
+ }
215
+
187
216
  private collectDeclarations(): void {
188
217
  const classes = this.sources.flatMap((source) =>
189
- source.statements.filter(ts.isClassDeclaration),
218
+ this.statements(source).filter(ts.isClassDeclaration),
190
219
  );
191
220
  this.blocks.index(classes);
192
221
  for (const node of classes) {
@@ -205,7 +234,7 @@ export class Analyzer {
205
234
  }
206
235
  for (const source of this.sources) {
207
236
  this.diagnostics.push(...suppressionDiagnostics(source, this.positions));
208
- for (const statement of source.statements) {
237
+ for (const statement of this.statements(source)) {
209
238
  this.guard(() => this.declaration(statement));
210
239
  }
211
240
  }
@@ -213,7 +242,7 @@ export class Analyzer {
213
242
 
214
243
  private initializeStorage(): void {
215
244
  for (const source of this.sources) {
216
- for (const statement of source.statements) {
245
+ for (const statement of this.statements(source)) {
217
246
  if (ts.isVariableStatement(statement) && !source.isDeclarationFile) {
218
247
  this.guard(() => {
219
248
  for (const declaration of statement.declarationList.declarations) {
@@ -715,7 +715,7 @@ export class CallSpecializer {
715
715
  if (work) {
716
716
  this.refuseStateInWork(work);
717
717
  }
718
- imports.kept.add(root.file, root.name);
718
+ const local = imports.kept.add(root.file, root.name);
719
719
  const args = node.arguments.map((argument) => {
720
720
  const contextual = checker.getContextualType(argument);
721
721
  return dialect.code(
@@ -728,8 +728,8 @@ export class CallSpecializer {
728
728
  kind: "code",
729
729
  text:
730
730
  name === null
731
- ? mapped`${root.name}(${joined(args, ", ")})`
732
- : mapped`${root.name}.${name}(${joined(args, ", ")})`,
731
+ ? mapped`${local}(${joined(args, ", ")})`
732
+ : mapped`${local}.${name}(${joined(args, ", ")})`,
733
733
  declared:
734
734
  name === null
735
735
  ? checker.getAwaitedType(checker.getTypeAtLocation(node))
@@ -800,7 +800,7 @@ export class CallSpecializer {
800
800
  "Create the service client in a module-level const, and import it where scan code calls it: the PLC calls the server through that one client, with the types the server's router gives each call.",
801
801
  );
802
802
  }
803
- imports.kept.add(client.file, client.name);
803
+ const local = imports.kept.add(client.file, client.name);
804
804
  const args = node.arguments.map((argument) => {
805
805
  const contextual = checker.getContextualType(argument);
806
806
  return dialect.code(
@@ -810,7 +810,7 @@ export class CallSpecializer {
810
810
  });
811
811
  return {
812
812
  kind: "code",
813
- text: mapped`${client.name}.${path}(${joined(args, ", ")})`,
813
+ text: mapped`${local}.${path}(${joined(args, ", ")})`,
814
814
  declared: checker.getTypeAtLocation(node),
815
815
  };
816
816
  }
@@ -1,7 +1,8 @@
1
1
  import type ts from "typescript";
2
2
  import { from, type Mapped, mapped, type Part } from "../positions";
3
- import { type ArrayLayout, unusedName } from "./layouts";
3
+ import type { ArrayLayout } from "./layouts";
4
4
  import type { ModelNode } from "./model";
5
+ import { unusedName } from "./names";
5
6
  import type { Env, Value } from "./values";
6
7
 
7
8
  /** Indented lines of one generated routine, with the local names it has used. */
@@ -1,4 +1,5 @@
1
1
  import { dirname, relative } from "node:path";
2
+ import type { Names } from "./names";
2
3
 
3
4
  /** The specifier a generated module imports `to` by: relative, dot-prefixed and without its extension. */
4
5
  export const importPath = (from: string, to: string): string => {
@@ -10,22 +11,47 @@ export const importPath = (from: string, to: string): string => {
10
11
 
11
12
  /** Names generated code imports from other modules, by module file. */
12
13
  export class ModuleImports {
13
- private readonly names = new Map<string, Set<string>>();
14
+ /** Each module's imported names, with the name generated code reads each by. */
15
+ private readonly imported = new Map<string, Map<string, string>>();
14
16
 
15
- /** Records an import of `name` from `file`, and returns the name to read it by. */
17
+ constructor(
18
+ private readonly names: Names,
19
+ /** Names the modules that import these declare themselves, which an import gives way to. */
20
+ private readonly avoided: ReadonlySet<string> = new Set(),
21
+ ) {}
22
+
23
+ /** Whether `name` of `file` is imported. */
24
+ has(file: string, name: string): boolean {
25
+ return this.imported.get(file)?.has(name) ?? false;
26
+ }
27
+
28
+ /**
29
+ * Records an import of `name` from `file`, and returns the name to read it by: its own, unless
30
+ * the generated modules already use that name for something else.
31
+ */
16
32
  add(file: string, name: string): string {
17
- const names = this.names.get(file) ?? new Set<string>();
18
- names.add(name);
19
- this.names.set(file, names);
20
- return name;
33
+ const names = this.imported.get(file) ?? new Map<string, string>();
34
+ this.imported.set(file, names);
35
+ const local = this.names.import(`${file}\0${name}`, name, this.avoided);
36
+ names.set(name, local);
37
+ return local;
21
38
  }
22
39
 
23
40
  /** One import statement per module, as `importer` reaches it. */
24
41
  lines(importer: string, keyword: "import" | "import type"): string[] {
25
- return [...this.names].map(
26
- ([file, names]) =>
27
- `${keyword} { ${[...names].join(", ")} } from "${importPath(importer, file)}";`,
28
- );
42
+ return [...this.imported].flatMap(([file, names]) => {
43
+ const list = [...names]
44
+ // A module already has what it declares itself, under its own name.
45
+ .filter(([name, local]) => file !== importer || name !== local)
46
+ .map(([name, local]) =>
47
+ name === local ? name : `${name} as ${local}`,
48
+ );
49
+ return list.length
50
+ ? [
51
+ `${keyword} { ${list.join(", ")} } from "${importPath(importer, file)}";`,
52
+ ]
53
+ : [];
54
+ });
29
55
  }
30
56
  }
31
57
 
@@ -35,12 +61,27 @@ export class ModuleImports {
35
61
  */
36
62
  export class EntryImports {
37
63
  /** Storage modules generated code reads through a namespace import, by import name. */
38
- readonly namespaces = new Map<string, string>();
39
- /** Bindings of kept modules, read by their own names. */
40
- readonly kept = new ModuleImports();
64
+ private readonly namespaces = new Map<string, string>();
65
+ /** Bindings of kept modules, read by their own names unless something else has them. */
66
+ readonly kept: ModuleImports;
41
67
  /** SDK functions by the module that declares them, such as `@tsplc/compiler/clock`. */
42
68
  private readonly sdk = new Map<string, Set<string>>();
43
69
 
70
+ constructor(private readonly names: Names) {
71
+ this.kept = new ModuleImports(names);
72
+ }
73
+
74
+ /**
75
+ * Records a namespace import of a storage module, and returns the name to read it by: `name`,
76
+ * unless the generated modules already use that for something else, as two roots whose names
77
+ * differ only in their separators would.
78
+ */
79
+ namespace(file: string, name: string): string {
80
+ const local = this.names.import(file, name);
81
+ this.namespaces.set(local, file);
82
+ return local;
83
+ }
84
+
44
85
  /** Records a call of `name`, which `module` of the SDK declares. */
45
86
  addSdk(module: string, name: string): void {
46
87
  const names = this.sdk.get(module) ?? new Set<string>();