ts2workflows 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +20 -7
- package/dist/ast/stepnames.js +3 -3
- package/dist/ast/steps.d.ts +10 -7
- package/dist/ast/steps.d.ts.map +1 -1
- package/dist/ast/steps.js +16 -12
- package/dist/ast/workflows.d.ts +0 -5
- package/dist/ast/workflows.d.ts.map +1 -1
- package/dist/ast/workflows.js +1 -25
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +4 -0
- package/dist/transpiler/expressions.d.ts +4 -3
- package/dist/transpiler/expressions.d.ts.map +1 -1
- package/dist/transpiler/expressions.js +15 -7
- package/dist/transpiler/index.d.ts +5 -0
- package/dist/transpiler/index.d.ts.map +1 -1
- package/dist/transpiler/index.js +12 -1
- package/dist/transpiler/statements.d.ts.map +1 -1
- package/dist/transpiler/statements.js +235 -116
- package/dist/transpiler/transformations.js +14 -6
- package/dist/utils.d.ts +0 -7
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -10
- package/language_reference.md +45 -15
- package/package.json +11 -9
- package/types/workflowslib.d.ts +58 -74
- package/dist/ast/validation.d.ts +0 -20
- package/dist/ast/validation.d.ts.map +0 -1
- package/dist/ast/validation.js +0 -214
|
@@ -3,18 +3,15 @@ import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
|
|
|
3
3
|
import { AssignStepAST, CallStepAST, ForRangeStepAST, ForStepAST, JumpTargetAST, NextStepAST, ParallelStepAST, RaiseStepAST, ReturnStepAST, StepsStepAST, SwitchStepAST, TryStepAST, } from '../ast/steps.js';
|
|
4
4
|
import { BinaryExpression, FunctionInvocationExpression, MemberExpression, PrimitiveExpression, VariableReferenceExpression, asExpression, isExpression, isFullyQualifiedName, isLiteral, isPure, nullEx, safeAsExpression, trueEx, } from '../ast/expressions.js';
|
|
5
5
|
import { InternalTranspilingError, WorkflowSyntaxError } from '../errors.js';
|
|
6
|
-
import {
|
|
7
|
-
import { convertExpression, convertMemberExpression, convertObjectExpression, convertObjectAsExpressionValues,
|
|
6
|
+
import { isRecord } from '../utils.js';
|
|
7
|
+
import { convertExpression, convertMemberExpression, convertObjectExpression, convertObjectAsExpressionValues, isIntrinsic, throwIfSpread, isIntrinsicStatment as isIntrinsicStatement, convertVariableNameExpression, } from './expressions.js';
|
|
8
8
|
import { blockingFunctions } from './generated/functionMetadata.js';
|
|
9
9
|
export function parseStatement(node, ctx) {
|
|
10
|
-
return parseStatementRecursively(node, undefined, ctx);
|
|
11
|
-
}
|
|
12
|
-
function parseStatementRecursively(node, nextNode, ctx) {
|
|
13
10
|
switch (node.type) {
|
|
14
11
|
case AST_NODE_TYPES.BlockStatement:
|
|
15
|
-
return
|
|
12
|
+
return node.body.flatMap((node) => parseStatement(node, ctx));
|
|
16
13
|
case AST_NODE_TYPES.VariableDeclaration:
|
|
17
|
-
return convertVariableDeclarations(node
|
|
14
|
+
return convertVariableDeclarations(node, ctx);
|
|
18
15
|
case AST_NODE_TYPES.ExpressionStatement:
|
|
19
16
|
if (node.expression.type === AST_NODE_TYPES.AssignmentExpression) {
|
|
20
17
|
return assignmentExpressionToSteps(node.expression, ctx);
|
|
@@ -45,14 +42,8 @@ function parseStatementRecursively(node, nextNode, ctx) {
|
|
|
45
42
|
return [breakStatementToNextStep(node, ctx)];
|
|
46
43
|
case AST_NODE_TYPES.ContinueStatement:
|
|
47
44
|
return [continueStatementToNextStep(node, ctx)];
|
|
48
|
-
case AST_NODE_TYPES.TryStatement:
|
|
49
|
-
|
|
50
|
-
if (nextNode?.type === AST_NODE_TYPES.ExpressionStatement &&
|
|
51
|
-
nextNode.expression.type === AST_NODE_TYPES.CallExpression) {
|
|
52
|
-
retryPolicy = parseRetryPolicy(nextNode.expression);
|
|
53
|
-
}
|
|
54
|
-
return tryStatementToTrySteps(node, retryPolicy, ctx);
|
|
55
|
-
}
|
|
45
|
+
case AST_NODE_TYPES.TryStatement:
|
|
46
|
+
return tryStatementToTrySteps(node, ctx);
|
|
56
47
|
case AST_NODE_TYPES.LabeledStatement:
|
|
57
48
|
return labeledStep(node, ctx);
|
|
58
49
|
case AST_NODE_TYPES.EmptyStatement:
|
|
@@ -68,8 +59,11 @@ function parseStatementRecursively(node, nextNode, ctx) {
|
|
|
68
59
|
throw new WorkflowSyntaxError(`TODO: encountered unsupported type: ${node.type}`, node.loc);
|
|
69
60
|
}
|
|
70
61
|
}
|
|
71
|
-
function convertVariableDeclarations(
|
|
72
|
-
|
|
62
|
+
function convertVariableDeclarations(node, ctx) {
|
|
63
|
+
if (node.kind !== 'const' && node.kind !== 'let') {
|
|
64
|
+
throw new WorkflowSyntaxError('Only const and let variable declarations are supported', node.loc);
|
|
65
|
+
}
|
|
66
|
+
return node.declarations.flatMap((decl) => {
|
|
73
67
|
if (decl.id.type === AST_NODE_TYPES.Identifier) {
|
|
74
68
|
return convertInitializer(decl.id.name, decl.init, ctx);
|
|
75
69
|
}
|
|
@@ -89,14 +83,15 @@ function convertInitializer(targetVariableName, initializer, ctx) {
|
|
|
89
83
|
const calleeName = initializer.callee.type === AST_NODE_TYPES.Identifier
|
|
90
84
|
? initializer.callee.name
|
|
91
85
|
: undefined;
|
|
92
|
-
if (calleeName &&
|
|
86
|
+
if (calleeName && isIntrinsicStatement(calleeName)) {
|
|
93
87
|
throw new WorkflowSyntaxError(`"${calleeName}" can't be called as part of an expression`, initializer.callee.loc);
|
|
94
88
|
}
|
|
95
89
|
return callExpressionToStep(initializer, targetVariableName, ctx);
|
|
96
90
|
}
|
|
97
91
|
else {
|
|
92
|
+
const name = new VariableReferenceExpression(targetVariableName);
|
|
98
93
|
const value = initializer === null ? nullEx : convertExpression(initializer);
|
|
99
|
-
return [new AssignStepAST([
|
|
94
|
+
return [new AssignStepAST([{ name, value }])];
|
|
100
95
|
}
|
|
101
96
|
}
|
|
102
97
|
function convertArrayDestructuring(arrayPattern, initializer, ctx) {
|
|
@@ -124,12 +119,12 @@ function arrayDestructuringSteps(patterns, initializerExpression, ctx) {
|
|
|
124
119
|
if (patterns.filter((p) => p !== null).length === 0) {
|
|
125
120
|
return [];
|
|
126
121
|
}
|
|
127
|
-
const __temp_len = `${tempName(ctx)}_len
|
|
122
|
+
const __temp_len = new VariableReferenceExpression(`${tempName(ctx)}_len`);
|
|
128
123
|
const initializeVariables = [
|
|
129
|
-
|
|
130
|
-
__temp_len,
|
|
131
|
-
new FunctionInvocationExpression('len', [initializerExpression]),
|
|
132
|
-
|
|
124
|
+
{
|
|
125
|
+
name: __temp_len,
|
|
126
|
+
value: new FunctionInvocationExpression('len', [initializerExpression]),
|
|
127
|
+
},
|
|
133
128
|
];
|
|
134
129
|
const branches = R.reverse(patterns).flatMap((pat, i) => {
|
|
135
130
|
if (pat === null) {
|
|
@@ -138,7 +133,7 @@ function arrayDestructuringSteps(patterns, initializerExpression, ctx) {
|
|
|
138
133
|
else {
|
|
139
134
|
return [
|
|
140
135
|
{
|
|
141
|
-
condition: new BinaryExpression(
|
|
136
|
+
condition: new BinaryExpression(__temp_len, '>=', new PrimitiveExpression(patterns.length - i)),
|
|
142
137
|
steps: arrayElementsDestructuringSteps(patterns, initializerExpression, patterns.length - i, ctx),
|
|
143
138
|
},
|
|
144
139
|
];
|
|
@@ -161,14 +156,15 @@ function arrayElementsDestructuringSteps(patterns, initializerExpression, take,
|
|
|
161
156
|
switch (pat?.type) {
|
|
162
157
|
case AST_NODE_TYPES.MemberExpression:
|
|
163
158
|
case AST_NODE_TYPES.Identifier: {
|
|
164
|
-
const name =
|
|
165
|
-
return [new AssignStepAST([
|
|
159
|
+
const name = convertVariableNameExpression(pat);
|
|
160
|
+
return [new AssignStepAST([{ name, value: iElement }])];
|
|
166
161
|
}
|
|
167
162
|
case AST_NODE_TYPES.AssignmentPattern: {
|
|
168
163
|
if (pat.left.type !== AST_NODE_TYPES.Identifier) {
|
|
169
164
|
throw new WorkflowSyntaxError('Default value can be used only with an identifier', pat.left.loc);
|
|
170
165
|
}
|
|
171
|
-
|
|
166
|
+
const name = new VariableReferenceExpression(pat.left.name);
|
|
167
|
+
return [new AssignStepAST([{ name, value: iElement }])];
|
|
172
168
|
}
|
|
173
169
|
case AST_NODE_TYPES.ObjectPattern:
|
|
174
170
|
return objectDestructuringSteps(pat.properties, iElement, ctx);
|
|
@@ -192,11 +188,18 @@ function extractDefaultAssignmentsFromDestructuringPattern(pat) {
|
|
|
192
188
|
if (pat.left.type !== AST_NODE_TYPES.Identifier) {
|
|
193
189
|
throw new WorkflowSyntaxError('Default value can be used only with an identifier', pat.left.loc);
|
|
194
190
|
}
|
|
195
|
-
return [
|
|
191
|
+
return [
|
|
192
|
+
{
|
|
193
|
+
name: new VariableReferenceExpression(pat.left.name),
|
|
194
|
+
value: convertExpression(pat.right),
|
|
195
|
+
},
|
|
196
|
+
];
|
|
196
197
|
case AST_NODE_TYPES.Identifier:
|
|
197
|
-
return [
|
|
198
|
+
return [
|
|
199
|
+
{ name: new VariableReferenceExpression(pat.name), value: nullEx },
|
|
200
|
+
];
|
|
198
201
|
case AST_NODE_TYPES.MemberExpression:
|
|
199
|
-
return [
|
|
202
|
+
return [{ name: convertVariableNameExpression(pat), value: nullEx }];
|
|
200
203
|
case AST_NODE_TYPES.ObjectPattern:
|
|
201
204
|
return pat.properties.flatMap((p) => {
|
|
202
205
|
if (p.type === AST_NODE_TYPES.RestElement) {
|
|
@@ -217,7 +220,12 @@ function extractDefaultAssignmentsFromDestructuringPattern(pat) {
|
|
|
217
220
|
if (pat.argument.type !== AST_NODE_TYPES.Identifier) {
|
|
218
221
|
throw new WorkflowSyntaxError('Identifier expected', pat.argument.loc);
|
|
219
222
|
}
|
|
220
|
-
return [
|
|
223
|
+
return [
|
|
224
|
+
{
|
|
225
|
+
name: new VariableReferenceExpression(pat.argument.name),
|
|
226
|
+
value: new PrimitiveExpression([]),
|
|
227
|
+
},
|
|
228
|
+
];
|
|
221
229
|
default:
|
|
222
230
|
return [];
|
|
223
231
|
}
|
|
@@ -233,23 +241,23 @@ function arrayRestDestructuringSteps(patterns, rest, initializerExpression, star
|
|
|
233
241
|
if (rest.argument.type !== AST_NODE_TYPES.Identifier) {
|
|
234
242
|
throw new WorkflowSyntaxError('Identifier expected', rest.argument.loc);
|
|
235
243
|
}
|
|
236
|
-
const restName = rest.argument.name;
|
|
244
|
+
const restName = new VariableReferenceExpression(rest.argument.name);
|
|
237
245
|
const __temp_len = new VariableReferenceExpression(`${tempName(ctx)}_len`);
|
|
238
246
|
const __temp_index = `${tempName(ctx)}_index`;
|
|
239
247
|
const one = new PrimitiveExpression(1);
|
|
240
248
|
const emptyArray = new PrimitiveExpression([]);
|
|
241
249
|
const copyLoop = new ForRangeStepAST([
|
|
242
250
|
new AssignStepAST([
|
|
243
|
-
|
|
244
|
-
restName,
|
|
245
|
-
new FunctionInvocationExpression('list.concat', [
|
|
246
|
-
|
|
251
|
+
{
|
|
252
|
+
name: restName,
|
|
253
|
+
value: new FunctionInvocationExpression('list.concat', [
|
|
254
|
+
restName,
|
|
247
255
|
new MemberExpression(initializerExpression, new VariableReferenceExpression(__temp_index), true),
|
|
248
256
|
]),
|
|
249
|
-
|
|
257
|
+
},
|
|
250
258
|
]),
|
|
251
259
|
], __temp_index, startIndex, new BinaryExpression(__temp_len, '-', one));
|
|
252
|
-
return [new AssignStepAST([
|
|
260
|
+
return [new AssignStepAST([{ name: restName, value: emptyArray }]), copyLoop];
|
|
253
261
|
}
|
|
254
262
|
function convertObjectDestructuring(objectPattern, initializer, ctx) {
|
|
255
263
|
let initExpression;
|
|
@@ -289,7 +297,14 @@ function objectDestructuringSteps(properties, initializerExpression, ctx) {
|
|
|
289
297
|
initializerExpression,
|
|
290
298
|
new PrimitiveExpression(prop.key.name),
|
|
291
299
|
]);
|
|
292
|
-
return [
|
|
300
|
+
return [
|
|
301
|
+
new AssignStepAST([
|
|
302
|
+
{
|
|
303
|
+
name: new VariableReferenceExpression(prop.value.name),
|
|
304
|
+
value: safeKeyExpression,
|
|
305
|
+
},
|
|
306
|
+
]),
|
|
307
|
+
];
|
|
293
308
|
}
|
|
294
309
|
else if (prop.value.type === AST_NODE_TYPES.AssignmentPattern) {
|
|
295
310
|
return objectAssignmentPatternSteps(prop.value, initializerExpression, keyExpression);
|
|
@@ -305,16 +320,17 @@ function objectAssignmentPatternSteps(pat, initializerExpression, keyExpression)
|
|
|
305
320
|
}
|
|
306
321
|
// Using Switch step instead of default() because pat.right must be evaluated only
|
|
307
322
|
// in the default value branch (in case it has side effects)
|
|
323
|
+
const name = new VariableReferenceExpression(pat.left.name);
|
|
308
324
|
return [
|
|
309
325
|
new SwitchStepAST([
|
|
310
326
|
{
|
|
311
327
|
condition: new BinaryExpression(new PrimitiveExpression(pat.left.name), 'in', initializerExpression),
|
|
312
|
-
steps: [new AssignStepAST([
|
|
328
|
+
steps: [new AssignStepAST([{ name, value: keyExpression }])],
|
|
313
329
|
},
|
|
314
330
|
{
|
|
315
331
|
condition: trueEx,
|
|
316
332
|
steps: [
|
|
317
|
-
new AssignStepAST([
|
|
333
|
+
new AssignStepAST([{ name, value: convertExpression(pat.right) }]),
|
|
318
334
|
],
|
|
319
335
|
},
|
|
320
336
|
]),
|
|
@@ -335,13 +351,14 @@ function objectDestructuringRestSteps(properties, rest, initializerExpression) {
|
|
|
335
351
|
return p;
|
|
336
352
|
})
|
|
337
353
|
.map((p) => p.name);
|
|
354
|
+
const name = new VariableReferenceExpression(rest.argument.name);
|
|
338
355
|
const value = nonRestKeys.reduce((acc, propertyName) =>
|
|
339
356
|
// map.delete returns a copy of the object and removes the specified property
|
|
340
357
|
new FunctionInvocationExpression('map.delete', [
|
|
341
358
|
acc,
|
|
342
359
|
new PrimitiveExpression(propertyName),
|
|
343
360
|
]), initializerExpression);
|
|
344
|
-
return [new AssignStepAST([
|
|
361
|
+
return [new AssignStepAST([{ name, value }])];
|
|
345
362
|
}
|
|
346
363
|
function assignmentExpressionToSteps(node, ctx) {
|
|
347
364
|
let compoundOperator = undefined;
|
|
@@ -373,54 +390,132 @@ function assignmentExpressionToSteps(node, ctx) {
|
|
|
373
390
|
default:
|
|
374
391
|
throw new WorkflowSyntaxError(`Operator ${node.operator} is not supported in assignment expressions`, node.loc);
|
|
375
392
|
}
|
|
376
|
-
if (
|
|
377
|
-
|
|
378
|
-
return convertArrayDestructuring(node.left, node.right, ctx);
|
|
379
|
-
}
|
|
380
|
-
else {
|
|
381
|
-
throw new WorkflowSyntaxError(`Invalid left-hand side in assignment`, node.left.loc);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
else if (node.left.type === AST_NODE_TYPES.ObjectPattern) {
|
|
385
|
-
if (node.operator === '=') {
|
|
386
|
-
return convertObjectDestructuring(node.left, node.right, ctx);
|
|
387
|
-
}
|
|
388
|
-
else {
|
|
389
|
-
throw new WorkflowSyntaxError(`Invalid left-hand side in assignment`, node.left.loc);
|
|
390
|
-
}
|
|
393
|
+
if (compoundOperator === undefined) {
|
|
394
|
+
return assignmentSteps(node.left, node.right, ctx);
|
|
391
395
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
throw new WorkflowSyntaxError('The left-hand side of an assignment must be an identifier or a property access', node.loc);
|
|
396
|
+
else {
|
|
397
|
+
return compoundAssignmentSteps(node.left, node.right, compoundOperator, ctx);
|
|
395
398
|
}
|
|
399
|
+
}
|
|
400
|
+
function assignmentSteps(left, right, ctx) {
|
|
396
401
|
let valueExpression;
|
|
397
402
|
const steps = [];
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
if (!needsTempVariable) {
|
|
411
|
-
return steps;
|
|
412
|
-
}
|
|
413
|
-
valueExpression = new VariableReferenceExpression(tempName(ctx));
|
|
403
|
+
if (left.type === AST_NODE_TYPES.ArrayPattern) {
|
|
404
|
+
return convertArrayDestructuring(left, right, ctx);
|
|
405
|
+
}
|
|
406
|
+
else if (left.type === AST_NODE_TYPES.ObjectPattern) {
|
|
407
|
+
return convertObjectDestructuring(left, right, ctx);
|
|
408
|
+
}
|
|
409
|
+
if (right.type === AST_NODE_TYPES.CallExpression &&
|
|
410
|
+
right.callee.type === AST_NODE_TYPES.Identifier &&
|
|
411
|
+
isIntrinsic(right.callee.name)) {
|
|
412
|
+
const tr = convertAssignmentExpressionIntrinsicRHS(right, ctx);
|
|
413
|
+
steps.push(...tr.steps);
|
|
414
|
+
valueExpression = tr.tempVariable;
|
|
414
415
|
}
|
|
415
416
|
else {
|
|
416
|
-
valueExpression = convertExpression(
|
|
417
|
+
valueExpression = convertExpression(right);
|
|
418
|
+
}
|
|
419
|
+
const targetExpression = convertVariableNameExpression(left);
|
|
420
|
+
steps.push(new AssignStepAST([{ name: targetExpression, value: valueExpression }]));
|
|
421
|
+
return steps;
|
|
422
|
+
}
|
|
423
|
+
function compoundAssignmentSteps(left, right, operator, ctx) {
|
|
424
|
+
let valueExpression;
|
|
425
|
+
const { expression: targetExpression, steps } = convertCompoundAssignmentLeftHandSide(left, ctx);
|
|
426
|
+
if (right.type === AST_NODE_TYPES.CallExpression &&
|
|
427
|
+
right.callee.type === AST_NODE_TYPES.Identifier &&
|
|
428
|
+
isIntrinsic(right.callee.name)) {
|
|
429
|
+
const tr = convertAssignmentExpressionIntrinsicRHS(right, ctx);
|
|
430
|
+
steps.push(...tr.steps);
|
|
431
|
+
valueExpression = tr.tempVariable;
|
|
417
432
|
}
|
|
418
|
-
|
|
419
|
-
valueExpression =
|
|
433
|
+
else {
|
|
434
|
+
valueExpression = convertExpression(right);
|
|
420
435
|
}
|
|
421
|
-
|
|
436
|
+
valueExpression = new BinaryExpression(targetExpression, operator, valueExpression);
|
|
437
|
+
steps.push(new AssignStepAST([{ name: targetExpression, value: valueExpression }]));
|
|
422
438
|
return steps;
|
|
423
439
|
}
|
|
440
|
+
function convertCompoundAssignmentLeftHandSide(left, ctx) {
|
|
441
|
+
if (left.type === AST_NODE_TYPES.ArrayPattern ||
|
|
442
|
+
left.type === AST_NODE_TYPES.ObjectPattern) {
|
|
443
|
+
throw new WorkflowSyntaxError(`Invalid left-hand side in assignment`, left.loc);
|
|
444
|
+
}
|
|
445
|
+
const leftEx = convertVariableNameExpression(left);
|
|
446
|
+
if (leftEx.expressionType === 'member') {
|
|
447
|
+
const { transformed, assignments } = extractSideEffectsFromMemberExpression(leftEx, tempName(ctx), 0);
|
|
448
|
+
const steps = [new AssignStepAST(assignments)];
|
|
449
|
+
return { expression: transformed, steps };
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
return {
|
|
453
|
+
expression: leftEx,
|
|
454
|
+
steps: [],
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Extract side-effecting computed properties into temporary variable assignments.
|
|
460
|
+
*
|
|
461
|
+
* This is used on the left-hand side of a compound assignment expression, which
|
|
462
|
+
* should only be evaluted once.
|
|
463
|
+
*/
|
|
464
|
+
function extractSideEffectsFromMemberExpression(ex, tempPrefix, tempIndex) {
|
|
465
|
+
if (ex.computed && !isPure(ex.property)) {
|
|
466
|
+
let transformedObject;
|
|
467
|
+
let objectAssignments;
|
|
468
|
+
if (ex.object.expressionType === 'member') {
|
|
469
|
+
const object2 = extractSideEffectsFromMemberExpression(ex.object, tempPrefix, tempIndex + 1);
|
|
470
|
+
transformedObject = object2.transformed;
|
|
471
|
+
objectAssignments = object2.assignments;
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
transformedObject = ex.object;
|
|
475
|
+
objectAssignments = [];
|
|
476
|
+
}
|
|
477
|
+
const tmp = new VariableReferenceExpression(`${tempPrefix}${tempIndex}`);
|
|
478
|
+
const transformed = new MemberExpression(transformedObject, tmp, true);
|
|
479
|
+
const assignments = objectAssignments;
|
|
480
|
+
assignments.push({
|
|
481
|
+
name: tmp,
|
|
482
|
+
value: ex.property,
|
|
483
|
+
});
|
|
484
|
+
return { transformed, assignments };
|
|
485
|
+
}
|
|
486
|
+
else if (ex.object.expressionType === 'member') {
|
|
487
|
+
const { transformed: object2, assignments: assignments } = extractSideEffectsFromMemberExpression(ex.object, tempPrefix, tempIndex);
|
|
488
|
+
const transformed = new MemberExpression(object2, ex.property, ex.computed);
|
|
489
|
+
return {
|
|
490
|
+
transformed,
|
|
491
|
+
assignments,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
return {
|
|
496
|
+
transformed: ex,
|
|
497
|
+
assignments: [],
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Special case for handling call_step() RHS in assignment expressions.
|
|
503
|
+
*
|
|
504
|
+
* This can be removed once the generic convertExpression() is able to handle call_step.
|
|
505
|
+
*/
|
|
506
|
+
function convertAssignmentExpressionIntrinsicRHS(callEx, ctx) {
|
|
507
|
+
if (callEx.callee.type !== AST_NODE_TYPES.Identifier) {
|
|
508
|
+
throw new InternalTranspilingError('The callee should be an identifier');
|
|
509
|
+
}
|
|
510
|
+
const calleeName = callEx.callee.name;
|
|
511
|
+
if (isIntrinsicStatement(calleeName)) {
|
|
512
|
+
throw new WorkflowSyntaxError(`"${calleeName}" can't be called as part of an expression`, callEx.callee.loc);
|
|
513
|
+
}
|
|
514
|
+
const resultVariable = tempName(ctx);
|
|
515
|
+
const steps = callExpressionToStep(callEx, resultVariable, ctx);
|
|
516
|
+
const tempVariable = new VariableReferenceExpression(resultVariable);
|
|
517
|
+
return { steps, tempVariable };
|
|
518
|
+
}
|
|
424
519
|
function callExpressionToStep(node, resultVariable, ctx) {
|
|
425
520
|
const calleeExpression = convertExpression(node.callee);
|
|
426
521
|
if (isFullyQualifiedName(calleeExpression)) {
|
|
@@ -456,10 +551,10 @@ function callExpressionToStep(node, resultVariable, ctx) {
|
|
|
456
551
|
function callExpressionAssignStep(functionName, argumentsNode, resultVariable) {
|
|
457
552
|
const argumentExpressions = throwIfSpread(argumentsNode).map(convertExpression);
|
|
458
553
|
return new AssignStepAST([
|
|
459
|
-
|
|
460
|
-
resultVariable,
|
|
461
|
-
new FunctionInvocationExpression(functionName, argumentExpressions),
|
|
462
|
-
|
|
554
|
+
{
|
|
555
|
+
name: new VariableReferenceExpression(resultVariable),
|
|
556
|
+
value: new FunctionInvocationExpression(functionName, argumentExpressions),
|
|
557
|
+
},
|
|
463
558
|
]);
|
|
464
559
|
}
|
|
465
560
|
function createCallStep(node, argumentsNode, resultVariable) {
|
|
@@ -600,7 +695,12 @@ function parseParallelOptions(node) {
|
|
|
600
695
|
};
|
|
601
696
|
}
|
|
602
697
|
function generalExpressionToAssignStep(node, ctx) {
|
|
603
|
-
return new AssignStepAST([
|
|
698
|
+
return new AssignStepAST([
|
|
699
|
+
{
|
|
700
|
+
name: new VariableReferenceExpression(tempName(ctx)),
|
|
701
|
+
value: convertExpression(node),
|
|
702
|
+
},
|
|
703
|
+
]);
|
|
604
704
|
}
|
|
605
705
|
function returnStatementToReturnStep(node, ctx) {
|
|
606
706
|
const value = node.argument ? convertExpression(node.argument) : undefined;
|
|
@@ -779,7 +879,8 @@ function continueStatementToNextStep(node, ctx) {
|
|
|
779
879
|
}
|
|
780
880
|
return new NextStepAST(target);
|
|
781
881
|
}
|
|
782
|
-
function tryStatementToTrySteps(node,
|
|
882
|
+
function tryStatementToTrySteps(node, ctx) {
|
|
883
|
+
const retryPolicy = extractRetryPolicy(node.block);
|
|
783
884
|
if (!node.finalizer) {
|
|
784
885
|
// Basic try-catch without a finally block
|
|
785
886
|
const baseTryStep = parseTryCatchRetry(node, ctx, retryPolicy);
|
|
@@ -852,8 +953,14 @@ function extractErrorVariableName(param) {
|
|
|
852
953
|
*/
|
|
853
954
|
function finalizerInitializer(conditionVariable, valueVariable) {
|
|
854
955
|
return new AssignStepAST([
|
|
855
|
-
|
|
856
|
-
|
|
956
|
+
{
|
|
957
|
+
name: new VariableReferenceExpression(conditionVariable),
|
|
958
|
+
value: nullEx,
|
|
959
|
+
},
|
|
960
|
+
{
|
|
961
|
+
name: new VariableReferenceExpression(valueVariable),
|
|
962
|
+
value: nullEx,
|
|
963
|
+
},
|
|
857
964
|
]);
|
|
858
965
|
}
|
|
859
966
|
/**
|
|
@@ -887,11 +994,14 @@ function finalizerFooter(conditionVariable, valueVariable) {
|
|
|
887
994
|
function finalizerDelayedException(exceptionVariableName, conditionVariableName, valueVariableName) {
|
|
888
995
|
return [
|
|
889
996
|
new AssignStepAST([
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
997
|
+
{
|
|
998
|
+
name: new VariableReferenceExpression(conditionVariableName),
|
|
999
|
+
value: new PrimitiveExpression('raise'),
|
|
1000
|
+
},
|
|
1001
|
+
{
|
|
1002
|
+
name: new VariableReferenceExpression(valueVariableName),
|
|
1003
|
+
value: new VariableReferenceExpression(exceptionVariableName),
|
|
1004
|
+
},
|
|
895
1005
|
]),
|
|
896
1006
|
];
|
|
897
1007
|
}
|
|
@@ -901,8 +1011,14 @@ function delayedReturnAndJumpToFinalizer(value, ctx) {
|
|
|
901
1011
|
: undefined;
|
|
902
1012
|
const [conditionVariable, valueVariable] = finalizerVariables(ctx);
|
|
903
1013
|
return new AssignStepAST([
|
|
904
|
-
|
|
905
|
-
|
|
1014
|
+
{
|
|
1015
|
+
name: new VariableReferenceExpression(conditionVariable),
|
|
1016
|
+
value: new PrimitiveExpression('return'),
|
|
1017
|
+
},
|
|
1018
|
+
{
|
|
1019
|
+
name: new VariableReferenceExpression(valueVariable),
|
|
1020
|
+
value: value ?? nullEx,
|
|
1021
|
+
},
|
|
906
1022
|
], finalizerTarget);
|
|
907
1023
|
}
|
|
908
1024
|
function labeledStep(node, ctx) {
|
|
@@ -912,27 +1028,30 @@ function labeledStep(node, ctx) {
|
|
|
912
1028
|
}
|
|
913
1029
|
return steps;
|
|
914
1030
|
}
|
|
915
|
-
function
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1031
|
+
function extractRetryPolicy(tryBlock) {
|
|
1032
|
+
// Find and parse the first retry_policy() in tryBlock
|
|
1033
|
+
for (const statement of tryBlock.body) {
|
|
1034
|
+
if (statement.type === AST_NODE_TYPES.ExpressionStatement &&
|
|
1035
|
+
statement.expression.type === AST_NODE_TYPES.CallExpression &&
|
|
1036
|
+
statement.expression.callee.type === AST_NODE_TYPES.Identifier &&
|
|
1037
|
+
statement.expression.callee.name === 'retry_policy') {
|
|
1038
|
+
if (statement.expression.arguments.length < 1) {
|
|
1039
|
+
throw new WorkflowSyntaxError('Required argument missing', statement.expression.loc);
|
|
1040
|
+
}
|
|
1041
|
+
const arg0 = throwIfSpread(statement.expression.arguments).map(convertExpression)[0];
|
|
1042
|
+
const argsLoc = statement.expression.arguments[0].loc;
|
|
1043
|
+
if (isFullyQualifiedName(arg0)) {
|
|
1044
|
+
return arg0.toString();
|
|
1045
|
+
}
|
|
1046
|
+
else if (arg0.expressionType === 'primitive' && isRecord(arg0.value)) {
|
|
1047
|
+
return retryPolicyFromParams(arg0.value, argsLoc);
|
|
1048
|
+
}
|
|
1049
|
+
else {
|
|
1050
|
+
throw new WorkflowSyntaxError('Unexpected type', argsLoc);
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
935
1053
|
}
|
|
1054
|
+
return undefined;
|
|
936
1055
|
}
|
|
937
1056
|
function retryPolicyFromParams(paramsObject, argsLoc) {
|
|
938
1057
|
const params = R.map(asExpression, paramsObject);
|
|
@@ -179,10 +179,16 @@ const transformStepExpressions = R.curry(function (transform, step) {
|
|
|
179
179
|
function transformExpressionsAssign(transform, step) {
|
|
180
180
|
if (step.assignments) {
|
|
181
181
|
const newSteps = [];
|
|
182
|
-
const newAssignments = step.assignments.map((
|
|
183
|
-
const [steps2,
|
|
182
|
+
const newAssignments = step.assignments.map(({ name, value }) => {
|
|
183
|
+
const [steps2, transformedKey] = transform(name);
|
|
184
|
+
const [steps3, transformedValue] = transform(value);
|
|
184
185
|
newSteps.push(...steps2);
|
|
185
|
-
|
|
186
|
+
newSteps.push(...steps3);
|
|
187
|
+
if (transformedKey.expressionType !== 'variableReference' &&
|
|
188
|
+
transformedKey.expressionType !== 'member') {
|
|
189
|
+
throw new InternalTranspilingError('Unexpected key type when transforming assign step');
|
|
190
|
+
}
|
|
191
|
+
return { name: transformedKey, value: transformedValue };
|
|
186
192
|
});
|
|
187
193
|
newSteps.push(new AssignStepAST(newAssignments, step.next, step.label));
|
|
188
194
|
return newSteps;
|
|
@@ -407,9 +413,11 @@ function extractMapsInMap(primitiveEx, generateName, nestingLevel) {
|
|
|
407
413
|
newValue = properties;
|
|
408
414
|
}
|
|
409
415
|
else {
|
|
410
|
-
|
|
411
|
-
tempVariables.push(
|
|
412
|
-
|
|
416
|
+
newValue = new VariableReferenceExpression(generateName());
|
|
417
|
+
tempVariables.push({
|
|
418
|
+
name: newValue,
|
|
419
|
+
value: new PrimitiveExpression(properties),
|
|
420
|
+
});
|
|
413
421
|
}
|
|
414
422
|
return {
|
|
415
423
|
transformed: newValue,
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,9 +1,2 @@
|
|
|
1
1
|
export declare function isRecord(object: unknown): object is Record<keyof never, unknown>;
|
|
2
|
-
/**
|
|
3
|
-
* Like arr.flatMap() but the callback takes two consecutive array elements.
|
|
4
|
-
*
|
|
5
|
-
* During the last execution of the callback, the second argument (which would
|
|
6
|
-
* be element after the last array element) will be undefined.
|
|
7
|
-
*/
|
|
8
|
-
export declare function chainPairs<T, U>(callback: (val: T, next: T | undefined) => U[], arr: readonly T[]): U[];
|
|
9
2
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,QAAQ,CACtB,MAAM,EAAE,OAAO,GACd,MAAM,IAAI,MAAM,CAAC,MAAM,KAAK,EAAE,OAAO,CAAC,CAExC"}
|
package/dist/utils.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
import * as R from 'ramda';
|
|
2
1
|
export function isRecord(object) {
|
|
3
2
|
return object instanceof Object && object.constructor === Object;
|
|
4
3
|
}
|
|
5
|
-
/**
|
|
6
|
-
* Like arr.flatMap() but the callback takes two consecutive array elements.
|
|
7
|
-
*
|
|
8
|
-
* During the last execution of the callback, the second argument (which would
|
|
9
|
-
* be element after the last array element) will be undefined.
|
|
10
|
-
*/
|
|
11
|
-
export function chainPairs(callback, arr) {
|
|
12
|
-
return R.chain(R.apply(callback), R.zip(arr, R.append(undefined, R.tail(arr))));
|
|
13
|
-
}
|