@tsplc/tmp-compiler 0.6.2 → 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/dist/codesys.js +25 -20
- package/dist/codesys.js.map +3 -3
- package/dist/index.js +814 -692
- package/dist/index.js.map +8 -7
- package/package.json +9 -9
- package/src/codesys/http.ts +37 -25
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/coroutines.ts +11 -3
- package/src/frontend/elaborate/expressions.ts +6 -0
- package/src/frontend/services.ts +15 -12
- package/src/frontend/typescript.ts +9 -1
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { createRequire as createRequire2 } from "node:module";
|
|
|
4
4
|
import { dirname as dirname7, join as join5, relative as relative4, resolve as resolve7 } from "node:path";
|
|
5
5
|
import { hasErrors } from "@tsplc/compiler/ir";
|
|
6
6
|
import { generatedDirectory as generatedDirectory2 } from "@tsplc/config/project-files";
|
|
7
|
-
import
|
|
7
|
+
import ts46 from "typescript";
|
|
8
8
|
|
|
9
9
|
// packages/compiler/src/frontend/analyzer.ts
|
|
10
10
|
import { resolve as resolve2 } from "node:path";
|
|
@@ -4406,15 +4406,18 @@ class Coroutine {
|
|
|
4406
4406
|
if (method === "GET" && json) {
|
|
4407
4407
|
this.context.fail(call, "GET requests cannot contain json.");
|
|
4408
4408
|
}
|
|
4409
|
-
const responseType = this.text(response);
|
|
4409
|
+
const responseType = response.kind === ts12.SyntaxKind.VoidKeyword ? null : this.text(response);
|
|
4410
|
+
if (!responseType && target) {
|
|
4411
|
+
this.context.fail(call, "A request that answers void has no value to store.");
|
|
4412
|
+
}
|
|
4410
4413
|
const httpName = this.context.names.name(`Tsplc${rpc ? "Rpc" : "Http"}`, call);
|
|
4411
4414
|
if (!json) {
|
|
4412
4415
|
extra.push(from(call, `interface ${httpName}Request {reserved:BOOL}`));
|
|
4413
4416
|
}
|
|
4414
4417
|
const requestType = json ? this.requestType(json, request) : `${httpName}Request`;
|
|
4415
4418
|
extra.push(from(call, `interface ${httpName}Input {start:BOOL;cancel:BOOL;request:${requestType}}
|
|
4416
|
-
interface ${httpName}Output {busy:BOOL;done:BOOL;error:UDINT
|
|
4417
|
-
class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { protected execute(input:${httpName}Input):${httpName}Output {return {busy:false,done:false,error:0
|
|
4419
|
+
interface ${httpName}Output {busy:BOOL;done:BOOL;error:UDINT${responseType ? `;result:${responseType}` : ""}}
|
|
4420
|
+
class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { protected execute(input:${httpName}Input):${httpName}Output {return {busy:false,done:false,error:0${responseType ? `,result:defaultValue<${responseType}>()` : ""}};} }`));
|
|
4418
4421
|
blocks.set(loweringKey(source.fileName, httpName), rpc ? { kind: "rpc", path: url } : {
|
|
4419
4422
|
kind: "http",
|
|
4420
4423
|
url,
|
|
@@ -5140,22 +5143,131 @@ var lowerEnvironment = (program, sdk, positions, environment) => {
|
|
|
5140
5143
|
return { sources, diagnostics, read };
|
|
5141
5144
|
};
|
|
5142
5145
|
|
|
5143
|
-
// packages/compiler/src/frontend/const-
|
|
5146
|
+
// packages/compiler/src/frontend/const-enums.ts
|
|
5144
5147
|
import { error as error5 } from "@tsplc/compiler/ir";
|
|
5145
5148
|
import ts16 from "typescript";
|
|
5149
|
+
var withoutParentheses = (type) => ts16.isParenthesizedTypeNode(type) ? withoutParentheses(type.type) : type;
|
|
5150
|
+
var valuesOf = (type) => {
|
|
5151
|
+
const access = withoutParentheses(type);
|
|
5152
|
+
if (!ts16.isIndexedAccessTypeNode(access)) {
|
|
5153
|
+
return;
|
|
5154
|
+
}
|
|
5155
|
+
const object = withoutParentheses(access.objectType);
|
|
5156
|
+
const index = withoutParentheses(access.indexType);
|
|
5157
|
+
if (!ts16.isTypeQueryNode(object) || !ts16.isIdentifier(object.exprName) || !ts16.isTypeOperatorNode(index) || index.operator !== ts16.SyntaxKind.KeyOfKeyword) {
|
|
5158
|
+
return;
|
|
5159
|
+
}
|
|
5160
|
+
const keys = withoutParentheses(index.type);
|
|
5161
|
+
return ts16.isTypeQueryNode(keys) && ts16.isIdentifier(keys.exprName) && keys.exprName.text === object.exprName.text ? object.exprName.text : undefined;
|
|
5162
|
+
};
|
|
5163
|
+
var unwrapped = (node) => ts16.isParenthesizedExpression(node) || ts16.isSatisfiesExpression(node) ? unwrapped(node.expression) : node;
|
|
5164
|
+
var objectLiteral = (initializer) => {
|
|
5165
|
+
const outer = unwrapped(initializer);
|
|
5166
|
+
const asConst = ts16.isAsExpression(outer) && ts16.isTypeReferenceNode(outer.type) && ts16.isIdentifier(outer.type.typeName) && outer.type.typeName.text === "const";
|
|
5167
|
+
const object = unwrapped(asConst ? outer.expression : outer);
|
|
5168
|
+
return ts16.isObjectLiteralExpression(object) ? { object, asConst } : undefined;
|
|
5169
|
+
};
|
|
5170
|
+
var isNumber = (value) => ts16.isNumericLiteral(value) || ts16.isPrefixUnaryExpression(value) && value.operator === ts16.SyntaxKind.MinusToken && ts16.isNumericLiteral(value.operand);
|
|
5171
|
+
var isMember = (property) => ts16.isPropertyAssignment(property) && (ts16.isIdentifier(property.name) || ts16.isStringLiteral(property.name)) && isNumber(property.initializer);
|
|
5172
|
+
var blank = (text) => text.replace(/[^\r\n]/g, " ");
|
|
5173
|
+
var enumEdits = (source, statement, declaration, alias) => {
|
|
5174
|
+
const written = declaration.initializer && !declaration.type && objectLiteral(declaration.initializer);
|
|
5175
|
+
if (!written) {
|
|
5176
|
+
return;
|
|
5177
|
+
}
|
|
5178
|
+
const { object, asConst } = written;
|
|
5179
|
+
if (statement.declarationList.declarations.length > 1) {
|
|
5180
|
+
return { refusal: "declare it in a const of its own.", at: declaration };
|
|
5181
|
+
}
|
|
5182
|
+
if (!(statement.declarationList.flags & ts16.NodeFlags.Const)) {
|
|
5183
|
+
return { refusal: "declare it with const.", at: declaration };
|
|
5184
|
+
}
|
|
5185
|
+
if (!asConst) {
|
|
5186
|
+
return {
|
|
5187
|
+
refusal: `declare it as const: const ${alias.name.text} = { ... } as const.`,
|
|
5188
|
+
at: declaration
|
|
5189
|
+
};
|
|
5190
|
+
}
|
|
5191
|
+
const odd = object.properties.find((property) => !isMember(property));
|
|
5192
|
+
if (odd) {
|
|
5193
|
+
return {
|
|
5194
|
+
refusal: "each member is a name and a number, such as Idle: 0.",
|
|
5195
|
+
at: odd
|
|
5196
|
+
};
|
|
5197
|
+
}
|
|
5198
|
+
if (hasModifier(alias, ts16.SyntaxKind.ExportKeyword) && !hasModifier(statement, ts16.SyntaxKind.ExportKeyword)) {
|
|
5199
|
+
return { refusal: "export it as its type alias is.", at: declaration };
|
|
5200
|
+
}
|
|
5201
|
+
const blanked = (start, end) => ({
|
|
5202
|
+
start,
|
|
5203
|
+
end,
|
|
5204
|
+
text: blank(source.text.slice(start, end))
|
|
5205
|
+
});
|
|
5206
|
+
const keyword = statement.declarationList.getStart(source);
|
|
5207
|
+
return {
|
|
5208
|
+
edits: [
|
|
5209
|
+
{ start: keyword, end: keyword + "const".length, text: "enum " },
|
|
5210
|
+
blanked(declaration.name.end, object.getStart(source)),
|
|
5211
|
+
...object.properties.filter(isMember).map((member) => {
|
|
5212
|
+
const colon = member.getChildren(source).find((child) => child.kind === ts16.SyntaxKind.ColonToken);
|
|
5213
|
+
const at = colon?.getStart(source) ?? member.name.end;
|
|
5214
|
+
return { start: at, end: at + 1, text: "=" };
|
|
5215
|
+
}),
|
|
5216
|
+
blanked(object.end, statement.end),
|
|
5217
|
+
blanked(alias.getStart(source), alias.end)
|
|
5218
|
+
]
|
|
5219
|
+
};
|
|
5220
|
+
};
|
|
5221
|
+
var lowerConstEnums = (program, positions) => {
|
|
5222
|
+
const sources = new Map;
|
|
5223
|
+
const diagnostics = [];
|
|
5224
|
+
const plc = new Set(plcSources(program));
|
|
5225
|
+
for (const source of program.getSourceFiles()) {
|
|
5226
|
+
if (source.isDeclarationFile) {
|
|
5227
|
+
continue;
|
|
5228
|
+
}
|
|
5229
|
+
const aliases = new Map(source.statements.filter(ts16.isTypeAliasDeclaration).filter((alias) => !alias.typeParameters && valuesOf(alias.type) === alias.name.text).map((alias) => [alias.name.text, alias]));
|
|
5230
|
+
if (!aliases.size) {
|
|
5231
|
+
continue;
|
|
5232
|
+
}
|
|
5233
|
+
const edits = [];
|
|
5234
|
+
for (const statement of source.statements.filter(ts16.isVariableStatement)) {
|
|
5235
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
5236
|
+
const alias = ts16.isIdentifier(declaration.name) && aliases.get(declaration.name.text);
|
|
5237
|
+
const result = alias && enumEdits(source, statement, declaration, alias);
|
|
5238
|
+
if (!alias || !result) {
|
|
5239
|
+
continue;
|
|
5240
|
+
}
|
|
5241
|
+
if ("edits" in result) {
|
|
5242
|
+
edits.push(...result.edits);
|
|
5243
|
+
} else if (plc.has(source)) {
|
|
5244
|
+
diagnostics.push(error5("PLC2001", `${alias.name.text} is an enum by its type alias, so ${result.refusal}`, positions.node(result.at)));
|
|
5245
|
+
}
|
|
5246
|
+
}
|
|
5247
|
+
}
|
|
5248
|
+
if (edits.length) {
|
|
5249
|
+
sources.set(source.fileName, edited(source, edits));
|
|
5250
|
+
}
|
|
5251
|
+
}
|
|
5252
|
+
return { sources, diagnostics };
|
|
5253
|
+
};
|
|
5254
|
+
|
|
5255
|
+
// packages/compiler/src/frontend/const-functions.ts
|
|
5256
|
+
import { error as error6 } from "@tsplc/compiler/ir";
|
|
5257
|
+
import ts17 from "typescript";
|
|
5146
5258
|
var functionBody = (body, result) => {
|
|
5147
|
-
if (
|
|
5259
|
+
if (ts17.isBlock(body)) {
|
|
5148
5260
|
return copied(body);
|
|
5149
5261
|
}
|
|
5150
|
-
return result?.kind ===
|
|
5262
|
+
return result?.kind === ts17.SyntaxKind.VoidKeyword ? mapped`{ ${copied(body)}; }` : mapped`{ return ${copied(body)}; }`;
|
|
5151
5263
|
};
|
|
5152
|
-
var voidAsyncAnnotations = (source, checker) => source.statements.flatMap((statement) =>
|
|
5153
|
-
if (!
|
|
5264
|
+
var voidAsyncAnnotations = (source, checker) => source.statements.flatMap((statement) => ts17.isClassDeclaration(statement) ? statement.members.filter(ts17.isMethodDeclaration) : [statement]).flatMap((routine) => {
|
|
5265
|
+
if (!ts17.isFunctionDeclaration(routine) && !ts17.isMethodDeclaration(routine) || routine.type || !routine.body || !hasModifier(routine, ts17.SyntaxKind.AsyncKeyword)) {
|
|
5154
5266
|
return [];
|
|
5155
5267
|
}
|
|
5156
5268
|
const signature = checker.getSignatureFromDeclaration(routine);
|
|
5157
5269
|
const result = signature && checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
5158
|
-
if (!result || !(result.flags &
|
|
5270
|
+
if (!result || !(result.flags & ts17.TypeFlags.Void)) {
|
|
5159
5271
|
return [];
|
|
5160
5272
|
}
|
|
5161
5273
|
const position = routine.body.getStart(source);
|
|
@@ -5169,38 +5281,38 @@ var voidAsyncAnnotations = (source, checker) => source.statements.flatMap((state
|
|
|
5169
5281
|
});
|
|
5170
5282
|
var lowerConstFunctions = (program, positions) => {
|
|
5171
5283
|
const checker = program.getTypeChecker();
|
|
5172
|
-
const printer =
|
|
5284
|
+
const printer = ts17.createPrinter();
|
|
5173
5285
|
const sources = new Map;
|
|
5174
5286
|
const diagnostics = [];
|
|
5175
5287
|
for (const source of plcSources(program)) {
|
|
5176
5288
|
const edits = voidAsyncAnnotations(source, checker);
|
|
5177
5289
|
for (const statement of source.statements) {
|
|
5178
|
-
if (!
|
|
5290
|
+
if (!ts17.isVariableStatement(statement) || !statement.declarationList.declarations.some((declaration) => declaration.initializer && ts17.isArrowFunction(declaration.initializer))) {
|
|
5179
5291
|
continue;
|
|
5180
5292
|
}
|
|
5181
|
-
const print = (node) => printer.printNode(
|
|
5293
|
+
const print = (node) => printer.printNode(ts17.EmitHint.Unspecified, node, source);
|
|
5182
5294
|
const declarations = [];
|
|
5183
5295
|
for (const declaration of statement.declarationList.declarations) {
|
|
5184
5296
|
const arrow = declaration.initializer;
|
|
5185
|
-
if (!arrow || !
|
|
5186
|
-
declarations.push(from(declaration, print(
|
|
5297
|
+
if (!arrow || !ts17.isArrowFunction(arrow)) {
|
|
5298
|
+
declarations.push(from(declaration, print(ts17.factory.updateVariableStatement(statement, statement.modifiers, ts17.factory.updateVariableDeclarationList(statement.declarationList, [declaration])))));
|
|
5187
5299
|
continue;
|
|
5188
5300
|
}
|
|
5189
|
-
if (!(statement.declarationList.flags &
|
|
5190
|
-
diagnostics.push(
|
|
5301
|
+
if (!(statement.declarationList.flags & ts17.NodeFlags.Const) || !ts17.isIdentifier(declaration.name)) {
|
|
5302
|
+
diagnostics.push(error6("PLC1007", "PLC routines require named const bindings; mutable function values are unsupported.", positions.node(declaration)));
|
|
5191
5303
|
continue;
|
|
5192
5304
|
}
|
|
5193
|
-
const contextual = declaration.type &&
|
|
5305
|
+
const contextual = declaration.type && ts17.isFunctionTypeNode(declaration.type) ? declaration.type : undefined;
|
|
5194
5306
|
const signature = checker.getTypeAtLocation(declaration.name).getCallSignatures()[0];
|
|
5195
|
-
const returnsVoid = signature && Boolean(checker.getReturnTypeOfSignature(signature).flags &
|
|
5307
|
+
const returnsVoid = signature && Boolean(checker.getReturnTypeOfSignature(signature).flags & ts17.TypeFlags.Void);
|
|
5196
5308
|
const inferredResult = signature && checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
5197
|
-
const asyncVoid = hasModifier(arrow,
|
|
5198
|
-
const inferredType = asyncVoid ?
|
|
5199
|
-
|
|
5309
|
+
const asyncVoid = hasModifier(arrow, ts17.SyntaxKind.AsyncKeyword) && inferredResult && inferredResult.flags & ts17.TypeFlags.Void;
|
|
5310
|
+
const inferredType = asyncVoid ? ts17.factory.createTypeReferenceNode("Promise", [
|
|
5311
|
+
ts17.factory.createKeywordTypeNode(ts17.SyntaxKind.VoidKeyword)
|
|
5200
5312
|
]) : undefined;
|
|
5201
|
-
const result = arrow.type ?? contextual?.type ?? (returnsVoid ?
|
|
5202
|
-
const parameters = arrow.parameters.map((parameter, index) =>
|
|
5203
|
-
const head = print(
|
|
5313
|
+
const result = arrow.type ?? contextual?.type ?? (returnsVoid ? ts17.factory.createKeywordTypeNode(ts17.SyntaxKind.VoidKeyword) : inferredType);
|
|
5314
|
+
const parameters = arrow.parameters.map((parameter, index) => ts17.factory.updateParameterDeclaration(parameter, parameter.modifiers, parameter.dotDotDotToken, parameter.name, parameter.questionToken, parameter.type ?? contextual?.parameters[index]?.type, parameter.initializer));
|
|
5315
|
+
const head = print(ts17.factory.createFunctionDeclaration([...statement.modifiers ?? [], ...arrow.modifiers ?? []], undefined, declaration.name, arrow.typeParameters ?? contextual?.typeParameters, parameters, result, ts17.factory.createBlock([]))).replace(/\{\s*\}$/, "");
|
|
5204
5316
|
declarations.push(from(declaration, mapped`${head}${functionBody(arrow.body, result)}`));
|
|
5205
5317
|
}
|
|
5206
5318
|
edits.push({
|
|
@@ -5218,15 +5330,15 @@ var lowerConstFunctions = (program, positions) => {
|
|
|
5218
5330
|
};
|
|
5219
5331
|
|
|
5220
5332
|
// packages/compiler/src/frontend/control-flow.ts
|
|
5221
|
-
import
|
|
5333
|
+
import ts19 from "typescript";
|
|
5222
5334
|
|
|
5223
5335
|
// packages/compiler/src/frontend/promise-chains.ts
|
|
5224
|
-
import
|
|
5336
|
+
import ts18 from "typescript";
|
|
5225
5337
|
var linkKinds = ["then", "catch"];
|
|
5226
5338
|
var asyncRoutine = (context, node) => {
|
|
5227
5339
|
const symbol = context.symbol(node);
|
|
5228
5340
|
const fn = symbol && context.routines.get(symbol);
|
|
5229
|
-
return fn && hasModifier(fn,
|
|
5341
|
+
return fn && hasModifier(fn, ts18.SyntaxKind.AsyncKeyword) ? fn : undefined;
|
|
5230
5342
|
};
|
|
5231
5343
|
var job = (context, node) => {
|
|
5232
5344
|
const [work] = backgroundJob(context, context.symbol(node))?.arguments ?? [];
|
|
@@ -5242,15 +5354,15 @@ var isSdk = (context, call, name) => {
|
|
|
5242
5354
|
};
|
|
5243
5355
|
var linkOf = (call) => {
|
|
5244
5356
|
const callee = call.expression;
|
|
5245
|
-
if (!
|
|
5357
|
+
if (!ts18.isPropertyAccessExpression(callee)) {
|
|
5246
5358
|
return;
|
|
5247
5359
|
}
|
|
5248
5360
|
const kind = linkKinds.find((link) => link === callee.name.text);
|
|
5249
5361
|
return kind && { kind, target: callee.expression };
|
|
5250
5362
|
};
|
|
5251
|
-
var promiseCallback = (node) => (
|
|
5363
|
+
var promiseCallback = (node) => (ts18.isArrowFunction(node) || ts18.isFunctionExpression(node)) && ts18.isCallExpression(node.parent) && linkOf(node.parent) !== undefined;
|
|
5252
5364
|
var chainOf = (context, expression) => {
|
|
5253
|
-
if (!
|
|
5365
|
+
if (!ts18.isCallExpression(expression)) {
|
|
5254
5366
|
return;
|
|
5255
5367
|
}
|
|
5256
5368
|
const link = linkOf(expression);
|
|
@@ -5295,12 +5407,12 @@ class PromiseChain {
|
|
|
5295
5407
|
this.chain = chain;
|
|
5296
5408
|
this.owner = owner;
|
|
5297
5409
|
this.source = expression.getSourceFile();
|
|
5298
|
-
this.classOwner =
|
|
5410
|
+
this.classOwner = ts18.isMethodDeclaration(owner) && ts18.isClassDeclaration(owner.parent) ? owner.parent : undefined;
|
|
5299
5411
|
context.ensureImports(this.source, expression);
|
|
5300
5412
|
context.useDiagnostics(expression);
|
|
5301
5413
|
for (const link of chain.links) {
|
|
5302
5414
|
for (const value of link.handlers) {
|
|
5303
|
-
if (
|
|
5415
|
+
if (ts18.isArrowFunction(value) || ts18.isFunctionExpression(value)) {
|
|
5304
5416
|
this.collectCaptures(value);
|
|
5305
5417
|
}
|
|
5306
5418
|
}
|
|
@@ -5322,10 +5434,10 @@ class PromiseChain {
|
|
|
5322
5434
|
}
|
|
5323
5435
|
collectCaptures(handler) {
|
|
5324
5436
|
const visit = (node) => {
|
|
5325
|
-
if (
|
|
5437
|
+
if (ts18.isIdentifier(node) && !isPropertyName(node)) {
|
|
5326
5438
|
this.capture(node);
|
|
5327
5439
|
}
|
|
5328
|
-
|
|
5440
|
+
ts18.forEachChild(node, visit);
|
|
5329
5441
|
};
|
|
5330
5442
|
visit(handler.body);
|
|
5331
5443
|
}
|
|
@@ -5333,10 +5445,10 @@ class PromiseChain {
|
|
|
5333
5445
|
const { context } = this;
|
|
5334
5446
|
const key = valueSymbol(context.checker, node);
|
|
5335
5447
|
const declaration = key?.valueDeclaration;
|
|
5336
|
-
if (!key || !declaration || !(
|
|
5448
|
+
if (!key || !declaration || !(ts18.isVariableDeclaration(declaration) || ts18.isParameter(declaration)) || ts18.findAncestor(declaration.parent, ts18.isFunctionLike) !== this.owner || this.captures.has(key)) {
|
|
5337
5449
|
return;
|
|
5338
5450
|
}
|
|
5339
|
-
if (
|
|
5451
|
+
if (ts18.isVariableDeclaration(declaration) && (!ts18.isVariableDeclarationList(declaration.parent) || !(declaration.parent.flags & ts18.NodeFlags.Const))) {
|
|
5340
5452
|
this.context.fail(node, "Continuation captures must be const values; store mutable state on the controller instead.");
|
|
5341
5453
|
}
|
|
5342
5454
|
if (!declaration.type) {
|
|
@@ -5409,12 +5521,12 @@ class PromiseChain {
|
|
|
5409
5521
|
if (!url) {
|
|
5410
5522
|
this.context.fail(root, "fetch requires a URL.");
|
|
5411
5523
|
}
|
|
5412
|
-
if (options && !
|
|
5524
|
+
if (options && !ts18.isObjectLiteralExpression(options)) {
|
|
5413
5525
|
this.context.fail(options, "fetch options must be an inline object.");
|
|
5414
5526
|
}
|
|
5415
5527
|
const parameters = [];
|
|
5416
5528
|
const optionText = options ? options.properties.map((property) => {
|
|
5417
|
-
if (!
|
|
5529
|
+
if (!ts18.isPropertyAssignment(property)) {
|
|
5418
5530
|
this.context.fail(property, "fetch requires named options.");
|
|
5419
5531
|
}
|
|
5420
5532
|
if (property.name.getText(source) !== "json") {
|
|
@@ -5436,20 +5548,20 @@ class PromiseChain {
|
|
|
5436
5548
|
expressionType(node) {
|
|
5437
5549
|
const { context, source } = this;
|
|
5438
5550
|
const { checker } = context;
|
|
5439
|
-
const typeArgument =
|
|
5440
|
-
if (typeArgument &&
|
|
5551
|
+
const typeArgument = ts18.isCallExpression(node) ? node.typeArguments?.[0] : undefined;
|
|
5552
|
+
if (typeArgument && ts18.isCallExpression(node) && [...typeArgumentIntrinsics].some((name) => context.namesSdk(node.expression, name))) {
|
|
5441
5553
|
return context.typeText(typeArgument, source);
|
|
5442
5554
|
}
|
|
5443
|
-
const name =
|
|
5555
|
+
const name = ts18.isPropertyAccessExpression(node) ? node.name : node;
|
|
5444
5556
|
const declaration = context.symbol(name)?.declarations?.find(isAnnotatedDeclaration);
|
|
5445
5557
|
if (declaration) {
|
|
5446
5558
|
return context.typeText(declaration.type, source);
|
|
5447
5559
|
}
|
|
5448
5560
|
const type = checker.getTypeAtLocation(node);
|
|
5449
|
-
if (type.flags &
|
|
5561
|
+
if (type.flags & ts18.TypeFlags.BooleanLike) {
|
|
5450
5562
|
return "BOOL";
|
|
5451
5563
|
}
|
|
5452
|
-
if (type.flags &
|
|
5564
|
+
if (type.flags & ts18.TypeFlags.Void) {
|
|
5453
5565
|
return "void";
|
|
5454
5566
|
}
|
|
5455
5567
|
return namedRecordText(checker, node) ?? this.context.fail(node, "Annotate this value with its concrete PLC type before using it in an async continuation.");
|
|
@@ -5514,7 +5626,7 @@ class PromiseChain {
|
|
|
5514
5626
|
};
|
|
5515
5627
|
}
|
|
5516
5628
|
callback(expression, inputType, slot) {
|
|
5517
|
-
if (!expression || expression.kind ===
|
|
5629
|
+
if (!expression || expression.kind === ts18.SyntaxKind.NullKeyword || ts18.isIdentifier(expression) && expression.text === "undefined") {
|
|
5518
5630
|
return;
|
|
5519
5631
|
}
|
|
5520
5632
|
const { context, source } = this;
|
|
@@ -5534,7 +5646,7 @@ class PromiseChain {
|
|
|
5534
5646
|
const names = new Map(this.captureNames);
|
|
5535
5647
|
const parameter = declaration.parameters[0];
|
|
5536
5648
|
if (parameter) {
|
|
5537
|
-
const symbol =
|
|
5649
|
+
const symbol = ts18.isIdentifier(parameter.name) ? context.symbol(parameter.name) : undefined;
|
|
5538
5650
|
if (!symbol) {
|
|
5539
5651
|
this.context.fail(parameter, "Promise callback parameters need names.");
|
|
5540
5652
|
}
|
|
@@ -5559,12 +5671,12 @@ class PromiseChain {
|
|
|
5559
5671
|
};
|
|
5560
5672
|
}
|
|
5561
5673
|
handlerDeclaration(expression) {
|
|
5562
|
-
if (
|
|
5674
|
+
if (ts18.isArrowFunction(expression) || ts18.isFunctionExpression(expression)) {
|
|
5563
5675
|
return expression;
|
|
5564
5676
|
}
|
|
5565
5677
|
const key = this.context.symbol(expression);
|
|
5566
5678
|
const named = key && this.context.routines.get(key);
|
|
5567
|
-
if (!named || !
|
|
5679
|
+
if (!named || !ts18.isFunctionDeclaration(named)) {
|
|
5568
5680
|
this.context.fail(expression, "Use a named function or inline arrow; bind instance methods with an arrow.");
|
|
5569
5681
|
}
|
|
5570
5682
|
return named;
|
|
@@ -5582,7 +5694,7 @@ class PromiseChain {
|
|
|
5582
5694
|
const callbackType = context.typeText(promisedType(result) ?? result, source);
|
|
5583
5695
|
const name = expression.getText(source);
|
|
5584
5696
|
const takesInput = declaration.parameters.length > 0;
|
|
5585
|
-
if (hasModifier(declaration,
|
|
5697
|
+
if (hasModifier(declaration, ts18.SyntaxKind.AsyncKeyword)) {
|
|
5586
5698
|
return {
|
|
5587
5699
|
call: (input) => `${name}(${takesInput ? input : ""})`,
|
|
5588
5700
|
type: callbackType,
|
|
@@ -5607,21 +5719,21 @@ class PromiseChain {
|
|
|
5607
5719
|
}
|
|
5608
5720
|
const resolved = context.checker.getSignatureFromDeclaration(declaration);
|
|
5609
5721
|
const inferred = resolved && context.checker.getAwaitedType(context.checker.getReturnTypeOfSignature(resolved));
|
|
5610
|
-
if (inferred && inferred.flags &
|
|
5722
|
+
if (inferred && inferred.flags & ts18.TypeFlags.Void) {
|
|
5611
5723
|
return "void";
|
|
5612
5724
|
}
|
|
5613
|
-
if (
|
|
5725
|
+
if (ts18.isBlock(body)) {
|
|
5614
5726
|
this.context.fail(declaration, "Annotate a callback that returns a value with its concrete PLC type.");
|
|
5615
5727
|
}
|
|
5616
|
-
if (
|
|
5728
|
+
if (ts18.isIdentifier(body) && parameter && context.symbol(body) === context.symbol(parameter.name)) {
|
|
5617
5729
|
return inputType;
|
|
5618
5730
|
}
|
|
5619
|
-
const returned =
|
|
5731
|
+
const returned = ts18.isCallExpression(body) ? operation(context, body)?.type : undefined;
|
|
5620
5732
|
const promised = returned && promisedType(returned);
|
|
5621
5733
|
return promised ? context.typeText(promised, source) : this.expressionType(body);
|
|
5622
5734
|
}
|
|
5623
5735
|
callbackCode(body, printed, type) {
|
|
5624
|
-
if (
|
|
5736
|
+
if (ts18.isBlock(body)) {
|
|
5625
5737
|
return printed.slice(1, -1);
|
|
5626
5738
|
}
|
|
5627
5739
|
if (type === "void") {
|
|
@@ -5631,42 +5743,42 @@ class PromiseChain {
|
|
|
5631
5743
|
}
|
|
5632
5744
|
replaceNames(node, names) {
|
|
5633
5745
|
const { context } = this;
|
|
5634
|
-
const transformed =
|
|
5746
|
+
const transformed = ts18.transform(node, [
|
|
5635
5747
|
(transformation) => (root) => {
|
|
5636
5748
|
const visit = (value) => {
|
|
5637
|
-
if (
|
|
5638
|
-
return
|
|
5749
|
+
if (ts18.isReturnStatement(value) && value.expression && !ts18.isAwaitExpression(value.expression) && this.isPromise(value.expression)) {
|
|
5750
|
+
return ts18.factory.updateReturnStatement(value, ts18.factory.createAwaitExpression(ts18.visitNode(value.expression, visit, ts18.isExpression)));
|
|
5639
5751
|
}
|
|
5640
|
-
if (
|
|
5752
|
+
if (ts18.isShorthandPropertyAssignment(value)) {
|
|
5641
5753
|
const key = context.checker.getShorthandAssignmentValueSymbol(value);
|
|
5642
5754
|
const name = key && names.get(key);
|
|
5643
5755
|
if (name) {
|
|
5644
|
-
return
|
|
5756
|
+
return ts18.factory.createPropertyAssignment(value.name, ts18.factory.createIdentifier(name));
|
|
5645
5757
|
}
|
|
5646
5758
|
}
|
|
5647
|
-
if (
|
|
5759
|
+
if (ts18.isIdentifier(value) && !isPropertyName(value)) {
|
|
5648
5760
|
const key = context.symbol(value);
|
|
5649
5761
|
const name = key && names.get(key);
|
|
5650
5762
|
if (name) {
|
|
5651
|
-
return
|
|
5763
|
+
return ts18.factory.createIdentifier(name);
|
|
5652
5764
|
}
|
|
5653
5765
|
}
|
|
5654
|
-
return
|
|
5766
|
+
return ts18.visitEachChild(value, visit, transformation);
|
|
5655
5767
|
};
|
|
5656
|
-
const visited =
|
|
5768
|
+
const visited = ts18.visitNode(root, visit);
|
|
5657
5769
|
if (!visited) {
|
|
5658
5770
|
throw new Error("A callback body lowered to nothing.");
|
|
5659
5771
|
}
|
|
5660
5772
|
return visited;
|
|
5661
5773
|
}
|
|
5662
5774
|
]);
|
|
5663
|
-
const printed = transformed.transformed.map((root) => printer.printNode(
|
|
5775
|
+
const printed = transformed.transformed.map((root) => printer.printNode(ts18.EmitHint.Unspecified, root, node.getSourceFile())).join("");
|
|
5664
5776
|
transformed.dispose();
|
|
5665
5777
|
return printed;
|
|
5666
5778
|
}
|
|
5667
5779
|
}
|
|
5668
|
-
var printer =
|
|
5669
|
-
var isAnnotatedDeclaration = (node) => (
|
|
5780
|
+
var printer = ts18.createPrinter();
|
|
5781
|
+
var isAnnotatedDeclaration = (node) => (ts18.isVariableDeclaration(node) || ts18.isParameter(node) || ts18.isPropertyDeclaration(node) || ts18.isPropertySignature(node)) && node.type !== undefined;
|
|
5670
5782
|
function linkBody(previous, {
|
|
5671
5783
|
fulfilled,
|
|
5672
5784
|
rejected,
|
|
@@ -5701,10 +5813,10 @@ var unique = (pending) => [
|
|
|
5701
5813
|
var anyOf = (pending) => unique(pending).map((item) => item.flag).join(" || ");
|
|
5702
5814
|
var unbraced2 = (statements) => {
|
|
5703
5815
|
const [only, ...others] = statements;
|
|
5704
|
-
return only && !others.length &&
|
|
5816
|
+
return only && !others.length && ts19.isBlock(only) ? only.statements : statements;
|
|
5705
5817
|
};
|
|
5706
|
-
var isJump = (statement) => statement !== undefined && (
|
|
5707
|
-
var containsAwaitOrThrow = (node) =>
|
|
5818
|
+
var isJump = (statement) => statement !== undefined && (ts19.isBreakStatement(statement) || ts19.isContinueStatement(statement) || ts19.isReturnStatement(statement) || ts19.isThrowStatement(statement));
|
|
5819
|
+
var containsAwaitOrThrow = (node) => ts19.isAwaitExpression(node) || ts19.isThrowStatement(node) || !ts19.isFunctionLike(node) && (ts19.forEachChild(node, containsAwaitOrThrow) ?? false);
|
|
5708
5820
|
var isNative = (kind, target, crossing) => {
|
|
5709
5821
|
if (crossing.some((item) => item.kind === "finally" && item.region !== "finally")) {
|
|
5710
5822
|
return false;
|
|
@@ -5782,16 +5894,16 @@ var lower = (node, routine) => {
|
|
|
5782
5894
|
return lowered.changed ? lowered : unchanged(node);
|
|
5783
5895
|
};
|
|
5784
5896
|
var lowerStatement = (node, routine) => {
|
|
5785
|
-
if (
|
|
5897
|
+
if (ts19.isLabeledStatement(node)) {
|
|
5786
5898
|
return labelled(node, routine);
|
|
5787
5899
|
}
|
|
5788
|
-
if (
|
|
5900
|
+
if (ts19.isIterationStatement(node, false)) {
|
|
5789
5901
|
return loop(node, [], routine);
|
|
5790
5902
|
}
|
|
5791
|
-
if (
|
|
5903
|
+
if (ts19.isSwitchStatement(node)) {
|
|
5792
5904
|
return switchStatement(node, [], routine);
|
|
5793
5905
|
}
|
|
5794
|
-
if (
|
|
5906
|
+
if (ts19.isBlock(node)) {
|
|
5795
5907
|
const list = lowerList(node.statements, undefined, routine);
|
|
5796
5908
|
return {
|
|
5797
5909
|
text: from(node, mapped`{ ${joined(list.parts, " ")} }`),
|
|
@@ -5799,23 +5911,23 @@ var lowerStatement = (node, routine) => {
|
|
|
5799
5911
|
changed: list.changed
|
|
5800
5912
|
};
|
|
5801
5913
|
}
|
|
5802
|
-
if (
|
|
5914
|
+
if (ts19.isIfStatement(node)) {
|
|
5803
5915
|
return ifStatement(node, routine);
|
|
5804
5916
|
}
|
|
5805
|
-
if (
|
|
5917
|
+
if (ts19.isTryStatement(node)) {
|
|
5806
5918
|
return node.finallyBlock ? finallyStatement(node, node.finallyBlock, routine) : tryStatement(node, routine);
|
|
5807
5919
|
}
|
|
5808
|
-
if (
|
|
5920
|
+
if (ts19.isBreakStatement(node) || ts19.isContinueStatement(node) || ts19.isReturnStatement(node)) {
|
|
5809
5921
|
return jump(node, routine);
|
|
5810
5922
|
}
|
|
5811
5923
|
return unchanged(node);
|
|
5812
5924
|
};
|
|
5813
5925
|
var labelled = (node, routine) => {
|
|
5814
5926
|
const { labels, inner } = unlabelled(node);
|
|
5815
|
-
if (
|
|
5927
|
+
if (ts19.isIterationStatement(inner, false)) {
|
|
5816
5928
|
return { ...loop(inner, labels, routine), changed: true };
|
|
5817
5929
|
}
|
|
5818
|
-
if (
|
|
5930
|
+
if (ts19.isSwitchStatement(inner)) {
|
|
5819
5931
|
return { ...switchStatement(inner, labels, routine), changed: true };
|
|
5820
5932
|
}
|
|
5821
5933
|
const target = {
|
|
@@ -5833,7 +5945,7 @@ var labelled = (node, routine) => {
|
|
|
5833
5945
|
};
|
|
5834
5946
|
};
|
|
5835
5947
|
var unlabelled = (node) => {
|
|
5836
|
-
if (!
|
|
5948
|
+
if (!ts19.isLabeledStatement(node.statement)) {
|
|
5837
5949
|
return { labels: [node.label.text], inner: node.statement };
|
|
5838
5950
|
}
|
|
5839
5951
|
const nested = unlabelled(node.statement);
|
|
@@ -5847,7 +5959,7 @@ var loop = (node, labels, routine) => {
|
|
|
5847
5959
|
};
|
|
5848
5960
|
routine.stack.push(target);
|
|
5849
5961
|
const body = node.statement;
|
|
5850
|
-
const list = lowerList(
|
|
5962
|
+
const list = lowerList(ts19.isBlock(body) ? body.statements : [body], target, routine);
|
|
5851
5963
|
routine.stack.pop();
|
|
5852
5964
|
if (!list.changed) {
|
|
5853
5965
|
return unchanged(node);
|
|
@@ -5886,7 +5998,7 @@ var switchStatement = (node, labels, routine) => {
|
|
|
5886
5998
|
const { source } = routine;
|
|
5887
5999
|
const bodies = node.caseBlock.clauses.map((clause) => unbraced2(clause.statements));
|
|
5888
6000
|
const clauses = node.caseBlock.clauses.map((clause, index) => {
|
|
5889
|
-
const label =
|
|
6001
|
+
const label = ts19.isCaseClause(clause) ? mapped`case ${copied(clause.expression)}:` : "default:";
|
|
5890
6002
|
if (!bodies[index]?.length) {
|
|
5891
6003
|
return { text: from(clause, label), pending: [], changed: false };
|
|
5892
6004
|
}
|
|
@@ -5902,7 +6014,7 @@ var switchStatement = (node, labels, routine) => {
|
|
|
5902
6014
|
}
|
|
5903
6015
|
const last = chain.at(-1) ?? [];
|
|
5904
6016
|
const end = last.at(-1);
|
|
5905
|
-
const ending = end &&
|
|
6017
|
+
const ending = end && ts19.isBreakStatement(end) && !end.label ? last.slice(0, -1) : last;
|
|
5906
6018
|
const pieces = [...chain.slice(0, -1), ending];
|
|
5907
6019
|
const items = pieces.length === 1 ? statementItems(ending, routine) : pieces.map((piece) => ({
|
|
5908
6020
|
origin: piece[0] ?? clause,
|
|
@@ -5916,7 +6028,7 @@ var switchStatement = (node, labels, routine) => {
|
|
|
5916
6028
|
}
|
|
5917
6029
|
}));
|
|
5918
6030
|
const list = lowerItems(items, undefined);
|
|
5919
|
-
const returns = end !== undefined && (
|
|
6031
|
+
const returns = end !== undefined && (ts19.isReturnStatement(end) || ts19.isThrowStatement(end)) && !list.pending.length;
|
|
5920
6032
|
const body = joined(list.parts, " ");
|
|
5921
6033
|
return {
|
|
5922
6034
|
text: from(clause, returns ? mapped`${label} { ${body} }` : mapped`${label} { ${body} } break;`),
|
|
@@ -6036,7 +6148,7 @@ var jump = (node, routine) => {
|
|
|
6036
6148
|
return { text: from(node, `${kind};`), pending: [], changed: true };
|
|
6037
6149
|
}
|
|
6038
6150
|
const flag = flagFor(kind, target, routine);
|
|
6039
|
-
const value =
|
|
6151
|
+
const value = ts19.isReturnStatement(node) && node.expression && routine.result?.value ? mapped`${routine.result.value} = ${copied(node.expression)}; ` : "";
|
|
6040
6152
|
const exit = innermostExit(crossing) === "loop" ? " break;" : "";
|
|
6041
6153
|
return {
|
|
6042
6154
|
text: from(node, mapped`{ ${flag} = true; ${value}${exit} }`),
|
|
@@ -6045,10 +6157,10 @@ var jump = (node, routine) => {
|
|
|
6045
6157
|
};
|
|
6046
6158
|
};
|
|
6047
6159
|
var jumpOf = (node) => {
|
|
6048
|
-
if (
|
|
6160
|
+
if (ts19.isBreakStatement(node)) {
|
|
6049
6161
|
return { kind: "break", label: node.label?.text };
|
|
6050
6162
|
}
|
|
6051
|
-
if (
|
|
6163
|
+
if (ts19.isContinueStatement(node)) {
|
|
6052
6164
|
return { kind: "continue", label: node.label?.text };
|
|
6053
6165
|
}
|
|
6054
6166
|
return { kind: "return", label: undefined };
|
|
@@ -6068,16 +6180,16 @@ var flagFor = (kind, target, routine) => {
|
|
|
6068
6180
|
target.breakFlag ??= routine.name("tsplcBreak");
|
|
6069
6181
|
return target.breakFlag;
|
|
6070
6182
|
};
|
|
6071
|
-
var isRoutine = (node) =>
|
|
6183
|
+
var isRoutine = (node) => ts19.isFunctionDeclaration(node) || ts19.isMethodDeclaration(node) || ts19.isGetAccessorDeclaration(node) || ts19.isSetAccessorDeclaration(node) || ts19.isConstructorDeclaration(node) || ts19.isArrowFunction(node) || ts19.isFunctionExpression(node);
|
|
6072
6184
|
var resultType = (node, checker) => {
|
|
6073
|
-
if (
|
|
6185
|
+
if (ts19.isConstructorDeclaration(node) || ts19.isSetAccessorDeclaration(node)) {
|
|
6074
6186
|
return;
|
|
6075
6187
|
}
|
|
6076
6188
|
const annotation = node.type;
|
|
6077
|
-
const isAsync = hasModifier(node,
|
|
6189
|
+
const isAsync = hasModifier(node, ts19.SyntaxKind.AsyncKeyword);
|
|
6078
6190
|
if (annotation) {
|
|
6079
6191
|
const inner = isAsync ? promisedType(annotation) : annotation;
|
|
6080
|
-
if (!inner || inner.kind ===
|
|
6192
|
+
if (!inner || inner.kind === ts19.SyntaxKind.VoidKeyword) {
|
|
6081
6193
|
return;
|
|
6082
6194
|
}
|
|
6083
6195
|
return inner.getText();
|
|
@@ -6088,7 +6200,7 @@ var resultType = (node, checker) => {
|
|
|
6088
6200
|
}
|
|
6089
6201
|
const returns = checker.getReturnTypeOfSignature(signature);
|
|
6090
6202
|
const awaited = isAsync ? checker.getAwaitedType(returns) : returns;
|
|
6091
|
-
if (!awaited || awaited.flags &
|
|
6203
|
+
if (!awaited || awaited.flags & ts19.TypeFlags.Void) {
|
|
6092
6204
|
return;
|
|
6093
6205
|
}
|
|
6094
6206
|
return checker.typeToString(awaited);
|
|
@@ -6110,14 +6222,14 @@ var lowerControlFlow = (program) => {
|
|
|
6110
6222
|
const uses = new Set;
|
|
6111
6223
|
const name = (base) => `${base}${++counter}`;
|
|
6112
6224
|
const visit = (node) => {
|
|
6113
|
-
if (!isRoutine(node) || !node.body || !
|
|
6114
|
-
|
|
6225
|
+
if (!isRoutine(node) || !node.body || !ts19.isBlock(node.body)) {
|
|
6226
|
+
ts19.forEachChild(node, visit);
|
|
6115
6227
|
return;
|
|
6116
6228
|
}
|
|
6117
6229
|
const body = node.body;
|
|
6118
6230
|
const routine = {
|
|
6119
6231
|
source,
|
|
6120
|
-
async: hasModifier(node,
|
|
6232
|
+
async: hasModifier(node, ts19.SyntaxKind.AsyncKeyword) || promiseCallback(node),
|
|
6121
6233
|
stack: [],
|
|
6122
6234
|
resultType: resultType(node, checker),
|
|
6123
6235
|
name,
|
|
@@ -6125,7 +6237,7 @@ var lowerControlFlow = (program) => {
|
|
|
6125
6237
|
};
|
|
6126
6238
|
const list = lowerList(body.statements, undefined, routine);
|
|
6127
6239
|
if (!list.changed) {
|
|
6128
|
-
|
|
6240
|
+
ts19.forEachChild(node, visit);
|
|
6129
6241
|
return;
|
|
6130
6242
|
}
|
|
6131
6243
|
const declarations = [];
|
|
@@ -6163,8 +6275,8 @@ var lowerControlFlow = (program) => {
|
|
|
6163
6275
|
};
|
|
6164
6276
|
|
|
6165
6277
|
// packages/compiler/src/frontend/dialect-names.ts
|
|
6166
|
-
import { error as
|
|
6167
|
-
import
|
|
6278
|
+
import { error as error7 } from "@tsplc/compiler/ir";
|
|
6279
|
+
import ts20 from "typescript";
|
|
6168
6280
|
var signals = "@tsplc/compiler/signals";
|
|
6169
6281
|
var unsupported = {
|
|
6170
6282
|
FunctionBlock: "write a plain class, whose instances keep their fields from one scan to the next",
|
|
@@ -6194,14 +6306,14 @@ var dialectUses = (program, sdk, positions) => {
|
|
|
6194
6306
|
}
|
|
6195
6307
|
const reported = new Set;
|
|
6196
6308
|
const visit = (node) => {
|
|
6197
|
-
if (
|
|
6309
|
+
if (ts20.isIdentifier(node) && !reported.has(node.text)) {
|
|
6198
6310
|
const message = dialectUse(checker, sdk, dialect, node);
|
|
6199
6311
|
if (message) {
|
|
6200
6312
|
reported.add(node.text);
|
|
6201
|
-
diagnostics.push(
|
|
6313
|
+
diagnostics.push(error7("PLC1015", message, positions.node(node)));
|
|
6202
6314
|
}
|
|
6203
6315
|
}
|
|
6204
|
-
|
|
6316
|
+
ts20.forEachChild(node, visit);
|
|
6205
6317
|
};
|
|
6206
6318
|
visit(source);
|
|
6207
6319
|
}
|
|
@@ -6232,15 +6344,15 @@ var dialectUse = (checker, sdk, dialect, node) => {
|
|
|
6232
6344
|
// packages/compiler/src/frontend/elaborate/index.ts
|
|
6233
6345
|
import { dirname as dirname5, join as join3, resolve as resolve6 } from "node:path";
|
|
6234
6346
|
import {
|
|
6235
|
-
error as
|
|
6347
|
+
error as error8,
|
|
6236
6348
|
identityDigest as identityDigest6
|
|
6237
6349
|
} from "@tsplc/compiler/ir";
|
|
6238
6350
|
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
6239
|
-
import
|
|
6351
|
+
import ts43 from "typescript";
|
|
6240
6352
|
|
|
6241
6353
|
// packages/compiler/src/frontend/elaborate/generics.ts
|
|
6242
|
-
import
|
|
6243
|
-
var isTypeReference = (type) => Boolean(type.flags &
|
|
6354
|
+
import ts21 from "typescript";
|
|
6355
|
+
var isTypeReference = (type) => Boolean(type.flags & ts21.TypeFlags.Object) && ("objectFlags" in type) && typeof type.objectFlags === "number" && Boolean(type.objectFlags & ts21.ObjectFlags.Reference);
|
|
6244
6356
|
|
|
6245
6357
|
class GenericTypes {
|
|
6246
6358
|
base;
|
|
@@ -6299,7 +6411,7 @@ class GenericTypes {
|
|
|
6299
6411
|
if (!this.scopes.length) {
|
|
6300
6412
|
return type;
|
|
6301
6413
|
}
|
|
6302
|
-
if (type.flags &
|
|
6414
|
+
if (type.flags & ts21.TypeFlags.TypeParameter) {
|
|
6303
6415
|
for (let index = this.scopes.length - 1;index >= 0; index--) {
|
|
6304
6416
|
const found = this.scopes[index]?.get(type);
|
|
6305
6417
|
if (found) {
|
|
@@ -6328,11 +6440,11 @@ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
|
6328
6440
|
import { tmpdir } from "node:os";
|
|
6329
6441
|
import { join as join2 } from "node:path";
|
|
6330
6442
|
import { fileURLToPath } from "node:url";
|
|
6331
|
-
import
|
|
6443
|
+
import ts24 from "typescript";
|
|
6332
6444
|
import { z as z2 } from "zod";
|
|
6333
6445
|
|
|
6334
6446
|
// packages/compiler/src/frontend/elaborate/capture-protocol.ts
|
|
6335
|
-
import
|
|
6447
|
+
import ts22 from "typescript";
|
|
6336
6448
|
import { z } from "zod";
|
|
6337
6449
|
var closureSiteSchema = z.strictObject({
|
|
6338
6450
|
start: z.number().int().nonnegative(),
|
|
@@ -6351,21 +6463,21 @@ var captureRequestSchema = z.strictObject({
|
|
|
6351
6463
|
bindings: z.record(z.string(), bindingNamesSchema),
|
|
6352
6464
|
configuration: z.record(z.string(), z.string())
|
|
6353
6465
|
});
|
|
6354
|
-
var boundNames = (name) =>
|
|
6466
|
+
var boundNames = (name) => ts22.isIdentifier(name) ? [name] : name.elements.flatMap((element) => ts22.isOmittedExpression(element) ? [] : boundNames(element.name));
|
|
6355
6467
|
var bindingNames = (source) => {
|
|
6356
6468
|
const found = { values: [], classes: [], functions: [] };
|
|
6357
6469
|
for (const statement of source.statements) {
|
|
6358
|
-
if (
|
|
6470
|
+
if (ts22.isVariableStatement(statement)) {
|
|
6359
6471
|
for (const declaration of statement.declarationList.declarations) {
|
|
6360
|
-
if (
|
|
6472
|
+
if (ts22.isIdentifier(declaration.name) && declaration.initializer && (ts22.isArrowFunction(declaration.initializer) || ts22.isFunctionExpression(declaration.initializer))) {
|
|
6361
6473
|
found.functions.push(declaration.name.text);
|
|
6362
6474
|
} else {
|
|
6363
6475
|
found.values.push(...boundNames(declaration.name).map((name) => name.text));
|
|
6364
6476
|
}
|
|
6365
6477
|
}
|
|
6366
|
-
} else if (
|
|
6478
|
+
} else if (ts22.isClassDeclaration(statement) && statement.name) {
|
|
6367
6479
|
found.classes.push(statement.name.text);
|
|
6368
|
-
} else if (
|
|
6480
|
+
} else if (ts22.isFunctionDeclaration(statement) && statement.name && statement.body) {
|
|
6369
6481
|
found.functions.push(statement.name.text);
|
|
6370
6482
|
}
|
|
6371
6483
|
}
|
|
@@ -6452,7 +6564,7 @@ var capturedGraphSchema = z.strictObject({
|
|
|
6452
6564
|
});
|
|
6453
6565
|
|
|
6454
6566
|
// packages/compiler/src/frontend/elaborate/writes.ts
|
|
6455
|
-
import
|
|
6567
|
+
import ts23 from "typescript";
|
|
6456
6568
|
class ScanWrites {
|
|
6457
6569
|
checker;
|
|
6458
6570
|
assigned = new Set;
|
|
@@ -6472,27 +6584,27 @@ class ScanWrites {
|
|
|
6472
6584
|
});
|
|
6473
6585
|
}
|
|
6474
6586
|
collectAssignments(node, inConstructor) {
|
|
6475
|
-
const constructing =
|
|
6587
|
+
const constructing = ts23.isConstructorDeclaration(node) || inConstructor && !ts23.isArrowFunction(node) && !ts23.isFunctionExpression(node);
|
|
6476
6588
|
if (!constructing) {
|
|
6477
6589
|
const write = (target) => {
|
|
6478
6590
|
const place = unparenthesized(target, { nonNull: true });
|
|
6479
|
-
if (
|
|
6591
|
+
if (ts23.isPropertyAccessExpression(place)) {
|
|
6480
6592
|
this.markAssigned(place.name);
|
|
6481
|
-
} else if (
|
|
6593
|
+
} else if (ts23.isElementAccessExpression(place)) {
|
|
6482
6594
|
const base = indexed(place.expression);
|
|
6483
|
-
if (
|
|
6595
|
+
if (ts23.isPropertyAccessExpression(base)) {
|
|
6484
6596
|
this.markAssigned(base.name);
|
|
6485
6597
|
}
|
|
6486
6598
|
}
|
|
6487
6599
|
};
|
|
6488
|
-
if (
|
|
6600
|
+
if (ts23.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) {
|
|
6489
6601
|
assignedTargets(node.left).forEach(write);
|
|
6490
6602
|
}
|
|
6491
6603
|
if (isUpdate(node)) {
|
|
6492
6604
|
write(node.operand);
|
|
6493
6605
|
}
|
|
6494
6606
|
}
|
|
6495
|
-
|
|
6607
|
+
ts23.forEachChild(node, (child) => this.collectAssignments(child, constructing));
|
|
6496
6608
|
}
|
|
6497
6609
|
markAssigned(name) {
|
|
6498
6610
|
const symbol = this.checker.getSymbolAtLocation(name);
|
|
@@ -6508,38 +6620,38 @@ class ScanWrites {
|
|
|
6508
6620
|
}
|
|
6509
6621
|
}
|
|
6510
6622
|
ownSymbol(declaration) {
|
|
6511
|
-
const name =
|
|
6623
|
+
const name = ts23.getNameOfDeclaration(declaration);
|
|
6512
6624
|
return name && this.checker.getSymbolAtLocation(name);
|
|
6513
6625
|
}
|
|
6514
6626
|
}
|
|
6515
|
-
var indexed = (node) =>
|
|
6516
|
-
var isUpdate = (node) => (
|
|
6517
|
-
var isAssignment = (kind) => kind >=
|
|
6627
|
+
var indexed = (node) => ts23.isElementAccessExpression(node) ? indexed(node.expression) : node;
|
|
6628
|
+
var isUpdate = (node) => (ts23.isPrefixUnaryExpression(node) || ts23.isPostfixUnaryExpression(node)) && (node.operator === ts23.SyntaxKind.PlusPlusToken || node.operator === ts23.SyntaxKind.MinusMinusToken);
|
|
6629
|
+
var isAssignment = (kind) => kind >= ts23.SyntaxKind.FirstAssignment && kind <= ts23.SyntaxKind.LastAssignment;
|
|
6518
6630
|
var assignedTargets = (left) => {
|
|
6519
6631
|
const place = unparenthesized(left, { nonNull: true });
|
|
6520
|
-
if (
|
|
6521
|
-
return place.elements.flatMap((element) => assignedTargets(
|
|
6632
|
+
if (ts23.isArrayLiteralExpression(place)) {
|
|
6633
|
+
return place.elements.flatMap((element) => assignedTargets(ts23.isSpreadElement(element) ? element.expression : element));
|
|
6522
6634
|
}
|
|
6523
|
-
if (
|
|
6635
|
+
if (ts23.isObjectLiteralExpression(place)) {
|
|
6524
6636
|
return place.properties.flatMap((property) => {
|
|
6525
|
-
if (
|
|
6637
|
+
if (ts23.isPropertyAssignment(property)) {
|
|
6526
6638
|
return assignedTargets(property.initializer);
|
|
6527
6639
|
}
|
|
6528
|
-
if (
|
|
6640
|
+
if (ts23.isShorthandPropertyAssignment(property)) {
|
|
6529
6641
|
return [property.name];
|
|
6530
6642
|
}
|
|
6531
|
-
if (
|
|
6643
|
+
if (ts23.isSpreadAssignment(property)) {
|
|
6532
6644
|
return assignedTargets(property.expression);
|
|
6533
6645
|
}
|
|
6534
6646
|
return [];
|
|
6535
6647
|
});
|
|
6536
6648
|
}
|
|
6537
|
-
if (
|
|
6649
|
+
if (ts23.isBinaryExpression(place) && place.operatorToken.kind === ts23.SyntaxKind.EqualsToken) {
|
|
6538
6650
|
return assignedTargets(place.left);
|
|
6539
6651
|
}
|
|
6540
6652
|
return [left];
|
|
6541
6653
|
};
|
|
6542
|
-
var occursOutsideFunctions = (node, test) => test(node) || !
|
|
6654
|
+
var occursOutsideFunctions = (node, test) => test(node) || !ts23.isFunctionLike(node) && (ts23.forEachChild(node, (child) => occursOutsideFunctions(child, test)) ?? false);
|
|
6543
6655
|
var within = (node, container) => node.pos >= container.pos && node.end <= container.end;
|
|
6544
6656
|
var assignsName = (body, binding, checker) => {
|
|
6545
6657
|
const declared = new Set(boundNames(binding).flatMap((name) => {
|
|
@@ -6550,40 +6662,40 @@ var assignsName = (body, binding, checker) => {
|
|
|
6550
6662
|
};
|
|
6551
6663
|
var assignsSymbol = (body, declared, checker) => {
|
|
6552
6664
|
const target = (node) => {
|
|
6553
|
-
const symbol =
|
|
6665
|
+
const symbol = ts23.isIdentifier(node) ? valueSymbol(checker, node) : undefined;
|
|
6554
6666
|
return symbol !== undefined && declared.has(symbol);
|
|
6555
6667
|
};
|
|
6556
|
-
const visit = (node) =>
|
|
6668
|
+
const visit = (node) => ts23.isBinaryExpression(node) && isAssignment(node.operatorToken.kind) && assignedTargets(node.left).some(target) || isUpdate(node) && target(node.operand) || (ts23.forEachChild(node, visit) ?? false);
|
|
6557
6669
|
return visit(body);
|
|
6558
6670
|
};
|
|
6559
6671
|
var writesThrough = (body, symbol, checker) => {
|
|
6560
|
-
const root = (node) =>
|
|
6561
|
-
const named = (node) =>
|
|
6672
|
+
const root = (node) => ts23.isPropertyAccessExpression(node) || ts23.isElementAccessExpression(node) || ts23.isParenthesizedExpression(node) || ts23.isNonNullExpression(node) ? root(node.expression) : node;
|
|
6673
|
+
const named = (node) => ts23.isIdentifier(node) && checker.getSymbolAtLocation(node) === symbol;
|
|
6562
6674
|
const throughIt = (target) => target !== root(target) && named(root(target));
|
|
6563
|
-
const visit = (node) =>
|
|
6675
|
+
const visit = (node) => ts23.isBinaryExpression(node) && isAssignment(node.operatorToken.kind) && assignedTargets(node.left).some(throughIt) || isUpdate(node) && throughIt(node.operand) || ts23.isCallExpression(node) && node.arguments.some(named) || (ts23.forEachChild(node, visit) ?? false);
|
|
6564
6676
|
return visit(body);
|
|
6565
6677
|
};
|
|
6566
6678
|
var aliasWindow = (name, checker) => {
|
|
6567
6679
|
const symbol = checker.getSymbolAtLocation(name);
|
|
6568
6680
|
const scope = enclosingScope(name);
|
|
6569
|
-
const binding =
|
|
6570
|
-
const declaration = binding ?
|
|
6571
|
-
if (!symbol || !scope || !declaration || !
|
|
6681
|
+
const binding = ts23.isBindingElement(name.parent) ? name.parent : undefined;
|
|
6682
|
+
const declaration = binding ? ts23.findAncestor(binding, ts23.isVariableDeclaration) : name.parent;
|
|
6683
|
+
if (!symbol || !scope || !declaration || !ts23.isVariableDeclaration(declaration) || !declaration.initializer || binding && !ts23.isVariableDeclaration(binding.parent.parent)) {
|
|
6572
6684
|
return;
|
|
6573
6685
|
}
|
|
6574
6686
|
const uses = [];
|
|
6575
6687
|
const collect = (node) => {
|
|
6576
|
-
if (
|
|
6688
|
+
if (ts23.isIdentifier(node) && node !== name && valueSymbol(checker, node) === symbol) {
|
|
6577
6689
|
uses.push(node);
|
|
6578
6690
|
}
|
|
6579
|
-
|
|
6691
|
+
ts23.forEachChild(node, collect);
|
|
6580
6692
|
};
|
|
6581
6693
|
collect(scope);
|
|
6582
|
-
const reentered = (use) =>
|
|
6694
|
+
const reentered = (use) => ts23.findAncestor(use.parent, (node) => {
|
|
6583
6695
|
if (node === scope) {
|
|
6584
6696
|
return "quit";
|
|
6585
6697
|
}
|
|
6586
|
-
return (
|
|
6698
|
+
return (ts23.isIterationStatement(node, false) || ts23.isFunctionLike(node)) && !within(declaration, node);
|
|
6587
6699
|
}) !== undefined;
|
|
6588
6700
|
const from = declaration.end;
|
|
6589
6701
|
return {
|
|
@@ -6595,22 +6707,22 @@ var aliasWindow = (name, checker) => {
|
|
|
6595
6707
|
};
|
|
6596
6708
|
};
|
|
6597
6709
|
var runsBetween = ({ scope, from, until }, matches) => {
|
|
6598
|
-
const visit = (node) => node.getStart() >= from && node.end <= until && matches(node) || (
|
|
6710
|
+
const visit = (node) => node.getStart() >= from && node.end <= until && matches(node) || (ts23.forEachChild(node, visit) ?? false);
|
|
6599
6711
|
return visit(scope);
|
|
6600
6712
|
};
|
|
6601
6713
|
var pathNames = (place) => {
|
|
6602
6714
|
const node = unparenthesized(place, { nonNull: true });
|
|
6603
|
-
if (
|
|
6715
|
+
if (ts23.isIdentifier(node)) {
|
|
6604
6716
|
return [node.text];
|
|
6605
6717
|
}
|
|
6606
|
-
if (node.kind ===
|
|
6718
|
+
if (node.kind === ts23.SyntaxKind.ThisKeyword) {
|
|
6607
6719
|
return [];
|
|
6608
6720
|
}
|
|
6609
|
-
if (
|
|
6721
|
+
if (ts23.isPropertyAccessExpression(node)) {
|
|
6610
6722
|
const base = pathNames(node.expression);
|
|
6611
6723
|
return base && [...base, node.name.text];
|
|
6612
6724
|
}
|
|
6613
|
-
if (
|
|
6725
|
+
if (ts23.isElementAccessExpression(node)) {
|
|
6614
6726
|
return pathNames(node.expression);
|
|
6615
6727
|
}
|
|
6616
6728
|
return;
|
|
@@ -6620,7 +6732,7 @@ var writesIn = (window) => {
|
|
|
6620
6732
|
runsBetween(window, (node) => {
|
|
6621
6733
|
if (isUpdate(node)) {
|
|
6622
6734
|
targets.push(node.operand);
|
|
6623
|
-
} else if (
|
|
6735
|
+
} else if (ts23.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) {
|
|
6624
6736
|
targets.push(node.left);
|
|
6625
6737
|
}
|
|
6626
6738
|
return false;
|
|
@@ -6634,17 +6746,17 @@ var stableAlias = (name, checker) => {
|
|
|
6634
6746
|
}
|
|
6635
6747
|
const read = new Set;
|
|
6636
6748
|
const readNames = (node) => {
|
|
6637
|
-
if (
|
|
6749
|
+
if (ts23.isIdentifier(node) || ts23.isPrivateIdentifier(node)) {
|
|
6638
6750
|
read.add(node.text);
|
|
6639
6751
|
}
|
|
6640
|
-
|
|
6752
|
+
ts23.forEachChild(node, readNames);
|
|
6641
6753
|
};
|
|
6642
6754
|
readNames(window.source);
|
|
6643
6755
|
const writesRead = (target) => {
|
|
6644
6756
|
const names = pathNames(target);
|
|
6645
6757
|
return names === undefined || names.some((written) => read.has(written));
|
|
6646
6758
|
};
|
|
6647
|
-
return !writesIn(window).some(writesRead) && !runsBetween(window, (node) =>
|
|
6759
|
+
return !writesIn(window).some(writesRead) && !runsBetween(window, (node) => ts23.isCallExpression(node) || ts23.isNewExpression(node) || ts23.isAwaitExpression(node) || ts23.isYieldExpression(node) || ts23.isTaggedTemplateExpression(node) || ts23.isDeleteExpression(node));
|
|
6648
6760
|
};
|
|
6649
6761
|
var replacedWhileAliased = (name, checker) => {
|
|
6650
6762
|
const window = aliasWindow(name, checker);
|
|
@@ -6659,13 +6771,13 @@ var replacedWhileAliased = (name, checker) => {
|
|
|
6659
6771
|
return last !== undefined && path.includes(last);
|
|
6660
6772
|
});
|
|
6661
6773
|
};
|
|
6662
|
-
var enclosingScope = (node) =>
|
|
6774
|
+
var enclosingScope = (node) => ts23.findAncestor(node.parent, (parent) => ts23.isBlock(parent) || ts23.isSourceFile(parent) || ts23.isCaseClause(parent) || ts23.isDefaultClause(parent) || ts23.isFunctionLike(parent));
|
|
6663
6775
|
|
|
6664
6776
|
// packages/compiler/src/frontend/elaborate/graph.ts
|
|
6665
6777
|
var closureSites = (source, checker) => {
|
|
6666
6778
|
const sites = [];
|
|
6667
6779
|
const visit = (node, enclosing) => {
|
|
6668
|
-
const closure =
|
|
6780
|
+
const closure = ts24.isArrowFunction(node) || ts24.isFunctionExpression(node);
|
|
6669
6781
|
if (closure && !moduleFunction(node)) {
|
|
6670
6782
|
sites.push({
|
|
6671
6783
|
start: node.getStart(source),
|
|
@@ -6673,8 +6785,8 @@ var closureSites = (source, checker) => {
|
|
|
6673
6785
|
captures: enclosing ? captures(node, enclosing, checker) : []
|
|
6674
6786
|
});
|
|
6675
6787
|
}
|
|
6676
|
-
const scope =
|
|
6677
|
-
|
|
6788
|
+
const scope = ts24.isFunctionLike(node) || ts24.isClassLike(node) ? node : enclosing;
|
|
6789
|
+
ts24.forEachChild(node, (child) => visit(child, scope));
|
|
6678
6790
|
};
|
|
6679
6791
|
visit(source, undefined);
|
|
6680
6792
|
return sites;
|
|
@@ -6682,17 +6794,17 @@ var closureSites = (source, checker) => {
|
|
|
6682
6794
|
var captures = (closure, enclosing, checker) => {
|
|
6683
6795
|
const names = new Set;
|
|
6684
6796
|
const visit = (node) => {
|
|
6685
|
-
if (node.kind ===
|
|
6797
|
+
if (node.kind === ts24.SyntaxKind.ThisKeyword && ts24.isArrowFunction(closure)) {
|
|
6686
6798
|
names.add("this");
|
|
6687
6799
|
}
|
|
6688
|
-
if (
|
|
6800
|
+
if (ts24.isIdentifier(node) && !isPropertyName(node)) {
|
|
6689
6801
|
const declaration = valueSymbol(checker, node)?.valueDeclaration;
|
|
6690
|
-
if (declaration && !within(declaration, closure) && within(declaration, enclosing) && !
|
|
6802
|
+
if (declaration && !within(declaration, closure) && within(declaration, enclosing) && !ts24.isClassLike(declaration)) {
|
|
6691
6803
|
names.add(node.text);
|
|
6692
6804
|
}
|
|
6693
6805
|
}
|
|
6694
|
-
if (!
|
|
6695
|
-
|
|
6806
|
+
if (!ts24.isTypeNode(node)) {
|
|
6807
|
+
ts24.forEachChild(node, visit);
|
|
6696
6808
|
}
|
|
6697
6809
|
};
|
|
6698
6810
|
visit(closure.body);
|
|
@@ -6701,7 +6813,7 @@ var captures = (closure, enclosing, checker) => {
|
|
|
6701
6813
|
var moduleFunction = (node) => {
|
|
6702
6814
|
const declaration = node.parent;
|
|
6703
6815
|
const statement = declaration?.parent?.parent;
|
|
6704
|
-
return
|
|
6816
|
+
return ts24.isVariableDeclaration(declaration) && statement !== undefined && ts24.isVariableStatement(statement) && ts24.isSourceFile(statement.parent);
|
|
6705
6817
|
};
|
|
6706
6818
|
var captureGraph = (entry, sources, checker, configuration) => {
|
|
6707
6819
|
const request = {
|
|
@@ -6802,11 +6914,11 @@ class EntryImports {
|
|
|
6802
6914
|
|
|
6803
6915
|
// packages/compiler/src/frontend/elaborate/layouts.ts
|
|
6804
6916
|
import { optionalRecord, pascalCase as pascalCase2, primitiveTypes as primitiveTypes2 } from "@tsplc/compiler/ir";
|
|
6805
|
-
import
|
|
6917
|
+
import ts28 from "typescript";
|
|
6806
6918
|
|
|
6807
6919
|
// packages/compiler/src/frontend/elaborate/model.ts
|
|
6808
6920
|
import { identityDigest as identityDigest3 } from "@tsplc/compiler/ir";
|
|
6809
|
-
import
|
|
6921
|
+
import ts25 from "typescript";
|
|
6810
6922
|
var tsPath = (owner) => owner.root.name + owner.steps.map((step) => stepPath(step, String)).join("");
|
|
6811
6923
|
var stepPath = (step, key) => {
|
|
6812
6924
|
if (step.kind === "index") {
|
|
@@ -6820,8 +6932,8 @@ var ownerAt = (owner, step) => ({
|
|
|
6820
6932
|
});
|
|
6821
6933
|
var sameStep = (left, right) => left.kind === "index" ? right.kind === "index" && left.index === right.index : right.kind === "field" && left.name === right.name;
|
|
6822
6934
|
var unionMembers = (type) => type.isUnion() ? type.types : [type];
|
|
6823
|
-
var isExported = (declaration) => Boolean(
|
|
6824
|
-
var listedExport = (source, name) => source.statements.some((statement) =>
|
|
6935
|
+
var isExported = (declaration) => Boolean(ts25.getCombinedModifierFlags(declaration) & ts25.ModifierFlags.Export);
|
|
6936
|
+
var listedExport = (source, name) => source.statements.some((statement) => ts25.isExportDeclaration(statement) && !statement.moduleSpecifier && !statement.isTypeOnly && statement.exportClause !== undefined && ts25.isNamedExports(statement.exportClause) && statement.exportClause.elements.some((element) => !element.isTypeOnly && (element.propertyName ?? element.name).text === name));
|
|
6825
6937
|
var children = (captured) => {
|
|
6826
6938
|
if (captured.kind === "array") {
|
|
6827
6939
|
return captured.items.map((item, index) => [
|
|
@@ -6928,10 +7040,10 @@ class Model {
|
|
|
6928
7040
|
return node;
|
|
6929
7041
|
}
|
|
6930
7042
|
binding(file, name) {
|
|
6931
|
-
const declarations = this.sourceFile(file).statements.flatMap((statement) =>
|
|
7043
|
+
const declarations = this.sourceFile(file).statements.flatMap((statement) => ts25.isVariableStatement(statement) ? [...statement.declarationList.declarations] : []);
|
|
6932
7044
|
for (const declaration of declarations) {
|
|
6933
7045
|
const named = boundNames(declaration.name).find((bound) => bound.text === name)?.parent;
|
|
6934
|
-
if (named && (
|
|
7046
|
+
if (named && (ts25.isVariableDeclaration(named) || ts25.isBindingElement(named))) {
|
|
6935
7047
|
return named;
|
|
6936
7048
|
}
|
|
6937
7049
|
}
|
|
@@ -6957,7 +7069,7 @@ class Model {
|
|
|
6957
7069
|
throw new Error("Not a class instance.");
|
|
6958
7070
|
}
|
|
6959
7071
|
const source = this.program.getSourceFile(captured.file);
|
|
6960
|
-
const found = source?.statements.find((statement) =>
|
|
7072
|
+
const found = source?.statements.find((statement) => ts25.isClassDeclaration(statement) && statement.name?.text === captured.name);
|
|
6961
7073
|
if (!found) {
|
|
6962
7074
|
throw new Error(`${captured.file}: no class ${captured.name}.`);
|
|
6963
7075
|
}
|
|
@@ -6970,10 +7082,10 @@ class Model {
|
|
|
6970
7082
|
return this.fields(declaration).find((field) => field.name.getText() === name);
|
|
6971
7083
|
}
|
|
6972
7084
|
ancestry(declaration) {
|
|
6973
|
-
const base = declaration.heritageClauses?.find((clause) => clause.token ===
|
|
7085
|
+
const base = declaration.heritageClauses?.find((clause) => clause.token === ts25.SyntaxKind.ExtendsKeyword)?.types[0]?.expression;
|
|
6974
7086
|
const symbol = base && this.checker.getSymbolAtLocation(base);
|
|
6975
|
-
const resolved = symbol && symbol.flags &
|
|
6976
|
-
const parent = resolved?.declarations?.find(
|
|
7087
|
+
const resolved = symbol && symbol.flags & ts25.SymbolFlags.Alias ? this.checker.getAliasedSymbol(symbol) : symbol;
|
|
7088
|
+
const parent = resolved?.declarations?.find(ts25.isClassDeclaration);
|
|
6977
7089
|
return parent ? [declaration, ...this.ancestry(parent)] : [declaration];
|
|
6978
7090
|
}
|
|
6979
7091
|
members(declaration) {
|
|
@@ -6984,11 +7096,11 @@ class Model {
|
|
|
6984
7096
|
}
|
|
6985
7097
|
ownFields(declaration) {
|
|
6986
7098
|
return declaration.members.flatMap((member) => {
|
|
6987
|
-
if (
|
|
7099
|
+
if (ts25.isPropertyDeclaration(member)) {
|
|
6988
7100
|
return [member];
|
|
6989
7101
|
}
|
|
6990
|
-
if (
|
|
6991
|
-
return member.parameters.filter((parameter) =>
|
|
7102
|
+
if (ts25.isConstructorDeclaration(member)) {
|
|
7103
|
+
return member.parameters.filter((parameter) => ts25.isParameterPropertyDeclaration(parameter, member));
|
|
6992
7104
|
}
|
|
6993
7105
|
return [];
|
|
6994
7106
|
});
|
|
@@ -7010,7 +7122,7 @@ class Model {
|
|
|
7010
7122
|
return property && this.checker.getTypeOfSymbol(property);
|
|
7011
7123
|
}
|
|
7012
7124
|
variant(type, value) {
|
|
7013
|
-
const candidates = unionMembers(type).filter((member) => !(member.flags &
|
|
7125
|
+
const candidates = unionMembers(type).filter((member) => !(member.flags & ts25.TypeFlags.Undefined));
|
|
7014
7126
|
const [first] = candidates;
|
|
7015
7127
|
if (!first) {
|
|
7016
7128
|
return type;
|
|
@@ -7028,7 +7140,7 @@ class Model {
|
|
|
7028
7140
|
})) ?? first;
|
|
7029
7141
|
}
|
|
7030
7142
|
elementType(type) {
|
|
7031
|
-
return this.checker.getIndexTypeOfType(this.checker.getNonNullableType(type),
|
|
7143
|
+
return this.checker.getIndexTypeOfType(this.checker.getNonNullableType(type), ts25.IndexKind.Number);
|
|
7032
7144
|
}
|
|
7033
7145
|
own(value, owner, type) {
|
|
7034
7146
|
const queue = [{ owner, type, value }];
|
|
@@ -7052,7 +7164,7 @@ class Model {
|
|
|
7052
7164
|
}
|
|
7053
7165
|
|
|
7054
7166
|
// packages/compiler/src/frontend/elaborate/names.ts
|
|
7055
|
-
import
|
|
7167
|
+
import ts26 from "typescript";
|
|
7056
7168
|
var unusedName = (base, taken) => {
|
|
7057
7169
|
if (!taken(base)) {
|
|
7058
7170
|
return base;
|
|
@@ -7094,15 +7206,15 @@ var routineNames = (sources) => {
|
|
|
7094
7206
|
const names = new Set;
|
|
7095
7207
|
for (const source of sources) {
|
|
7096
7208
|
for (const statement of source.statements) {
|
|
7097
|
-
if (
|
|
7209
|
+
if (ts26.isFunctionDeclaration(statement) && statement.name && !statement.parameters.length) {
|
|
7098
7210
|
names.add(statement.name.text);
|
|
7099
7211
|
}
|
|
7100
|
-
if (!
|
|
7212
|
+
if (!ts26.isVariableStatement(statement)) {
|
|
7101
7213
|
continue;
|
|
7102
7214
|
}
|
|
7103
7215
|
for (const declaration of statement.declarationList.declarations) {
|
|
7104
7216
|
const initializer = declaration.initializer;
|
|
7105
|
-
if (
|
|
7217
|
+
if (ts26.isIdentifier(declaration.name) && initializer && (ts26.isArrowFunction(initializer) || ts26.isFunctionExpression(initializer)) && !initializer.parameters.length) {
|
|
7106
7218
|
names.add(declaration.name.text);
|
|
7107
7219
|
}
|
|
7108
7220
|
}
|
|
@@ -7129,7 +7241,7 @@ var rootFieldNames = (model) => {
|
|
|
7129
7241
|
};
|
|
7130
7242
|
|
|
7131
7243
|
// packages/compiler/src/frontend/elaborate/values.ts
|
|
7132
|
-
import
|
|
7244
|
+
import ts27 from "typescript";
|
|
7133
7245
|
var constant = (value) => ({
|
|
7134
7246
|
kind: "const",
|
|
7135
7247
|
value
|
|
@@ -7160,9 +7272,9 @@ var restoring = (map) => {
|
|
|
7160
7272
|
}
|
|
7161
7273
|
};
|
|
7162
7274
|
};
|
|
7163
|
-
var isStoredState = (value) => value.kind === "code" && (value.copyOf !== undefined || value.place === true && value.declared !== undefined && Boolean(value.declared.flags &
|
|
7275
|
+
var isStoredState = (value) => value.kind === "code" && (value.copyOf !== undefined || value.place === true && value.declared !== undefined && Boolean(value.declared.flags & ts27.TypeFlags.Object));
|
|
7164
7276
|
var holdsSetupObjects = (value) => [...value.fields?.values() ?? [], ...value.items ?? []].some((item) => item.kind === "static" || item.kind === "function" || item.kind === "choice");
|
|
7165
|
-
var
|
|
7277
|
+
var unwrapped2 = (node) => isWrapper(node) ? unwrapped2(node.expression) : node;
|
|
7166
7278
|
var repeatable = (text) => /^[\w.[\]]+$/.test(String(text));
|
|
7167
7279
|
|
|
7168
7280
|
class SpecializeError extends Error {
|
|
@@ -7216,13 +7328,13 @@ class Layouts {
|
|
|
7216
7328
|
const fields = rootFieldNames(model);
|
|
7217
7329
|
this.imports = new ModuleImports(moduleNames, fields);
|
|
7218
7330
|
this.typeImports = new ModuleImports(moduleNames, fields);
|
|
7219
|
-
const typeNames = new Set(model.program.getSourceFiles().filter((source) => !source.isDeclarationFile).flatMap((source) => source.statements.flatMap((statement) =>
|
|
7331
|
+
const typeNames = new Set(model.program.getSourceFiles().filter((source) => !source.isDeclarationFile).flatMap((source) => source.statements.flatMap((statement) => ts28.isVariableStatement(statement) || ts28.isFunctionDeclaration(statement) ? [] : declaredNames(statement))));
|
|
7220
7332
|
this.takenNames = new Set(model.roots.map((root) => pascalCase2(root.name)).filter((name) => !typeNames.has(name)));
|
|
7221
7333
|
const collectLiterals = (node) => {
|
|
7222
|
-
if (
|
|
7334
|
+
if (ts28.isTypeLiteralNode(node)) {
|
|
7223
7335
|
this.keptLiterals.push(node);
|
|
7224
7336
|
}
|
|
7225
|
-
|
|
7337
|
+
ts28.forEachChild(node, collectLiterals);
|
|
7226
7338
|
};
|
|
7227
7339
|
for (const source of keptSources) {
|
|
7228
7340
|
collectLiterals(source);
|
|
@@ -7230,7 +7342,7 @@ class Layouts {
|
|
|
7230
7342
|
for (const name of declaredNames(statement)) {
|
|
7231
7343
|
this.takenNames.add(name);
|
|
7232
7344
|
}
|
|
7233
|
-
if ((
|
|
7345
|
+
if ((ts28.isTypeAliasDeclaration(statement) || ts28.isInterfaceDeclaration(statement)) && isExported(statement)) {
|
|
7234
7346
|
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
7235
7347
|
if (symbol) {
|
|
7236
7348
|
this.keptTypes.set(checker.getDeclaredTypeOfSymbol(symbol), {
|
|
@@ -7326,7 +7438,7 @@ class Layouts {
|
|
|
7326
7438
|
}
|
|
7327
7439
|
const variants = type.types.filter((part) => !isAbsent(part));
|
|
7328
7440
|
const [first, ...others] = variants;
|
|
7329
|
-
if (!first || !others.length || variants.some((part) => !(part.flags &
|
|
7441
|
+
if (!first || !others.length || variants.some((part) => !(part.flags & ts28.TypeFlags.Object))) {
|
|
7330
7442
|
return;
|
|
7331
7443
|
}
|
|
7332
7444
|
const literal = (variant, name) => {
|
|
@@ -7396,17 +7508,17 @@ class Layouts {
|
|
|
7396
7508
|
if (alias && iecNames.has(alias)) {
|
|
7397
7509
|
return alias;
|
|
7398
7510
|
}
|
|
7399
|
-
if (type.flags &
|
|
7511
|
+
if (type.flags & ts28.TypeFlags.BooleanLike) {
|
|
7400
7512
|
return "boolean";
|
|
7401
7513
|
}
|
|
7402
7514
|
const enumDeclaration = this.enumDeclaration(type);
|
|
7403
7515
|
if (enumDeclaration) {
|
|
7404
7516
|
return this.enumName(enumDeclaration);
|
|
7405
7517
|
}
|
|
7406
|
-
if (type.flags &
|
|
7518
|
+
if (type.flags & ts28.TypeFlags.NumberLike) {
|
|
7407
7519
|
return fractional(value) ? defaultNumericTypes.float : "number";
|
|
7408
7520
|
}
|
|
7409
|
-
if (type.flags &
|
|
7521
|
+
if (type.flags & ts28.TypeFlags.BigIntLike) {
|
|
7410
7522
|
return defaultNumericTypes.bigint;
|
|
7411
7523
|
}
|
|
7412
7524
|
const literal = this.literalEnum(type);
|
|
@@ -7421,7 +7533,7 @@ class Layouts {
|
|
|
7421
7533
|
if (named) {
|
|
7422
7534
|
return named;
|
|
7423
7535
|
}
|
|
7424
|
-
if (type.flags &
|
|
7536
|
+
if (type.flags & ts28.TypeFlags.StringLike) {
|
|
7425
7537
|
return "string";
|
|
7426
7538
|
}
|
|
7427
7539
|
if (type.isUnion()) {
|
|
@@ -7438,7 +7550,7 @@ class Layouts {
|
|
|
7438
7550
|
if (record) {
|
|
7439
7551
|
return record;
|
|
7440
7552
|
}
|
|
7441
|
-
if (type.flags &
|
|
7553
|
+
if (type.flags & ts28.TypeFlags.Object) {
|
|
7442
7554
|
return this.record(type, value, hint);
|
|
7443
7555
|
}
|
|
7444
7556
|
throw new Error(`${checker.typeToString(type)} has no PLC layout.`);
|
|
@@ -7446,16 +7558,16 @@ class Layouts {
|
|
|
7446
7558
|
enumDeclaration(type) {
|
|
7447
7559
|
const parts = unionMembers(type);
|
|
7448
7560
|
const [first] = parts;
|
|
7449
|
-
if (!first || !parts.every((part) => part.flags &
|
|
7561
|
+
if (!first || !parts.every((part) => part.flags & ts28.TypeFlags.EnumLike)) {
|
|
7450
7562
|
return;
|
|
7451
7563
|
}
|
|
7452
7564
|
const symbol = first.symbol;
|
|
7453
|
-
const declaration = symbol?.declarations?.find(
|
|
7454
|
-
return declaration &&
|
|
7565
|
+
const declaration = symbol?.declarations?.find(ts28.isEnumDeclaration) ?? symbol?.valueDeclaration?.parent;
|
|
7566
|
+
return declaration && ts28.isEnumDeclaration(declaration) ? declaration : undefined;
|
|
7455
7567
|
}
|
|
7456
7568
|
iecName(declaration) {
|
|
7457
7569
|
const node = annotation(declaration);
|
|
7458
|
-
const awaits = declaration !== undefined &&
|
|
7570
|
+
const awaits = declaration !== undefined && ts28.canHaveModifiers(declaration) && hasModifier(declaration, ts28.SyntaxKind.AsyncKeyword);
|
|
7459
7571
|
return iecTypeName(node && awaits ? promisedType(node) : node);
|
|
7460
7572
|
}
|
|
7461
7573
|
elementIec(declaration) {
|
|
@@ -7537,7 +7649,7 @@ class Layouts {
|
|
|
7537
7649
|
if (!symbol || !declaration) {
|
|
7538
7650
|
return;
|
|
7539
7651
|
}
|
|
7540
|
-
if (!
|
|
7652
|
+
if (!ts28.isTypeAliasDeclaration(declaration) && !ts28.isInterfaceDeclaration(declaration)) {
|
|
7541
7653
|
return;
|
|
7542
7654
|
}
|
|
7543
7655
|
const file = declaration.getSourceFile().fileName;
|
|
@@ -7608,7 +7720,7 @@ class Layouts {
|
|
|
7608
7720
|
const captured = (name) => node && (node.captured.kind === "object" || node.captured.kind === "instance") ? node.captured.fields[name] : undefined;
|
|
7609
7721
|
const body = this.recordBody(type, captured);
|
|
7610
7722
|
const symbolName = type.aliasSymbol?.name ?? type.getSymbol()?.name;
|
|
7611
|
-
const base = symbolName ===
|
|
7723
|
+
const base = symbolName === ts28.InternalSymbolName.Type || symbolName === ts28.InternalSymbolName.Object ? undefined : symbolName;
|
|
7612
7724
|
const origin = (type.aliasSymbol ?? type.getSymbol())?.declarations?.[0];
|
|
7613
7725
|
if (base === undefined && this.likeKeptLiteral(type, origin, body, captured)) {
|
|
7614
7726
|
return `{ ${body} }`;
|
|
@@ -7624,7 +7736,7 @@ class Layouts {
|
|
|
7624
7736
|
return name;
|
|
7625
7737
|
}
|
|
7626
7738
|
likeKeptLiteral(type, origin, body, captured) {
|
|
7627
|
-
if (origin &&
|
|
7739
|
+
if (origin && ts28.isTypeLiteralNode(origin) && this.kept(origin.getSourceFile().fileName)) {
|
|
7628
7740
|
return true;
|
|
7629
7741
|
}
|
|
7630
7742
|
const names = fieldNames(this.checker, type);
|
|
@@ -7643,7 +7755,7 @@ class Layouts {
|
|
|
7643
7755
|
}
|
|
7644
7756
|
}
|
|
7645
7757
|
var stringEnums = (sources, checker) => sources.flatMap((source) => source.statements.flatMap((statement) => {
|
|
7646
|
-
if (!
|
|
7758
|
+
if (!ts28.isEnumDeclaration(statement) || !statement.members.some((member) => typeof checker.getConstantValue(member) === "string")) {
|
|
7647
7759
|
return [];
|
|
7648
7760
|
}
|
|
7649
7761
|
const values = statement.members.map((member) => checker.getConstantValue(member));
|
|
@@ -7653,10 +7765,10 @@ var stringEnums = (sources, checker) => sources.flatMap((source) => source.state
|
|
|
7653
7765
|
];
|
|
7654
7766
|
}));
|
|
7655
7767
|
var declaredNames = (statement) => {
|
|
7656
|
-
if (
|
|
7657
|
-
return statement.declarationList.declarations.flatMap((declaration) =>
|
|
7768
|
+
if (ts28.isVariableStatement(statement)) {
|
|
7769
|
+
return statement.declarationList.declarations.flatMap((declaration) => ts28.isIdentifier(declaration.name) ? [declaration.name.text] : []);
|
|
7658
7770
|
}
|
|
7659
|
-
if ((
|
|
7771
|
+
if ((ts28.isTypeAliasDeclaration(statement) || ts28.isInterfaceDeclaration(statement) || ts28.isEnumDeclaration(statement) || ts28.isClassDeclaration(statement) || ts28.isFunctionDeclaration(statement)) && statement.name) {
|
|
7660
7772
|
return [statement.name.text];
|
|
7661
7773
|
}
|
|
7662
7774
|
return [];
|
|
@@ -7668,23 +7780,23 @@ var recordTypes = (type) => {
|
|
|
7668
7780
|
const [key, value] = type.aliasTypeArguments ?? [];
|
|
7669
7781
|
return type.aliasSymbol?.name === "Record" && key && value ? { key, value } : undefined;
|
|
7670
7782
|
};
|
|
7671
|
-
var annotation = (declaration) => declaration && (
|
|
7783
|
+
var annotation = (declaration) => declaration && (ts28.isVariableDeclaration(declaration) || ts28.isPropertyDeclaration(declaration) || ts28.isPropertySignature(declaration) || ts28.isParameter(declaration) || ts28.isFunctionLike(declaration)) ? declaration.type : undefined;
|
|
7672
7784
|
var arrayElement = (node) => {
|
|
7673
|
-
if (node &&
|
|
7785
|
+
if (node && ts28.isArrayTypeNode(node)) {
|
|
7674
7786
|
return node.elementType;
|
|
7675
7787
|
}
|
|
7676
|
-
if (node &&
|
|
7788
|
+
if (node && ts28.isTypeReferenceNode(node) && ["Array", "ReadonlyArray", "FixedArray"].includes(node.typeName.getText())) {
|
|
7677
7789
|
return node.typeArguments?.[0];
|
|
7678
7790
|
}
|
|
7679
7791
|
return;
|
|
7680
7792
|
};
|
|
7681
|
-
var recordValue = (node) => node &&
|
|
7793
|
+
var recordValue = (node) => node && ts28.isTypeReferenceNode(node) && node.typeName.getText() === "Record" && node.typeArguments?.length === 2 ? node.typeArguments[1] : undefined;
|
|
7682
7794
|
var iecTypeName = (node) => {
|
|
7683
|
-
if (node &&
|
|
7684
|
-
const [defined, ...others] = node.types.filter((member) => !(member.kind ===
|
|
7795
|
+
if (node && ts28.isUnionTypeNode(node)) {
|
|
7796
|
+
const [defined, ...others] = node.types.filter((member) => !(member.kind === ts28.SyntaxKind.UndefinedKeyword || ts28.isLiteralTypeNode(member) && member.literal.kind === ts28.SyntaxKind.NullKeyword));
|
|
7685
7797
|
return others.length ? undefined : iecTypeName(defined);
|
|
7686
7798
|
}
|
|
7687
|
-
if (!node || !
|
|
7799
|
+
if (!node || !ts28.isTypeReferenceNode(node)) {
|
|
7688
7800
|
return;
|
|
7689
7801
|
}
|
|
7690
7802
|
const name = node.typeName.getText();
|
|
@@ -7692,7 +7804,7 @@ var iecTypeName = (node) => {
|
|
|
7692
7804
|
if (!length) {
|
|
7693
7805
|
return iecNames.has(name) ? name : undefined;
|
|
7694
7806
|
}
|
|
7695
|
-
return (name === "STRING" || name === "WSTRING") && !rest.length &&
|
|
7807
|
+
return (name === "STRING" || name === "WSTRING") && !rest.length && ts28.isLiteralTypeNode(length) && ts28.isNumericLiteral(length.literal) ? `${name}<${length.literal.text}>` : undefined;
|
|
7696
7808
|
};
|
|
7697
7809
|
var isReadonlyArray = (type) => type.getSymbol()?.name === "ReadonlyArray";
|
|
7698
7810
|
var enumKey = (members) => [...members].sort().join("|");
|
|
@@ -7706,60 +7818,60 @@ var literalMember = (value) => {
|
|
|
7706
7818
|
const name = pascalCase2(value);
|
|
7707
7819
|
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
7708
7820
|
};
|
|
7709
|
-
var isAbsent = (type) => Boolean(type.flags & (
|
|
7821
|
+
var isAbsent = (type) => Boolean(type.flags & (ts28.TypeFlags.Undefined | ts28.TypeFlags.Null));
|
|
7710
7822
|
var bothAbsent = (checker, type) => {
|
|
7711
|
-
if (type.isUnion() && type.types.some((part) => part.flags &
|
|
7823
|
+
if (type.isUnion() && type.types.some((part) => part.flags & ts28.TypeFlags.Null) && type.types.some((part) => part.flags & ts28.TypeFlags.Undefined)) {
|
|
7712
7824
|
throw new Error(`${checker.typeToString(type)}: null and undefined are one empty value in the PLC; use one of them.`);
|
|
7713
7825
|
}
|
|
7714
7826
|
};
|
|
7715
7827
|
|
|
7716
7828
|
// packages/compiler/src/frontend/elaborate/reduce.ts
|
|
7717
|
-
import
|
|
7829
|
+
import ts29 from "typescript";
|
|
7718
7830
|
var constantInitializer = (node, checker, sdk) => {
|
|
7719
|
-
if (
|
|
7831
|
+
if (ts29.isNumericLiteral(node) || ts29.isStringLiteral(node) || ts29.isNoSubstitutionTemplateLiteral(node) || ts29.isBigIntLiteral(node) || node.kind === ts29.SyntaxKind.TrueKeyword || node.kind === ts29.SyntaxKind.FalseKeyword) {
|
|
7720
7832
|
return true;
|
|
7721
7833
|
}
|
|
7722
|
-
if (
|
|
7834
|
+
if (ts29.isPrefixUnaryExpression(node)) {
|
|
7723
7835
|
return constantInitializer(node.operand, checker, sdk);
|
|
7724
7836
|
}
|
|
7725
|
-
if (
|
|
7837
|
+
if (ts29.isAsExpression(node) || ts29.isSatisfiesExpression(node) || ts29.isParenthesizedExpression(node)) {
|
|
7726
7838
|
return constantInitializer(node.expression, checker, sdk);
|
|
7727
7839
|
}
|
|
7728
|
-
if (
|
|
7729
|
-
return
|
|
7840
|
+
if (ts29.isPropertyAccessExpression(node)) {
|
|
7841
|
+
return ts29.isIdentifier(node.expression);
|
|
7730
7842
|
}
|
|
7731
|
-
if (
|
|
7843
|
+
if (ts29.isIdentifier(node)) {
|
|
7732
7844
|
return true;
|
|
7733
7845
|
}
|
|
7734
|
-
if (
|
|
7735
|
-
return node.properties.every((property) =>
|
|
7846
|
+
if (ts29.isObjectLiteralExpression(node)) {
|
|
7847
|
+
return node.properties.every((property) => ts29.isPropertyAssignment(property) && constantInitializer(property.initializer, checker, sdk) || ts29.isShorthandPropertyAssignment(property));
|
|
7736
7848
|
}
|
|
7737
|
-
if (
|
|
7849
|
+
if (ts29.isArrayLiteralExpression(node)) {
|
|
7738
7850
|
return node.elements.every((element) => constantInitializer(element, checker, sdk));
|
|
7739
7851
|
}
|
|
7740
|
-
if (
|
|
7852
|
+
if (ts29.isCallExpression(node)) {
|
|
7741
7853
|
return namesSdk(checker, sdk, node.expression, "createServiceClient") || definesTask(node, checker, sdk);
|
|
7742
7854
|
}
|
|
7743
7855
|
return false;
|
|
7744
7856
|
};
|
|
7745
|
-
var definesTask = (call, checker, sdk) =>
|
|
7746
|
-
var serviceClients = (source, checker, sdk) => source.statements.filter((statement) =>
|
|
7857
|
+
var definesTask = (call, checker, sdk) => ts29.isPropertyAccessExpression(call.expression) && call.expression.name.text === "define" && (namesSdk(checker, sdk, call.expression.expression, "tasks") || namesSdk(checker, sdk, call.expression.expression, "backgroundTasks"));
|
|
7858
|
+
var serviceClients = (source, checker, sdk) => source.statements.filter((statement) => ts29.isVariableStatement(statement) && statement.declarationList.declarations.every((declaration) => declaration.initializer && ts29.isCallExpression(declaration.initializer) && namesSdk(checker, sdk, declaration.initializer.expression, "createServiceClient")));
|
|
7747
7859
|
var importsSdk = (statement) => {
|
|
7748
7860
|
const specifier = statement.moduleSpecifier;
|
|
7749
|
-
return
|
|
7861
|
+
return ts29.isStringLiteral(specifier) && Object.values(sdkModules).some((module) => module === specifier.text);
|
|
7750
7862
|
};
|
|
7751
7863
|
var constantStatement = (declaration) => {
|
|
7752
|
-
if (!declaration || !
|
|
7864
|
+
if (!declaration || !ts29.isVariableDeclaration(declaration) || !ts29.isIdentifier(declaration.name) || !(declaration.parent.flags & ts29.NodeFlags.Const)) {
|
|
7753
7865
|
return;
|
|
7754
7866
|
}
|
|
7755
7867
|
const statement = declaration.parent.parent;
|
|
7756
|
-
return
|
|
7868
|
+
return ts29.isVariableStatement(statement) && ts29.isSourceFile(statement.parent) ? statement : undefined;
|
|
7757
7869
|
};
|
|
7758
7870
|
var literalType = (checker, type) => {
|
|
7759
|
-
if (type.isLiteral() || type.flags &
|
|
7871
|
+
if (type.isLiteral() || type.flags & ts29.TypeFlags.BooleanLiteral) {
|
|
7760
7872
|
return true;
|
|
7761
7873
|
}
|
|
7762
|
-
if (!(type.flags &
|
|
7874
|
+
if (!(type.flags & ts29.TypeFlags.Object) || type.getCallSignatures().length || type.getConstructSignatures().length) {
|
|
7763
7875
|
return false;
|
|
7764
7876
|
}
|
|
7765
7877
|
if (checker.isTupleType(type)) {
|
|
@@ -7768,23 +7880,23 @@ var literalType = (checker, type) => {
|
|
|
7768
7880
|
const properties = type.getProperties();
|
|
7769
7881
|
return properties.length > 0 && !type.getStringIndexType() && !type.getNumberIndexType() && properties.every((property) => {
|
|
7770
7882
|
const declaration = property.valueDeclaration;
|
|
7771
|
-
return declaration !== undefined &&
|
|
7883
|
+
return declaration !== undefined && ts29.isPropertyAssignment(declaration) && literalType(checker, checker.getTypeOfSymbol(property));
|
|
7772
7884
|
});
|
|
7773
7885
|
};
|
|
7774
7886
|
var literalConstant = (checker, symbol) => {
|
|
7775
7887
|
const declaration = symbol.valueDeclaration;
|
|
7776
|
-
return declaration &&
|
|
7888
|
+
return declaration && ts29.isVariableDeclaration(declaration) && constantStatement(declaration) && literalType(checker, checker.getTypeAtLocation(declaration.name)) ? declaration : undefined;
|
|
7777
7889
|
};
|
|
7778
7890
|
var readNames = (node) => {
|
|
7779
7891
|
const found = [];
|
|
7780
7892
|
const visit = (inner) => {
|
|
7781
|
-
if (
|
|
7893
|
+
if (ts29.isTypeNode(inner)) {
|
|
7782
7894
|
return;
|
|
7783
7895
|
}
|
|
7784
|
-
if (
|
|
7896
|
+
if (ts29.isIdentifier(inner)) {
|
|
7785
7897
|
found.push(inner);
|
|
7786
7898
|
}
|
|
7787
|
-
|
|
7899
|
+
ts29.forEachChild(inner, visit);
|
|
7788
7900
|
};
|
|
7789
7901
|
visit(node);
|
|
7790
7902
|
return found;
|
|
@@ -7792,10 +7904,10 @@ var readNames = (node) => {
|
|
|
7792
7904
|
var allNames = (node) => {
|
|
7793
7905
|
const found = [];
|
|
7794
7906
|
const visit = (inner) => {
|
|
7795
|
-
if (
|
|
7907
|
+
if (ts29.isIdentifier(inner)) {
|
|
7796
7908
|
found.push(inner);
|
|
7797
7909
|
}
|
|
7798
|
-
|
|
7910
|
+
ts29.forEachChild(inner, visit);
|
|
7799
7911
|
};
|
|
7800
7912
|
visit(node);
|
|
7801
7913
|
return found;
|
|
@@ -7823,7 +7935,7 @@ class Reduction {
|
|
|
7823
7935
|
continue;
|
|
7824
7936
|
}
|
|
7825
7937
|
for (const statement of source.statements) {
|
|
7826
|
-
if (!
|
|
7938
|
+
if (!ts29.isImportDeclaration(statement) && !ts29.isExportDeclaration(statement)) {
|
|
7827
7939
|
this.need(statement);
|
|
7828
7940
|
}
|
|
7829
7941
|
}
|
|
@@ -7836,7 +7948,7 @@ class Reduction {
|
|
|
7836
7948
|
continue;
|
|
7837
7949
|
}
|
|
7838
7950
|
for (const statement of source.statements) {
|
|
7839
|
-
if (
|
|
7951
|
+
if (ts29.isInterfaceDeclaration(statement) || ts29.isTypeAliasDeclaration(statement) || this.keptWritten(statement)) {
|
|
7840
7952
|
this.declareClasses(statement);
|
|
7841
7953
|
}
|
|
7842
7954
|
}
|
|
@@ -7849,9 +7961,9 @@ class Reduction {
|
|
|
7849
7961
|
for (const name of readNames(node)) {
|
|
7850
7962
|
const symbol = valueSymbol(this.checker, name);
|
|
7851
7963
|
const declaration = symbol && unaliased(this.checker, symbol).valueDeclaration;
|
|
7852
|
-
if (declaration &&
|
|
7964
|
+
if (declaration && ts29.isVariableDeclaration(declaration) && this.cutDown.has(declaration.getSourceFile()) && !this.needed.has(declaration) && this.staysWritten(declaration)) {
|
|
7853
7965
|
const list = declaration.parent;
|
|
7854
|
-
for (const each of
|
|
7966
|
+
for (const each of ts29.isVariableDeclarationList(list) ? list.declarations : []) {
|
|
7855
7967
|
this.needed.add(each);
|
|
7856
7968
|
if (each.initializer) {
|
|
7857
7969
|
this.need(each.initializer);
|
|
@@ -7876,11 +7988,11 @@ class Reduction {
|
|
|
7876
7988
|
const { written, bound } = this.options;
|
|
7877
7989
|
const symbol = checker.getSymbolAtLocation(declaration.name);
|
|
7878
7990
|
const initializer = declaration.initializer;
|
|
7879
|
-
const calls = (node) =>
|
|
7880
|
-
return
|
|
7991
|
+
const calls = (node) => ts29.isCallExpression(node) || Boolean(ts29.forEachChild(node, calls));
|
|
7992
|
+
return ts29.isIdentifier(declaration.name) && initializer !== undefined && constantInitializer(initializer, checker, sdk) && !calls(initializer) && !(symbol && written.has(symbol)) && !bound.has(declaration.name.text) && readNames(initializer).every((name) => this.readable(name));
|
|
7881
7993
|
}
|
|
7882
7994
|
readable(name) {
|
|
7883
|
-
if (
|
|
7995
|
+
if (ts29.isPropertyAccessExpression(name.parent) && name.parent.name === name || ts29.isPropertyAssignment(name.parent) && name.parent.name === name) {
|
|
7884
7996
|
return true;
|
|
7885
7997
|
}
|
|
7886
7998
|
const symbol = valueSymbol(this.checker, name);
|
|
@@ -7892,13 +8004,13 @@ class Reduction {
|
|
|
7892
8004
|
if (!this.runtime.has(source) || this.options.kept(source.fileName)) {
|
|
7893
8005
|
return true;
|
|
7894
8006
|
}
|
|
7895
|
-
if (
|
|
8007
|
+
if (ts29.isEnumDeclaration(declaration)) {
|
|
7896
8008
|
return source !== this.options.entry && this.keepsEnum(declaration);
|
|
7897
8009
|
}
|
|
7898
|
-
return
|
|
8010
|
+
return ts29.isVariableDeclaration(declaration) && this.cutDown.has(source) && this.staysWritten(declaration);
|
|
7899
8011
|
}
|
|
7900
8012
|
keptWritten(statement) {
|
|
7901
|
-
return
|
|
8013
|
+
return ts29.isVariableStatement(statement) && statement.declarationList.declarations.some((declaration) => this.needed.has(declaration));
|
|
7902
8014
|
}
|
|
7903
8015
|
declareClasses(node) {
|
|
7904
8016
|
for (const name of allNames(node)) {
|
|
@@ -7907,7 +8019,7 @@ class Reduction {
|
|
|
7907
8019
|
}
|
|
7908
8020
|
}
|
|
7909
8021
|
declareClass(symbol) {
|
|
7910
|
-
const declaration = symbol?.declarations?.find(
|
|
8022
|
+
const declaration = symbol?.declarations?.find(ts29.isClassDeclaration);
|
|
7911
8023
|
if (!symbol || !declaration?.name || !this.cutDown.has(declaration.getSourceFile()) || this.classes.has(declaration)) {
|
|
7912
8024
|
return;
|
|
7913
8025
|
}
|
|
@@ -7915,7 +8027,7 @@ class Reduction {
|
|
|
7915
8027
|
const instance = this.checker.getDeclaredTypeOfSymbol(symbol);
|
|
7916
8028
|
for (const property of this.checker.getPropertiesOfType(instance)) {
|
|
7917
8029
|
const member = property.valueDeclaration;
|
|
7918
|
-
if (member && !(
|
|
8030
|
+
if (member && !(ts29.getCombinedModifierFlags(member) & ts29.ModifierFlags.Private)) {
|
|
7919
8031
|
this.declareTypeClasses(this.checker.getTypeOfSymbolAtLocation(property, declaration), new Set);
|
|
7920
8032
|
}
|
|
7921
8033
|
}
|
|
@@ -7929,7 +8041,7 @@ class Reduction {
|
|
|
7929
8041
|
const parts = [
|
|
7930
8042
|
...type.isUnionOrIntersection() ? type.types : [],
|
|
7931
8043
|
...type.aliasTypeArguments ?? [],
|
|
7932
|
-
...type.flags &
|
|
8044
|
+
...type.flags & ts29.TypeFlags.Object && type.objectFlags & ts29.ObjectFlags.Reference ? this.checker.getTypeArguments(type) : []
|
|
7933
8045
|
];
|
|
7934
8046
|
for (const part of parts) {
|
|
7935
8047
|
this.declareTypeClasses(part, seen);
|
|
@@ -7942,7 +8054,7 @@ class Reduction {
|
|
|
7942
8054
|
}
|
|
7943
8055
|
const fixed = declaration.members.every((member) => !member.initializer || readNames(member.initializer).every((name) => {
|
|
7944
8056
|
const symbol = this.checker.getSymbolAtLocation(name);
|
|
7945
|
-
return symbol !== undefined && Boolean(symbol.flags &
|
|
8057
|
+
return symbol !== undefined && Boolean(symbol.flags & ts29.SymbolFlags.EnumMember);
|
|
7946
8058
|
}));
|
|
7947
8059
|
return fixed && (source !== this.options.entry || this.exportedByName(declaration));
|
|
7948
8060
|
}
|
|
@@ -7961,7 +8073,7 @@ class Reduction {
|
|
|
7961
8073
|
}
|
|
7962
8074
|
survives(symbol) {
|
|
7963
8075
|
const declaration = symbol.valueDeclaration;
|
|
7964
|
-
if (!(symbol.flags &
|
|
8076
|
+
if (!(symbol.flags & ts29.SymbolFlags.Value) || !declaration) {
|
|
7965
8077
|
return true;
|
|
7966
8078
|
}
|
|
7967
8079
|
const source = declaration.getSourceFile();
|
|
@@ -7971,13 +8083,13 @@ class Reduction {
|
|
|
7971
8083
|
if (source === this.options.entry) {
|
|
7972
8084
|
return this.exportedNames(symbol).some((name) => this.entryDeclares.has(name));
|
|
7973
8085
|
}
|
|
7974
|
-
if (
|
|
8086
|
+
if (ts29.isEnumDeclaration(declaration)) {
|
|
7975
8087
|
return this.keepsEnum(declaration);
|
|
7976
8088
|
}
|
|
7977
|
-
if (
|
|
8089
|
+
if (ts29.isClassDeclaration(declaration)) {
|
|
7978
8090
|
return this.classes.has(declaration);
|
|
7979
8091
|
}
|
|
7980
|
-
return
|
|
8092
|
+
return ts29.isVariableDeclaration(declaration) && (this.needed.has(declaration) || literalConstant(this.checker, symbol) !== undefined || serviceClients(source, this.checker, this.sdk).includes(declaration.parent.parent));
|
|
7981
8093
|
}
|
|
7982
8094
|
exportedNames(symbol) {
|
|
7983
8095
|
const module = this.checker.getSymbolAtLocation(this.options.entry);
|
|
@@ -7995,16 +8107,16 @@ class Reduction {
|
|
|
7995
8107
|
const read = new Set(source.statements.filter((statement) => this.keptWritten(statement)).flatMap(readNames).flatMap((name) => valueSymbol(checker, name) ?? []));
|
|
7996
8108
|
return joined([
|
|
7997
8109
|
...source.statements.flatMap((statement) => {
|
|
7998
|
-
if (
|
|
8110
|
+
if (ts29.isInterfaceDeclaration(statement) || ts29.isTypeAliasDeclaration(statement) || ts29.isEnumDeclaration(statement) && this.keepsEnum(statement) || clients.includes(statement) || this.keptWritten(statement)) {
|
|
7999
8111
|
return [copied(statement)];
|
|
8000
8112
|
}
|
|
8001
|
-
if (
|
|
8113
|
+
if (ts29.isClassDeclaration(statement)) {
|
|
8002
8114
|
return this.classes.has(statement) ? [from(statement, this.declaredClass(statement))] : [];
|
|
8003
8115
|
}
|
|
8004
|
-
if (
|
|
8116
|
+
if (ts29.isImportDeclaration(statement)) {
|
|
8005
8117
|
return statement.importClause?.isTypeOnly || clients.length > 0 && importsSdk(statement) ? [copied(statement)] : this.imports(statement, read);
|
|
8006
8118
|
}
|
|
8007
|
-
if (
|
|
8119
|
+
if (ts29.isExportDeclaration(statement) && statement.moduleSpecifier) {
|
|
8008
8120
|
return this.reExports(statement);
|
|
8009
8121
|
}
|
|
8010
8122
|
return [];
|
|
@@ -8015,20 +8127,20 @@ class Reduction {
|
|
|
8015
8127
|
`);
|
|
8016
8128
|
}
|
|
8017
8129
|
stays(declaration, clients) {
|
|
8018
|
-
if (
|
|
8130
|
+
if (ts29.isInterfaceDeclaration(declaration) || ts29.isTypeAliasDeclaration(declaration)) {
|
|
8019
8131
|
return true;
|
|
8020
8132
|
}
|
|
8021
|
-
if (
|
|
8133
|
+
if (ts29.isEnumDeclaration(declaration)) {
|
|
8022
8134
|
return this.keepsEnum(declaration);
|
|
8023
8135
|
}
|
|
8024
|
-
if (
|
|
8136
|
+
if (ts29.isClassDeclaration(declaration)) {
|
|
8025
8137
|
return this.classes.has(declaration);
|
|
8026
8138
|
}
|
|
8027
|
-
return
|
|
8139
|
+
return ts29.isVariableDeclaration(declaration) && (this.needed.has(declaration) || clients.includes(declaration.parent.parent));
|
|
8028
8140
|
}
|
|
8029
8141
|
entry() {
|
|
8030
8142
|
const source = this.options.entry;
|
|
8031
|
-
const enums = source.statements.filter(
|
|
8143
|
+
const enums = source.statements.filter(ts29.isEnumDeclaration).filter((statement) => this.sharesEnum(statement));
|
|
8032
8144
|
const text = (statement) => isExported(statement) ? copied(statement) : joined(["export ", copied(statement)], "");
|
|
8033
8145
|
return {
|
|
8034
8146
|
enums: enums.map((statement) => ({
|
|
@@ -8044,9 +8156,9 @@ class Reduction {
|
|
|
8044
8156
|
const lines = [];
|
|
8045
8157
|
const declared = [];
|
|
8046
8158
|
for (const exported of module ? checker.getExportsOfModule(module) : []) {
|
|
8047
|
-
const here = exported.declarations?.find((declaration) => declaration.getSourceFile() === source && !(
|
|
8159
|
+
const here = exported.declarations?.find((declaration) => declaration.getSourceFile() === source && !(ts29.isExportSpecifier(declaration) && declaration.parent.parent.moduleSpecifier));
|
|
8048
8160
|
const target = unaliased(checker, exported);
|
|
8049
|
-
const declaration = target.declarations?.find((candidate) => candidate.getSourceFile() === source && !
|
|
8161
|
+
const declaration = target.declarations?.find((candidate) => candidate.getSourceFile() === source && !ts29.isExportSpecifier(candidate) && !ts29.isExportAssignment(candidate));
|
|
8050
8162
|
if (!here || !declaration) {
|
|
8051
8163
|
continue;
|
|
8052
8164
|
}
|
|
@@ -8055,10 +8167,10 @@ class Reduction {
|
|
|
8055
8167
|
if (here === declaration && !isDefault) {
|
|
8056
8168
|
continue;
|
|
8057
8169
|
}
|
|
8058
|
-
if (here === declaration &&
|
|
8170
|
+
if (here === declaration && ts29.isClassDeclaration(declaration)) {
|
|
8059
8171
|
continue;
|
|
8060
8172
|
}
|
|
8061
|
-
const keyword = target.flags &
|
|
8173
|
+
const keyword = target.flags & ts29.SymbolFlags.Value ? "export" : "export type";
|
|
8062
8174
|
lines.push(from(here, isDefault ? `export default ${target.name};` : `${keyword} { ${target.name === exported.name ? target.name : `${target.name} as ${exported.name}`} };`));
|
|
8063
8175
|
continue;
|
|
8064
8176
|
}
|
|
@@ -8066,7 +8178,7 @@ class Reduction {
|
|
|
8066
8178
|
if (!constant || isDefault && source === this.options.entry) {
|
|
8067
8179
|
continue;
|
|
8068
8180
|
}
|
|
8069
|
-
const type = checker.typeToString(checker.getTypeAtLocation(constant.name), undefined,
|
|
8181
|
+
const type = checker.typeToString(checker.getTypeAtLocation(constant.name), undefined, ts29.TypeFormatFlags.NoTruncation);
|
|
8070
8182
|
const line = from(constant, isDefault ? `declare const tsplcDefault: ${type};
|
|
8071
8183
|
export default tsplcDefault;` : `export declare const ${exported.name}: ${type};`);
|
|
8072
8184
|
declared.push({ name: exported.name, line });
|
|
@@ -8091,7 +8203,7 @@ export default tsplcDefault;` : `export declare const ${exported.name}: ${type};
|
|
|
8091
8203
|
};
|
|
8092
8204
|
const specifier = statement.moduleSpecifier.getText();
|
|
8093
8205
|
const bindings = clause.namedBindings;
|
|
8094
|
-
const named = bindings &&
|
|
8206
|
+
const named = bindings && ts29.isNamedImports(bindings) ? bindings.elements.filter((element) => surviving(element.name)) : [];
|
|
8095
8207
|
const list = (elements) => elements.map((element) => element.propertyName ? `${element.propertyName.getText()} as ${element.name.text}` : element.name.text).join(", ");
|
|
8096
8208
|
const values = named.filter((element) => !element.isTypeOnly && isRead(element.name));
|
|
8097
8209
|
const types = named.filter((element) => !values.includes(element));
|
|
@@ -8099,7 +8211,7 @@ export default tsplcDefault;` : `export declare const ${exported.name}: ${type};
|
|
|
8099
8211
|
if (clause.name && surviving(clause.name)) {
|
|
8100
8212
|
lines.push(`${isRead(clause.name) ? "import" : "import type"} ${clause.name.text} from ${specifier};`);
|
|
8101
8213
|
}
|
|
8102
|
-
if (bindings &&
|
|
8214
|
+
if (bindings && ts29.isNamespaceImport(bindings)) {
|
|
8103
8215
|
lines.push(`${isRead(bindings.name) ? "import" : "import type"} * as ${bindings.name.text} from ${specifier};`);
|
|
8104
8216
|
}
|
|
8105
8217
|
if (values.length) {
|
|
@@ -8112,7 +8224,7 @@ export default tsplcDefault;` : `export declare const ${exported.name}: ${type};
|
|
|
8112
8224
|
}
|
|
8113
8225
|
reExports(statement) {
|
|
8114
8226
|
const clause = statement.exportClause;
|
|
8115
|
-
if (statement.isTypeOnly || !clause || !
|
|
8227
|
+
if (statement.isTypeOnly || !clause || !ts29.isNamedExports(clause)) {
|
|
8116
8228
|
return [copied(statement)];
|
|
8117
8229
|
}
|
|
8118
8230
|
const kept = clause.elements.filter((element) => {
|
|
@@ -8127,34 +8239,34 @@ export default tsplcDefault;` : `export declare const ${exported.name}: ${type};
|
|
|
8127
8239
|
const { checker } = this;
|
|
8128
8240
|
const symbol = node.name && checker.getSymbolAtLocation(node.name);
|
|
8129
8241
|
const name = node.name?.text ?? "";
|
|
8130
|
-
const flags =
|
|
8242
|
+
const flags = ts29.TypeFormatFlags.NoTruncation;
|
|
8131
8243
|
const members = symbol ? checker.getPropertiesOfType(checker.getDeclaredTypeOfSymbol(symbol)).flatMap((property) => {
|
|
8132
8244
|
const member = property.valueDeclaration;
|
|
8133
8245
|
if (!member || !plainName(property.name)) {
|
|
8134
8246
|
return [];
|
|
8135
8247
|
}
|
|
8136
|
-
const modifiers =
|
|
8137
|
-
if (modifiers &
|
|
8248
|
+
const modifiers = ts29.getCombinedModifierFlags(member);
|
|
8249
|
+
if (modifiers & ts29.ModifierFlags.Private) {
|
|
8138
8250
|
return [`private ${property.name};`];
|
|
8139
8251
|
}
|
|
8140
|
-
const access = modifiers &
|
|
8252
|
+
const access = modifiers & ts29.ModifierFlags.Protected ? "protected " : "";
|
|
8141
8253
|
const type = checker.getTypeOfSymbolAtLocation(property, node);
|
|
8142
|
-
if (property.flags &
|
|
8254
|
+
if (property.flags & ts29.SymbolFlags.Method) {
|
|
8143
8255
|
return type.getCallSignatures().map((signature) => `${access}${property.name}${checker.signatureToString(signature, node, flags)};`);
|
|
8144
8256
|
}
|
|
8145
|
-
const written = (
|
|
8146
|
-
const readonly = modifiers &
|
|
8147
|
-
const optional = property.flags &
|
|
8257
|
+
const written = (ts29.isPropertyDeclaration(member) || ts29.isParameter(member)) && member.type && member.getSourceFile() === node.getSourceFile() ? member.type.getText() : checker.typeToString(type, node, flags);
|
|
8258
|
+
const readonly = modifiers & ts29.ModifierFlags.Readonly || property.flags & ts29.SymbolFlags.GetAccessor && !(property.flags & ts29.SymbolFlags.SetAccessor) ? "readonly " : "";
|
|
8259
|
+
const optional = property.flags & ts29.SymbolFlags.Optional ? "?" : "";
|
|
8148
8260
|
return [
|
|
8149
8261
|
`${access}${readonly}${property.name}${optional}: ${written};`
|
|
8150
8262
|
];
|
|
8151
8263
|
}) : [];
|
|
8152
|
-
const modifiers =
|
|
8153
|
-
const isDefault = Boolean(modifiers &
|
|
8264
|
+
const modifiers = ts29.getCombinedModifierFlags(node);
|
|
8265
|
+
const isDefault = Boolean(modifiers & ts29.ModifierFlags.Default);
|
|
8154
8266
|
const head = [
|
|
8155
|
-
modifiers &
|
|
8267
|
+
modifiers & ts29.ModifierFlags.Export && !isDefault ? "export " : "",
|
|
8156
8268
|
"declare ",
|
|
8157
|
-
modifiers &
|
|
8269
|
+
modifiers & ts29.ModifierFlags.Abstract ? "abstract " : "",
|
|
8158
8270
|
`class ${name}`,
|
|
8159
8271
|
node.typeParameters ? `<${node.typeParameters.map((parameter) => parameter.getText()).join(", ")}>` : ""
|
|
8160
8272
|
].join("");
|
|
@@ -8169,7 +8281,7 @@ export default tsplcDefault;` : `export declare const ${exported.name}: ${type};
|
|
|
8169
8281
|
}
|
|
8170
8282
|
|
|
8171
8283
|
// packages/compiler/src/frontend/elaborate/retention.ts
|
|
8172
|
-
import
|
|
8284
|
+
import ts30 from "typescript";
|
|
8173
8285
|
var unstored = "retain retains nothing the PLC stores; retain state scan code writes, or exported data.";
|
|
8174
8286
|
var unpersisted = "persist persists nothing the PLC stores; persist state scan code writes, or exported data.";
|
|
8175
8287
|
var primitive = (type) => {
|
|
@@ -8179,23 +8291,23 @@ var primitive = (type) => {
|
|
|
8179
8291
|
if (type.isIntersection()) {
|
|
8180
8292
|
return type.types.some(primitive);
|
|
8181
8293
|
}
|
|
8182
|
-
return !(type.flags & (
|
|
8294
|
+
return !(type.flags & (ts30.TypeFlags.Object | ts30.TypeFlags.NonPrimitive));
|
|
8183
8295
|
};
|
|
8184
|
-
var importedThroughRetain = (checker, sdk, symbol) => symbol !== undefined && Boolean(symbol.flags &
|
|
8296
|
+
var importedThroughRetain = (checker, sdk, symbol) => symbol !== undefined && Boolean(symbol.flags & ts30.SymbolFlags.Alias) && ((symbol.declarations ?? []).some((declaration) => declaredInSdkModule(sdk, sdkModules.retain, declaration)) || importedThroughRetain(checker, sdk, checker.getImmediateAliasedSymbol(symbol)));
|
|
8185
8297
|
var moduleBinding = (call) => {
|
|
8186
8298
|
const parent = call.parent;
|
|
8187
|
-
return
|
|
8299
|
+
return ts30.isVariableDeclaration(parent) && parent.initializer === call && ts30.isVariableDeclarationList(parent.parent) && ts30.isVariableStatement(parent.parent.parent) && ts30.isSourceFile(parent.parent.parent.parent) ? parent : undefined;
|
|
8188
8300
|
};
|
|
8189
8301
|
var retainedDeclaration = (call) => {
|
|
8190
8302
|
const parent = call.parent;
|
|
8191
|
-
if (
|
|
8303
|
+
if (ts30.isPropertyDeclaration(parent) && parent.initializer === call) {
|
|
8192
8304
|
return parent;
|
|
8193
8305
|
}
|
|
8194
8306
|
return moduleBinding(call);
|
|
8195
8307
|
};
|
|
8196
8308
|
var checkImported = (call, name, checker, sdk) => {
|
|
8197
8309
|
const symbol = checker.getSymbolAtLocation(call.expression);
|
|
8198
|
-
if (!symbol || !(symbol.flags &
|
|
8310
|
+
if (!symbol || !(symbol.flags & ts30.SymbolFlags.Alias)) {
|
|
8199
8311
|
throw new SpecializeError(call, `Setup runs under Bun, where the global ${name} has no value: import { ${name} } from "${sdkModules.retain}".`);
|
|
8200
8312
|
}
|
|
8201
8313
|
if (!importedThroughRetain(checker, sdk, symbol)) {
|
|
@@ -8206,7 +8318,7 @@ var retentionDeclarations = (sources, checker, sdk) => {
|
|
|
8206
8318
|
const retained = new Set;
|
|
8207
8319
|
const persisted = new Set;
|
|
8208
8320
|
const visit = (node) => {
|
|
8209
|
-
if (
|
|
8321
|
+
if (ts30.isCallExpression(node) && namesSdk(checker, sdk, node.expression, "retain")) {
|
|
8210
8322
|
checkImported(node, "retain", checker, sdk);
|
|
8211
8323
|
const [argument] = node.arguments;
|
|
8212
8324
|
if (argument && primitive(checker.getTypeAtLocation(argument))) {
|
|
@@ -8217,7 +8329,7 @@ var retentionDeclarations = (sources, checker, sdk) => {
|
|
|
8217
8329
|
retained.add(declaration);
|
|
8218
8330
|
}
|
|
8219
8331
|
}
|
|
8220
|
-
if (
|
|
8332
|
+
if (ts30.isCallExpression(node) && namesSdk(checker, sdk, node.expression, "persist")) {
|
|
8221
8333
|
checkImported(node, "persist", checker, sdk);
|
|
8222
8334
|
const binding = moduleBinding(node);
|
|
8223
8335
|
if (!binding) {
|
|
@@ -8225,7 +8337,7 @@ var retentionDeclarations = (sources, checker, sdk) => {
|
|
|
8225
8337
|
}
|
|
8226
8338
|
persisted.add(binding);
|
|
8227
8339
|
}
|
|
8228
|
-
|
|
8340
|
+
ts30.forEachChild(node, visit);
|
|
8229
8341
|
};
|
|
8230
8342
|
for (const source of sources) {
|
|
8231
8343
|
visit(source);
|
|
@@ -8314,15 +8426,15 @@ class Retention {
|
|
|
8314
8426
|
|
|
8315
8427
|
// packages/compiler/src/frontend/elaborate/specialize.ts
|
|
8316
8428
|
import { identityDigest as identityDigest5 } from "@tsplc/compiler/ir";
|
|
8317
|
-
import
|
|
8429
|
+
import ts42 from "typescript";
|
|
8318
8430
|
|
|
8319
8431
|
// packages/compiler/src/frontend/elaborate/declarations.ts
|
|
8320
|
-
import
|
|
8432
|
+
import ts31 from "typescript";
|
|
8321
8433
|
var providedByRuntime = (checker, node) => {
|
|
8322
8434
|
const declaration = checker.getSymbolAtLocation(node)?.valueDeclaration;
|
|
8323
|
-
return declaration !== undefined && (declaration.getSourceFile().isDeclarationFile || (
|
|
8435
|
+
return declaration !== undefined && (declaration.getSourceFile().isDeclarationFile || (ts31.getCombinedModifierFlags(declaration) & ts31.ModifierFlags.Ambient) !== 0);
|
|
8324
8436
|
};
|
|
8325
|
-
var isGlobal = (checker, node, name) =>
|
|
8437
|
+
var isGlobal = (checker, node, name) => ts31.isIdentifier(node) && node.text === name && providedByRuntime(checker, node);
|
|
8326
8438
|
var symbolOf = (checker, name) => {
|
|
8327
8439
|
const symbol = valueSymbol(checker, name);
|
|
8328
8440
|
if (!symbol) {
|
|
@@ -8334,7 +8446,7 @@ var runTimeOptional = (checker, value) => {
|
|
|
8334
8446
|
if (value.kind === "choice") {
|
|
8335
8447
|
return value.optional && !value.narrowed ? value : undefined;
|
|
8336
8448
|
}
|
|
8337
|
-
if (value.kind !== "code" || !value.declared || !(value.declared.flags &
|
|
8449
|
+
if (value.kind !== "code" || !value.declared || !(value.declared.flags & ts31.TypeFlags.Union) || checker.getNonNullableType(value.declared) === value.declared) {
|
|
8338
8450
|
return;
|
|
8339
8451
|
}
|
|
8340
8452
|
return { ...value, declared: value.declared };
|
|
@@ -8352,7 +8464,7 @@ var parameterDefault = (checker, arg, parameter) => {
|
|
|
8352
8464
|
return;
|
|
8353
8465
|
};
|
|
8354
8466
|
var namedElements = (pattern, declared) => pattern.elements.flatMap((element, index) => {
|
|
8355
|
-
if (
|
|
8467
|
+
if (ts31.isOmittedExpression(element)) {
|
|
8356
8468
|
return [];
|
|
8357
8469
|
}
|
|
8358
8470
|
if (element.dotDotDotToken) {
|
|
@@ -8370,21 +8482,21 @@ var namedFields = (pattern, declared) => pattern.elements.map((element) => {
|
|
|
8370
8482
|
// packages/compiler/src/frontend/elaborate/dialect.ts
|
|
8371
8483
|
import { canAssign as canAssign2, integerType, primitiveTypes as primitiveTypes3 } from "@tsplc/compiler/ir";
|
|
8372
8484
|
import { elementaryType as elementaryType2, fitsNumber, isIntegerValued } from "@tsplc/iec";
|
|
8373
|
-
import
|
|
8485
|
+
import ts34 from "typescript";
|
|
8374
8486
|
|
|
8375
8487
|
// packages/compiler/src/frontend/elaborate/references.ts
|
|
8376
|
-
import
|
|
8488
|
+
import ts33 from "typescript";
|
|
8377
8489
|
|
|
8378
8490
|
// packages/compiler/src/frontend/elaborate/assignments.ts
|
|
8379
|
-
import
|
|
8491
|
+
import ts32 from "typescript";
|
|
8380
8492
|
var compoundOperators = new Map([
|
|
8381
|
-
[
|
|
8382
|
-
[
|
|
8383
|
-
[
|
|
8384
|
-
[
|
|
8385
|
-
[
|
|
8493
|
+
[ts32.SyntaxKind.PlusEqualsToken, "+"],
|
|
8494
|
+
[ts32.SyntaxKind.MinusEqualsToken, "-"],
|
|
8495
|
+
[ts32.SyntaxKind.AsteriskEqualsToken, "*"],
|
|
8496
|
+
[ts32.SyntaxKind.BarEqualsToken, "|"],
|
|
8497
|
+
[ts32.SyntaxKind.AmpersandEqualsToken, "&"]
|
|
8386
8498
|
]);
|
|
8387
|
-
var containsWrite = (node) => occursOutsideFunctions(node, (inner) =>
|
|
8499
|
+
var containsWrite = (node) => occursOutsideFunctions(node, (inner) => ts32.isBinaryExpression(inner) && isAssignment(inner.operatorToken.kind) || isUpdate(inner));
|
|
8388
8500
|
var storeStatement = (place, value) => mapped`${place} = ${value};`;
|
|
8389
8501
|
|
|
8390
8502
|
class AssignmentSpecializer {
|
|
@@ -8401,7 +8513,7 @@ class AssignmentSpecializer {
|
|
|
8401
8513
|
return this.context.dialect;
|
|
8402
8514
|
}
|
|
8403
8515
|
assign(node, frame) {
|
|
8404
|
-
if (
|
|
8516
|
+
if (ts32.isArrayLiteralExpression(node.left) || ts32.isObjectLiteralExpression(node.left)) {
|
|
8405
8517
|
const value = this.expressions.expression(node.right, frame);
|
|
8406
8518
|
this.destructure(node.left, value, frame);
|
|
8407
8519
|
return value;
|
|
@@ -8422,7 +8534,7 @@ class AssignmentSpecializer {
|
|
|
8422
8534
|
const text = this.placeText(target, node.left);
|
|
8423
8535
|
const declared = target.kind === "code" ? target.declared : undefined;
|
|
8424
8536
|
const operator = node.operatorToken.kind;
|
|
8425
|
-
if (operator ===
|
|
8537
|
+
if (operator === ts32.SyntaxKind.QuestionQuestionEqualsToken) {
|
|
8426
8538
|
const value = this.expressions.expression(node.right, frame, declared && this.checker.getNonNullableType(declared));
|
|
8427
8539
|
frame.emitter.open(mapped`if (${this.dialect.isUndefinedText(text, declared)}) {`);
|
|
8428
8540
|
frame.emitter.line(this.store(target, this.dialect.wrapDefined(this.dialect.code(value, declared && this.checker.getNonNullableType(declared)), declared)));
|
|
@@ -8430,7 +8542,7 @@ class AssignmentSpecializer {
|
|
|
8430
8542
|
return target;
|
|
8431
8543
|
}
|
|
8432
8544
|
const value = this.expressions.expression(node.right, frame, declared);
|
|
8433
|
-
if (operator ===
|
|
8545
|
+
if (operator === ts32.SyntaxKind.EqualsToken) {
|
|
8434
8546
|
frame.emitter.line(this.stored(target, value));
|
|
8435
8547
|
return target;
|
|
8436
8548
|
}
|
|
@@ -8453,8 +8565,8 @@ class AssignmentSpecializer {
|
|
|
8453
8565
|
}
|
|
8454
8566
|
stepOn(target, node, frame, { used }) {
|
|
8455
8567
|
const text = this.placeText(target, node.operand);
|
|
8456
|
-
const before = used &&
|
|
8457
|
-
frame.emitter.line(this.store(target, mapped`${text} ${node.operator ===
|
|
8568
|
+
const before = used && ts32.isPostfixUnaryExpression(node) ? this.expressions.held(target, frame) : target;
|
|
8569
|
+
frame.emitter.line(this.store(target, mapped`${text} ${node.operator === ts32.SyntaxKind.PlusPlusToken ? "+" : "-"} 1`));
|
|
8458
8570
|
return before;
|
|
8459
8571
|
}
|
|
8460
8572
|
destructure(pattern, value, frame) {
|
|
@@ -8465,26 +8577,26 @@ class AssignmentSpecializer {
|
|
|
8465
8577
|
}
|
|
8466
8578
|
}
|
|
8467
8579
|
collect(pattern, value, frame, writes) {
|
|
8468
|
-
if (
|
|
8580
|
+
if (ts32.isArrayLiteralExpression(pattern)) {
|
|
8469
8581
|
pattern.elements.forEach((element, index) => {
|
|
8470
|
-
if (
|
|
8582
|
+
if (ts32.isOmittedExpression(element)) {
|
|
8471
8583
|
return;
|
|
8472
8584
|
}
|
|
8473
|
-
if (
|
|
8585
|
+
if (ts32.isSpreadElement(element)) {
|
|
8474
8586
|
throw new SpecializeError(element, "Assign the rest of an array element by element.");
|
|
8475
8587
|
}
|
|
8476
8588
|
this.collectTarget(element, this.expressions.element(value, constant(index), element, frame), frame, writes);
|
|
8477
8589
|
});
|
|
8478
8590
|
return;
|
|
8479
8591
|
}
|
|
8480
|
-
if (
|
|
8592
|
+
if (ts32.isObjectLiteralExpression(pattern)) {
|
|
8481
8593
|
for (const property of pattern.properties) {
|
|
8482
|
-
if (
|
|
8594
|
+
if (ts32.isShorthandPropertyAssignment(property)) {
|
|
8483
8595
|
const part = this.expressions.member(value, property.name.text, property, frame);
|
|
8484
8596
|
this.collectTarget(property.name, this.expressions.defaulted(part, property.objectAssignmentInitializer, frame), frame, writes);
|
|
8485
8597
|
continue;
|
|
8486
8598
|
}
|
|
8487
|
-
if (!
|
|
8599
|
+
if (!ts32.isPropertyAssignment(property)) {
|
|
8488
8600
|
throw new SpecializeError(property, "Assign each field of a pattern by name.");
|
|
8489
8601
|
}
|
|
8490
8602
|
this.collectTarget(property.initializer, this.expressions.member(value, property.name.getText(), property, frame), frame, writes);
|
|
@@ -8494,11 +8606,11 @@ class AssignmentSpecializer {
|
|
|
8494
8606
|
throw new SpecializeError(pattern, "Not a pattern.");
|
|
8495
8607
|
}
|
|
8496
8608
|
collectTarget(target, part, frame, writes) {
|
|
8497
|
-
if (
|
|
8609
|
+
if (ts32.isBinaryExpression(target) && target.operatorToken.kind === ts32.SyntaxKind.EqualsToken) {
|
|
8498
8610
|
this.collectTarget(target.left, this.expressions.defaulted(part, target.right, frame), frame, writes);
|
|
8499
8611
|
return;
|
|
8500
8612
|
}
|
|
8501
|
-
if (
|
|
8613
|
+
if (ts32.isArrayLiteralExpression(target) || ts32.isObjectLiteralExpression(target)) {
|
|
8502
8614
|
this.collect(target, part, frame, writes);
|
|
8503
8615
|
return;
|
|
8504
8616
|
}
|
|
@@ -8546,8 +8658,8 @@ var referenceType = (type) => {
|
|
|
8546
8658
|
const defined = unionMembers(type).filter((part) => !isAbsent(part));
|
|
8547
8659
|
return defined.length > 0 && defined.every(isInstanceType);
|
|
8548
8660
|
};
|
|
8549
|
-
var isInstanceType = (type) => Boolean(type.flags &
|
|
8550
|
-
var classesOf = (type) => type.getSymbol()?.declarations?.filter(
|
|
8661
|
+
var isInstanceType = (type) => Boolean(type.flags & ts33.TypeFlags.Object) && type.getConstructSignatures().length === 0 && classesOf(type).length > 0;
|
|
8662
|
+
var classesOf = (type) => type.getSymbol()?.declarations?.filter(ts33.isClassDeclaration) ?? [];
|
|
8551
8663
|
var referable = (node) => node.captured.kind === "instance" || node.captured.kind === "object" || node.captured.kind === "array";
|
|
8552
8664
|
var referenceIndex = (value) => {
|
|
8553
8665
|
if (value.kind === "choice") {
|
|
@@ -8674,7 +8786,7 @@ class References {
|
|
|
8674
8786
|
if (value.kind === "virtual") {
|
|
8675
8787
|
return true;
|
|
8676
8788
|
}
|
|
8677
|
-
return value.kind === "code" && value.declared !== undefined && Boolean(this.checker.getNonNullableType(value.declared).flags &
|
|
8789
|
+
return value.kind === "code" && value.declared !== undefined && Boolean(this.checker.getNonNullableType(value.declared).flags & ts33.TypeFlags.Object);
|
|
8678
8790
|
}
|
|
8679
8791
|
held(choice, frame) {
|
|
8680
8792
|
if (!choice.place) {
|
|
@@ -8724,11 +8836,11 @@ class References {
|
|
|
8724
8836
|
}
|
|
8725
8837
|
frame.emitter.line(storeStatement(target.index, index));
|
|
8726
8838
|
};
|
|
8727
|
-
if (operator ===
|
|
8839
|
+
if (operator === ts33.SyntaxKind.EqualsToken) {
|
|
8728
8840
|
store();
|
|
8729
8841
|
return target;
|
|
8730
8842
|
}
|
|
8731
|
-
if (operator ===
|
|
8843
|
+
if (operator === ts33.SyntaxKind.QuestionQuestionEqualsToken) {
|
|
8732
8844
|
frame.emitter.open(mapped`if (${absent(target.index)}) {`);
|
|
8733
8845
|
store();
|
|
8734
8846
|
frame.emitter.close();
|
|
@@ -8738,7 +8850,7 @@ class References {
|
|
|
8738
8850
|
}
|
|
8739
8851
|
target(node, frame) {
|
|
8740
8852
|
const place = unparenthesized(node, { nonNull: true });
|
|
8741
|
-
if (!
|
|
8853
|
+
if (!ts33.isPropertyAccessExpression(place) || place.expression.kind === ts33.SyntaxKind.ThisKeyword || place.expression.kind === ts33.SyntaxKind.SuperKeyword || !referenceType(this.checker.getTypeAtLocation(place.expression))) {
|
|
8742
8854
|
return this.expressions.expression(node, frame);
|
|
8743
8855
|
}
|
|
8744
8856
|
const object = this.expressions.expression(place.expression, frame);
|
|
@@ -8766,7 +8878,7 @@ class References {
|
|
|
8766
8878
|
}
|
|
8767
8879
|
memberType(option, name) {
|
|
8768
8880
|
if (option.captured.kind === "instance") {
|
|
8769
|
-
const accessor = this.model.memberNamed(this.model.classOf(option), name,
|
|
8881
|
+
const accessor = this.model.memberNamed(this.model.classOf(option), name, ts33.isGetAccessorDeclaration);
|
|
8770
8882
|
const signature = accessor && this.checker.getSignatureFromDeclaration(accessor);
|
|
8771
8883
|
if (signature) {
|
|
8772
8884
|
return this.checker.getReturnTypeOfSignature(signature);
|
|
@@ -8810,7 +8922,7 @@ class References {
|
|
|
8810
8922
|
});
|
|
8811
8923
|
}
|
|
8812
8924
|
const sample = arms.map((arm) => arm.value).find((value) => value.kind === "code");
|
|
8813
|
-
const declared = type ?? (sample?.kind === "code" ? sample.declared : undefined) ?? (
|
|
8925
|
+
const declared = type ?? (sample?.kind === "code" ? sample.declared : undefined) ?? (ts33.isExpression(at) ? this.checker.getTypeAtLocation(at) : undefined);
|
|
8814
8926
|
if (!declared || arms.some((arm) => arm.value.kind !== "const" && arm.value.kind !== "code")) {
|
|
8815
8927
|
throw new SpecializeError(at, "This can't be read through a reference chosen at run time: read a value, or call the method where it is named.");
|
|
8816
8928
|
}
|
|
@@ -8903,7 +9015,7 @@ class References {
|
|
|
8903
9015
|
}
|
|
8904
9016
|
call(choice, name, node, frame, expected) {
|
|
8905
9017
|
const [sample] = choice.options;
|
|
8906
|
-
const declaration = sample?.captured.kind === "instance" ? this.model.memberNamed(this.model.classOf(sample), name,
|
|
9018
|
+
const declaration = sample?.captured.kind === "instance" ? this.model.memberNamed(this.model.classOf(sample), name, ts33.isMethodDeclaration) : undefined;
|
|
8907
9019
|
const args = this.expressions.inOrder(node.arguments, (argument, index) => {
|
|
8908
9020
|
const parameter = declaration?.parameters[index];
|
|
8909
9021
|
return this.expressions.expression(argument, frame, parameter && this.checker.getTypeAtLocation(parameter));
|
|
@@ -8911,7 +9023,7 @@ class References {
|
|
|
8911
9023
|
return this.dispatch(choice, frame, node, undefined, (option) => {
|
|
8912
9024
|
const method = this.expressions.member({ kind: "static", node: option }, name, node.expression, frame);
|
|
8913
9025
|
const result = this.expressions.invoke(method, args, node, frame, expected);
|
|
8914
|
-
if (result.kind === "code" && (!result.declared || result.declared.flags &
|
|
9026
|
+
if (result.kind === "code" && (!result.declared || result.declared.flags & ts33.TypeFlags.Void)) {
|
|
8915
9027
|
frame.emitter.line(mapped`${result.text};`);
|
|
8916
9028
|
return constant(undefined);
|
|
8917
9029
|
}
|
|
@@ -9129,9 +9241,9 @@ class DialectText {
|
|
|
9129
9241
|
}
|
|
9130
9242
|
if (value.kind === "code" && value.declared) {
|
|
9131
9243
|
const declared = value.declared;
|
|
9132
|
-
if (this.layouts.isOptionalRecord(declared) && !
|
|
9244
|
+
if (this.layouts.isOptionalRecord(declared) && !ts34.isPrefixUnaryExpression(at)) {
|
|
9133
9245
|
const type = this.checker.getTypeAtLocation(at);
|
|
9134
|
-
if (!(type.flags &
|
|
9246
|
+
if (!(type.flags & ts34.TypeFlags.BooleanLike)) {
|
|
9135
9247
|
return optionalHas(value.text);
|
|
9136
9248
|
}
|
|
9137
9249
|
}
|
|
@@ -9200,7 +9312,7 @@ var bigintWidth = (iec) => {
|
|
|
9200
9312
|
};
|
|
9201
9313
|
var bigintBeside = (value, other, text) => value.kind === "const" && typeof value.value === "number" && Number.isInteger(value.value) && other.kind === "code" && bigintWidth(other.iec) ? `${value.value}n` : text;
|
|
9202
9314
|
var integerTypes = new Map(primitiveTypes3.filter(integerType).map((type) => [type.id, type]));
|
|
9203
|
-
var storedWidth = (declared, iec) => iec ?? (declared && declared.flags &
|
|
9315
|
+
var storedWidth = (declared, iec) => iec ?? (declared && declared.flags & ts34.TypeFlags.Number ? defaultNumericTypes.number : undefined);
|
|
9204
9316
|
var commonWidth = (from, to) => {
|
|
9205
9317
|
if (!needsConversion(from, to)) {
|
|
9206
9318
|
return to;
|
|
@@ -9225,7 +9337,7 @@ import {
|
|
|
9225
9337
|
optionalRecord as optionalRecord2,
|
|
9226
9338
|
primitiveTypes as primitiveTypes4
|
|
9227
9339
|
} from "@tsplc/compiler/ir";
|
|
9228
|
-
import
|
|
9340
|
+
import ts38 from "typescript";
|
|
9229
9341
|
|
|
9230
9342
|
// packages/compiler/src/frontend/elaborate/calls.ts
|
|
9231
9343
|
import { calendarLiteral as calendarLiteral2 } from "@tsplc/compiler/ir";
|
|
@@ -9234,7 +9346,7 @@ import {
|
|
|
9234
9346
|
elementaryFunctions as elementaryFunctions2,
|
|
9235
9347
|
elementaryValue
|
|
9236
9348
|
} from "@tsplc/iec/arithmetic";
|
|
9237
|
-
import
|
|
9349
|
+
import ts35 from "typescript";
|
|
9238
9350
|
|
|
9239
9351
|
// packages/compiler/src/frontend/elaborate/array-searches.ts
|
|
9240
9352
|
class ArraySearches {
|
|
@@ -9369,7 +9481,7 @@ class CallSpecializer {
|
|
|
9369
9481
|
}
|
|
9370
9482
|
call(node, frame, expected) {
|
|
9371
9483
|
const callee = node.expression;
|
|
9372
|
-
if (
|
|
9484
|
+
if (ts35.isPropertyAccessExpression(callee) || ts35.isPropertyAccessChain(callee)) {
|
|
9373
9485
|
return this.method(node, callee, frame, expected);
|
|
9374
9486
|
}
|
|
9375
9487
|
if (isGlobal(this.context.checker, callee, "structuredClone")) {
|
|
@@ -9389,11 +9501,11 @@ class CallSpecializer {
|
|
|
9389
9501
|
const declaration = this.functionDeclaration(target.node.captured.file, target.node.captured.name);
|
|
9390
9502
|
return this.invokeCall({ kind: "function", declaration }, node, frame, expected);
|
|
9391
9503
|
}
|
|
9392
|
-
const sdkFile =
|
|
9393
|
-
if (
|
|
9504
|
+
const sdkFile = ts35.isIdentifier(callee) && this.sdkDeclaration(callee);
|
|
9505
|
+
if (ts35.isIdentifier(callee) && sdkFile && callee.text === "cancelAsync" && sdkModule(sdkFile) === sdkModules.async && node.arguments.length) {
|
|
9394
9506
|
return this.cancelOwner(node, frame);
|
|
9395
9507
|
}
|
|
9396
|
-
if (
|
|
9508
|
+
if (ts35.isIdentifier(callee) && sdkFile) {
|
|
9397
9509
|
return this.sdk(callee.text, sdkModule(sdkFile), node, frame);
|
|
9398
9510
|
}
|
|
9399
9511
|
return this.invokeCall(target, node, frame, expected);
|
|
@@ -9404,7 +9516,7 @@ class CallSpecializer {
|
|
|
9404
9516
|
if (name === "from" && isGlobal(checker, callee.expression, "Array")) {
|
|
9405
9517
|
return this.arrayFrom(node, frame);
|
|
9406
9518
|
}
|
|
9407
|
-
if (callee.expression.kind ===
|
|
9519
|
+
if (callee.expression.kind === ts35.SyntaxKind.SuperKeyword) {
|
|
9408
9520
|
return this.invokeCall(this.expressions.superMember(callee, frame), node, frame, expected);
|
|
9409
9521
|
}
|
|
9410
9522
|
if (isGlobal(checker, callee.expression, "Math")) {
|
|
@@ -9457,8 +9569,8 @@ class CallSpecializer {
|
|
|
9457
9569
|
return this.invokeCall(method, node, frame, expected);
|
|
9458
9570
|
}
|
|
9459
9571
|
receiver(node, frame) {
|
|
9460
|
-
const inner =
|
|
9461
|
-
if (!
|
|
9572
|
+
const inner = unwrapped2(node);
|
|
9573
|
+
if (!ts35.isElementAccessExpression(inner)) {
|
|
9462
9574
|
return this.expressions.expression(node, frame);
|
|
9463
9575
|
}
|
|
9464
9576
|
const object = this.expressions.expression(inner.expression, frame);
|
|
@@ -9490,7 +9602,7 @@ class CallSpecializer {
|
|
|
9490
9602
|
const fulfilled = name === "then" ? first : undefined;
|
|
9491
9603
|
const rejected = name === "then" ? second : first;
|
|
9492
9604
|
const result = checker.getAwaitedType(checker.getTypeAtLocation(node));
|
|
9493
|
-
const resultType = result && !(result.flags &
|
|
9605
|
+
const resultType = result && !(result.flags & ts35.TypeFlags.Void) ? result : undefined;
|
|
9494
9606
|
const fulfil = () => {
|
|
9495
9607
|
const value = this.awaitedValue(this.expressions.expression(callee.expression, frame), frame);
|
|
9496
9608
|
return fulfilled ? this.handled(fulfilled, [value], frame) : value;
|
|
@@ -9530,7 +9642,7 @@ class CallSpecializer {
|
|
|
9530
9642
|
if (promise.kind !== "code") {
|
|
9531
9643
|
return promise;
|
|
9532
9644
|
}
|
|
9533
|
-
if (!promise.declared || promise.declared.flags &
|
|
9645
|
+
if (!promise.declared || promise.declared.flags & ts35.TypeFlags.Void) {
|
|
9534
9646
|
frame.emitter.line(mapped`await ${promise.text};`);
|
|
9535
9647
|
return constant(undefined);
|
|
9536
9648
|
}
|
|
@@ -9547,25 +9659,25 @@ class CallSpecializer {
|
|
|
9547
9659
|
handled(callback, args, frame) {
|
|
9548
9660
|
const target = this.expressions.expression(callback, frame);
|
|
9549
9661
|
const value = this.expressions.invoke(target, args, callback, frame);
|
|
9550
|
-
const isAsync = target.kind === "function" &&
|
|
9662
|
+
const isAsync = target.kind === "function" && ts35.canHaveModifiers(target.declaration) && ts35.getModifiers(target.declaration)?.some((modifier) => modifier.kind === ts35.SyntaxKind.AsyncKeyword);
|
|
9551
9663
|
return isAsync ? this.awaitedValue(value, frame) : value;
|
|
9552
9664
|
}
|
|
9553
9665
|
invokeCall(target, node, frame, expected) {
|
|
9554
9666
|
return this.expressions.invoke(target, this.expressions.inOrder(node.arguments, (argument, index) => this.argument(argument, target, index, frame), frame), node, frame, expected);
|
|
9555
9667
|
}
|
|
9556
9668
|
argument(node, callee, index, frame) {
|
|
9557
|
-
const parameter = callee.kind === "function" && !
|
|
9669
|
+
const parameter = callee.kind === "function" && !ts35.isGetAccessorDeclaration(callee.declaration) ? callee.declaration.parameters[index] : undefined;
|
|
9558
9670
|
return this.expressions.expression(node, frame, parameter && this.context.checker.getTypeAtLocation(parameter));
|
|
9559
9671
|
}
|
|
9560
9672
|
functionDeclaration(file, name) {
|
|
9561
9673
|
const source = this.context.sources.find((item) => item.fileName === file);
|
|
9562
9674
|
for (const statement of source?.statements ?? []) {
|
|
9563
|
-
if (
|
|
9675
|
+
if (ts35.isFunctionDeclaration(statement) && statement.name?.text === name) {
|
|
9564
9676
|
return statement;
|
|
9565
9677
|
}
|
|
9566
|
-
if (
|
|
9678
|
+
if (ts35.isVariableStatement(statement)) {
|
|
9567
9679
|
for (const declaration of statement.declarationList.declarations) {
|
|
9568
|
-
if (
|
|
9680
|
+
if (ts35.isIdentifier(declaration.name) && declaration.name.text === name && declaration.initializer && (ts35.isArrowFunction(declaration.initializer) || ts35.isFunctionExpression(declaration.initializer))) {
|
|
9569
9681
|
return declaration.initializer;
|
|
9570
9682
|
}
|
|
9571
9683
|
}
|
|
@@ -9587,16 +9699,16 @@ class CallSpecializer {
|
|
|
9587
9699
|
}
|
|
9588
9700
|
telemetry(node, callee, frame) {
|
|
9589
9701
|
const { checker, dialect } = this.context;
|
|
9590
|
-
const value = (argument) =>
|
|
9702
|
+
const value = (argument) => ts35.isCallExpression(argument) && ts35.isPropertyAccessExpression(argument.expression) && this.telemetryCall(argument.expression) ? this.telemetry(argument, argument.expression, frame) : dialect.code(this.expressions.expression(argument, frame), checker.getTypeAtLocation(argument));
|
|
9591
9703
|
const args = node.arguments.map((argument) => {
|
|
9592
|
-
if (
|
|
9704
|
+
if (ts35.isStringLiteral(argument) || ts35.isNoSubstitutionTemplateLiteral(argument)) {
|
|
9593
9705
|
return copied(argument);
|
|
9594
9706
|
}
|
|
9595
|
-
if (
|
|
9707
|
+
if (ts35.isObjectLiteralExpression(argument)) {
|
|
9596
9708
|
return this.telemetryProperties(argument, value, frame);
|
|
9597
9709
|
}
|
|
9598
9710
|
const argumentType = checker.getTypeAtLocation(argument);
|
|
9599
|
-
if (argumentType.flags &
|
|
9711
|
+
if (argumentType.flags & ts35.TypeFlags.Object && !checker.isArrayType(argumentType)) {
|
|
9600
9712
|
const fields = this.expressions.recordFields(argument, frame).map(([name, field]) => mapped`${name}: ${dialect.code(field, undefined)}`);
|
|
9601
9713
|
return mapped`{ ${joined(fields, ", ")} }`;
|
|
9602
9714
|
}
|
|
@@ -9608,15 +9720,15 @@ class CallSpecializer {
|
|
|
9608
9720
|
telemetryProperties(argument, value, frame) {
|
|
9609
9721
|
const { checker, dialect } = this.context;
|
|
9610
9722
|
const properties = argument.properties.flatMap((property) => {
|
|
9611
|
-
if (
|
|
9723
|
+
if (ts35.isShorthandPropertyAssignment(property)) {
|
|
9612
9724
|
return [
|
|
9613
9725
|
mapped`${copied(property.name)}: ${dialect.code(this.expressions.shorthand(property, frame), checker.getTypeAtLocation(property.name))}`
|
|
9614
9726
|
];
|
|
9615
9727
|
}
|
|
9616
|
-
if (
|
|
9728
|
+
if (ts35.isSpreadAssignment(property)) {
|
|
9617
9729
|
return this.expressions.recordFields(property.expression, frame).map(([name, field]) => mapped`${name}: ${dialect.code(field, undefined)}`);
|
|
9618
9730
|
}
|
|
9619
|
-
if (!
|
|
9731
|
+
if (!ts35.isPropertyAssignment(property)) {
|
|
9620
9732
|
throw new SpecializeError(property, "Name each telemetry property.");
|
|
9621
9733
|
}
|
|
9622
9734
|
return [mapped`${copied(property.name)}: ${value(property.initializer)}`];
|
|
@@ -9631,11 +9743,11 @@ class CallSpecializer {
|
|
|
9631
9743
|
}
|
|
9632
9744
|
return this.context.dialect.code(value, value.kind === "virtual" ? this.context.checker.getTypeAtLocation(argument) : undefined);
|
|
9633
9745
|
};
|
|
9634
|
-
const args = node.arguments.map((argument) =>
|
|
9635
|
-
if (
|
|
9746
|
+
const args = node.arguments.map((argument) => ts35.isObjectLiteralExpression(argument) ? mapped`{ ${joined(argument.properties.map((property) => {
|
|
9747
|
+
if (ts35.isShorthandPropertyAssignment(property)) {
|
|
9636
9748
|
return mapped`${copied(property.name)}: ${this.context.dialect.code(this.expressions.shorthand(property, frame), undefined)}`;
|
|
9637
9749
|
}
|
|
9638
|
-
if (!
|
|
9750
|
+
if (!ts35.isPropertyAssignment(property)) {
|
|
9639
9751
|
throw new SpecializeError(property, "Name each option.");
|
|
9640
9752
|
}
|
|
9641
9753
|
return mapped`${copied(property.name)}: ${code(property.initializer)}`;
|
|
@@ -9656,7 +9768,7 @@ class CallSpecializer {
|
|
|
9656
9768
|
kind: "code",
|
|
9657
9769
|
text,
|
|
9658
9770
|
declared,
|
|
9659
|
-
...iec && declared.flags &
|
|
9771
|
+
...iec && declared.flags & ts35.TypeFlags.NumberLike && { iec }
|
|
9660
9772
|
};
|
|
9661
9773
|
}
|
|
9662
9774
|
math(name, node, frame) {
|
|
@@ -9733,14 +9845,14 @@ class CallSpecializer {
|
|
|
9733
9845
|
}
|
|
9734
9846
|
workFunction(declaration) {
|
|
9735
9847
|
const init = declaration.initializer;
|
|
9736
|
-
const work = init &&
|
|
9737
|
-
const symbol = work &&
|
|
9848
|
+
const work = init && ts35.isCallExpression(init) ? init.arguments[0] : undefined;
|
|
9849
|
+
const symbol = work && ts35.isIdentifier(work) ? this.context.checker.getSymbolAtLocation(work) : undefined;
|
|
9738
9850
|
const fn = symbol && unaliased(this.context.checker, symbol).valueDeclaration;
|
|
9739
|
-
return fn &&
|
|
9851
|
+
return fn && ts35.isFunctionDeclaration(fn) && fn.body ? fn : undefined;
|
|
9740
9852
|
}
|
|
9741
9853
|
refuseStateInWork(fn) {
|
|
9742
9854
|
const visit = (node) => {
|
|
9743
|
-
if (
|
|
9855
|
+
if (ts35.isIdentifier(node)) {
|
|
9744
9856
|
const read = valueSymbol(this.context.checker, node);
|
|
9745
9857
|
const declared = read && unaliased(this.context.checker, read).valueDeclaration;
|
|
9746
9858
|
const file = declared?.getSourceFile().fileName;
|
|
@@ -9748,9 +9860,9 @@ class CallSpecializer {
|
|
|
9748
9860
|
throw new SpecializeError(node, `A task's work runs on its own, given only its arguments, so it can't read ${node.text} from a module of program state: pass what it needs as arguments, and use its result in scan code.`);
|
|
9749
9861
|
}
|
|
9750
9862
|
}
|
|
9751
|
-
|
|
9863
|
+
ts35.forEachChild(node, visit);
|
|
9752
9864
|
};
|
|
9753
|
-
|
|
9865
|
+
ts35.forEachChild(fn, visit);
|
|
9754
9866
|
}
|
|
9755
9867
|
serviceCall(path, node, frame) {
|
|
9756
9868
|
const { model, checker, dialect, imports } = this.context;
|
|
@@ -9927,7 +10039,7 @@ class CallSpecializer {
|
|
|
9927
10039
|
}
|
|
9928
10040
|
arrayFrom(node, frame) {
|
|
9929
10041
|
const [shape, make] = node.arguments;
|
|
9930
|
-
const length = shape &&
|
|
10042
|
+
const length = shape && ts35.isObjectLiteralExpression(shape) && this.expressions.member(this.expressions.expression(shape, frame), "length", shape, frame);
|
|
9931
10043
|
if (!length || length.kind !== "const" || typeof length.value !== "number") {
|
|
9932
10044
|
throw new SpecializeError(node, "Array.from needs a length known when the program compiles.");
|
|
9933
10045
|
}
|
|
@@ -9957,7 +10069,7 @@ class CallSpecializer {
|
|
|
9957
10069
|
throw new SpecializeError(node, "structuredClone needs a value.");
|
|
9958
10070
|
}
|
|
9959
10071
|
const value = this.expressions.expression(argument, frame);
|
|
9960
|
-
if (value.kind !== "code" || value.declared && !(value.declared.flags &
|
|
10072
|
+
if (value.kind !== "code" || value.declared && !(value.declared.flags & ts35.TypeFlags.Object)) {
|
|
9961
10073
|
return value;
|
|
9962
10074
|
}
|
|
9963
10075
|
this.context.imports.addSdk(sdkModules.types, "copy");
|
|
@@ -9976,19 +10088,19 @@ class CallSpecializer {
|
|
|
9976
10088
|
}
|
|
9977
10089
|
var awaitedLink = (node) => {
|
|
9978
10090
|
const parent = node.parent;
|
|
9979
|
-
if (
|
|
10091
|
+
if (ts35.isAwaitExpression(parent)) {
|
|
9980
10092
|
return true;
|
|
9981
10093
|
}
|
|
9982
|
-
if (
|
|
9983
|
-
return
|
|
10094
|
+
if (ts35.isParenthesizedExpression(parent)) {
|
|
10095
|
+
return ts35.isAwaitExpression(parent.parent);
|
|
9984
10096
|
}
|
|
9985
|
-
return
|
|
10097
|
+
return ts35.isPropertyAccessExpression(parent) && isPromiseLink(parent.parent) && awaitedLink(parent.parent);
|
|
9986
10098
|
};
|
|
9987
10099
|
var moduleLevel = (declaration) => {
|
|
9988
|
-
if (
|
|
9989
|
-
return
|
|
10100
|
+
if (ts35.isVariableDeclaration(declaration)) {
|
|
10101
|
+
return ts35.isSourceFile(declaration.parent.parent.parent);
|
|
9990
10102
|
}
|
|
9991
|
-
return (
|
|
10103
|
+
return (ts35.isFunctionDeclaration(declaration) || ts35.isClassDeclaration(declaration)) && ts35.isSourceFile(declaration.parent);
|
|
9992
10104
|
};
|
|
9993
10105
|
var refuseTimeArgument = (name, value, argument) => {
|
|
9994
10106
|
const calendar = calendarFormats.get(name);
|
|
@@ -10097,7 +10209,7 @@ var sliceBound = (bound) => {
|
|
|
10097
10209
|
};
|
|
10098
10210
|
|
|
10099
10211
|
// packages/compiler/src/frontend/elaborate/filtered-arrays.ts
|
|
10100
|
-
import
|
|
10212
|
+
import ts36 from "typescript";
|
|
10101
10213
|
var isTrue = (value) => value.kind === "const" && Boolean(value.value);
|
|
10102
10214
|
var isFalse = (value) => value.kind === "const" && !value.value;
|
|
10103
10215
|
var whole = (items) => items.map((item) => ({ item, present: constant(true) }));
|
|
@@ -10191,7 +10303,7 @@ class FilteredArrays {
|
|
|
10191
10303
|
return this.number(total);
|
|
10192
10304
|
}
|
|
10193
10305
|
isFilter(node) {
|
|
10194
|
-
return
|
|
10306
|
+
return ts36.isCallExpression(node) && ts36.isPropertyAccessExpression(node.expression) && node.expression.name.text === "filter" && node.arguments.length === 1 && providedByRuntime(this.context.checker, node.expression.name);
|
|
10195
10307
|
}
|
|
10196
10308
|
count(node, frame) {
|
|
10197
10309
|
const receiver = this.expressions.expression(node.expression.expression, frame);
|
|
@@ -10456,7 +10568,7 @@ class FilteredArrays {
|
|
|
10456
10568
|
}
|
|
10457
10569
|
|
|
10458
10570
|
// packages/compiler/src/frontend/elaborate/iterators.ts
|
|
10459
|
-
import
|
|
10571
|
+
import ts37 from "typescript";
|
|
10460
10572
|
|
|
10461
10573
|
// packages/compiler/src/frontend/elaborate/frame.ts
|
|
10462
10574
|
class Emitter {
|
|
@@ -10561,7 +10673,7 @@ class Iterators {
|
|
|
10561
10673
|
return this.terminal(helper.name, this.pipeline(helper.receiver, frame), node, frame);
|
|
10562
10674
|
}
|
|
10563
10675
|
iterated(node, frame) {
|
|
10564
|
-
const helper = this.helper(
|
|
10676
|
+
const helper = this.helper(unwrapped2(node));
|
|
10565
10677
|
return helper && adapters.has(helper.name) ? this.pipeline(node, frame) : undefined;
|
|
10566
10678
|
}
|
|
10567
10679
|
arrayCall(name, value, node, frame) {
|
|
@@ -10580,15 +10692,15 @@ class Iterators {
|
|
|
10580
10692
|
}, items, node, frame);
|
|
10581
10693
|
}
|
|
10582
10694
|
generator(node, frame) {
|
|
10583
|
-
if (!
|
|
10695
|
+
if (!ts37.isCallExpression(node)) {
|
|
10584
10696
|
return;
|
|
10585
10697
|
}
|
|
10586
10698
|
const declaration = this.context.checker.getResolvedSignature(node)?.declaration;
|
|
10587
|
-
if (!declaration || !(
|
|
10699
|
+
if (!declaration || !(ts37.isFunctionDeclaration(declaration) || ts37.isMethodDeclaration(declaration) || ts37.isFunctionExpression(declaration)) || !declaration.asteriskToken || !declaration.body) {
|
|
10588
10700
|
return;
|
|
10589
10701
|
}
|
|
10590
10702
|
const callee = node.expression;
|
|
10591
|
-
const target =
|
|
10703
|
+
const target = ts37.isPropertyAccessExpression(callee) ? this.expressions.member(this.expressions.expression(callee.expression, frame), callee.name.text, callee, frame) : this.expressions.expression(callee, frame);
|
|
10592
10704
|
const self = target.kind === "function" ? target.self : undefined;
|
|
10593
10705
|
const args = this.expressions.inOrder(node.arguments, (argument) => this.expressions.expression(argument, frame), frame);
|
|
10594
10706
|
return { declaration, self, args };
|
|
@@ -10618,7 +10730,7 @@ class Iterators {
|
|
|
10618
10730
|
}
|
|
10619
10731
|
readInPlace(node) {
|
|
10620
10732
|
const user = outside(node);
|
|
10621
|
-
if (!
|
|
10733
|
+
if (!ts37.isPropertyAccessExpression(user) && !ts37.isElementAccessExpression(user) && !ts37.isForOfStatement(user)) {
|
|
10622
10734
|
throw new SpecializeError(node, "A slice with bounds decided at run time has no array to keep: read it where it is made, with every, some, find, findIndex, forEach, includes, indexOf, length or for…of.");
|
|
10623
10735
|
}
|
|
10624
10736
|
}
|
|
@@ -10750,13 +10862,13 @@ class Iterators {
|
|
|
10750
10862
|
};
|
|
10751
10863
|
}
|
|
10752
10864
|
traverses(node, frame) {
|
|
10753
|
-
if (
|
|
10865
|
+
if (ts37.isFunctionLike(node)) {
|
|
10754
10866
|
return false;
|
|
10755
10867
|
}
|
|
10756
|
-
if (
|
|
10868
|
+
if (ts37.isCallExpression(node) && this.traversing(node, frame)) {
|
|
10757
10869
|
return true;
|
|
10758
10870
|
}
|
|
10759
|
-
return
|
|
10871
|
+
return ts37.forEachChild(node, (child) => this.traverses(child, frame)) ?? false;
|
|
10760
10872
|
}
|
|
10761
10873
|
get generators() {
|
|
10762
10874
|
if (!this.runner) {
|
|
@@ -10765,7 +10877,7 @@ class Iterators {
|
|
|
10765
10877
|
return this.runner;
|
|
10766
10878
|
}
|
|
10767
10879
|
helper(node) {
|
|
10768
|
-
if (!
|
|
10880
|
+
if (!ts37.isCallExpression(node) || !ts37.isPropertyAccessExpression(node.expression)) {
|
|
10769
10881
|
return;
|
|
10770
10882
|
}
|
|
10771
10883
|
const receiver = node.expression.expression;
|
|
@@ -10779,11 +10891,11 @@ class Iterators {
|
|
|
10779
10891
|
if (this.helper(node)) {
|
|
10780
10892
|
return true;
|
|
10781
10893
|
}
|
|
10782
|
-
return
|
|
10894
|
+
return ts37.isPropertyAccessExpression(node.expression) && node.expression.name.text !== "slice" && this.loopsAtRunTime(node.expression.expression, frame, { sliced: false });
|
|
10783
10895
|
}
|
|
10784
10896
|
loopsAtRunTime(node, frame, { sliced }) {
|
|
10785
|
-
const inner =
|
|
10786
|
-
if (
|
|
10897
|
+
const inner = unwrapped2(node);
|
|
10898
|
+
if (ts37.isCallExpression(inner) && ts37.isPropertyAccessExpression(inner.expression) && inner.expression.name.text === "slice") {
|
|
10787
10899
|
return this.loopsAtRunTime(inner.expression.expression, frame, {
|
|
10788
10900
|
sliced: true
|
|
10789
10901
|
});
|
|
@@ -10795,11 +10907,11 @@ class Iterators {
|
|
|
10795
10907
|
return sliced && value !== undefined && this.storage(value) !== undefined;
|
|
10796
10908
|
}
|
|
10797
10909
|
glance(node, frame, { sliced }) {
|
|
10798
|
-
if (
|
|
10910
|
+
if (ts37.isIdentifier(node)) {
|
|
10799
10911
|
const symbol = this.context.checker.getSymbolAtLocation(node);
|
|
10800
10912
|
return symbol && frame.env.get(symbol);
|
|
10801
10913
|
}
|
|
10802
|
-
if (!sliced || !
|
|
10914
|
+
if (!sliced || !ts37.isPropertyAccessExpression(node)) {
|
|
10803
10915
|
return;
|
|
10804
10916
|
}
|
|
10805
10917
|
try {
|
|
@@ -10815,13 +10927,13 @@ class Iterators {
|
|
|
10815
10927
|
}
|
|
10816
10928
|
}
|
|
10817
10929
|
pipeline(node, frame) {
|
|
10818
|
-
const inner =
|
|
10930
|
+
const inner = unwrapped2(node);
|
|
10819
10931
|
const generator = this.generator(inner, frame);
|
|
10820
10932
|
if (generator) {
|
|
10821
10933
|
return this.generated(generator, inner);
|
|
10822
10934
|
}
|
|
10823
10935
|
const helper = this.helper(inner);
|
|
10824
|
-
if (!helper || !
|
|
10936
|
+
if (!helper || !ts37.isCallExpression(inner)) {
|
|
10825
10937
|
throw new SpecializeError(node, "A generator runs in place: call it where a for…of or an iterator helper reads it.");
|
|
10826
10938
|
}
|
|
10827
10939
|
const source = this.pipeline(helper.receiver, frame);
|
|
@@ -11415,7 +11527,7 @@ var all = (conditions) => {
|
|
|
11415
11527
|
const tests = conditions.filter((condition) => condition instanceof Mapped);
|
|
11416
11528
|
return tests.length ? joined(tests, " && ") : true;
|
|
11417
11529
|
};
|
|
11418
|
-
var takes = (callback, parameter) => callback.kind === "function" && !
|
|
11530
|
+
var takes = (callback, parameter) => callback.kind === "function" && !ts37.isGetAccessorDeclaration(callback.declaration) && callback.declaration.parameters.some((declared, position) => position === parameter || declared.dotDotDotToken !== undefined && position < parameter);
|
|
11419
11531
|
var outside = (node) => isWrapper(node.parent) ? outside(node.parent) : node.parent;
|
|
11420
11532
|
|
|
11421
11533
|
// packages/compiler/src/frontend/elaborate/expressions.ts
|
|
@@ -11446,50 +11558,50 @@ class ExpressionSpecializer {
|
|
|
11446
11558
|
}
|
|
11447
11559
|
return {
|
|
11448
11560
|
...value,
|
|
11449
|
-
text: from(node,
|
|
11561
|
+
text: from(node, ts38.isIdentifier(node) ? String(value.text) : value.text)
|
|
11450
11562
|
};
|
|
11451
11563
|
}
|
|
11452
11564
|
evaluate(node, frame, expected) {
|
|
11453
|
-
if (
|
|
11565
|
+
if (ts38.isNonNullExpression(node)) {
|
|
11454
11566
|
return this.narrowed(this.expression(node.expression, frame, expected), node);
|
|
11455
11567
|
}
|
|
11456
|
-
const inner =
|
|
11568
|
+
const inner = unwrapped2(node);
|
|
11457
11569
|
if (inner !== node) {
|
|
11458
11570
|
return this.expression(inner, frame, expected);
|
|
11459
11571
|
}
|
|
11460
|
-
if (
|
|
11572
|
+
if (ts38.isNumericLiteral(node)) {
|
|
11461
11573
|
return constant(Number(node.text));
|
|
11462
11574
|
}
|
|
11463
|
-
if (
|
|
11575
|
+
if (ts38.isStringLiteral(node) || ts38.isNoSubstitutionTemplateLiteral(node)) {
|
|
11464
11576
|
return constant(node.text);
|
|
11465
11577
|
}
|
|
11466
|
-
if (node.kind ===
|
|
11578
|
+
if (node.kind === ts38.SyntaxKind.TrueKeyword) {
|
|
11467
11579
|
return constant(true);
|
|
11468
11580
|
}
|
|
11469
|
-
if (node.kind ===
|
|
11581
|
+
if (node.kind === ts38.SyntaxKind.FalseKeyword) {
|
|
11470
11582
|
return constant(false);
|
|
11471
11583
|
}
|
|
11472
|
-
if (node.kind ===
|
|
11584
|
+
if (node.kind === ts38.SyntaxKind.NullKeyword) {
|
|
11473
11585
|
return constant(undefined);
|
|
11474
11586
|
}
|
|
11475
|
-
if (
|
|
11587
|
+
if (ts38.isBigIntLiteral(node)) {
|
|
11476
11588
|
return constant(BigInt(node.text.slice(0, -1)));
|
|
11477
11589
|
}
|
|
11478
|
-
if (node.kind ===
|
|
11590
|
+
if (node.kind === ts38.SyntaxKind.ThisKeyword) {
|
|
11479
11591
|
if (!frame.self) {
|
|
11480
11592
|
throw new SpecializeError(node, "`this` outside a method.");
|
|
11481
11593
|
}
|
|
11482
11594
|
return { kind: "static", node: frame.self };
|
|
11483
11595
|
}
|
|
11484
|
-
if (
|
|
11596
|
+
if (ts38.isIdentifier(node)) {
|
|
11485
11597
|
return this.identifier(node, frame);
|
|
11486
11598
|
}
|
|
11487
|
-
if (
|
|
11599
|
+
if (ts38.isPropertyAccessExpression(node)) {
|
|
11488
11600
|
const member = this.context.checker.getSymbolAtLocation(node.name)?.valueDeclaration;
|
|
11489
|
-
if (member &&
|
|
11601
|
+
if (member && ts38.isEnumMember(member)) {
|
|
11490
11602
|
return constant(this.context.checker.getConstantValue(member));
|
|
11491
11603
|
}
|
|
11492
|
-
if (node.expression.kind ===
|
|
11604
|
+
if (node.expression.kind === ts38.SyntaxKind.SuperKeyword) {
|
|
11493
11605
|
return this.superMember(node, frame);
|
|
11494
11606
|
}
|
|
11495
11607
|
const mathConstant = mathConstants.get(node.name.text);
|
|
@@ -11501,17 +11613,17 @@ class ExpressionSpecializer {
|
|
|
11501
11613
|
}
|
|
11502
11614
|
return this.property(this.expression(node.expression, frame), node, frame);
|
|
11503
11615
|
}
|
|
11504
|
-
if (
|
|
11616
|
+
if (ts38.isElementAccessExpression(node)) {
|
|
11505
11617
|
const object = this.expression(node.expression, frame);
|
|
11506
11618
|
const index = this.expression(node.argumentExpression, frame);
|
|
11507
11619
|
return this.element(object, index, node, frame);
|
|
11508
11620
|
}
|
|
11509
|
-
if (
|
|
11621
|
+
if (ts38.isCallExpression(node)) {
|
|
11510
11622
|
return this.calls.call(node, frame, expected);
|
|
11511
11623
|
}
|
|
11512
|
-
if (
|
|
11624
|
+
if (ts38.isAwaitExpression(node)) {
|
|
11513
11625
|
const value = this.expression(node.expression, frame);
|
|
11514
|
-
if (isPromiseLink(
|
|
11626
|
+
if (isPromiseLink(unwrapped2(node.expression)) || value.kind !== "code") {
|
|
11515
11627
|
return value;
|
|
11516
11628
|
}
|
|
11517
11629
|
return {
|
|
@@ -11525,16 +11637,16 @@ class ExpressionSpecializer {
|
|
|
11525
11637
|
if (isUpdate(node)) {
|
|
11526
11638
|
return this.assignments.step(node, frame, { used: true });
|
|
11527
11639
|
}
|
|
11528
|
-
if (
|
|
11640
|
+
if (ts38.isTypeOfExpression(node)) {
|
|
11529
11641
|
return this.typeOf(this.expression(node.expression, frame));
|
|
11530
11642
|
}
|
|
11531
|
-
if (
|
|
11643
|
+
if (ts38.isPrefixUnaryExpression(node)) {
|
|
11532
11644
|
return this.unary(node, frame);
|
|
11533
11645
|
}
|
|
11534
|
-
if (
|
|
11646
|
+
if (ts38.isBinaryExpression(node)) {
|
|
11535
11647
|
return this.binary(node, frame, expected);
|
|
11536
11648
|
}
|
|
11537
|
-
if (
|
|
11649
|
+
if (ts38.isConditionalExpression(node)) {
|
|
11538
11650
|
const condition = this.expression(node.condition, frame);
|
|
11539
11651
|
if (condition.kind === "const") {
|
|
11540
11652
|
return this.expression(condition.value ? node.whenTrue : node.whenFalse, frame, expected);
|
|
@@ -11559,7 +11671,7 @@ class ExpressionSpecializer {
|
|
|
11559
11671
|
copyOf: [whenTrue, whenFalse].find((branch) => branch.kind === "code" && branch.copyOf !== undefined)?.copyOf
|
|
11560
11672
|
};
|
|
11561
11673
|
}
|
|
11562
|
-
if (
|
|
11674
|
+
if (ts38.isArrowFunction(node) || ts38.isFunctionExpression(node)) {
|
|
11563
11675
|
return {
|
|
11564
11676
|
kind: "function",
|
|
11565
11677
|
declaration: node,
|
|
@@ -11567,13 +11679,13 @@ class ExpressionSpecializer {
|
|
|
11567
11679
|
env: frame.env
|
|
11568
11680
|
};
|
|
11569
11681
|
}
|
|
11570
|
-
if (
|
|
11682
|
+
if (ts38.isObjectLiteralExpression(node)) {
|
|
11571
11683
|
return this.objectLiteral(node, frame);
|
|
11572
11684
|
}
|
|
11573
|
-
if (
|
|
11685
|
+
if (ts38.isArrayLiteralExpression(node)) {
|
|
11574
11686
|
const items = [];
|
|
11575
11687
|
for (const element of node.elements) {
|
|
11576
|
-
if (
|
|
11688
|
+
if (ts38.isSpreadElement(element)) {
|
|
11577
11689
|
const spread = this.items(this.expression(element.expression, frame));
|
|
11578
11690
|
if (!spread) {
|
|
11579
11691
|
throw new SpecializeError(element, "Spread an array setup built.");
|
|
@@ -11585,10 +11697,10 @@ class ExpressionSpecializer {
|
|
|
11585
11697
|
}
|
|
11586
11698
|
return { kind: "virtual", items };
|
|
11587
11699
|
}
|
|
11588
|
-
if (
|
|
11700
|
+
if (ts38.isTemplateExpression(node)) {
|
|
11589
11701
|
throw new SpecializeError(node, "Template strings are not supported in scan code.");
|
|
11590
11702
|
}
|
|
11591
|
-
throw new SpecializeError(node, `${
|
|
11703
|
+
throw new SpecializeError(node, `${ts38.SyntaxKind[node.kind]} is not supported in scan code.`);
|
|
11592
11704
|
}
|
|
11593
11705
|
property(object, node, frame) {
|
|
11594
11706
|
if (node.questionDotToken && object.kind === "const" && object.value === undefined) {
|
|
@@ -11623,11 +11735,11 @@ class ExpressionSpecializer {
|
|
|
11623
11735
|
if (!declaration) {
|
|
11624
11736
|
throw new SpecializeError(node, `${node.text} has no declaration.`);
|
|
11625
11737
|
}
|
|
11626
|
-
if (
|
|
11738
|
+
if (ts38.isFunctionDeclaration(declaration)) {
|
|
11627
11739
|
return { kind: "function", declaration };
|
|
11628
11740
|
}
|
|
11629
|
-
const initializer =
|
|
11630
|
-
if (initializer && (
|
|
11741
|
+
const initializer = ts38.isVariableDeclaration(declaration) && declaration.initializer;
|
|
11742
|
+
if (initializer && (ts38.isArrowFunction(initializer) || ts38.isFunctionExpression(initializer))) {
|
|
11631
11743
|
return { kind: "function", declaration: initializer };
|
|
11632
11744
|
}
|
|
11633
11745
|
const root = this.context.model.roots.find((item) => item.declaration === declaration);
|
|
@@ -11636,13 +11748,13 @@ class ExpressionSpecializer {
|
|
|
11636
11748
|
this.refuseOutsideWidth(value, this.context.layouts.iecName(root.declaration), root.declaration);
|
|
11637
11749
|
return value;
|
|
11638
11750
|
}
|
|
11639
|
-
if (initializer &&
|
|
11751
|
+
if (initializer && ts38.isVariableStatement(declaration.parent.parent) && declaration.parent.parent.parent && ts38.isSourceFile(declaration.parent.parent.parent)) {
|
|
11640
11752
|
return this.expression(initializer, { ...frame, env: new Map });
|
|
11641
11753
|
}
|
|
11642
|
-
if (
|
|
11754
|
+
if (ts38.isEnumMember(declaration)) {
|
|
11643
11755
|
return constant(this.context.checker.getConstantValue(declaration));
|
|
11644
11756
|
}
|
|
11645
|
-
if (
|
|
11757
|
+
if (ts38.isParameter(declaration)) {
|
|
11646
11758
|
throw new SpecializeError(node, `${node.text} is a parameter of a scan function, which its task calls with no arguments: read the value from the program's state instead.`);
|
|
11647
11759
|
}
|
|
11648
11760
|
throw new SpecializeError(node, `${node.text} is not available in scan code.`);
|
|
@@ -11895,11 +12007,11 @@ class ExpressionSpecializer {
|
|
|
11895
12007
|
objectLiteral(node, frame) {
|
|
11896
12008
|
const fields = new Map;
|
|
11897
12009
|
for (const property of node.properties) {
|
|
11898
|
-
if (
|
|
12010
|
+
if (ts38.isPropertyAssignment(property)) {
|
|
11899
12011
|
fields.set(property.name.getText(), this.expression(property.initializer, frame, this.context.checker.getContextualType(property.initializer)));
|
|
11900
|
-
} else if (
|
|
12012
|
+
} else if (ts38.isShorthandPropertyAssignment(property)) {
|
|
11901
12013
|
fields.set(property.name.text, this.shorthand(property, frame));
|
|
11902
|
-
} else if (
|
|
12014
|
+
} else if (ts38.isSpreadAssignment(property)) {
|
|
11903
12015
|
for (const [name, value] of this.recordFields(property.expression, frame)) {
|
|
11904
12016
|
fields.set(name, value);
|
|
11905
12017
|
}
|
|
@@ -11917,7 +12029,7 @@ class ExpressionSpecializer {
|
|
|
11917
12029
|
unary(node, frame) {
|
|
11918
12030
|
const operand = this.expression(node.operand, frame);
|
|
11919
12031
|
switch (node.operator) {
|
|
11920
|
-
case
|
|
12032
|
+
case ts38.SyntaxKind.ExclamationToken:
|
|
11921
12033
|
if (operand.kind === "const") {
|
|
11922
12034
|
return constant(!operand.value);
|
|
11923
12035
|
}
|
|
@@ -11929,7 +12041,7 @@ class ExpressionSpecializer {
|
|
|
11929
12041
|
text: mapped`!${this.context.dialect.condition(operand, node.operand)}`,
|
|
11930
12042
|
declared: this.context.checker.getBooleanType()
|
|
11931
12043
|
};
|
|
11932
|
-
case
|
|
12044
|
+
case ts38.SyntaxKind.MinusToken:
|
|
11933
12045
|
if (operand.kind === "const" && typeof operand.value === "number") {
|
|
11934
12046
|
return constant(-operand.value);
|
|
11935
12047
|
}
|
|
@@ -11938,17 +12050,17 @@ class ExpressionSpecializer {
|
|
|
11938
12050
|
text: mapped`-${this.context.dialect.code(operand, undefined)}`,
|
|
11939
12051
|
declared: this.context.checker.getTypeAtLocation(node)
|
|
11940
12052
|
};
|
|
11941
|
-
case
|
|
12053
|
+
case ts38.SyntaxKind.PlusToken:
|
|
11942
12054
|
return operand;
|
|
11943
12055
|
default:
|
|
11944
|
-
throw new SpecializeError(node, `${
|
|
12056
|
+
throw new SpecializeError(node, `${ts38.tokenToString(node.operator)} is not supported.`);
|
|
11945
12057
|
}
|
|
11946
12058
|
}
|
|
11947
12059
|
binary(node, frame, expected) {
|
|
11948
12060
|
const kind = node.operatorToken.kind;
|
|
11949
|
-
if (kind ===
|
|
12061
|
+
if (kind === ts38.SyntaxKind.AmpersandAmpersandToken || kind === ts38.SyntaxKind.BarBarToken) {
|
|
11950
12062
|
const left = this.expression(node.left, frame);
|
|
11951
|
-
const and = kind ===
|
|
12063
|
+
const and = kind === ts38.SyntaxKind.AmpersandAmpersandToken;
|
|
11952
12064
|
if (left.kind === "const") {
|
|
11953
12065
|
if (Boolean(left.value) !== and) {
|
|
11954
12066
|
return left;
|
|
@@ -12001,22 +12113,22 @@ class ExpressionSpecializer {
|
|
|
12001
12113
|
declared: this.context.checker.getBooleanType()
|
|
12002
12114
|
};
|
|
12003
12115
|
}
|
|
12004
|
-
if (kind ===
|
|
12116
|
+
if (kind === ts38.SyntaxKind.QuestionQuestionToken) {
|
|
12005
12117
|
return this.orElse(this.expression(node.left, frame), node.right, frame, expected);
|
|
12006
12118
|
}
|
|
12007
12119
|
if (isAssignment(kind)) {
|
|
12008
12120
|
return this.assignments.assign(node, frame);
|
|
12009
12121
|
}
|
|
12010
12122
|
const equality = [
|
|
12011
|
-
|
|
12012
|
-
|
|
12013
|
-
|
|
12014
|
-
|
|
12123
|
+
ts38.SyntaxKind.EqualsEqualsEqualsToken,
|
|
12124
|
+
ts38.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
12125
|
+
ts38.SyntaxKind.EqualsEqualsToken,
|
|
12126
|
+
ts38.SyntaxKind.ExclamationEqualsToken
|
|
12015
12127
|
].includes(kind);
|
|
12016
12128
|
const read = this.expression(node.left, frame);
|
|
12017
12129
|
const left = containsWrite(node.right) ? this.held(read, frame) : read;
|
|
12018
12130
|
const right = this.expression(node.right, frame, read.kind === "code" ? read.declared : undefined);
|
|
12019
|
-
const negate = kind ===
|
|
12131
|
+
const negate = kind === ts38.SyntaxKind.ExclamationEqualsEqualsToken || kind === ts38.SyntaxKind.ExclamationEqualsToken;
|
|
12020
12132
|
if (equality) {
|
|
12021
12133
|
this.references.identities(left, right, node);
|
|
12022
12134
|
return this.equality(left, right, { negate });
|
|
@@ -12027,7 +12139,7 @@ class ExpressionSpecializer {
|
|
|
12027
12139
|
return constant(finite(folded, node));
|
|
12028
12140
|
}
|
|
12029
12141
|
}
|
|
12030
|
-
if (kind ===
|
|
12142
|
+
if (kind === ts38.SyntaxKind.AsteriskAsteriskToken) {
|
|
12031
12143
|
return this.calls.floatMath("pow", [left, right], node, frame);
|
|
12032
12144
|
}
|
|
12033
12145
|
const declared = this.context.checker.getTypeAtLocation(node);
|
|
@@ -12040,7 +12152,7 @@ class ExpressionSpecializer {
|
|
|
12040
12152
|
declared
|
|
12041
12153
|
};
|
|
12042
12154
|
}
|
|
12043
|
-
if (kind ===
|
|
12155
|
+
if (kind === ts38.SyntaxKind.SlashToken && !floatIec(left) && !floatIec(right)) {
|
|
12044
12156
|
throw new SpecializeError(node, "/ on integers gives a fraction, which no integer holds: divide with div<T>(a, b) from @tsplc/compiler/types, which truncates toward zero, or make an operand REAL or LREAL.");
|
|
12045
12157
|
}
|
|
12046
12158
|
const operator = arithmeticOperators.get(kind);
|
|
@@ -12057,7 +12169,7 @@ class ExpressionSpecializer {
|
|
|
12057
12169
|
}
|
|
12058
12170
|
isNumber(node) {
|
|
12059
12171
|
const flags = this.context.checker.getTypeAtLocation(node).flags;
|
|
12060
|
-
return Boolean(flags & (
|
|
12172
|
+
return Boolean(flags & (ts38.TypeFlags.Number | ts38.TypeFlags.NumberLiteral)) && !(flags & ts38.TypeFlags.EnumLike);
|
|
12061
12173
|
}
|
|
12062
12174
|
firstNonZero(left, node, frame) {
|
|
12063
12175
|
const once = this.temporaryOf(left, frame);
|
|
@@ -12123,7 +12235,7 @@ class ExpressionSpecializer {
|
|
|
12123
12235
|
const name = declaration.name?.getText() ?? "This function";
|
|
12124
12236
|
throw new SpecializeError(at, `${name} is declared without a body, as JavaScript's own functions are: scan code calls functions the program writes and the SDK's, so compute the value in the program, or in setup.`);
|
|
12125
12237
|
}
|
|
12126
|
-
if (target.env &&
|
|
12238
|
+
if (target.env && ts38.isArrowFunction(declaration) && !ts38.isBlock(declaration.body)) {
|
|
12127
12239
|
const inline = {
|
|
12128
12240
|
...frame,
|
|
12129
12241
|
env: new Map(target.env),
|
|
@@ -12154,9 +12266,9 @@ class ExpressionSpecializer {
|
|
|
12154
12266
|
const called = this.routineTable.routine(declaration, target.self, args, target.env && { env: target.env, caller: frame.emitter });
|
|
12155
12267
|
const text = mapped`${called.name}(${joined(called.args, ", ")})`;
|
|
12156
12268
|
const returns = called.returns;
|
|
12157
|
-
if (!returns || returns.flags &
|
|
12158
|
-
const statement =
|
|
12159
|
-
if (
|
|
12269
|
+
if (!returns || returns.flags & ts38.TypeFlags.Void) {
|
|
12270
|
+
const statement = ts38.isAwaitExpression(at.parent) ? at.parent : at;
|
|
12271
|
+
if (ts38.isExpressionStatement(statement.parent) || linksPromise(at)) {
|
|
12160
12272
|
return { kind: "code", text, declared: returns };
|
|
12161
12273
|
}
|
|
12162
12274
|
frame.emitter.line(mapped`${text};`);
|
|
@@ -12176,7 +12288,7 @@ class ExpressionSpecializer {
|
|
|
12176
12288
|
};
|
|
12177
12289
|
}
|
|
12178
12290
|
superMember(node, frame) {
|
|
12179
|
-
const owner =
|
|
12291
|
+
const owner = ts38.findAncestor(node, ts38.isClassDeclaration);
|
|
12180
12292
|
if (!owner || !frame.self) {
|
|
12181
12293
|
throw new SpecializeError(node, "`super` outside a method.");
|
|
12182
12294
|
}
|
|
@@ -12222,10 +12334,10 @@ class ExpressionSpecializer {
|
|
|
12222
12334
|
}
|
|
12223
12335
|
const declared = value.declared;
|
|
12224
12336
|
const kinds = [
|
|
12225
|
-
[
|
|
12226
|
-
[
|
|
12227
|
-
[
|
|
12228
|
-
[
|
|
12337
|
+
[ts38.TypeFlags.NumberLike, "number"],
|
|
12338
|
+
[ts38.TypeFlags.StringLike, "string"],
|
|
12339
|
+
[ts38.TypeFlags.BooleanLike, "boolean"],
|
|
12340
|
+
[ts38.TypeFlags.BigIntLike, "bigint"]
|
|
12229
12341
|
];
|
|
12230
12342
|
const found = kinds.find(([flag]) => declared.flags & flag);
|
|
12231
12343
|
return constant(found ? found[1] : "object");
|
|
@@ -12255,7 +12367,7 @@ class ExpressionSpecializer {
|
|
|
12255
12367
|
return this.references.branch(present, [read, () => constant(undefined)], node, frame);
|
|
12256
12368
|
}
|
|
12257
12369
|
const defined = this.context.checker.getNonNullableType(result);
|
|
12258
|
-
if (result.flags &
|
|
12370
|
+
if (result.flags & ts38.TypeFlags.Void || defined.flags & (ts38.TypeFlags.Void | ts38.TypeFlags.Never)) {
|
|
12259
12371
|
return read();
|
|
12260
12372
|
}
|
|
12261
12373
|
if (!this.context.layouts.isOptionalRecord(result) && !this.context.layouts.literals(result)?.optional) {
|
|
@@ -12414,15 +12526,15 @@ class ExpressionSpecializer {
|
|
|
12414
12526
|
mutable,
|
|
12415
12527
|
iec
|
|
12416
12528
|
}) {
|
|
12417
|
-
const bound = !
|
|
12418
|
-
if (
|
|
12529
|
+
const bound = !ts38.isIdentifier(name) && value.kind === "code" && !value.place ? this.temporaryOf(value, frame) : value;
|
|
12530
|
+
if (ts38.isObjectBindingPattern(name)) {
|
|
12419
12531
|
for (const { element, field } of namedFields(name, "pattern")) {
|
|
12420
12532
|
const inner = this.defaulted(this.member(bound, field, element, frame), element.initializer, frame);
|
|
12421
12533
|
this.bind(element.name, inner, frame, this.context.checker.getTypeAtLocation(element.name), { mutable, iec: undefined });
|
|
12422
12534
|
}
|
|
12423
12535
|
return;
|
|
12424
12536
|
}
|
|
12425
|
-
if (
|
|
12537
|
+
if (ts38.isArrayBindingPattern(name)) {
|
|
12426
12538
|
for (const { element, index } of namedElements(name, "pattern")) {
|
|
12427
12539
|
const inner = this.defaulted(this.element(bound, constant(index), element, frame), element.initializer, frame);
|
|
12428
12540
|
this.bind(element.name, inner, frame, this.context.checker.getTypeAtLocation(element.name), { mutable, iec: undefined });
|
|
@@ -12451,7 +12563,7 @@ class ExpressionSpecializer {
|
|
|
12451
12563
|
});
|
|
12452
12564
|
return;
|
|
12453
12565
|
}
|
|
12454
|
-
if (mutable && bound.kind === "function" &&
|
|
12566
|
+
if (mutable && bound.kind === "function" && ts38.isIdentifier(name)) {
|
|
12455
12567
|
throw new SpecializeError(name, `${name.text} is a let that holds a function, and the PLC calls only functions known when the program compiles: declare it with const, or choose which function to call with an if.`);
|
|
12456
12568
|
}
|
|
12457
12569
|
if (!mutable && bound.kind !== "code") {
|
|
@@ -12459,19 +12571,19 @@ class ExpressionSpecializer {
|
|
|
12459
12571
|
frame.env.set(symbol, bound);
|
|
12460
12572
|
return;
|
|
12461
12573
|
}
|
|
12462
|
-
if (!mutable && bound.kind === "code" && bound.place &&
|
|
12574
|
+
if (!mutable && bound.kind === "code" && bound.place && ts38.isIdentifier(name) && this.isAggregate(this.context.checker.getNonNullableType(declared)) && replacedWhileAliased(name, this.context.checker)) {
|
|
12463
12575
|
this.snapshot(name, symbol, bound, declared, frame);
|
|
12464
12576
|
return;
|
|
12465
12577
|
}
|
|
12466
12578
|
if (!mutable && bound.kind === "code" && bound.place) {
|
|
12467
12579
|
if (this.isAggregate(declared)) {
|
|
12468
12580
|
const pin = bound.pinned;
|
|
12469
|
-
const repeats =
|
|
12581
|
+
const repeats = ts38.isIdentifier(name) && pureRead(bound.text) && stableAlias(name, this.context.checker);
|
|
12470
12582
|
frame.env.set(symbol, pin && !repeats ? pin(frame) : bound);
|
|
12471
12583
|
return;
|
|
12472
12584
|
}
|
|
12473
12585
|
}
|
|
12474
|
-
if (bound.kind === "code" && bound.awaited && bound.declared && bound.declared !== declared && this.isAggregate(declared) && !this.context.checker.isArrayType(declared) &&
|
|
12586
|
+
if (bound.kind === "code" && bound.awaited && bound.declared && bound.declared !== declared && this.isAggregate(declared) && !this.context.checker.isArrayType(declared) && ts38.isVariableDeclaration(name.parent) && name.parent.type !== undefined) {
|
|
12475
12587
|
const reply = frame.emitter.temporary();
|
|
12476
12588
|
frame.emitter.line(mapped`const ${reply} = ${bound.text};`);
|
|
12477
12589
|
const held = {
|
|
@@ -12495,12 +12607,15 @@ class ExpressionSpecializer {
|
|
|
12495
12607
|
const width = iec ?? (bound.kind === "code" ? bound.iec : undefined);
|
|
12496
12608
|
const layout = bound.kind === "code" ? bound.layout : undefined;
|
|
12497
12609
|
const length = bound.kind === "code" ? bound.length : undefined;
|
|
12610
|
+
if (declared.flags & ts38.TypeFlags.Void) {
|
|
12611
|
+
throw new SpecializeError(name, `${name.text} holds what a call that answers nothing returns, which is no value; make the call a statement of its own.`);
|
|
12612
|
+
}
|
|
12498
12613
|
const type = width ?? layout?.text ?? this.localType(declared);
|
|
12499
12614
|
if (iec !== undefined && bound.kind === "code" && bound.awaited && this.context.dialect.converts(bound, declared, iec)) {
|
|
12500
12615
|
throw new SpecializeError(name, `An awaited value keeps its routine's result type, ${bound.iec ?? defaultNumericTypes.number}; store it in a local of that type, then convert it.`);
|
|
12501
12616
|
}
|
|
12502
12617
|
const text = iec ? this.context.dialect.assignmentText(bound, declared, iec) : this.context.dialect.code(bound, declared);
|
|
12503
|
-
const typedByCall = bound.kind === "code" && (bound.awaited || bound.typedByTask) && !iec && !(bound.awaited && this.isAggregate(declared) &&
|
|
12618
|
+
const typedByCall = bound.kind === "code" && (bound.awaited || bound.typedByTask) && !iec && !(bound.awaited && this.isAggregate(declared) && ts38.isVariableDeclaration(name.parent) && name.parent.type !== undefined);
|
|
12504
12619
|
const annotation = typedByCall ? "" : `: ${type}`;
|
|
12505
12620
|
frame.emitter.line(mapped`${mutable || changes ? "let" : "const"} ${local}${annotation} = ${text};`);
|
|
12506
12621
|
frame.env.set(symbol, {
|
|
@@ -12538,7 +12653,7 @@ class ExpressionSpecializer {
|
|
|
12538
12653
|
});
|
|
12539
12654
|
}
|
|
12540
12655
|
isAggregate(type) {
|
|
12541
|
-
return Boolean(type.flags &
|
|
12656
|
+
return Boolean(type.flags & ts38.TypeFlags.Object) && !this.context.layouts.literalEnum(type);
|
|
12542
12657
|
}
|
|
12543
12658
|
items(value) {
|
|
12544
12659
|
if (value.kind === "virtual" && value.items) {
|
|
@@ -12566,44 +12681,44 @@ class ExpressionSpecializer {
|
|
|
12566
12681
|
}
|
|
12567
12682
|
}
|
|
12568
12683
|
var comparisonOperators2 = new Map([
|
|
12569
|
-
[
|
|
12570
|
-
[
|
|
12571
|
-
[
|
|
12572
|
-
[
|
|
12684
|
+
[ts38.SyntaxKind.LessThanToken, "<"],
|
|
12685
|
+
[ts38.SyntaxKind.LessThanEqualsToken, "<="],
|
|
12686
|
+
[ts38.SyntaxKind.GreaterThanToken, ">"],
|
|
12687
|
+
[ts38.SyntaxKind.GreaterThanEqualsToken, ">="]
|
|
12573
12688
|
]);
|
|
12574
12689
|
var arithmeticOperators = new Map([
|
|
12575
|
-
[
|
|
12576
|
-
[
|
|
12577
|
-
[
|
|
12578
|
-
[
|
|
12579
|
-
[
|
|
12580
|
-
[
|
|
12581
|
-
[
|
|
12582
|
-
[
|
|
12583
|
-
[
|
|
12584
|
-
[
|
|
12690
|
+
[ts38.SyntaxKind.PlusToken, "+"],
|
|
12691
|
+
[ts38.SyntaxKind.MinusToken, "-"],
|
|
12692
|
+
[ts38.SyntaxKind.AsteriskToken, "*"],
|
|
12693
|
+
[ts38.SyntaxKind.SlashToken, "/"],
|
|
12694
|
+
[ts38.SyntaxKind.PercentToken, "%"],
|
|
12695
|
+
[ts38.SyntaxKind.AmpersandToken, "&"],
|
|
12696
|
+
[ts38.SyntaxKind.BarToken, "|"],
|
|
12697
|
+
[ts38.SyntaxKind.CaretToken, "^"],
|
|
12698
|
+
[ts38.SyntaxKind.LessThanLessThanToken, "<<"],
|
|
12699
|
+
[ts38.SyntaxKind.GreaterThanGreaterThanToken, ">>"]
|
|
12585
12700
|
]);
|
|
12586
12701
|
var foldNumber = (kind, left, right) => {
|
|
12587
12702
|
switch (kind) {
|
|
12588
|
-
case
|
|
12703
|
+
case ts38.SyntaxKind.PlusToken:
|
|
12589
12704
|
return left + right;
|
|
12590
|
-
case
|
|
12705
|
+
case ts38.SyntaxKind.MinusToken:
|
|
12591
12706
|
return left - right;
|
|
12592
|
-
case
|
|
12707
|
+
case ts38.SyntaxKind.AsteriskToken:
|
|
12593
12708
|
return left * right;
|
|
12594
|
-
case
|
|
12709
|
+
case ts38.SyntaxKind.AsteriskAsteriskToken:
|
|
12595
12710
|
return left ** right;
|
|
12596
|
-
case
|
|
12711
|
+
case ts38.SyntaxKind.SlashToken:
|
|
12597
12712
|
return left / right;
|
|
12598
|
-
case
|
|
12713
|
+
case ts38.SyntaxKind.PercentToken:
|
|
12599
12714
|
return right === 0 ? undefined : left % right;
|
|
12600
|
-
case
|
|
12715
|
+
case ts38.SyntaxKind.LessThanToken:
|
|
12601
12716
|
return left < right;
|
|
12602
|
-
case
|
|
12717
|
+
case ts38.SyntaxKind.LessThanEqualsToken:
|
|
12603
12718
|
return left <= right;
|
|
12604
|
-
case
|
|
12719
|
+
case ts38.SyntaxKind.GreaterThanToken:
|
|
12605
12720
|
return left > right;
|
|
12606
|
-
case
|
|
12721
|
+
case ts38.SyntaxKind.GreaterThanEqualsToken:
|
|
12607
12722
|
return left >= right;
|
|
12608
12723
|
default:
|
|
12609
12724
|
return;
|
|
@@ -12623,50 +12738,50 @@ var sourceName = (target) => {
|
|
|
12623
12738
|
const own = target.declaration.name?.getText() ?? "this function";
|
|
12624
12739
|
return target.self?.captured.kind === "instance" ? `${target.self.captured.name}.${own}` : own;
|
|
12625
12740
|
};
|
|
12626
|
-
var isPromiseLink = (node) =>
|
|
12741
|
+
var isPromiseLink = (node) => ts38.isCallExpression(node) && ts38.isPropertyAccessExpression(node.expression) && promiseLinks.has(node.expression.name.text);
|
|
12627
12742
|
var promiseLinks = new Set(["then", "catch"]);
|
|
12628
|
-
var linksPromise = (node) =>
|
|
12629
|
-
var scopeOf = (name) =>
|
|
12630
|
-
var getterNamed = (members, name) => members.find((member) =>
|
|
12631
|
-
var methodNamed = (members, name) => members.find((member) =>
|
|
12743
|
+
var linksPromise = (node) => ts38.isPropertyAccessExpression(node.parent) && node.parent.expression === node && isPromiseLink(node.parent.parent);
|
|
12744
|
+
var scopeOf = (name) => ts38.findAncestor(name.parent, ts38.isFunctionLike) ?? name.getSourceFile();
|
|
12745
|
+
var getterNamed = (members, name) => members.find((member) => ts38.isGetAccessorDeclaration(member) && member.name.getText() === name);
|
|
12746
|
+
var methodNamed = (members, name) => members.find((member) => ts38.isMethodDeclaration(member) && member.name.getText() === name);
|
|
12632
12747
|
var isReadonly = (type, checker) => !checker.isArrayType(type) && type.getSymbol()?.name === "ReadonlyArray" || type.aliasSymbol?.name === "Readonly" || checker.typeToString(type).startsWith("readonly ");
|
|
12633
12748
|
var findClosure = (source, start) => {
|
|
12634
12749
|
const visit = (node) => {
|
|
12635
|
-
if ((
|
|
12750
|
+
if ((ts38.isArrowFunction(node) || ts38.isFunctionExpression(node)) && node.getStart(source) === start) {
|
|
12636
12751
|
return node;
|
|
12637
12752
|
}
|
|
12638
|
-
return node.pos <= start && start < node.end ?
|
|
12753
|
+
return node.pos <= start && start < node.end ? ts38.forEachChild(node, visit) : undefined;
|
|
12639
12754
|
};
|
|
12640
12755
|
return visit(source);
|
|
12641
12756
|
};
|
|
12642
12757
|
var closureSymbols = (declaration, checker) => {
|
|
12643
12758
|
const found = new Map;
|
|
12644
12759
|
const visit = (node) => {
|
|
12645
|
-
if (
|
|
12760
|
+
if (ts38.isIdentifier(node)) {
|
|
12646
12761
|
const symbol = valueSymbol(checker, node);
|
|
12647
12762
|
const known = found.get(node.text) ?? [];
|
|
12648
12763
|
if (symbol && !known.includes(symbol)) {
|
|
12649
12764
|
found.set(node.text, [...known, symbol]);
|
|
12650
12765
|
}
|
|
12651
12766
|
}
|
|
12652
|
-
|
|
12767
|
+
ts38.forEachChild(node, visit);
|
|
12653
12768
|
};
|
|
12654
12769
|
visit(declaration);
|
|
12655
12770
|
return found;
|
|
12656
12771
|
};
|
|
12657
12772
|
var onlyReturned = (accessor) => {
|
|
12658
12773
|
const [only, ...others] = accessor.body?.statements ?? [];
|
|
12659
|
-
return only && !others.length &&
|
|
12774
|
+
return only && !others.length && ts38.isReturnStatement(only) ? only.expression : undefined;
|
|
12660
12775
|
};
|
|
12661
12776
|
var pureRead = (text) => !/[\w\]>]\s*\(/.test(String(text).replace(/\b(?:wrap|exact)<\w+>\(/g, "("));
|
|
12662
12777
|
|
|
12663
12778
|
// packages/compiler/src/frontend/elaborate/initializers.ts
|
|
12664
|
-
import
|
|
12779
|
+
import ts40 from "typescript";
|
|
12665
12780
|
|
|
12666
12781
|
// packages/compiler/src/frontend/elaborate/storage.ts
|
|
12667
12782
|
import { basename as basename2 } from "node:path";
|
|
12668
12783
|
import { pascalCase as pascalCase3 } from "@tsplc/compiler/ir";
|
|
12669
|
-
import
|
|
12784
|
+
import ts39 from "typescript";
|
|
12670
12785
|
class Storage {
|
|
12671
12786
|
model;
|
|
12672
12787
|
checker;
|
|
@@ -12686,7 +12801,7 @@ class Storage {
|
|
|
12686
12801
|
this.kept = kept;
|
|
12687
12802
|
this.rootModule = rootModule;
|
|
12688
12803
|
this.writes = new ScanWrites(checker, sources);
|
|
12689
|
-
this.typeNames = new Set(sources.flatMap((source) => source.statements.flatMap((statement) => (
|
|
12804
|
+
this.typeNames = new Set(sources.flatMap((source) => source.statements.flatMap((statement) => (ts39.isInterfaceDeclaration(statement) || ts39.isTypeAliasDeclaration(statement) || ts39.isClassDeclaration(statement) || ts39.isEnumDeclaration(statement)) && statement.name ? [statement.name.text] : [])));
|
|
12690
12805
|
}
|
|
12691
12806
|
typeNames;
|
|
12692
12807
|
fieldSymbol(holder, name) {
|
|
@@ -12707,7 +12822,7 @@ class Storage {
|
|
|
12707
12822
|
}
|
|
12708
12823
|
fixed(holder, name) {
|
|
12709
12824
|
const declaration = this.fieldDeclaration(holder, name);
|
|
12710
|
-
return holder.owner !== undefined && !holder.owner.root.bound && declaration !== undefined &&
|
|
12825
|
+
return holder.owner !== undefined && !holder.owner.root.bound && declaration !== undefined && ts39.canHaveModifiers(declaration) && hasModifier(declaration, ts39.SyntaxKind.ReadonlyKeyword) && !this.written(holder, name);
|
|
12711
12826
|
}
|
|
12712
12827
|
written(holder, name) {
|
|
12713
12828
|
const symbol = this.fieldSymbol(holder, name);
|
|
@@ -12795,7 +12910,7 @@ class Storage {
|
|
|
12795
12910
|
return false;
|
|
12796
12911
|
}
|
|
12797
12912
|
const key = node.type && recordTypes(node.type)?.key;
|
|
12798
|
-
return !key || (key.flags & (
|
|
12913
|
+
return !key || (key.flags & (ts39.TypeFlags.EnumLike | ts39.TypeFlags.NumberLike)) === 0;
|
|
12799
12914
|
}
|
|
12800
12915
|
reach(root) {
|
|
12801
12916
|
const module = this.rootModule(root);
|
|
@@ -13004,7 +13119,7 @@ class Storage {
|
|
|
13004
13119
|
}
|
|
13005
13120
|
return { kind: "static", node };
|
|
13006
13121
|
}
|
|
13007
|
-
if (this.kept(root.file) ||
|
|
13122
|
+
if (this.kept(root.file) || ts39.getCombinedNodeFlags(root.declaration) & ts39.NodeFlags.Const) {
|
|
13008
13123
|
return capturedPrimitive(value);
|
|
13009
13124
|
}
|
|
13010
13125
|
const namespace = this.reach(root);
|
|
@@ -13023,7 +13138,7 @@ class Storage {
|
|
|
13023
13138
|
kind: "code",
|
|
13024
13139
|
text: this.imports.kept.add(root.file, root.name),
|
|
13025
13140
|
declared: this.checker.getTypeAtLocation(root.declaration.name),
|
|
13026
|
-
place: !(
|
|
13141
|
+
place: !(ts39.getCombinedNodeFlags(root.declaration) & ts39.NodeFlags.Const)
|
|
13027
13142
|
};
|
|
13028
13143
|
}
|
|
13029
13144
|
fromCaptured(value, holder, step) {
|
|
@@ -13304,13 +13419,13 @@ class Initializers {
|
|
|
13304
13419
|
}
|
|
13305
13420
|
}
|
|
13306
13421
|
defaultOf(type) {
|
|
13307
|
-
if (!type || type.flags &
|
|
13422
|
+
if (!type || type.flags & ts40.TypeFlags.Void) {
|
|
13308
13423
|
return "";
|
|
13309
13424
|
}
|
|
13310
|
-
if (type.flags &
|
|
13425
|
+
if (type.flags & ts40.TypeFlags.BooleanLike) {
|
|
13311
13426
|
return "false";
|
|
13312
13427
|
}
|
|
13313
|
-
if (type.flags &
|
|
13428
|
+
if (type.flags & ts40.TypeFlags.NumberLike) {
|
|
13314
13429
|
return "0";
|
|
13315
13430
|
}
|
|
13316
13431
|
return `defaultValue<${this.layouts.text(type)}>()`;
|
|
@@ -13319,7 +13434,7 @@ class Initializers {
|
|
|
13319
13434
|
|
|
13320
13435
|
// packages/compiler/src/frontend/elaborate/statements.ts
|
|
13321
13436
|
import { loopIterations } from "@tsplc/iec/arithmetic";
|
|
13322
|
-
import
|
|
13437
|
+
import ts41 from "typescript";
|
|
13323
13438
|
class StatementSpecializer {
|
|
13324
13439
|
context;
|
|
13325
13440
|
expressions;
|
|
@@ -13334,7 +13449,7 @@ class StatementSpecializer {
|
|
|
13334
13449
|
});
|
|
13335
13450
|
}
|
|
13336
13451
|
body(node, frame) {
|
|
13337
|
-
if (
|
|
13452
|
+
if (ts41.isBlock(node)) {
|
|
13338
13453
|
this.statements(node.statements, frame);
|
|
13339
13454
|
} else {
|
|
13340
13455
|
frame.emitter.origin = node;
|
|
@@ -13344,7 +13459,7 @@ class StatementSpecializer {
|
|
|
13344
13459
|
statements(nodes, frame) {
|
|
13345
13460
|
for (const [index, statement] of nodes.entries()) {
|
|
13346
13461
|
const rest = nodes.slice(index + 1);
|
|
13347
|
-
if (
|
|
13462
|
+
if (ts41.isIfStatement(statement) && !statement.elseStatement && rest.length) {
|
|
13348
13463
|
return this.recorded(statement, frame, () => this.guard(statement, rest, frame));
|
|
13349
13464
|
}
|
|
13350
13465
|
if (this.statement(statement, frame)) {
|
|
@@ -13390,7 +13505,7 @@ class StatementSpecializer {
|
|
|
13390
13505
|
try {
|
|
13391
13506
|
return specialize();
|
|
13392
13507
|
} catch (cause) {
|
|
13393
|
-
if (!(cause instanceof SpecializeError) ||
|
|
13508
|
+
if (!(cause instanceof SpecializeError) || ts41.isBlock(node) || node.getChildren().some((child) => ts41.isBlock(child))) {
|
|
13394
13509
|
throw cause;
|
|
13395
13510
|
}
|
|
13396
13511
|
if (!this.failures.some((failure) => failure.node === cause.node && failure.message === cause.message)) {
|
|
@@ -13403,54 +13518,54 @@ class StatementSpecializer {
|
|
|
13403
13518
|
}
|
|
13404
13519
|
specializeStatement(node, frame) {
|
|
13405
13520
|
const { emitter } = frame;
|
|
13406
|
-
if (
|
|
13521
|
+
if (ts41.isBlock(node)) {
|
|
13407
13522
|
emitter.open("{");
|
|
13408
13523
|
const jumps = this.statements(node.statements, frame);
|
|
13409
13524
|
emitter.close();
|
|
13410
13525
|
return jumps;
|
|
13411
13526
|
}
|
|
13412
|
-
if (
|
|
13413
|
-
const mutable = !(node.declarationList.flags &
|
|
13527
|
+
if (ts41.isVariableStatement(node)) {
|
|
13528
|
+
const mutable = !(node.declarationList.flags & ts41.NodeFlags.Const);
|
|
13414
13529
|
for (const declaration of node.declarationList.declarations) {
|
|
13415
13530
|
this.declare(declaration, mutable, frame);
|
|
13416
13531
|
}
|
|
13417
13532
|
return false;
|
|
13418
13533
|
}
|
|
13419
|
-
if (
|
|
13420
|
-
if (
|
|
13534
|
+
if (ts41.isExpressionStatement(node)) {
|
|
13535
|
+
if (ts41.isYieldExpression(node.expression)) {
|
|
13421
13536
|
this.yieldStatement(node.expression, frame);
|
|
13422
13537
|
return false;
|
|
13423
13538
|
}
|
|
13424
13539
|
this.effect(node.expression, frame);
|
|
13425
13540
|
return false;
|
|
13426
13541
|
}
|
|
13427
|
-
if (
|
|
13542
|
+
if (ts41.isIfStatement(node)) {
|
|
13428
13543
|
return this.ifStatement(node, frame);
|
|
13429
13544
|
}
|
|
13430
|
-
if (
|
|
13545
|
+
if (ts41.isReturnStatement(node)) {
|
|
13431
13546
|
this.returnStatement(node, frame);
|
|
13432
13547
|
return true;
|
|
13433
13548
|
}
|
|
13434
|
-
if (
|
|
13549
|
+
if (ts41.isIterationStatement(node, false) || ts41.isSwitchStatement(node)) {
|
|
13435
13550
|
this.loopOrSwitch(node, [], frame);
|
|
13436
13551
|
return false;
|
|
13437
13552
|
}
|
|
13438
|
-
if (
|
|
13553
|
+
if (ts41.isTryStatement(node)) {
|
|
13439
13554
|
this.tryStatement(node, frame);
|
|
13440
13555
|
return false;
|
|
13441
13556
|
}
|
|
13442
|
-
if (
|
|
13557
|
+
if (ts41.isBreakStatement(node) || ts41.isContinueStatement(node)) {
|
|
13443
13558
|
emitter.line(this.jumpText(node));
|
|
13444
13559
|
return true;
|
|
13445
13560
|
}
|
|
13446
|
-
if (
|
|
13561
|
+
if (ts41.isLabeledStatement(node)) {
|
|
13447
13562
|
this.labelled(node, frame);
|
|
13448
13563
|
return false;
|
|
13449
13564
|
}
|
|
13450
|
-
if (
|
|
13565
|
+
if (ts41.isEmptyStatement(node)) {
|
|
13451
13566
|
return false;
|
|
13452
13567
|
}
|
|
13453
|
-
if (
|
|
13568
|
+
if (ts41.isThrowStatement(node) && frame.async) {
|
|
13454
13569
|
const code = this.expressions.expression(node.expression, frame);
|
|
13455
13570
|
emitter.line(mapped`throw ${this.dialect.code(code, undefined)};`);
|
|
13456
13571
|
return true;
|
|
@@ -13458,22 +13573,22 @@ class StatementSpecializer {
|
|
|
13458
13573
|
throw unsupported2(node);
|
|
13459
13574
|
}
|
|
13460
13575
|
loopOrSwitch(node, labels, frame) {
|
|
13461
|
-
if (
|
|
13576
|
+
if (ts41.isForOfStatement(node)) {
|
|
13462
13577
|
this.forOf(node, labels, frame);
|
|
13463
13578
|
return;
|
|
13464
13579
|
}
|
|
13465
|
-
if (
|
|
13580
|
+
if (ts41.isSwitchStatement(node)) {
|
|
13466
13581
|
this.switch(node, labels, frame);
|
|
13467
13582
|
return;
|
|
13468
13583
|
}
|
|
13469
|
-
if (
|
|
13584
|
+
if (ts41.isForStatement(node)) {
|
|
13470
13585
|
this.forLoop(node, labels, frame);
|
|
13471
13586
|
return;
|
|
13472
13587
|
}
|
|
13473
|
-
if (!
|
|
13588
|
+
if (!ts41.isWhileStatement(node) && !ts41.isDoStatement(node)) {
|
|
13474
13589
|
throw unsupported2(node);
|
|
13475
13590
|
}
|
|
13476
|
-
if (frame.async && occursOutsideFunctions(node.statement,
|
|
13591
|
+
if (frame.async && occursOutsideFunctions(node.statement, ts41.isAwaitExpression)) {
|
|
13477
13592
|
this.suspendingLoop(node, labels, frame);
|
|
13478
13593
|
return;
|
|
13479
13594
|
}
|
|
@@ -13485,15 +13600,15 @@ class StatementSpecializer {
|
|
|
13485
13600
|
if (label !== undefined) {
|
|
13486
13601
|
return item.labels.includes(label);
|
|
13487
13602
|
}
|
|
13488
|
-
return item.kind === "loop" ||
|
|
13603
|
+
return item.kind === "loop" || ts41.isBreakStatement(node) && item.kind === "switch";
|
|
13489
13604
|
});
|
|
13490
|
-
const emitted =
|
|
13605
|
+
const emitted = ts41.isBreakStatement(node) ? scope?.breakLabel : scope?.continueLabel;
|
|
13491
13606
|
return emitted ? `break ${emitted};` : copied(node);
|
|
13492
13607
|
}
|
|
13493
13608
|
labelled(node, frame) {
|
|
13494
13609
|
const labels = [];
|
|
13495
13610
|
const inner = innermostLabelled(node, labels);
|
|
13496
|
-
if (
|
|
13611
|
+
if (ts41.isIterationStatement(inner, false) || ts41.isSwitchStatement(inner)) {
|
|
13497
13612
|
this.recorded(inner, frame, () => {
|
|
13498
13613
|
this.loopOrSwitch(inner, labels, frame);
|
|
13499
13614
|
return false;
|
|
@@ -13549,7 +13664,7 @@ class StatementSpecializer {
|
|
|
13549
13664
|
if (isStoredState(value)) {
|
|
13550
13665
|
frame.returnsStored = true;
|
|
13551
13666
|
}
|
|
13552
|
-
const element = frame.returns && this.checker.getIndexTypeOfType(frame.returns,
|
|
13667
|
+
const element = frame.returns && this.checker.getIndexTypeOfType(frame.returns, ts41.IndexKind.Number);
|
|
13553
13668
|
if (element && value.kind === "virtual" && value.items) {
|
|
13554
13669
|
frame.resultLayout = arrayLayout(this.layouts.text(element), value.items.length);
|
|
13555
13670
|
frame.resultLength = value.items.length;
|
|
@@ -13561,7 +13676,7 @@ class StatementSpecializer {
|
|
|
13561
13676
|
}
|
|
13562
13677
|
resultIec(frame) {
|
|
13563
13678
|
const declaration = frame.emitter.origin;
|
|
13564
|
-
return this.layouts.iecName(
|
|
13679
|
+
return this.layouts.iecName(ts41.findAncestor(declaration, ts41.isFunctionLike));
|
|
13565
13680
|
}
|
|
13566
13681
|
tryStatement(node, frame) {
|
|
13567
13682
|
if (!frame.async && !node.finallyBlock) {
|
|
@@ -13575,7 +13690,7 @@ class StatementSpecializer {
|
|
|
13575
13690
|
if (node.catchClause && frame.async) {
|
|
13576
13691
|
const binding = node.catchClause.variableDeclaration?.name;
|
|
13577
13692
|
frame.emitter.reopen(`} catch${binding ? ` (${binding.getText()})` : ""} {`);
|
|
13578
|
-
if (binding &&
|
|
13693
|
+
if (binding && ts41.isIdentifier(binding)) {
|
|
13579
13694
|
this.bindLocal(binding, binding.text, frame, undefined);
|
|
13580
13695
|
}
|
|
13581
13696
|
this.statements(node.catchClause.block.statements, frame);
|
|
@@ -13587,7 +13702,7 @@ class StatementSpecializer {
|
|
|
13587
13702
|
frame.emitter.close();
|
|
13588
13703
|
}
|
|
13589
13704
|
inner(node, frame) {
|
|
13590
|
-
return
|
|
13705
|
+
return ts41.isBlock(node) ? this.statements(node.statements, frame) : this.statement(node, frame);
|
|
13591
13706
|
}
|
|
13592
13707
|
bindLocal(name, local, frame, iec) {
|
|
13593
13708
|
const symbol = this.checker.getSymbolAtLocation(name);
|
|
@@ -13633,14 +13748,14 @@ class StatementSpecializer {
|
|
|
13633
13748
|
return;
|
|
13634
13749
|
}
|
|
13635
13750
|
const call = node.expression;
|
|
13636
|
-
if (
|
|
13751
|
+
if (ts41.isCallExpression(call) && ts41.isPropertyAccessExpression(call.expression) && call.expression.name.text === "entries" && !call.arguments.length) {
|
|
13637
13752
|
const array = this.expressions.expression(call.expression.expression, frame);
|
|
13638
13753
|
if (array.kind === "code") {
|
|
13639
13754
|
this.forEntries(node, mapped`${array.text}.entries()`, labels, frame);
|
|
13640
13755
|
return;
|
|
13641
13756
|
}
|
|
13642
13757
|
}
|
|
13643
|
-
if (
|
|
13758
|
+
if (ts41.isCallExpression(call) && namesSdk(this.checker, this.context.sdkFile, call.expression, "entries")) {
|
|
13644
13759
|
const [argument] = call.arguments;
|
|
13645
13760
|
const record = argument && this.expressions.expression(argument, frame);
|
|
13646
13761
|
if (record?.kind === "code") {
|
|
@@ -13687,7 +13802,7 @@ class StatementSpecializer {
|
|
|
13687
13802
|
if (iterable.kind !== "code") {
|
|
13688
13803
|
throw new SpecializeError(node.expression, "for…of needs an array.");
|
|
13689
13804
|
}
|
|
13690
|
-
if (!
|
|
13805
|
+
if (!ts41.isIdentifier(name)) {
|
|
13691
13806
|
throw new SpecializeError(name, "Destructure a runtime element inside the loop.");
|
|
13692
13807
|
}
|
|
13693
13808
|
const local = frame.emitter.local(name.text);
|
|
@@ -13726,7 +13841,7 @@ class StatementSpecializer {
|
|
|
13726
13841
|
throw new SpecializeError(at, "A generator runs in place, so it can't yield from itself.");
|
|
13727
13842
|
}
|
|
13728
13843
|
const body = declaration.body;
|
|
13729
|
-
if (!body || !
|
|
13844
|
+
if (!body || !ts41.isBlock(body)) {
|
|
13730
13845
|
throw new SpecializeError(at, "A generator needs a body.");
|
|
13731
13846
|
}
|
|
13732
13847
|
const whole = frame.emitter.local("tsplcGenerator");
|
|
@@ -13751,7 +13866,7 @@ class StatementSpecializer {
|
|
|
13751
13866
|
}
|
|
13752
13867
|
}
|
|
13753
13868
|
};
|
|
13754
|
-
if (!
|
|
13869
|
+
if (!ts41.isGetAccessorDeclaration(declaration)) {
|
|
13755
13870
|
declaration.parameters.forEach((parameter, index) => {
|
|
13756
13871
|
const given = generator.args[index] ?? constant(undefined);
|
|
13757
13872
|
const fallback = parameterDefault(this.checker, given, parameter);
|
|
@@ -13809,7 +13924,7 @@ class StatementSpecializer {
|
|
|
13809
13924
|
}
|
|
13810
13925
|
forEntries(node, iterated, labels, frame) {
|
|
13811
13926
|
const binding = onlyDeclaration(node.initializer)?.name;
|
|
13812
|
-
const names = binding &&
|
|
13927
|
+
const names = binding && ts41.isArrayBindingPattern(binding) ? binding.elements.map((element) => ts41.isBindingElement(element) && ts41.isIdentifier(element.name) ? element.name : undefined) : [];
|
|
13813
13928
|
const [indexName, itemName] = names;
|
|
13814
13929
|
if (names.length !== 2 || !indexName || !itemName) {
|
|
13815
13930
|
throw new SpecializeError(node.initializer, "Destructure [index, item] from array.entries().");
|
|
@@ -13825,13 +13940,13 @@ class StatementSpecializer {
|
|
|
13825
13940
|
}
|
|
13826
13941
|
suspendingLoop(node, labels, frame) {
|
|
13827
13942
|
const test = () => this.dialect.condition(this.expressions.expression(node.expression, frame), node.expression);
|
|
13828
|
-
if (
|
|
13943
|
+
if (ts41.isWhileStatement(node)) {
|
|
13829
13944
|
frame.emitter.open(mapped`${labelText(labels)}while (${test()}) {`);
|
|
13830
13945
|
} else {
|
|
13831
13946
|
frame.emitter.open(`${labelText(labels)}do {`);
|
|
13832
13947
|
}
|
|
13833
13948
|
this.loop(node.statement, labels, undefined, frame);
|
|
13834
|
-
if (
|
|
13949
|
+
if (ts41.isWhileStatement(node)) {
|
|
13835
13950
|
frame.emitter.close();
|
|
13836
13951
|
return;
|
|
13837
13952
|
}
|
|
@@ -13840,14 +13955,14 @@ class StatementSpecializer {
|
|
|
13840
13955
|
forLoop(node, labels, frame) {
|
|
13841
13956
|
const { condition, incrementor, initializer } = node;
|
|
13842
13957
|
const declaration = onlyDeclaration(initializer);
|
|
13843
|
-
if (!declaration || !(declaration.parent.flags &
|
|
13958
|
+
if (!declaration || !(declaration.parent.flags & ts41.NodeFlags.Let)) {
|
|
13844
13959
|
throw new SpecializeError(node, "Declare one let counter in the for header.");
|
|
13845
13960
|
}
|
|
13846
13961
|
const name = declaration.name;
|
|
13847
|
-
if (!
|
|
13962
|
+
if (!ts41.isIdentifier(name) || !declaration.initializer) {
|
|
13848
13963
|
throw new SpecializeError(declaration, "Initialize the loop counter.");
|
|
13849
13964
|
}
|
|
13850
|
-
if (!condition || !
|
|
13965
|
+
if (!condition || !ts41.isBinaryExpression(condition) || !incrementor) {
|
|
13851
13966
|
throw new SpecializeError(node, "Compare the counter to a bound and step it in the for header.");
|
|
13852
13967
|
}
|
|
13853
13968
|
if (assignsName(node.statement, name, this.checker)) {
|
|
@@ -13871,10 +13986,10 @@ class StatementSpecializer {
|
|
|
13871
13986
|
loopStep(incrementor, counter, local, declared, frame) {
|
|
13872
13987
|
const counterSymbol = this.checker.getSymbolAtLocation(counter);
|
|
13873
13988
|
const isCounter = (target) => this.checker.getSymbolAtLocation(unparenthesized(target)) === counterSymbol;
|
|
13874
|
-
if ((
|
|
13875
|
-
return `${local}${
|
|
13989
|
+
if ((ts41.isPrefixUnaryExpression(incrementor) || ts41.isPostfixUnaryExpression(incrementor)) && unitSteps.has(incrementor.operator) && isCounter(incrementor.operand)) {
|
|
13990
|
+
return `${local}${ts41.tokenToString(incrementor.operator)}`;
|
|
13876
13991
|
}
|
|
13877
|
-
if (
|
|
13992
|
+
if (ts41.isBinaryExpression(incrementor) && compoundSteps2.has(incrementor.operatorToken.kind) && isCounter(incrementor.left)) {
|
|
13878
13993
|
return mapped`${local} ${incrementor.operatorToken.getText()} ${this.dialect.code(this.expressions.expression(incrementor.right, frame), declared)}`;
|
|
13879
13994
|
}
|
|
13880
13995
|
throw new SpecializeError(incrementor, "Use i++, i--, i += step or i -= step in the for header.");
|
|
@@ -13892,10 +14007,10 @@ class StatementSpecializer {
|
|
|
13892
14007
|
}
|
|
13893
14008
|
switchClauses(node, declared, frame) {
|
|
13894
14009
|
for (const clause of node.caseBlock.clauses) {
|
|
13895
|
-
if (
|
|
14010
|
+
if (ts41.isDefaultClause(clause)) {
|
|
13896
14011
|
frame.emitter.open("default: {");
|
|
13897
14012
|
if (clause.statements.some(unreachableEnd)) {
|
|
13898
|
-
frame.emitter.line(frame.returns && !(frame.returns.flags &
|
|
14013
|
+
frame.emitter.line(frame.returns && !(frame.returns.flags & ts41.TypeFlags.Void) ? `return ${this.initializers.defaultOf(frame.returns)};` : "break;");
|
|
13899
14014
|
} else {
|
|
13900
14015
|
this.statements(clause.statements, frame);
|
|
13901
14016
|
}
|
|
@@ -13909,7 +14024,7 @@ class StatementSpecializer {
|
|
|
13909
14024
|
}
|
|
13910
14025
|
}
|
|
13911
14026
|
constantSwitch(node, subject, labels, frame) {
|
|
13912
|
-
const nested = node.caseBlock.clauses.some((clause) => clause.statements.some((statement) => !
|
|
14027
|
+
const nested = node.caseBlock.clauses.some((clause) => clause.statements.some((statement) => !ts41.isBreakStatement(statement) && containsJump(statement)));
|
|
13913
14028
|
const label = nested ? frame.emitter.local("tsplcSwitch") : undefined;
|
|
13914
14029
|
if (label) {
|
|
13915
14030
|
frame.emitter.open(`${label}: {`);
|
|
@@ -13922,7 +14037,7 @@ class StatementSpecializer {
|
|
|
13922
14037
|
matchedClauses(node, subject, labels, frame) {
|
|
13923
14038
|
const clauses = node.caseBlock.clauses;
|
|
13924
14039
|
const start = clauses.findIndex((clause) => {
|
|
13925
|
-
if (
|
|
14040
|
+
if (ts41.isDefaultClause(clause)) {
|
|
13926
14041
|
return true;
|
|
13927
14042
|
}
|
|
13928
14043
|
const label = this.expressions.expression(clause.expression, frame);
|
|
@@ -13933,7 +14048,7 @@ class StatementSpecializer {
|
|
|
13933
14048
|
}
|
|
13934
14049
|
for (const clause of clauses.slice(start)) {
|
|
13935
14050
|
for (const statement of clause.statements) {
|
|
13936
|
-
if (
|
|
14051
|
+
if (ts41.isBreakStatement(statement) && (!statement.label || labels.includes(statement.label.text))) {
|
|
13937
14052
|
return;
|
|
13938
14053
|
}
|
|
13939
14054
|
if (this.statement(statement, frame)) {
|
|
@@ -13943,14 +14058,14 @@ class StatementSpecializer {
|
|
|
13943
14058
|
}
|
|
13944
14059
|
}
|
|
13945
14060
|
effect(node, frame) {
|
|
13946
|
-
if (
|
|
14061
|
+
if (ts41.isVoidExpression(node)) {
|
|
13947
14062
|
const value = this.expressions.expression(node.expression, frame);
|
|
13948
14063
|
if (value.kind === "code") {
|
|
13949
14064
|
frame.emitter.line(mapped`void ${value.text};`);
|
|
13950
14065
|
}
|
|
13951
14066
|
return;
|
|
13952
14067
|
}
|
|
13953
|
-
if (
|
|
14068
|
+
if (ts41.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) {
|
|
13954
14069
|
this.expressions.assignments.assign(node, frame);
|
|
13955
14070
|
return;
|
|
13956
14071
|
}
|
|
@@ -13972,7 +14087,7 @@ var loopDeclaration = (node) => {
|
|
|
13972
14087
|
return declaration;
|
|
13973
14088
|
};
|
|
13974
14089
|
var onlyDeclaration = (initializer) => {
|
|
13975
|
-
if (!initializer || !
|
|
14090
|
+
if (!initializer || !ts41.isVariableDeclarationList(initializer)) {
|
|
13976
14091
|
return;
|
|
13977
14092
|
}
|
|
13978
14093
|
const [declaration, ...others] = initializer.declarations;
|
|
@@ -13986,18 +14101,18 @@ var iterationCount = (start, comparison, limit, incrementor) => {
|
|
|
13986
14101
|
return;
|
|
13987
14102
|
}
|
|
13988
14103
|
const rising = step > 0;
|
|
13989
|
-
const exclusive = rising ?
|
|
13990
|
-
const inclusive = rising ?
|
|
14104
|
+
const exclusive = rising ? ts41.SyntaxKind.LessThanToken : ts41.SyntaxKind.GreaterThanToken;
|
|
14105
|
+
const inclusive = rising ? ts41.SyntaxKind.LessThanEqualsToken : ts41.SyntaxKind.GreaterThanEqualsToken;
|
|
13991
14106
|
if (comparison !== exclusive && comparison !== inclusive) {
|
|
13992
14107
|
return;
|
|
13993
14108
|
}
|
|
13994
14109
|
return Number(loopIterations(BigInt(first), BigInt(last), comparison === exclusive, BigInt(step)));
|
|
13995
14110
|
};
|
|
13996
14111
|
var stepOf = (incrementor) => {
|
|
13997
|
-
if (
|
|
14112
|
+
if (ts41.isPrefixUnaryExpression(incrementor) || ts41.isPostfixUnaryExpression(incrementor)) {
|
|
13998
14113
|
return unitSteps.get(incrementor.operator);
|
|
13999
14114
|
}
|
|
14000
|
-
if (!
|
|
14115
|
+
if (!ts41.isBinaryExpression(incrementor) || !ts41.isNumericLiteral(incrementor.right)) {
|
|
14001
14116
|
return;
|
|
14002
14117
|
}
|
|
14003
14118
|
const size = Number(incrementor.right.text);
|
|
@@ -14008,21 +14123,21 @@ var stepOf = (incrementor) => {
|
|
|
14008
14123
|
return sign === undefined ? undefined : sign * size;
|
|
14009
14124
|
};
|
|
14010
14125
|
var unitSteps = new Map([
|
|
14011
|
-
[
|
|
14012
|
-
[
|
|
14126
|
+
[ts41.SyntaxKind.PlusPlusToken, 1],
|
|
14127
|
+
[ts41.SyntaxKind.MinusMinusToken, -1]
|
|
14013
14128
|
]);
|
|
14014
14129
|
var compoundSteps2 = new Map([
|
|
14015
|
-
[
|
|
14016
|
-
[
|
|
14130
|
+
[ts41.SyntaxKind.PlusEqualsToken, 1],
|
|
14131
|
+
[ts41.SyntaxKind.MinusEqualsToken, -1]
|
|
14017
14132
|
]);
|
|
14018
14133
|
var labelText = (labels) => labels.map((label) => `${label}: `).join("");
|
|
14019
14134
|
var innermostLabelled = (node, labels) => {
|
|
14020
14135
|
labels.push(node.label.text);
|
|
14021
|
-
return
|
|
14136
|
+
return ts41.isLabeledStatement(node.statement) ? innermostLabelled(node.statement, labels) : node.statement;
|
|
14022
14137
|
};
|
|
14023
|
-
var containsJump = (node) => occursOutsideFunctions(node, (inner) =>
|
|
14024
|
-
var unreachableEnd = (statement) =>
|
|
14025
|
-
var unsupported2 = (node) => new SpecializeError(node, `${
|
|
14138
|
+
var containsJump = (node) => occursOutsideFunctions(node, (inner) => ts41.isBreakStatement(inner) || ts41.isContinueStatement(inner));
|
|
14139
|
+
var unreachableEnd = (statement) => ts41.isReturnStatement(statement) && statement.expression !== undefined && ts41.isSatisfiesExpression(statement.expression) && statement.expression.type.kind === ts41.SyntaxKind.NeverKeyword;
|
|
14140
|
+
var unsupported2 = (node) => new SpecializeError(node, `${ts41.SyntaxKind[node.kind]} is not supported in scan code.`);
|
|
14026
14141
|
|
|
14027
14142
|
// packages/compiler/src/frontend/elaborate/specialize.ts
|
|
14028
14143
|
class Specializer {
|
|
@@ -14138,7 +14253,7 @@ class Specializer {
|
|
|
14138
14253
|
specializeRoutine(declaration, self, args, closure) {
|
|
14139
14254
|
const signature = this.checker.getSignatureFromDeclaration(declaration);
|
|
14140
14255
|
const returnType = signature && this.checker.getReturnTypeOfSignature(signature);
|
|
14141
|
-
const isAsync = Boolean(
|
|
14256
|
+
const isAsync = Boolean(ts42.canHaveModifiers(declaration) && ts42.getModifiers(declaration)?.some((modifier) => modifier.kind === ts42.SyntaxKind.AsyncKeyword));
|
|
14142
14257
|
const frame = {
|
|
14143
14258
|
emitter: new Emitter(declaration),
|
|
14144
14259
|
env: new Map(closure?.env),
|
|
@@ -14152,7 +14267,7 @@ class Specializer {
|
|
|
14152
14267
|
};
|
|
14153
14268
|
const parameters = [];
|
|
14154
14269
|
const keys = [];
|
|
14155
|
-
const parameterList =
|
|
14270
|
+
const parameterList = ts42.isGetAccessorDeclaration(declaration) ? [] : declaration.parameters;
|
|
14156
14271
|
parameterList.forEach((parameter, index) => {
|
|
14157
14272
|
const declared = this.checker.getTypeAtLocation(parameter);
|
|
14158
14273
|
if (parameter.dotDotDotToken) {
|
|
@@ -14263,7 +14378,7 @@ class Specializer {
|
|
|
14263
14378
|
const resultText = this.resultText(declaration, frame);
|
|
14264
14379
|
const head = `${frame.async ? "async " : ""}function ${name}(${kept.map((parameter) => `${parameter.name}: ${parameter.type}`).join(", ")}): ${frame.async ? `Promise<${resultText}>` : resultText} {`;
|
|
14265
14380
|
const instance = self?.owner && tsPath(self.owner);
|
|
14266
|
-
if (self?.captured.kind === "instance" && (
|
|
14381
|
+
if (self?.captured.kind === "instance" && (ts42.isMethodDeclaration(declaration) || ts42.isGetAccessorDeclaration(declaration))) {
|
|
14267
14382
|
this.origins.set(name, {
|
|
14268
14383
|
name: `${self.captured.name}.${declaration.name.getText()}`,
|
|
14269
14384
|
...instance && { instance }
|
|
@@ -14287,11 +14402,11 @@ class Specializer {
|
|
|
14287
14402
|
return this.call(name, declaration, kept.map(({ argument }) => argument), frame.returns);
|
|
14288
14403
|
}
|
|
14289
14404
|
specializeBody(declaration, frame) {
|
|
14290
|
-
if (
|
|
14405
|
+
if (ts42.isArrowFunction(declaration) && !ts42.isBlock(declaration.body)) {
|
|
14291
14406
|
frame.emitter.origin = declaration.body;
|
|
14292
14407
|
const result = this.expressions.expression(declaration.body, frame, frame.returns);
|
|
14293
14408
|
this.expressions.references.returned(result, declaration.body, frame);
|
|
14294
|
-
if (!frame.returns || frame.returns.flags &
|
|
14409
|
+
if (!frame.returns || frame.returns.flags & ts42.TypeFlags.Void) {
|
|
14295
14410
|
if (result.kind === "code") {
|
|
14296
14411
|
frame.emitter.line(mapped`${result.text};`);
|
|
14297
14412
|
}
|
|
@@ -14322,14 +14437,14 @@ class Specializer {
|
|
|
14322
14437
|
};
|
|
14323
14438
|
}
|
|
14324
14439
|
resultText(declaration, frame) {
|
|
14325
|
-
if (!frame.returns || frame.returns.flags &
|
|
14440
|
+
if (!frame.returns || frame.returns.flags & ts42.TypeFlags.Void) {
|
|
14326
14441
|
return "void";
|
|
14327
14442
|
}
|
|
14328
14443
|
return frame.resultLayout?.text ?? this.layouts.iecName(declaration) ?? (frame.referents ? referenceLayout : this.layouts.text(frame.returns));
|
|
14329
14444
|
}
|
|
14330
14445
|
routineName(declaration, self, keys) {
|
|
14331
|
-
const own = declaration.name &&
|
|
14332
|
-
const variable = !own && declaration.parent &&
|
|
14446
|
+
const own = declaration.name && ts42.isIdentifier(declaration.name) ? declaration.name.text : undefined;
|
|
14447
|
+
const variable = !own && declaration.parent && ts42.isVariableDeclaration(declaration.parent) && ts42.isIdentifier(declaration.parent.name) ? declaration.parent.name.text : undefined;
|
|
14333
14448
|
const owner = self?.captured.kind === "instance" ? self.captured.name : undefined;
|
|
14334
14449
|
const named = [owner, own ?? variable ?? "callback"].filter(Boolean).join("_");
|
|
14335
14450
|
const instance = self && this.model.identity(self);
|
|
@@ -14348,23 +14463,23 @@ class Specializer {
|
|
|
14348
14463
|
return this.results.has(name) || this.names.importedAs(name);
|
|
14349
14464
|
}
|
|
14350
14465
|
bindParameter(name, arg, declared, frame, parameters, keys) {
|
|
14351
|
-
if (
|
|
14466
|
+
if (ts42.isObjectBindingPattern(name)) {
|
|
14352
14467
|
for (const { element, field } of namedFields(name, "parameter")) {
|
|
14353
14468
|
this.bindElement(element, this.expressions.member(arg, field, element, frame), frame, parameters, keys);
|
|
14354
14469
|
}
|
|
14355
14470
|
return;
|
|
14356
14471
|
}
|
|
14357
|
-
if (
|
|
14472
|
+
if (ts42.isArrayBindingPattern(name)) {
|
|
14358
14473
|
for (const { element, index } of namedElements(name, "parameter")) {
|
|
14359
14474
|
this.bindElement(element, this.expressions.element(arg, constant(index), element, frame), frame, parameters, keys);
|
|
14360
14475
|
}
|
|
14361
14476
|
return;
|
|
14362
14477
|
}
|
|
14363
14478
|
const symbol = symbolOf(this.checker, name);
|
|
14364
|
-
if (arg.kind === "code" || arg.kind === "virtual" && !this.isKnownShape(arg) || arg.kind === "const" && !(declared.flags &
|
|
14479
|
+
if (arg.kind === "code" || arg.kind === "virtual" && !this.isKnownShape(arg) || arg.kind === "const" && !(declared.flags & ts42.TypeFlags.Undefined) && arg.value !== undefined) {
|
|
14365
14480
|
const local = frame.emitter.local(name.text);
|
|
14366
14481
|
const iec = this.layouts.iecName(name.parent) ?? (arg.kind === "code" ? arg.iec : undefined);
|
|
14367
|
-
const element = this.checker.getIndexTypeOfType(declared,
|
|
14482
|
+
const element = this.checker.getIndexTypeOfType(declared, ts42.IndexKind.Number);
|
|
14368
14483
|
const literal = arg.kind === "virtual" && arg.items && element ? arrayLayout(this.layouts.text(element), arg.items.length) : undefined;
|
|
14369
14484
|
const layout = arg.kind === "code" ? arg.layout : literal;
|
|
14370
14485
|
const type = iec ?? layout?.text ?? this.layouts.text(declared);
|
|
@@ -14373,7 +14488,7 @@ class Specializer {
|
|
|
14373
14488
|
type,
|
|
14374
14489
|
argument: this.dialect.assignmentText(arg, declared, iec)
|
|
14375
14490
|
});
|
|
14376
|
-
const routine =
|
|
14491
|
+
const routine = ts42.findAncestor(name, ts42.isFunctionLike);
|
|
14377
14492
|
const assigned = routine !== undefined && "body" in routine && routine.body !== undefined && assignsSymbol(routine.body, new Set([symbol]), this.checker);
|
|
14378
14493
|
const held = assigned ? frame.emitter.local(name.text) : local;
|
|
14379
14494
|
if (assigned) {
|
|
@@ -14418,7 +14533,7 @@ class Specializer {
|
|
|
14418
14533
|
}
|
|
14419
14534
|
passedPart(element, part, frame, parameters, keys) {
|
|
14420
14535
|
const name = element.name.getText();
|
|
14421
|
-
const base =
|
|
14536
|
+
const base = ts42.isIdentifier(element.name) ? element.name.text : "part";
|
|
14422
14537
|
if (part.kind === "choice") {
|
|
14423
14538
|
const lifted = this.lifted(part, base, frame, everyValue(frame, parameters));
|
|
14424
14539
|
keys.push({ name, value: lifted }, { name, value: "default?" });
|
|
@@ -14544,10 +14659,10 @@ class Specializer {
|
|
|
14544
14659
|
this.bindParameter(parameter.name, value, declared, frame, parameters, keys);
|
|
14545
14660
|
}
|
|
14546
14661
|
bindRest(parameter, args, declared, frame, parameters, keys) {
|
|
14547
|
-
if (!
|
|
14662
|
+
if (!ts42.isIdentifier(parameter.name)) {
|
|
14548
14663
|
throw new SpecializeError(parameter.name, "Name the rest parameter; destructure it in the body.");
|
|
14549
14664
|
}
|
|
14550
|
-
const element = this.checker.getIndexTypeOfType(declared,
|
|
14665
|
+
const element = this.checker.getIndexTypeOfType(declared, ts42.IndexKind.Number) ?? this.checker.getAnyType();
|
|
14551
14666
|
const name = parameter.name.text;
|
|
14552
14667
|
const items = args.map((arg, index) => {
|
|
14553
14668
|
if (arg.kind !== "code" && arg.kind !== "const") {
|
|
@@ -14577,7 +14692,7 @@ class Specializer {
|
|
|
14577
14692
|
return holdsSetupObjects(value) || inner.some((item) => this.isKnownShape(item)) || value.items !== undefined && inner.every((item) => item.kind === "const");
|
|
14578
14693
|
}
|
|
14579
14694
|
}
|
|
14580
|
-
var closingBrace = (declaration) => declaration.body &&
|
|
14695
|
+
var closingBrace = (declaration) => declaration.body && ts42.isBlock(declaration.body) && declaration.body.getLastToken() || declaration;
|
|
14581
14696
|
var everyValue = (frame, parameters) => ({
|
|
14582
14697
|
parameter: (text, base, type) => {
|
|
14583
14698
|
const local = frame.emitter.local(base);
|
|
@@ -14631,13 +14746,13 @@ var reread = (sample, array, index) => {
|
|
|
14631
14746
|
var readSymbols = (declaration, checker) => {
|
|
14632
14747
|
const read = new Set;
|
|
14633
14748
|
const visit = (node) => {
|
|
14634
|
-
if (
|
|
14749
|
+
if (ts42.isIdentifier(node)) {
|
|
14635
14750
|
const symbol = valueSymbol(checker, node);
|
|
14636
14751
|
if (symbol) {
|
|
14637
14752
|
read.add(symbol);
|
|
14638
14753
|
}
|
|
14639
14754
|
}
|
|
14640
|
-
|
|
14755
|
+
ts42.forEachChild(node, visit);
|
|
14641
14756
|
};
|
|
14642
14757
|
visit(declaration);
|
|
14643
14758
|
return read;
|
|
@@ -14650,26 +14765,26 @@ var keptModule = (source, checker, sdk, {
|
|
|
14650
14765
|
}) => {
|
|
14651
14766
|
const work = taskWork(source, checker, sdk);
|
|
14652
14767
|
return source.statements.every((statement) => {
|
|
14653
|
-
if (
|
|
14768
|
+
if (ts43.isFunctionDeclaration(statement) && statement.name && work.has(statement.name.text)) {
|
|
14654
14769
|
return true;
|
|
14655
14770
|
}
|
|
14656
|
-
if (
|
|
14771
|
+
if (ts43.isImportDeclaration(statement) || ts43.isExportDeclaration(statement) || ts43.isInterfaceDeclaration(statement) || ts43.isTypeAliasDeclaration(statement) || ts43.isEnumDeclaration(statement)) {
|
|
14657
14772
|
return true;
|
|
14658
14773
|
}
|
|
14659
|
-
if (!
|
|
14774
|
+
if (!ts43.isVariableStatement(statement)) {
|
|
14660
14775
|
return false;
|
|
14661
14776
|
}
|
|
14662
14777
|
return statement.declarationList.declarations.every((declaration) => {
|
|
14663
14778
|
const symbol = checker.getSymbolAtLocation(declaration.name);
|
|
14664
|
-
return declaration.initializer && constantInitializer(declaration.initializer, checker, sdk) && !(symbol && written.has(symbol)) && !(
|
|
14779
|
+
return declaration.initializer && constantInitializer(declaration.initializer, checker, sdk) && !(symbol && written.has(symbol)) && !(ts43.isIdentifier(declaration.name) && bound.has(declaration.name.text));
|
|
14665
14780
|
});
|
|
14666
14781
|
});
|
|
14667
14782
|
};
|
|
14668
14783
|
var placeRoot = (node) => {
|
|
14669
|
-
if (
|
|
14784
|
+
if (ts43.isIdentifier(node)) {
|
|
14670
14785
|
return node;
|
|
14671
14786
|
}
|
|
14672
|
-
if (
|
|
14787
|
+
if (ts43.isPropertyAccessExpression(node) || ts43.isElementAccessExpression(node) || ts43.isParenthesizedExpression(node) || ts43.isNonNullExpression(node)) {
|
|
14673
14788
|
return placeRoot(node.expression);
|
|
14674
14789
|
}
|
|
14675
14790
|
return;
|
|
@@ -14684,13 +14799,13 @@ var writtenBindings = (sources, checker) => {
|
|
|
14684
14799
|
}
|
|
14685
14800
|
};
|
|
14686
14801
|
const visit = (node) => {
|
|
14687
|
-
if (
|
|
14802
|
+
if (ts43.isBinaryExpression(node) && node.operatorToken.kind >= ts43.SyntaxKind.FirstAssignment && node.operatorToken.kind <= ts43.SyntaxKind.LastAssignment) {
|
|
14688
14803
|
note(node.left);
|
|
14689
14804
|
}
|
|
14690
|
-
if ((
|
|
14805
|
+
if ((ts43.isPrefixUnaryExpression(node) || ts43.isPostfixUnaryExpression(node)) && (node.operator === ts43.SyntaxKind.PlusPlusToken || node.operator === ts43.SyntaxKind.MinusMinusToken)) {
|
|
14691
14806
|
note(node.operand);
|
|
14692
14807
|
}
|
|
14693
|
-
|
|
14808
|
+
ts43.forEachChild(node, visit);
|
|
14694
14809
|
};
|
|
14695
14810
|
for (const source of sources) {
|
|
14696
14811
|
visit(source);
|
|
@@ -14698,7 +14813,7 @@ var writtenBindings = (sources, checker) => {
|
|
|
14698
14813
|
return written;
|
|
14699
14814
|
};
|
|
14700
14815
|
var taskWork = (source, checker, sdk) => {
|
|
14701
|
-
const functions = new Map(source.statements.flatMap((statement) =>
|
|
14816
|
+
const functions = new Map(source.statements.flatMap((statement) => ts43.isFunctionDeclaration(statement) && statement.name ? [[statement.name.text, statement]] : []));
|
|
14702
14817
|
const work = new Set;
|
|
14703
14818
|
const add = (name) => {
|
|
14704
14819
|
const declaration = functions.get(name);
|
|
@@ -14707,31 +14822,31 @@ var taskWork = (source, checker, sdk) => {
|
|
|
14707
14822
|
}
|
|
14708
14823
|
work.add(name);
|
|
14709
14824
|
const visit = (node) => {
|
|
14710
|
-
if (
|
|
14711
|
-
const called = checker.getSymbolAtLocation(node.expression)?.declarations?.find(
|
|
14825
|
+
if (ts43.isCallExpression(node) && ts43.isIdentifier(node.expression)) {
|
|
14826
|
+
const called = checker.getSymbolAtLocation(node.expression)?.declarations?.find(ts43.isFunctionDeclaration);
|
|
14712
14827
|
if (called?.name && called.parent === source) {
|
|
14713
14828
|
add(called.name.text);
|
|
14714
14829
|
}
|
|
14715
14830
|
}
|
|
14716
|
-
|
|
14831
|
+
ts43.forEachChild(node, visit);
|
|
14717
14832
|
};
|
|
14718
|
-
|
|
14833
|
+
ts43.forEachChild(declaration, visit);
|
|
14719
14834
|
};
|
|
14720
14835
|
for (const statement of source.statements) {
|
|
14721
|
-
if (!
|
|
14836
|
+
if (!ts43.isVariableStatement(statement)) {
|
|
14722
14837
|
continue;
|
|
14723
14838
|
}
|
|
14724
14839
|
for (const declaration of statement.declarationList.declarations) {
|
|
14725
14840
|
const call = declaration.initializer;
|
|
14726
|
-
const argument = call &&
|
|
14727
|
-
if (argument &&
|
|
14841
|
+
const argument = call && ts43.isCallExpression(call) && definesTask(call, checker, sdk) ? call.arguments[0] : undefined;
|
|
14842
|
+
if (argument && ts43.isIdentifier(argument)) {
|
|
14728
14843
|
add(argument.text);
|
|
14729
14844
|
}
|
|
14730
14845
|
}
|
|
14731
14846
|
}
|
|
14732
14847
|
return work;
|
|
14733
14848
|
};
|
|
14734
|
-
var needsElaboration = (sources, kept, checker, sdk) => sources.some((source) => !kept(source.fileName) && source.statements.some((statement) =>
|
|
14849
|
+
var needsElaboration = (sources, kept, checker, sdk) => sources.some((source) => !kept(source.fileName) && source.statements.some((statement) => ts43.isClassDeclaration(statement) && !statement.heritageClauses?.some((clause) => clause.types.some((type) => namesSdk(checker, sdk, type.expression, "FunctionBlock")))));
|
|
14735
14850
|
var foldedConstant = (value, iec) => {
|
|
14736
14851
|
switch (value.kind) {
|
|
14737
14852
|
case "number":
|
|
@@ -14795,12 +14910,12 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14795
14910
|
const [entryFile, ...otherModules] = modules;
|
|
14796
14911
|
const [entry] = Object.values(entries);
|
|
14797
14912
|
if (!entry || !entryFile || otherModules.length) {
|
|
14798
|
-
diagnostics.push(
|
|
14913
|
+
diagnostics.push(error8("PLC1001", "A plain TypeScript program exports all its scan functions from one module."));
|
|
14799
14914
|
return result;
|
|
14800
14915
|
}
|
|
14801
14916
|
const entrySource = program.getSourceFile(entryFile);
|
|
14802
14917
|
if (!entrySource) {
|
|
14803
|
-
diagnostics.push(
|
|
14918
|
+
diagnostics.push(error8("PLC1001", `${entry.module} is not part of the program.`));
|
|
14804
14919
|
return result;
|
|
14805
14920
|
}
|
|
14806
14921
|
const directory = join3(dirname5(entryFile), generatedDirectory).replace(/\\/g, "/");
|
|
@@ -15075,21 +15190,21 @@ var reservedWords = new Set([
|
|
|
15075
15190
|
]);
|
|
15076
15191
|
var failure = (cause, positions, debug) => {
|
|
15077
15192
|
if (cause instanceof SpecializeError) {
|
|
15078
|
-
return
|
|
15193
|
+
return error8("PLC1010", cause.message, positions.node(cause.node));
|
|
15079
15194
|
}
|
|
15080
15195
|
if (!(cause instanceof Error)) {
|
|
15081
|
-
return
|
|
15196
|
+
return error8("PLC1010", String(cause));
|
|
15082
15197
|
}
|
|
15083
|
-
return
|
|
15198
|
+
return error8("PLC1010", debug.stackTraces ? String(cause.stack) : cause.message);
|
|
15084
15199
|
};
|
|
15085
15200
|
var findEntry = (source, name) => {
|
|
15086
15201
|
for (const statement of source.statements) {
|
|
15087
|
-
if (
|
|
15202
|
+
if (ts43.isFunctionDeclaration(statement) && statement.name?.text === name && statement.body) {
|
|
15088
15203
|
return statement;
|
|
15089
15204
|
}
|
|
15090
|
-
if (
|
|
15205
|
+
if (ts43.isVariableStatement(statement)) {
|
|
15091
15206
|
for (const declaration of statement.declarationList.declarations) {
|
|
15092
|
-
if (
|
|
15207
|
+
if (ts43.isIdentifier(declaration.name) && declaration.name.text === name && declaration.initializer && ts43.isArrowFunction(declaration.initializer)) {
|
|
15093
15208
|
return declaration.initializer;
|
|
15094
15209
|
}
|
|
15095
15210
|
}
|
|
@@ -15127,8 +15242,8 @@ var generatedModules = (roots, {
|
|
|
15127
15242
|
};
|
|
15128
15243
|
|
|
15129
15244
|
// packages/compiler/src/frontend/promises.ts
|
|
15130
|
-
import { error as
|
|
15131
|
-
import
|
|
15245
|
+
import { error as error9 } from "@tsplc/compiler/ir";
|
|
15246
|
+
import ts44 from "typescript";
|
|
15132
15247
|
var lowerPromises = (program, sdk, positions, root) => {
|
|
15133
15248
|
const files = plcSources(program);
|
|
15134
15249
|
const rewrite = new PromiseRewrite(program.getTypeChecker(), sdk, positions, files, new GeneratedNames(root));
|
|
@@ -15176,30 +15291,30 @@ function rewriteChains(rewrite, files, diagnostics) {
|
|
|
15176
15291
|
};
|
|
15177
15292
|
for (const source of files) {
|
|
15178
15293
|
const visit = (node, owner) => {
|
|
15179
|
-
if (
|
|
15294
|
+
if (ts44.isFunctionDeclaration(node) || ts44.isMethodDeclaration(node)) {
|
|
15180
15295
|
if (node.body) {
|
|
15181
|
-
|
|
15296
|
+
ts44.forEachChild(node.body, (child) => visit(child, node));
|
|
15182
15297
|
}
|
|
15183
15298
|
return;
|
|
15184
15299
|
}
|
|
15185
15300
|
if (!owner) {
|
|
15186
|
-
|
|
15301
|
+
ts44.forEachChild(node, (child) => visit(child));
|
|
15187
15302
|
return;
|
|
15188
15303
|
}
|
|
15189
|
-
if (
|
|
15304
|
+
if (ts44.isCallExpression(node) && rewrite.namesSdk(node.expression, "asyncDiagnostics")) {
|
|
15190
15305
|
rewrite.useDiagnostics(node);
|
|
15191
15306
|
diagnosticsCalls.push(node);
|
|
15192
15307
|
return;
|
|
15193
15308
|
}
|
|
15194
|
-
if (
|
|
15309
|
+
if (ts44.isCallExpression(node) && rewrite.namesSdk(node.expression, "cancelAsync")) {
|
|
15195
15310
|
cancelCalls.push({ node, owner });
|
|
15196
15311
|
return;
|
|
15197
15312
|
}
|
|
15198
|
-
if (
|
|
15199
|
-
const expression =
|
|
15313
|
+
if (ts44.isExpressionStatement(node)) {
|
|
15314
|
+
const expression = ts44.isVoidExpression(node.expression) ? node.expression.expression : node.expression;
|
|
15200
15315
|
const chain = chainOf(rewrite, expression);
|
|
15201
15316
|
if (chain) {
|
|
15202
|
-
if (hasModifier(owner,
|
|
15317
|
+
if (hasModifier(owner, ts44.SyntaxKind.AsyncKeyword)) {
|
|
15203
15318
|
diagnostics.push(rewrite.diagnostic(node, "Await async work inside an async routine; detached calls belong in synchronous controllers."));
|
|
15204
15319
|
return;
|
|
15205
15320
|
}
|
|
@@ -15207,26 +15322,26 @@ function rewriteChains(rewrite, files, diagnostics) {
|
|
|
15207
15322
|
return;
|
|
15208
15323
|
}
|
|
15209
15324
|
}
|
|
15210
|
-
if (
|
|
15325
|
+
if (ts44.isCallExpression(node)) {
|
|
15211
15326
|
const chain = chainOf(rewrite, node);
|
|
15212
15327
|
if (chain?.links.length) {
|
|
15213
15328
|
lower(node, node, chain, owner, { detached: false });
|
|
15214
15329
|
return;
|
|
15215
15330
|
}
|
|
15216
15331
|
}
|
|
15217
|
-
|
|
15332
|
+
ts44.forEachChild(node, (child) => visit(child, owner));
|
|
15218
15333
|
};
|
|
15219
15334
|
visit(source);
|
|
15220
15335
|
}
|
|
15221
15336
|
return { diagnosticsCalls, cancelCalls };
|
|
15222
15337
|
}
|
|
15223
15338
|
function cancelScope(rewrite, node, owner, diagnostics) {
|
|
15224
|
-
const scope =
|
|
15225
|
-
const member =
|
|
15339
|
+
const scope = ts44.isMethodDeclaration(owner) && ts44.isClassDeclaration(owner.parent) ? owner.parent : owner.getSourceFile();
|
|
15340
|
+
const member = ts44.isClassDeclaration(scope);
|
|
15226
15341
|
const [argument, ...rest] = node.arguments;
|
|
15227
|
-
const roots = member ? undefined : node.arguments.map((routine) =>
|
|
15342
|
+
const roots = member ? undefined : node.arguments.map((routine) => ts44.isIdentifier(routine) ? rewrite.symbol(routine) : undefined);
|
|
15228
15343
|
const byRoutine = roots !== undefined && roots.length > 0 && roots.every((root) => root !== undefined && rewrite.routines.has(root));
|
|
15229
|
-
if (argument && !byRoutine && (rest.length || argument.kind !==
|
|
15344
|
+
if (argument && !byRoutine && (rest.length || argument.kind !== ts44.SyntaxKind.ThisKeyword)) {
|
|
15230
15345
|
diagnostics.push(rewrite.diagnostic(node, "cancelAsync takes this in a controller, or no argument in a module."));
|
|
15231
15346
|
}
|
|
15232
15347
|
const name = rewrite.names.name("tsplcCancelScope", node);
|
|
@@ -15262,7 +15377,7 @@ class PromiseRewrite {
|
|
|
15262
15377
|
return namesSdk(this.checker, this.sdk, node, name);
|
|
15263
15378
|
}
|
|
15264
15379
|
diagnostic(node, message) {
|
|
15265
|
-
return
|
|
15380
|
+
return error9("PLC1012", message, this.positions.node(node));
|
|
15266
15381
|
}
|
|
15267
15382
|
fail(node, message) {
|
|
15268
15383
|
throw new SemanticError(this.diagnostic(node, message));
|
|
@@ -15281,10 +15396,10 @@ class PromiseRewrite {
|
|
|
15281
15396
|
]);
|
|
15282
15397
|
}
|
|
15283
15398
|
extendedFiles() {
|
|
15284
|
-
return [...this.additions.keys()].filter(
|
|
15399
|
+
return [...this.additions.keys()].filter(ts44.isSourceFile);
|
|
15285
15400
|
}
|
|
15286
15401
|
typeText(node, source) {
|
|
15287
|
-
if (node.kind ===
|
|
15402
|
+
if (node.kind === ts44.SyntaxKind.VoidKeyword) {
|
|
15288
15403
|
return "void";
|
|
15289
15404
|
}
|
|
15290
15405
|
if (node.getSourceFile() === source) {
|
|
@@ -15318,7 +15433,7 @@ class PromiseRewrite {
|
|
|
15318
15433
|
}
|
|
15319
15434
|
sources(files) {
|
|
15320
15435
|
for (const [owner, texts] of this.additions) {
|
|
15321
|
-
if (!
|
|
15436
|
+
if (!ts44.isClassDeclaration(owner)) {
|
|
15322
15437
|
continue;
|
|
15323
15438
|
}
|
|
15324
15439
|
const source = owner.getSourceFile();
|
|
@@ -15348,7 +15463,7 @@ class PromiseRewrite {
|
|
|
15348
15463
|
|
|
15349
15464
|
// packages/compiler/src/frontend/services.ts
|
|
15350
15465
|
import { dirname as dirname6, join as join4 } from "node:path";
|
|
15351
|
-
import { error as
|
|
15466
|
+
import { error as error10 } from "@tsplc/compiler/ir";
|
|
15352
15467
|
import {
|
|
15353
15468
|
isIntegerType,
|
|
15354
15469
|
plcProcedureKind,
|
|
@@ -15357,7 +15472,7 @@ import {
|
|
|
15357
15472
|
wireRecord
|
|
15358
15473
|
} from "@tsplc/server/contract";
|
|
15359
15474
|
import { servicePath } from "@tsplc/server/wire";
|
|
15360
|
-
import
|
|
15475
|
+
import ts45 from "typescript";
|
|
15361
15476
|
class WireLayoutError extends Error {
|
|
15362
15477
|
}
|
|
15363
15478
|
var contractCheck = (check) => {
|
|
@@ -15394,12 +15509,12 @@ var layoutNumber = (checker, type, name, label) => {
|
|
|
15394
15509
|
return value.value;
|
|
15395
15510
|
};
|
|
15396
15511
|
var scalarLayout = (checker, type, label) => {
|
|
15397
|
-
if (type.flags &
|
|
15512
|
+
if (type.flags & ts45.TypeFlags.Boolean) {
|
|
15398
15513
|
return { field: { kind: "boolean" } };
|
|
15399
15514
|
}
|
|
15400
15515
|
const first = type.isUnion() ? type.types[0] : type;
|
|
15401
15516
|
const member = first?.symbol?.valueDeclaration;
|
|
15402
|
-
if (first && first.flags &
|
|
15517
|
+
if (first && first.flags & ts45.TypeFlags.EnumLiteral && member && ts45.isEnumMember(member)) {
|
|
15403
15518
|
const members = {};
|
|
15404
15519
|
for (const item of member.parent.members) {
|
|
15405
15520
|
const value = checker.getConstantValue(item);
|
|
@@ -15428,13 +15543,13 @@ var scalarLayout = (checker, type, label) => {
|
|
|
15428
15543
|
var recordLayout = (checker, type, label, output) => {
|
|
15429
15544
|
const fields = {};
|
|
15430
15545
|
const enums = {};
|
|
15431
|
-
if (!(type.flags &
|
|
15432
|
-
if (!(type.flags &
|
|
15546
|
+
if (!(type.flags & ts45.TypeFlags.Void)) {
|
|
15547
|
+
if (!(type.flags & ts45.TypeFlags.Object) || type.getStringIndexType() || type.getNumberIndexType() || type.getCallSignatures().length) {
|
|
15433
15548
|
throw new WireLayoutError(`${label}: PLC procedures exchange one closed z.strictObject() record.`);
|
|
15434
15549
|
}
|
|
15435
15550
|
for (const field of type.getProperties()) {
|
|
15436
15551
|
const key = `${label}.${field.name}`;
|
|
15437
|
-
if (field.flags &
|
|
15552
|
+
if (field.flags & ts45.SymbolFlags.Optional) {
|
|
15438
15553
|
throw new WireLayoutError(`${key}: optional wire fields are unsupported.`);
|
|
15439
15554
|
}
|
|
15440
15555
|
const value = checker.getTypeOfSymbol(field);
|
|
@@ -15477,15 +15592,16 @@ var procedureAt = (checker, router, path) => {
|
|
|
15477
15592
|
};
|
|
15478
15593
|
var pascalServicePath = (path) => path.split(".").map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)).join("");
|
|
15479
15594
|
var serviceExportName = (path) => `tsplcService_${path.replaceAll(".", "_")}`;
|
|
15595
|
+
var answersNothing = (type) => Boolean(type.flags & (ts45.TypeFlags.Void | ts45.TypeFlags.Undefined));
|
|
15480
15596
|
var serviceCall = (checker, path, procedure, record) => wireLayoutOf(() => {
|
|
15481
15597
|
const kind = contractCheck(() => plcProcedureKind(path, procedure.kind));
|
|
15482
15598
|
const input = recordLayout(checker, procedure.input, `${path}.input`, false);
|
|
15483
|
-
const output = recordLayout(checker, procedure.output, `${path}.output`, true);
|
|
15484
|
-
const contract = contractCheck(() => serviceContract(path, kind, { fields: input.fields }, { fields: output.fields }));
|
|
15599
|
+
const output = answersNothing(procedure.output) ? null : recordLayout(checker, procedure.output, `${path}.output`, true);
|
|
15600
|
+
const contract = contractCheck(() => serviceContract(path, kind, { fields: input.fields }, output && { fields: output.fields }));
|
|
15485
15601
|
return {
|
|
15486
15602
|
contract,
|
|
15487
15603
|
input: record(input, `${pascalServicePath(path)}Input`),
|
|
15488
|
-
output: record(output, `${pascalServicePath(path)}Output`)
|
|
15604
|
+
output: output && record(output, `${pascalServicePath(path)}Output`)
|
|
15489
15605
|
};
|
|
15490
15606
|
});
|
|
15491
15607
|
var lowerServices = (program, sdk, positions, options) => {
|
|
@@ -15494,7 +15610,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15494
15610
|
const records = new Map;
|
|
15495
15611
|
const calls = new Map;
|
|
15496
15612
|
const checker = program.getTypeChecker();
|
|
15497
|
-
const fail = (node, code, message) => diagnostics.push(
|
|
15613
|
+
const fail = (node, code, message) => diagnostics.push(error10(code, message, positions.node(node)));
|
|
15498
15614
|
const record = (layout, name) => {
|
|
15499
15615
|
const key = recordKey(layout);
|
|
15500
15616
|
const existing = records.get(key);
|
|
@@ -15509,17 +15625,17 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15509
15625
|
const sourceFiles = plcSources(program);
|
|
15510
15626
|
for (const source of sourceFiles) {
|
|
15511
15627
|
for (const statement of source.statements) {
|
|
15512
|
-
if (!
|
|
15628
|
+
if (!ts45.isVariableStatement(statement)) {
|
|
15513
15629
|
continue;
|
|
15514
15630
|
}
|
|
15515
15631
|
for (const declaration of statement.declarationList.declarations) {
|
|
15516
15632
|
const call = declaration.initializer;
|
|
15517
|
-
if (!call || !
|
|
15633
|
+
if (!call || !ts45.isCallExpression(call) || !namesSdk(checker, sdk, call.expression, "createServiceClient")) {
|
|
15518
15634
|
continue;
|
|
15519
15635
|
}
|
|
15520
15636
|
const router = call.typeArguments?.[0];
|
|
15521
15637
|
const client = symbolAt(checker, declaration.name);
|
|
15522
|
-
if (!router || !client || !(statement.declarationList.flags &
|
|
15638
|
+
if (!router || !client || !(statement.declarationList.flags & ts45.NodeFlags.Const)) {
|
|
15523
15639
|
fail(declaration, "PLC1013", "Create a service client as a module constant: const services = createServiceClient<AppRouter>().");
|
|
15524
15640
|
} else if (!options) {
|
|
15525
15641
|
fail(call, "PLC1013", "Configure server in tsplc.config.ts to call the server.");
|
|
@@ -15539,11 +15655,11 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15539
15655
|
const edits = [];
|
|
15540
15656
|
const imports = new Map;
|
|
15541
15657
|
const visit = (node) => {
|
|
15542
|
-
if (
|
|
15658
|
+
if (ts45.isImportDeclaration(node) && ts45.isStringLiteral(node.moduleSpecifier)) {
|
|
15543
15659
|
const clause = node.importClause;
|
|
15544
15660
|
const bindings = clause?.namedBindings;
|
|
15545
15661
|
const runtime = runtimeImport(node, checker);
|
|
15546
|
-
if (runtime && clause && bindings &&
|
|
15662
|
+
if (runtime && clause && bindings && ts45.isNamedImports(bindings)) {
|
|
15547
15663
|
const elements = bindings.elements.filter((element) => {
|
|
15548
15664
|
const imported = symbolAt(checker, element.name);
|
|
15549
15665
|
return !imported || !clients.has(imported);
|
|
@@ -15552,7 +15668,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15552
15668
|
edits.push({
|
|
15553
15669
|
start: node.getStart(source),
|
|
15554
15670
|
end: node.end,
|
|
15555
|
-
text: elements.length || clause.name ?
|
|
15671
|
+
text: elements.length || clause.name ? ts45.createPrinter().printNode(ts45.EmitHint.Unspecified, ts45.factory.updateImportDeclaration(node, node.modifiers, ts45.factory.updateImportClause(clause, clause.isTypeOnly, clause.name, ts45.factory.updateNamedImports(bindings, elements)), node.moduleSpecifier, node.attributes), source) : ""
|
|
15556
15672
|
});
|
|
15557
15673
|
}
|
|
15558
15674
|
}
|
|
@@ -15569,7 +15685,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15569
15685
|
fail(node, "PLC1013", `Server runtime import ${node.moduleSpecifier.text} cannot execute in PLC code. Expose a PLC procedure on the server router and call it through createServiceClient instead.`);
|
|
15570
15686
|
}
|
|
15571
15687
|
}
|
|
15572
|
-
if (
|
|
15688
|
+
if (ts45.isVariableStatement(node)) {
|
|
15573
15689
|
const clientStatement = node.declarationList.declarations.some((declaration) => {
|
|
15574
15690
|
const declared = symbolAt(checker, declaration.name);
|
|
15575
15691
|
return declared && clients.has(declared);
|
|
@@ -15579,8 +15695,8 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15579
15695
|
return;
|
|
15580
15696
|
}
|
|
15581
15697
|
}
|
|
15582
|
-
if (clients.size &&
|
|
15583
|
-
const named =
|
|
15698
|
+
if (clients.size && ts45.isTypeQueryNode(node)) {
|
|
15699
|
+
const named = ts45.isQualifiedName(node.exprName) ? undefined : symbolAt(checker, node.exprName);
|
|
15584
15700
|
if (named && clients.has(named)) {
|
|
15585
15701
|
edits.push({
|
|
15586
15702
|
start: node.getStart(source),
|
|
@@ -15590,10 +15706,10 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15590
15706
|
return;
|
|
15591
15707
|
}
|
|
15592
15708
|
}
|
|
15593
|
-
if (clients.size &&
|
|
15709
|
+
if (clients.size && ts45.isPropertyAccessExpression(node)) {
|
|
15594
15710
|
const parts = [];
|
|
15595
15711
|
let root = node;
|
|
15596
|
-
while (
|
|
15712
|
+
while (ts45.isPropertyAccessExpression(root)) {
|
|
15597
15713
|
parts.unshift(root.name.text);
|
|
15598
15714
|
root = root.expression;
|
|
15599
15715
|
}
|
|
@@ -15623,7 +15739,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
15623
15739
|
return;
|
|
15624
15740
|
}
|
|
15625
15741
|
}
|
|
15626
|
-
|
|
15742
|
+
ts45.forEachChild(node, visit);
|
|
15627
15743
|
};
|
|
15628
15744
|
visit(source);
|
|
15629
15745
|
if (!edits.length) {
|
|
@@ -15675,8 +15791,9 @@ var serviceModule = (records, calls, options) => {
|
|
|
15675
15791
|
const functions = [...calls.values()].sort((left, right) => left.contract.path.localeCompare(right.contract.path)).map(({ contract, input, output }) => {
|
|
15676
15792
|
const hasInput = Object.keys(contract.input.fields).length > 0;
|
|
15677
15793
|
const request = hasInput ? "input" : `defaultValue<${input}>()`;
|
|
15678
|
-
const
|
|
15679
|
-
|
|
15794
|
+
const answer = output ?? "void";
|
|
15795
|
+
const operation = options.transport === "http" ? `tsplcServiceFetch<${answer}>(\`\${tsplcServiceBaseUrl}${servicePath(contract)}\`,{method:"POST",json:${request}})` : `tsplcServiceCall<${answer},${input}>(${JSON.stringify(servicePath(contract))},${request})`;
|
|
15796
|
+
return `export const ${serviceExportName(contract.path)}=async(${hasInput ? `input:${input}` : ""}):Promise<${answer}>=>{${output ? "return " : ""}await ${operation};};`;
|
|
15680
15797
|
});
|
|
15681
15798
|
const transport = options.transport === "http" ? `import {fetch as tsplcServiceFetch} from "${sdkModules.async}";
|
|
15682
15799
|
import {env as tsplcServiceEnv} from "${sdkModules.config}";` : `import {opcuaCall as tsplcServiceCall} from "${sdkModules.async}";`;
|
|
@@ -15698,7 +15815,7 @@ var serviceRecords = (program, lowering) => {
|
|
|
15698
15815
|
return null;
|
|
15699
15816
|
}
|
|
15700
15817
|
const checker = program.getTypeChecker();
|
|
15701
|
-
const declarations = new Map(source.statements.filter(
|
|
15818
|
+
const declarations = new Map(source.statements.filter(ts45.isInterfaceDeclaration).map((value) => [value.name.text, value]));
|
|
15702
15819
|
return (type) => {
|
|
15703
15820
|
const layout = wireLayoutOf(() => recordLayout(checker, type, "record", false));
|
|
15704
15821
|
if (layout instanceof WireLayoutError) {
|
|
@@ -15719,8 +15836,8 @@ function tsDiagnostic(diagnostic, locate) {
|
|
|
15719
15836
|
const result = {
|
|
15720
15837
|
origin: "typescript",
|
|
15721
15838
|
code: `TS${diagnostic.code}`,
|
|
15722
|
-
severity: diagnostic.category ===
|
|
15723
|
-
message:
|
|
15839
|
+
severity: diagnostic.category === ts46.DiagnosticCategory.Error ? "error" : "warning",
|
|
15840
|
+
message: ts46.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
15724
15841
|
`)
|
|
15725
15842
|
};
|
|
15726
15843
|
if (diagnostic.file && diagnostic.start !== undefined) {
|
|
@@ -15745,11 +15862,11 @@ function compileFrontend(options, dialect) {
|
|
|
15745
15862
|
const debug = compileDebug(environment);
|
|
15746
15863
|
const filename = resolve7(options.tsconfig);
|
|
15747
15864
|
const root = dirname7(filename);
|
|
15748
|
-
const config =
|
|
15865
|
+
const config = ts46.readConfigFile(filename, ts46.sys.readFile);
|
|
15749
15866
|
if (config.error) {
|
|
15750
15867
|
return { diagnostics: [tsDiagnostic(config.error, sourceSpan)] };
|
|
15751
15868
|
}
|
|
15752
|
-
const parsed =
|
|
15869
|
+
const parsed = ts46.parseJsonConfigFileContent(config.config, ts46.sys, root, undefined, filename);
|
|
15753
15870
|
if (parsed.errors.length) {
|
|
15754
15871
|
return {
|
|
15755
15872
|
diagnostics: parsed.errors.map((diagnostic) => tsDiagnostic(diagnostic, sourceSpan))
|
|
@@ -15773,18 +15890,22 @@ function compileFrontend(options, dialect) {
|
|
|
15773
15890
|
skipLibCheck: false,
|
|
15774
15891
|
types: [],
|
|
15775
15892
|
lib: ["lib.es2025.d.ts"],
|
|
15776
|
-
module:
|
|
15777
|
-
moduleResolution:
|
|
15893
|
+
module: ts46.ModuleKind.ESNext,
|
|
15894
|
+
moduleResolution: ts46.ModuleResolutionKind.Bundler
|
|
15778
15895
|
};
|
|
15779
15896
|
delete compilerOptions.rootDir;
|
|
15780
15897
|
delete compilerOptions.outDir;
|
|
15898
|
+
const loweredOptions = {
|
|
15899
|
+
...compilerOptions,
|
|
15900
|
+
erasableSyntaxOnly: false
|
|
15901
|
+
};
|
|
15781
15902
|
const sdk = sdkFor(filename, compilerOptions);
|
|
15782
15903
|
const rootNames = [
|
|
15783
15904
|
...new Set([...projectFiles, ...programFiles, sdk, dialectFor(sdk)])
|
|
15784
15905
|
];
|
|
15785
15906
|
const virtualSources = new Map;
|
|
15786
15907
|
const host = loweringHost(compilerOptions, virtualSources);
|
|
15787
|
-
const program =
|
|
15908
|
+
const program = ts46.createProgram(rootNames, compilerOptions, host);
|
|
15788
15909
|
const plcFiles = (program) => {
|
|
15789
15910
|
const files = new Set(runtimeSources(program));
|
|
15790
15911
|
for (const source of program.getSourceFiles()) {
|
|
@@ -15841,9 +15962,9 @@ function compileFrontend(options, dialect) {
|
|
|
15841
15962
|
writeFileSync2(target, text.text);
|
|
15842
15963
|
}
|
|
15843
15964
|
}
|
|
15844
|
-
const lowered =
|
|
15965
|
+
const lowered = ts46.createProgram({
|
|
15845
15966
|
rootNames,
|
|
15846
|
-
options:
|
|
15967
|
+
options: loweredOptions,
|
|
15847
15968
|
host,
|
|
15848
15969
|
oldProgram: program
|
|
15849
15970
|
});
|
|
@@ -15859,6 +15980,7 @@ function compileFrontend(options, dialect) {
|
|
|
15859
15980
|
}));
|
|
15860
15981
|
return lowered;
|
|
15861
15982
|
});
|
|
15983
|
+
lowerings.run((program) => lowerConstEnums(program, positions));
|
|
15862
15984
|
const configuration = lowerings.run((program) => lowerEnvironment(program, sdk, positions, environment));
|
|
15863
15985
|
const elaboration = configuration && lowerings.run((program) => lowerElaboration(program, sdk, options.programs, root, positions, {
|
|
15864
15986
|
debug,
|
|
@@ -15887,13 +16009,13 @@ function storesGeneratedGlobal(program, diagnostic, generated) {
|
|
|
15887
16009
|
return false;
|
|
15888
16010
|
}
|
|
15889
16011
|
const target = reportedAccess(file, start, start + length);
|
|
15890
|
-
if (!target || !
|
|
16012
|
+
if (!target || !ts46.isIdentifier(target.expression)) {
|
|
15891
16013
|
return false;
|
|
15892
16014
|
}
|
|
15893
16015
|
const checker = program.getTypeChecker();
|
|
15894
|
-
const namespace = checker.getSymbolAtLocation(target.expression)?.declarations?.find(
|
|
16016
|
+
const namespace = checker.getSymbolAtLocation(target.expression)?.declarations?.find(ts46.isNamespaceImport);
|
|
15895
16017
|
const source = namespace && checker.getSymbolAtLocation(namespace.parent.parent.moduleSpecifier)?.valueDeclaration;
|
|
15896
|
-
return Boolean(source &&
|
|
16018
|
+
return Boolean(source && ts46.isSourceFile(source) && generated(source));
|
|
15897
16019
|
}
|
|
15898
16020
|
var missingExportCodes = new Set([
|
|
15899
16021
|
2305,
|
|
@@ -15903,11 +16025,11 @@ var missingExportCodes = new Set([
|
|
|
15903
16025
|
2724
|
|
15904
16026
|
]);
|
|
15905
16027
|
function importedFile(checker, statement) {
|
|
15906
|
-
if (!(
|
|
16028
|
+
if (!(ts46.isImportDeclaration(statement) || ts46.isExportDeclaration(statement)) || !statement.moduleSpecifier) {
|
|
15907
16029
|
return;
|
|
15908
16030
|
}
|
|
15909
16031
|
const module = checker.getSymbolAtLocation(statement.moduleSpecifier)?.valueDeclaration;
|
|
15910
|
-
return module &&
|
|
16032
|
+
return module && ts46.isSourceFile(module) ? module.fileName : undefined;
|
|
15911
16033
|
}
|
|
15912
16034
|
function lostImports({
|
|
15913
16035
|
before,
|
|
@@ -15950,12 +16072,12 @@ function lostImports({
|
|
|
15950
16072
|
var readOnlyAssignment = 2540;
|
|
15951
16073
|
function reportedAccess(file, start, end) {
|
|
15952
16074
|
const visit = (node) => {
|
|
15953
|
-
if (
|
|
16075
|
+
if (ts46.isPropertyAccessExpression(node) && node.name.getStart(file) === start && node.name.end === end) {
|
|
15954
16076
|
return node;
|
|
15955
16077
|
}
|
|
15956
|
-
return node.pos <= start && end <= node.end ?
|
|
16078
|
+
return node.pos <= start && end <= node.end ? ts46.forEachChild(node, visit) : undefined;
|
|
15957
16079
|
};
|
|
15958
|
-
return
|
|
16080
|
+
return ts46.forEachChild(file, visit);
|
|
15959
16081
|
}
|
|
15960
16082
|
|
|
15961
16083
|
class LoweringPipeline {
|
|
@@ -15978,7 +16100,7 @@ class LoweringPipeline {
|
|
|
15978
16100
|
}
|
|
15979
16101
|
}
|
|
15980
16102
|
function loweringHost(options, virtualSources) {
|
|
15981
|
-
const host =
|
|
16103
|
+
const host = ts46.createCompilerHost(options);
|
|
15982
16104
|
const read = host.readFile;
|
|
15983
16105
|
const exists = host.fileExists;
|
|
15984
16106
|
const directoryExists = host.directoryExists?.bind(host);
|
|
@@ -16054,17 +16176,17 @@ function attachLowerings(module, root, {
|
|
|
16054
16176
|
}
|
|
16055
16177
|
function checkHostConfig(file) {
|
|
16056
16178
|
const bunTypes = createRequire2(import.meta.url).resolve("@types/bun/index.d.ts");
|
|
16057
|
-
const program =
|
|
16179
|
+
const program = ts46.createProgram([file], {
|
|
16058
16180
|
strict: true,
|
|
16059
16181
|
noEmit: true,
|
|
16060
|
-
target:
|
|
16061
|
-
module:
|
|
16062
|
-
moduleResolution:
|
|
16182
|
+
target: ts46.ScriptTarget.ES2022,
|
|
16183
|
+
module: ts46.ModuleKind.ESNext,
|
|
16184
|
+
moduleResolution: ts46.ModuleResolutionKind.Bundler,
|
|
16063
16185
|
allowImportingTsExtensions: true,
|
|
16064
16186
|
skipLibCheck: true,
|
|
16065
16187
|
types: [bunTypes]
|
|
16066
16188
|
});
|
|
16067
|
-
return
|
|
16189
|
+
return ts46.getPreEmitDiagnostics(program).map((diagnostic) => tsDiagnostic(diagnostic, sourceSpan));
|
|
16068
16190
|
}
|
|
16069
16191
|
export {
|
|
16070
16192
|
analyze,
|
|
@@ -16072,5 +16194,5 @@ export {
|
|
|
16072
16194
|
typeCheckOnly
|
|
16073
16195
|
};
|
|
16074
16196
|
|
|
16075
|
-
//# debugId=
|
|
16197
|
+
//# debugId=42EAE8051E662CAC64756E2164756E21
|
|
16076
16198
|
//# sourceMappingURL=index.js.map
|