@tsplc/compiler 0.5.13 → 0.5.15
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/README.md +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { dirname, relative } from "node:path";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
-
import { symbolAt } from "./sdk";
|
|
3
|
+
import { namesSdk, symbolAt } from "./sdk";
|
|
4
4
|
|
|
5
5
|
/** The quoted specifier an import generated into `importer` uses to reach `target`. */
|
|
6
6
|
export const importSpecifier = (importer: string, target: string): string => {
|
|
@@ -9,6 +9,60 @@ export const importSpecifier = (importer: string, target: string): string => {
|
|
|
9
9
|
return JSON.stringify(/^\.\.?\//.test(path) ? path : `./${path}`);
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Whether an import or re-export loads its module when the importer runs: not when it is
|
|
14
|
+
* type-only, nor when every name it lists is a type.
|
|
15
|
+
*/
|
|
16
|
+
export const runtimeImport = (
|
|
17
|
+
statement: ts.ImportDeclaration | ts.ExportDeclaration,
|
|
18
|
+
checker: ts.TypeChecker,
|
|
19
|
+
): boolean => {
|
|
20
|
+
const typeOnly = ts.isImportDeclaration(statement)
|
|
21
|
+
? statement.importClause?.isTypeOnly
|
|
22
|
+
: statement.isTypeOnly;
|
|
23
|
+
if (typeOnly) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const bindings = ts.isImportDeclaration(statement)
|
|
27
|
+
? statement.importClause?.namedBindings
|
|
28
|
+
: statement.exportClause;
|
|
29
|
+
const defaultName = ts.isImportDeclaration(statement)
|
|
30
|
+
? statement.importClause?.name
|
|
31
|
+
: undefined;
|
|
32
|
+
const namesOnlyTypes =
|
|
33
|
+
bindings &&
|
|
34
|
+
(ts.isNamedImports(bindings) || ts.isNamedExports(bindings)) &&
|
|
35
|
+
bindings.elements.length > 0 &&
|
|
36
|
+
bindings.elements.every((binding) => {
|
|
37
|
+
const symbol = symbolAt(checker, binding.name);
|
|
38
|
+
return (
|
|
39
|
+
binding.isTypeOnly ||
|
|
40
|
+
(symbol !== undefined && !(symbol.flags & ts.SymbolFlags.Value))
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
return !namesOnlyTypes || defaultName !== undefined;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** `Block` in a `FixedArray<Block, ...>` annotation, where `namesFixedArray` tells whether a reference names `FixedArray`. */
|
|
47
|
+
const fixedArrayElement = (
|
|
48
|
+
node: ts.Node,
|
|
49
|
+
namesFixedArray: (name: ts.EntityName) => boolean,
|
|
50
|
+
): ts.TypeReferenceNode | undefined => {
|
|
51
|
+
if (!ts.isTypeReferenceNode(node) || !namesFixedArray(node.typeName)) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
const [element] = node.typeArguments ?? [];
|
|
55
|
+
return element && ts.isTypeReferenceNode(element) ? element : undefined;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** `Block` in a `FixedArray<Block, ...>` annotation, whose instances the array allocates when it names a block class. */
|
|
59
|
+
export const blockArrayElement = (
|
|
60
|
+
node: ts.TypeNode,
|
|
61
|
+
checker: ts.TypeChecker,
|
|
62
|
+
sdk: string,
|
|
63
|
+
): ts.TypeReferenceNode | undefined =>
|
|
64
|
+
fixedArrayElement(node, (name) => namesSdk(checker, sdk, name, "FixedArray"));
|
|
65
|
+
|
|
12
66
|
/** Runtime imports define execution; type-only imports provide layouts without allocating state. */
|
|
13
67
|
export const runtimeSources = (
|
|
14
68
|
program: ts.Program,
|
|
@@ -17,19 +71,17 @@ export const runtimeSources = (
|
|
|
17
71
|
const visited = new Set<ts.SourceFile>();
|
|
18
72
|
// A block array allocates through its type, so its block's module runs even when type-imported.
|
|
19
73
|
const visitBlockArrays = (node: ts.Node): void => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
visit(block.getSourceFile());
|
|
32
|
-
}
|
|
74
|
+
const element = fixedArrayElement(
|
|
75
|
+
node,
|
|
76
|
+
(name) => name.getText() === "FixedArray",
|
|
77
|
+
);
|
|
78
|
+
const block =
|
|
79
|
+
element &&
|
|
80
|
+
symbolAt(checker, element.typeName)?.declarations?.find(
|
|
81
|
+
ts.isClassDeclaration,
|
|
82
|
+
);
|
|
83
|
+
if (block) {
|
|
84
|
+
visit(block.getSourceFile());
|
|
33
85
|
}
|
|
34
86
|
ts.forEachChild(node, visitBlockArrays);
|
|
35
87
|
};
|
|
@@ -43,38 +95,12 @@ export const runtimeSources = (
|
|
|
43
95
|
}
|
|
44
96
|
visitBlockArrays(source);
|
|
45
97
|
for (const statement of source.statements) {
|
|
46
|
-
if (ts.isImportDeclaration(statement)) {
|
|
47
|
-
if (statement.importClause?.isTypeOnly) {
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
const bindings = statement.importClause?.namedBindings;
|
|
51
|
-
if (
|
|
52
|
-
!statement.importClause?.name &&
|
|
53
|
-
bindings &&
|
|
54
|
-
ts.isNamedImports(bindings) &&
|
|
55
|
-
bindings.elements.every((value) => value.isTypeOnly)
|
|
56
|
-
) {
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const target = checker
|
|
60
|
-
.getSymbolAtLocation(statement.moduleSpecifier)
|
|
61
|
-
?.declarations?.find(ts.isSourceFile);
|
|
62
|
-
if (target) {
|
|
63
|
-
visit(target);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
98
|
if (
|
|
67
|
-
ts.
|
|
99
|
+
(ts.isImportDeclaration(statement) ||
|
|
100
|
+
ts.isExportDeclaration(statement)) &&
|
|
68
101
|
statement.moduleSpecifier &&
|
|
69
|
-
|
|
102
|
+
runtimeImport(statement, checker)
|
|
70
103
|
) {
|
|
71
|
-
if (
|
|
72
|
-
statement.exportClause &&
|
|
73
|
-
ts.isNamedExports(statement.exportClause) &&
|
|
74
|
-
statement.exportClause.elements.every((value) => value.isTypeOnly)
|
|
75
|
-
) {
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
104
|
const target = checker
|
|
79
105
|
.getSymbolAtLocation(statement.moduleSpecifier)
|
|
80
106
|
?.declarations?.find(ts.isSourceFile);
|
|
@@ -92,3 +118,39 @@ export const runtimeSources = (
|
|
|
92
118
|
}
|
|
93
119
|
return program.getSourceFiles().filter((source) => visited.has(source));
|
|
94
120
|
};
|
|
121
|
+
|
|
122
|
+
/** The program's runtime sources the compiler lowers: its own TypeScript, not declaration files. */
|
|
123
|
+
export const plcSources = (program: ts.Program): readonly ts.SourceFile[] =>
|
|
124
|
+
runtimeSources(program).filter((source) => !source.isDeclarationFile);
|
|
125
|
+
|
|
126
|
+
/** A named top-level function or class method. */
|
|
127
|
+
export type NamedRoutine = (ts.FunctionDeclaration | ts.MethodDeclaration) & {
|
|
128
|
+
readonly name: ts.PropertyName;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const isNamedRoutine = (node: ts.Node): node is NamedRoutine =>
|
|
132
|
+
(ts.isFunctionDeclaration(node) || ts.isMethodDeclaration(node)) &&
|
|
133
|
+
node.name !== undefined;
|
|
134
|
+
|
|
135
|
+
/** The named top-level functions and class methods of `sources`, by symbol. */
|
|
136
|
+
export const namedRoutines = (
|
|
137
|
+
sources: readonly ts.SourceFile[],
|
|
138
|
+
checker: ts.TypeChecker,
|
|
139
|
+
): ReadonlyMap<ts.Symbol, NamedRoutine> => {
|
|
140
|
+
const routines = new Map<ts.Symbol, NamedRoutine>();
|
|
141
|
+
for (const source of sources) {
|
|
142
|
+
for (const statement of source.statements) {
|
|
143
|
+
const candidates = ts.isClassDeclaration(statement)
|
|
144
|
+
? statement.members.filter(ts.isMethodDeclaration)
|
|
145
|
+
: [statement];
|
|
146
|
+
for (const candidate of candidates) {
|
|
147
|
+
const symbol =
|
|
148
|
+
isNamedRoutine(candidate) && symbolAt(checker, candidate.name);
|
|
149
|
+
if (symbol) {
|
|
150
|
+
routines.set(symbol, candidate);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return routines;
|
|
156
|
+
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
|
|
2
1
|
import type { SourceSpan } from "@tsplc/compiler/ir";
|
|
3
2
|
import type ts from "typescript";
|
|
4
3
|
|
|
@@ -41,20 +40,6 @@ const append = (segments: Segment[], text: Part, offset: number): void => {
|
|
|
41
40
|
}
|
|
42
41
|
};
|
|
43
42
|
|
|
44
|
-
/** A template tag that concatenates like a template literal and keeps the origins of `Mapped` parts. */
|
|
45
|
-
export const mapped = (
|
|
46
|
-
strings: TemplateStringsArray,
|
|
47
|
-
...parts: readonly Part[]
|
|
48
|
-
): Mapped => {
|
|
49
|
-
let text = strings[0]!;
|
|
50
|
-
const segments: Segment[] = [];
|
|
51
|
-
parts.forEach((part, index) => {
|
|
52
|
-
append(segments, part, text.length);
|
|
53
|
-
text += `${part}${strings[index + 1]}`;
|
|
54
|
-
});
|
|
55
|
-
return new Mapped(text, segments);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
43
|
export const joined = (parts: readonly Part[], separator: string): Mapped => {
|
|
59
44
|
let text = "";
|
|
60
45
|
const segments: Segment[] = [];
|
|
@@ -68,6 +53,19 @@ export const joined = (parts: readonly Part[], separator: string): Mapped => {
|
|
|
68
53
|
return new Mapped(text, segments);
|
|
69
54
|
};
|
|
70
55
|
|
|
56
|
+
/** A template tag that concatenates like a template literal and keeps the origins of `Mapped` parts. */
|
|
57
|
+
export const mapped = (
|
|
58
|
+
strings: TemplateStringsArray,
|
|
59
|
+
...parts: readonly Part[]
|
|
60
|
+
): Mapped =>
|
|
61
|
+
joined(
|
|
62
|
+
strings.flatMap((text, index): Part[] => {
|
|
63
|
+
const part = parts[index];
|
|
64
|
+
return part === undefined ? [text] : [text, part];
|
|
65
|
+
}),
|
|
66
|
+
"",
|
|
67
|
+
);
|
|
68
|
+
|
|
71
69
|
const fill = (text: Part, file: string, from: number, to: number): Mapped => {
|
|
72
70
|
const value = text instanceof Mapped ? text : new Mapped(String(text));
|
|
73
71
|
const segments: Segment[] = [];
|
|
@@ -193,7 +191,8 @@ const search = (segments: readonly Segment[], offset: number): number => {
|
|
|
193
191
|
let high = segments.length;
|
|
194
192
|
while (low < high) {
|
|
195
193
|
const middle = (low + high) >> 1;
|
|
196
|
-
|
|
194
|
+
const segment = segments[middle];
|
|
195
|
+
if (segment !== undefined && segment.end > offset) {
|
|
197
196
|
high = middle;
|
|
198
197
|
} else {
|
|
199
198
|
low = middle + 1;
|
|
@@ -213,11 +212,10 @@ const resolve = (
|
|
|
213
212
|
if (!head || head.start >= Math.max(end, start + 1)) {
|
|
214
213
|
return undefined;
|
|
215
214
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const tail = last > first ? segments[last]! : head;
|
|
215
|
+
const candidate = end > start ? search(segments, end - 1) : first;
|
|
216
|
+
const last =
|
|
217
|
+
(segments[candidate]?.start ?? end) >= end ? candidate - 1 : candidate;
|
|
218
|
+
const tail = (last > first && segments[last]) || head;
|
|
221
219
|
const from = head.copied
|
|
222
220
|
? head.from + Math.max(0, start - head.start)
|
|
223
221
|
: head.from;
|
|
@@ -232,6 +230,17 @@ const resolve = (
|
|
|
232
230
|
return { file: head.file, from, to: Math.max(from, until(head)) };
|
|
233
231
|
};
|
|
234
232
|
|
|
233
|
+
/** A source the program read, which every recorded segment's file is. */
|
|
234
|
+
const sourceOf = (program: ts.Program, file: string): ts.SourceFile => {
|
|
235
|
+
const source = program.getSourceFile(file);
|
|
236
|
+
if (!source) {
|
|
237
|
+
throw new Error(`The lowered program does not include ${file}.`);
|
|
238
|
+
}
|
|
239
|
+
return source;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const excerptLength = 120;
|
|
243
|
+
|
|
235
244
|
/** A span of a source as its text reads. */
|
|
236
245
|
export const sourceSpan = (
|
|
237
246
|
source: ts.SourceFile,
|
|
@@ -267,10 +276,7 @@ export class Positions {
|
|
|
267
276
|
const previous = this.maps.get(segment.file);
|
|
268
277
|
if (!previous) {
|
|
269
278
|
if (!this.originals.has(segment.file)) {
|
|
270
|
-
this.originals.set(
|
|
271
|
-
segment.file,
|
|
272
|
-
program.getSourceFile(segment.file)!,
|
|
273
|
-
);
|
|
279
|
+
this.originals.set(segment.file, sourceOf(program, segment.file));
|
|
274
280
|
}
|
|
275
281
|
segments.push(segment);
|
|
276
282
|
} else if (segment.copied) {
|
|
@@ -279,8 +285,8 @@ export class Positions {
|
|
|
279
285
|
index < previous.length;
|
|
280
286
|
index++
|
|
281
287
|
) {
|
|
282
|
-
const inner = previous[index]
|
|
283
|
-
if (inner.start >= segment.to) {
|
|
288
|
+
const inner = previous[index];
|
|
289
|
+
if (!inner || inner.start >= segment.to) {
|
|
284
290
|
break;
|
|
285
291
|
}
|
|
286
292
|
const first = Math.max(inner.start, segment.from);
|
|
@@ -317,11 +323,11 @@ export class Positions {
|
|
|
317
323
|
if (!origin) {
|
|
318
324
|
return { source, from: start, to: end };
|
|
319
325
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
};
|
|
326
|
+
const original = this.originals.get(origin.file);
|
|
327
|
+
if (!original) {
|
|
328
|
+
throw new Error(`No source was recorded for ${origin.file}.`);
|
|
329
|
+
}
|
|
330
|
+
return { source: original, from: origin.from, to: origin.to };
|
|
325
331
|
}
|
|
326
332
|
|
|
327
333
|
readonly span = (
|
|
@@ -338,14 +344,16 @@ export class Positions {
|
|
|
338
344
|
return this.span(source, node.getStart(source), node.end);
|
|
339
345
|
};
|
|
340
346
|
|
|
341
|
-
/** The user's text a node came from, on one line and at most `
|
|
342
|
-
excerpt(node: ts.Node
|
|
347
|
+
/** The user's text a node came from, on one line and at most `excerptLength` characters. */
|
|
348
|
+
excerpt(node: ts.Node): string {
|
|
343
349
|
const source = node.getSourceFile();
|
|
344
350
|
const origin = this.origin(source, node.getStart(source), node.end);
|
|
345
351
|
const text = origin.source.text
|
|
346
352
|
.slice(origin.from, origin.to)
|
|
347
353
|
.replace(/\s+/g, " ")
|
|
348
354
|
.trim();
|
|
349
|
-
return text.length >
|
|
355
|
+
return text.length > excerptLength
|
|
356
|
+
? `${text.slice(0, excerptLength - 1)}…`
|
|
357
|
+
: text;
|
|
350
358
|
}
|
|
351
359
|
}
|