@output.ai/core 0.1.9-dev.pr156.0 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -228
- package/package.json +5 -2
- package/src/tracing/processors/local/index.js +52 -14
- package/src/tracing/processors/local/index.spec.js +73 -5
- package/src/tracing/trace_engine.js +9 -1
- package/src/worker/webpack_loaders/tools.js +117 -1
- package/src/worker/webpack_loaders/workflow_rewriter/collect_target_imports.spec.js +20 -26
- package/src/worker/webpack_loaders/workflow_rewriter/index.spec.js +73 -88
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.js +157 -33
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.spec.js +91 -15
|
@@ -6,11 +6,14 @@ import {
|
|
|
6
6
|
callExpression,
|
|
7
7
|
functionExpression,
|
|
8
8
|
identifier,
|
|
9
|
+
isArrowFunctionExpression,
|
|
9
10
|
isAssignmentPattern,
|
|
10
11
|
isBlockStatement,
|
|
11
12
|
isCallExpression,
|
|
12
13
|
isExportNamedDeclaration,
|
|
14
|
+
isFunctionExpression,
|
|
13
15
|
isIdentifier,
|
|
16
|
+
isVariableDeclarator,
|
|
14
17
|
isStringLiteral,
|
|
15
18
|
isVariableDeclaration,
|
|
16
19
|
isObjectExpression,
|
|
@@ -18,7 +21,8 @@ import {
|
|
|
18
21
|
returnStatement,
|
|
19
22
|
stringLiteral,
|
|
20
23
|
thisExpression,
|
|
21
|
-
isExportDefaultDeclaration
|
|
24
|
+
isExportDefaultDeclaration,
|
|
25
|
+
isFunctionDeclaration
|
|
22
26
|
} from '@babel/types';
|
|
23
27
|
import { ComponentFile, EXTRANEOUS_FILE, ExtraneousFileList, NodeType } from './consts.js';
|
|
24
28
|
|
|
@@ -170,6 +174,28 @@ export const getFileKind = path => {
|
|
|
170
174
|
export const createThisMethodCall = ( method, literalName, args ) =>
|
|
171
175
|
callExpression( memberExpression( thisExpression(), identifier( method ) ), [ stringLiteral( literalName ), ...args ] );
|
|
172
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Build a CallExpression that binds `this` at the call site:
|
|
179
|
+
* fn(arg1, arg2) -> fn.call(this, arg1, arg2)
|
|
180
|
+
*
|
|
181
|
+
* When to use:
|
|
182
|
+
* - Inside workflow `fn` rewriting, local call-chain functions must receive the dynamic `this`
|
|
183
|
+
* so that emitted `this.invokeStep(...)` and similar calls inside them operate correctly.
|
|
184
|
+
*
|
|
185
|
+
* Example:
|
|
186
|
+
* // Input AST intent:
|
|
187
|
+
* foo(a, b);
|
|
188
|
+
*
|
|
189
|
+
* // Rewritten AST:
|
|
190
|
+
* foo.call(this, a, b);
|
|
191
|
+
*
|
|
192
|
+
* @param {string} calleeName - Identifier name of the function being called (e.g., 'foo').
|
|
193
|
+
* @param {import('@babel/types').Expression[]} args - Original call arguments.
|
|
194
|
+
* @returns {import('@babel/types').CallExpression} CallExpression node representing `callee.call(this, ...args)`.
|
|
195
|
+
*/
|
|
196
|
+
export const bindThisAtCallSite = ( calleeName, args ) =>
|
|
197
|
+
callExpression( memberExpression( identifier( calleeName ), identifier( 'call' ) ), [ thisExpression(), ...args ] );
|
|
198
|
+
|
|
173
199
|
/**
|
|
174
200
|
* Resolve an options object's name property to a string.
|
|
175
201
|
* Accepts literal strings or top-level const string identifiers.
|
|
@@ -332,3 +358,93 @@ export const buildWorkflowNameMap = ( path, cache ) => {
|
|
|
332
358
|
cache.set( path, result );
|
|
333
359
|
return result;
|
|
334
360
|
};
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Determine whether a node represents a function body usable as a workflow `fn`.
|
|
364
|
+
*
|
|
365
|
+
* Why this matters:
|
|
366
|
+
* - Workflow `fn` needs a dynamic `this` so the rewriter can emit calls like `this.invokeStep(...)`.
|
|
367
|
+
* - Arrow functions do not have their own `this`; they capture `this` lexically, which breaks the runtime contract.
|
|
368
|
+
*
|
|
369
|
+
* Accepts:
|
|
370
|
+
* - FunctionExpression (possibly async/generator), e.g.:
|
|
371
|
+
* const obj = {
|
|
372
|
+
* fn: async function (input) {
|
|
373
|
+
* return input;
|
|
374
|
+
* }
|
|
375
|
+
* };
|
|
376
|
+
*
|
|
377
|
+
* Rejects:
|
|
378
|
+
* - ArrowFunctionExpression, e.g.:
|
|
379
|
+
* const obj = {
|
|
380
|
+
* fn: async (input) => input
|
|
381
|
+
* };
|
|
382
|
+
*
|
|
383
|
+
* - Any other non-function expression.
|
|
384
|
+
*
|
|
385
|
+
* Notes:
|
|
386
|
+
* - The rewriter will proactively convert arrow `fn` to a FunctionExpression before further processing.
|
|
387
|
+
*
|
|
388
|
+
* @param {import('@babel/types').Expression} v - Candidate node for `fn` value.
|
|
389
|
+
* @returns {boolean} True if `v` is a FunctionExpression and not an arrow function.
|
|
390
|
+
*/
|
|
391
|
+
export const isFunction = v => isFunctionExpression( v ) && !isArrowFunctionExpression( v );
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Determine whether a variable declarator represents a function-like value.
|
|
395
|
+
*
|
|
396
|
+
* Use case:
|
|
397
|
+
* - When `fn` calls a locally-declared function (directly or transitively), we need to:
|
|
398
|
+
* - propagate `this` to that function call (`callee.call(this, ...)`)
|
|
399
|
+
* - traverse into that function's body to rewrite imported step/workflow/evaluator calls.
|
|
400
|
+
*
|
|
401
|
+
* Matches patterns like:
|
|
402
|
+
* - Function expression:
|
|
403
|
+
* const foo = function (x) { return x + 1; };
|
|
404
|
+
*
|
|
405
|
+
* - Async/generator function expression:
|
|
406
|
+
* const foo = async function (x) { return await work(x); };
|
|
407
|
+
*
|
|
408
|
+
* - Arrow function (will be normalized to FunctionExpression by the rewriter):
|
|
409
|
+
* const foo = (x) => x + 1;
|
|
410
|
+
* const foo = async (x) => await work(x);
|
|
411
|
+
*
|
|
412
|
+
* Does not match:
|
|
413
|
+
* - Non-function initializers:
|
|
414
|
+
* const foo = 42;
|
|
415
|
+
* const foo = someIdentifier;
|
|
416
|
+
*
|
|
417
|
+
* @param {import('@babel/types').Node} v - AST node (typically a VariableDeclarator).
|
|
418
|
+
* @returns {boolean} True if the declarator's initializer is a function (arrow or function expression).
|
|
419
|
+
*/
|
|
420
|
+
export const isVarFunction = v =>
|
|
421
|
+
isVariableDeclarator( v ) && ( isFunctionExpression( v.init ) || isArrowFunctionExpression( v.init ) );
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Determine whether a binding node corresponds to a function-like declaration usable
|
|
425
|
+
* as a call-chain function target during workflow rewriting.
|
|
426
|
+
*
|
|
427
|
+
* Matches:
|
|
428
|
+
* - FunctionDeclaration:
|
|
429
|
+
* function foo(x) { return x + 1; }
|
|
430
|
+
*
|
|
431
|
+
* - VariableDeclarator initialized with a function or arrow (normalized later):
|
|
432
|
+
* const foo = function (x) { return x + 1; };
|
|
433
|
+
* const foo = (x) => x + 1;
|
|
434
|
+
*
|
|
435
|
+
* Non-matches:
|
|
436
|
+
* - Any binding that is not a function declaration nor a variable declarator with a function initializer.
|
|
437
|
+
*
|
|
438
|
+
* Why this matters:
|
|
439
|
+
* - The rewriter traverses call chains from the workflow `fn`. It must recognize which local
|
|
440
|
+
* callees are valid function bodies to rewrite and into which it can propagate `this`.
|
|
441
|
+
*
|
|
442
|
+
* @param {import('@babel/types').Node} node - Binding path node (FunctionDeclaration or VariableDeclarator).
|
|
443
|
+
* @returns {boolean} True if the node represents a function-like binding.
|
|
444
|
+
*/
|
|
445
|
+
export const isFunctionLikeBinding = node =>
|
|
446
|
+
isFunctionDeclaration( node ) ||
|
|
447
|
+
(
|
|
448
|
+
isVariableDeclarator( node ) &&
|
|
449
|
+
( isFunctionExpression( node.init ) || isArrowFunctionExpression( node.init ) )
|
|
450
|
+
);
|
|
@@ -12,24 +12,19 @@ function makeAst( source, filename ) {
|
|
|
12
12
|
describe( 'collect_target_imports', () => {
|
|
13
13
|
it( 'collects ESM imports for steps and workflows and flags changes', () => {
|
|
14
14
|
const dir = mkdtempSync( join( tmpdir(), 'collect-esm-' ) );
|
|
15
|
-
writeFileSync( join( dir, 'steps.js' ),
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
writeFileSync( join( dir, '
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
writeFileSync( join( dir, 'workflow.js' ), [
|
|
23
|
-
'export const FlowA = workflow({ name: "flow.a" })',
|
|
24
|
-
'export default workflow({ name: "flow.def" })'
|
|
25
|
-
].join( '\n' ) );
|
|
15
|
+
writeFileSync( join( dir, 'steps.js' ), `
|
|
16
|
+
export const StepA = step({ name: 'step.a' });
|
|
17
|
+
export const StepB = step({ name: 'step.b' });` );
|
|
18
|
+
writeFileSync( join( dir, 'evaluators.js' ), 'export const EvalA = evaluator({ name: \'eval.a\' });' );
|
|
19
|
+
writeFileSync( join( dir, 'workflow.js' ), `
|
|
20
|
+
export const FlowA = workflow({ name: 'flow.a' });
|
|
21
|
+
export default workflow({ name: 'flow.def' });` );
|
|
26
22
|
|
|
27
|
-
const source =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
].join( '\n' );
|
|
23
|
+
const source = `
|
|
24
|
+
import { StepA } from './steps.js';
|
|
25
|
+
import { EvalA } from './evaluators.js';
|
|
26
|
+
import WF, { FlowA } from './workflow.js';
|
|
27
|
+
const x = 1;`;
|
|
33
28
|
|
|
34
29
|
const ast = makeAst( source, join( dir, 'file.js' ) );
|
|
35
30
|
const { stepImports, evaluatorImports, flowImports } = collectTargetImports(
|
|
@@ -52,16 +47,15 @@ describe( 'collect_target_imports', () => {
|
|
|
52
47
|
|
|
53
48
|
it( 'collects CJS requires and removes declarators (steps + default workflow)', () => {
|
|
54
49
|
const dir = mkdtempSync( join( tmpdir(), 'collect-cjs-' ) );
|
|
55
|
-
writeFileSync( join( dir, 'steps.js' ), 'export const StepB = step({ name:
|
|
56
|
-
writeFileSync( join( dir, 'evaluators.js' ), 'export const EvalB = evaluator({ name:
|
|
57
|
-
writeFileSync( join( dir, 'workflow.js' ), 'export default workflow({ name:
|
|
50
|
+
writeFileSync( join( dir, 'steps.js' ), 'export const StepB = step({ name: \'step.b\' })' );
|
|
51
|
+
writeFileSync( join( dir, 'evaluators.js' ), 'export const EvalB = evaluator({ name: \'eval.b\' })' );
|
|
52
|
+
writeFileSync( join( dir, 'workflow.js' ), 'export default workflow({ name: \'flow.c\' })' );
|
|
58
53
|
|
|
59
|
-
const source =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
].join( '\n' );
|
|
54
|
+
const source = `
|
|
55
|
+
const { StepB } = require( './steps.js' );
|
|
56
|
+
const { EvalB } = require( './evaluators.js' );
|
|
57
|
+
const WF = require( './workflow.js' );
|
|
58
|
+
const obj = {};`;
|
|
65
59
|
|
|
66
60
|
const ast = makeAst( source, join( dir, 'file.js' ) );
|
|
67
61
|
const { stepImports, evaluatorImports, flowImports } = collectTargetImports(
|
|
@@ -19,25 +19,22 @@ function runLoader( source, resourcePath ) {
|
|
|
19
19
|
describe( 'workflows_rewriter Webpack loader spec', () => {
|
|
20
20
|
it( 'rewrites ESM imports and converts fn arrow to function', async () => {
|
|
21
21
|
const dir = mkdtempSync( join( tmpdir(), 'ast-loader-esm-' ) );
|
|
22
|
-
writeFileSync( join( dir, 'steps.js' ), 'export const StepA = step({ name: \'step.a\' })
|
|
23
|
-
writeFileSync( join( dir, 'workflow.js' ),
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
'}',
|
|
39
|
-
''
|
|
40
|
-
].join( '\n' );
|
|
22
|
+
writeFileSync( join( dir, 'steps.js' ), 'export const StepA = step({ name: \'step.a\' });' );
|
|
23
|
+
writeFileSync( join( dir, 'workflow.js' ), `
|
|
24
|
+
export const FlowA = workflow({ name: 'flow.a' });
|
|
25
|
+
export default workflow({ name: 'flow.def' });` );
|
|
26
|
+
|
|
27
|
+
const source = `
|
|
28
|
+
import { StepA } from './steps.js';
|
|
29
|
+
import FlowDef, { FlowA } from './workflow.js';
|
|
30
|
+
|
|
31
|
+
const obj = {
|
|
32
|
+
fn: async (x) => {
|
|
33
|
+
StepA(1);
|
|
34
|
+
FlowA(2);
|
|
35
|
+
FlowDef(3);
|
|
36
|
+
}
|
|
37
|
+
}`;
|
|
41
38
|
|
|
42
39
|
const { code } = await runLoader( source, join( dir, 'file.js' ) );
|
|
43
40
|
|
|
@@ -53,18 +50,16 @@ describe( 'workflows_rewriter Webpack loader spec', () => {
|
|
|
53
50
|
|
|
54
51
|
it( 'rewrites ESM shared_steps imports to invokeSharedStep', async () => {
|
|
55
52
|
const dir = mkdtempSync( join( tmpdir(), 'ast-loader-esm-shared-' ) );
|
|
56
|
-
writeFileSync( join( dir, 'shared_steps.js' ), 'export const SharedA = step({ name: \'shared.a\' })
|
|
57
|
-
|
|
58
|
-
const source =
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
''
|
|
67
|
-
].join( '\n' );
|
|
53
|
+
writeFileSync( join( dir, 'shared_steps.js' ), 'export const SharedA = step({ name: \'shared.a\' });' );
|
|
54
|
+
|
|
55
|
+
const source = `
|
|
56
|
+
import { SharedA } from './shared_steps.js';
|
|
57
|
+
|
|
58
|
+
const obj = {
|
|
59
|
+
fn: async (x) => {
|
|
60
|
+
SharedA(1);
|
|
61
|
+
}
|
|
62
|
+
}`;
|
|
68
63
|
|
|
69
64
|
const { code } = await runLoader( source, join( dir, 'file.js' ) );
|
|
70
65
|
|
|
@@ -77,18 +72,16 @@ describe( 'workflows_rewriter Webpack loader spec', () => {
|
|
|
77
72
|
|
|
78
73
|
it( 'rewrites CJS shared_steps requires to invokeSharedStep', async () => {
|
|
79
74
|
const dir = mkdtempSync( join( tmpdir(), 'ast-loader-cjs-shared-' ) );
|
|
80
|
-
writeFileSync( join( dir, 'shared_steps.js' ), 'export const SharedB = step({ name: \'shared.b\' })
|
|
81
|
-
|
|
82
|
-
const source =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
''
|
|
91
|
-
].join( '\n' );
|
|
75
|
+
writeFileSync( join( dir, 'shared_steps.js' ), 'export const SharedB = step({ name: \'shared.b\' });' );
|
|
76
|
+
|
|
77
|
+
const source = `
|
|
78
|
+
const { SharedB } = require( './shared_steps.js' );
|
|
79
|
+
|
|
80
|
+
const obj = {
|
|
81
|
+
fn: async (y) => {
|
|
82
|
+
SharedB();
|
|
83
|
+
}
|
|
84
|
+
}`;
|
|
92
85
|
|
|
93
86
|
const { code } = await runLoader( source, join( dir, 'file.js' ) );
|
|
94
87
|
|
|
@@ -101,21 +94,19 @@ describe( 'workflows_rewriter Webpack loader spec', () => {
|
|
|
101
94
|
|
|
102
95
|
it( 'rewrites CJS requires and converts fn arrow to function', async () => {
|
|
103
96
|
const dir = mkdtempSync( join( tmpdir(), 'ast-loader-cjs-' ) );
|
|
104
|
-
writeFileSync( join( dir, 'steps.js' ), 'export const StepB = step({ name: \'step.b\' })
|
|
105
|
-
writeFileSync( join( dir, 'workflow.js' ), 'export default workflow({ name: \'flow.c\' })
|
|
106
|
-
|
|
107
|
-
const source =
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
''
|
|
118
|
-
].join( '\n' );
|
|
97
|
+
writeFileSync( join( dir, 'steps.js' ), 'export const StepB = step({ name: \'step.b\' });' );
|
|
98
|
+
writeFileSync( join( dir, 'workflow.js' ), 'export default workflow({ name: \'flow.c\' });' );
|
|
99
|
+
|
|
100
|
+
const source = `
|
|
101
|
+
const { StepB } = require( './steps.js' );
|
|
102
|
+
const FlowDefault = require( './workflow.js' );
|
|
103
|
+
|
|
104
|
+
const obj = {
|
|
105
|
+
fn: async (y) => {
|
|
106
|
+
StepB();
|
|
107
|
+
FlowDefault();
|
|
108
|
+
}
|
|
109
|
+
}`;
|
|
119
110
|
|
|
120
111
|
const { code } = await runLoader( source, join( dir, 'file.js' ) );
|
|
121
112
|
|
|
@@ -130,22 +121,19 @@ describe( 'workflows_rewriter Webpack loader spec', () => {
|
|
|
130
121
|
|
|
131
122
|
it( 'resolves top-level const name variables', async () => {
|
|
132
123
|
const dir = mkdtempSync( join( tmpdir(), 'ast-loader-const-' ) );
|
|
133
|
-
writeFileSync( join( dir, 'steps.js' ),
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
'import FlowDef, { FlowC } from \'./workflow.js\';',
|
|
147
|
-
'const obj = { fn: async () => { StepC(); FlowC(); FlowDef(); } }'
|
|
148
|
-
].join( '\n' );
|
|
124
|
+
writeFileSync( join( dir, 'steps.js' ), `
|
|
125
|
+
const NAME = 'step.const';
|
|
126
|
+
export const StepC = step({ name: NAME });` );
|
|
127
|
+
writeFileSync( join( dir, 'workflow.js' ), `
|
|
128
|
+
const WF = 'wf.const';
|
|
129
|
+
export const FlowC = workflow({ name: WF });
|
|
130
|
+
const D = 'wf.def';
|
|
131
|
+
export default workflow({ name: D });` );
|
|
132
|
+
|
|
133
|
+
const source = `
|
|
134
|
+
import { StepC } from './steps.js';
|
|
135
|
+
import FlowDef, { FlowC } from './workflow.js';
|
|
136
|
+
const obj = { fn: async () => { StepC(); FlowC(); FlowDef(); } }`;
|
|
149
137
|
|
|
150
138
|
const { code } = await runLoader( source, join( dir, 'file.js' ) );
|
|
151
139
|
expect( code ).toMatch( /this\.invokeStep\('step\.const'\)/ );
|
|
@@ -156,20 +144,17 @@ describe( 'workflows_rewriter Webpack loader spec', () => {
|
|
|
156
144
|
|
|
157
145
|
it( 'throws on non-static name', async () => {
|
|
158
146
|
const dir = mkdtempSync( join( tmpdir(), 'ast-loader-error-' ) );
|
|
159
|
-
writeFileSync( join( dir, 'steps.js' ),
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
'import WF from \'./workflow.js\';',
|
|
171
|
-
'const obj = { fn: async () => { StepX(); WF(); } }'
|
|
172
|
-
].join( '\n' );
|
|
147
|
+
writeFileSync( join( dir, 'steps.js' ), `
|
|
148
|
+
function n() { return 'x'; }
|
|
149
|
+
export const StepX = step({ name: n() });` );
|
|
150
|
+
writeFileSync( join( dir, 'workflow.js' ), `
|
|
151
|
+
const base = 'a';
|
|
152
|
+
export default workflow({ name: \`\${base}-b\` });` );
|
|
153
|
+
|
|
154
|
+
const source = `
|
|
155
|
+
import { StepX } from './steps.js';
|
|
156
|
+
import WF from './workflow.js';
|
|
157
|
+
const obj = { fn: async () => { StepX(); WF(); } }`;
|
|
173
158
|
|
|
174
159
|
await expect( runLoader( source, join( dir, 'file.js' ) ) ).rejects.toThrow( /Invalid (step|default workflow) name/ );
|
|
175
160
|
rmSync( dir, { recursive: true, force: true } );
|
|
@@ -1,10 +1,147 @@
|
|
|
1
1
|
import traverseModule from '@babel/traverse';
|
|
2
|
-
import { isArrowFunctionExpression, isIdentifier
|
|
3
|
-
import {
|
|
2
|
+
import { isArrowFunctionExpression, isIdentifier } from '@babel/types';
|
|
3
|
+
import {
|
|
4
|
+
toFunctionExpression,
|
|
5
|
+
createThisMethodCall,
|
|
6
|
+
isFunction,
|
|
7
|
+
bindThisAtCallSite,
|
|
8
|
+
isFunctionLikeBinding
|
|
9
|
+
} from '../tools.js';
|
|
4
10
|
|
|
5
11
|
// Handle CJS/ESM interop for Babel packages when executed as a webpack loader
|
|
6
12
|
const traverse = traverseModule.default ?? traverseModule;
|
|
7
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Check whether a CallExpression callee is a simple Identifier.
|
|
16
|
+
* Only direct identifier calls are rewritten; member/dynamic calls are skipped.
|
|
17
|
+
*
|
|
18
|
+
* We only support rewriting `Foo()` calls that refer to imported steps/flows/evaluators
|
|
19
|
+
* or local call-chain functions. Calls like `obj.Foo()` or `(getFn())()` are out of scope.
|
|
20
|
+
*
|
|
21
|
+
* Examples:
|
|
22
|
+
* - Supported: `Foo()`
|
|
23
|
+
* - Skipped: `obj.Foo()`, `(getFn())()`
|
|
24
|
+
*
|
|
25
|
+
* @param {import('@babel/traverse').NodePath} cPath - Path to a CallExpression node.
|
|
26
|
+
* @returns {boolean} True when callee is an Identifier.
|
|
27
|
+
*/
|
|
28
|
+
const isIdentifierCallee = cPath => isIdentifier( cPath.node.callee );
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Convert an ArrowFunctionExpression at the given path into a FunctionExpression
|
|
32
|
+
* to ensure dynamic `this` semantics inside the function body.
|
|
33
|
+
*
|
|
34
|
+
* Workflow code relies on `this` to invoke steps/flows (e.g., `this.invokeStep(...)`).
|
|
35
|
+
* Arrow functions capture `this` lexically, which would break that contract.
|
|
36
|
+
*
|
|
37
|
+
* If the node is an arrow, it is replaced by an equivalent FunctionExpression and
|
|
38
|
+
* the `state.rewrote` flag is set. If not an arrow, this is a no-op.
|
|
39
|
+
*
|
|
40
|
+
* @param {import('@babel/traverse').NodePath} nodePath - Path to a function node.
|
|
41
|
+
* @param {{ rewrote: boolean }} state - Mutation target to indicate a rewrite occurred.
|
|
42
|
+
* @returns {void}
|
|
43
|
+
*/
|
|
44
|
+
const normalizeArrowToFunctionPath = ( nodePath, state ) => {
|
|
45
|
+
if ( isArrowFunctionExpression( nodePath.node ) ) {
|
|
46
|
+
nodePath.replaceWith( toFunctionExpression( nodePath.node ) );
|
|
47
|
+
state.rewrote = true;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Rewrite calls inside a function body and collect call-chain functions discovered within.
|
|
52
|
+
* - Imported calls (steps/shared/evaluators/flows) are rewritten to `this.invokeX` or `this.startWorkflow`.
|
|
53
|
+
* - Local call-chain function calls are rewritten to `fn.call(this, ...)` to bind `this` correctly.
|
|
54
|
+
* - Returns a map of call-chain function name -> binding path for further recursive processing.
|
|
55
|
+
*
|
|
56
|
+
* @param {import('@babel/traverse').NodePath} bodyPath - Path to a function's body node.
|
|
57
|
+
* @param {Array<{ list: Array<any>, method: string, key: string }>} descriptors - Import rewrite descriptors.
|
|
58
|
+
* @param {{ rewrote: boolean }} state - Mutable state used to flag that edits were performed.
|
|
59
|
+
* @returns {Map<string, import('@babel/traverse').NodePath>} Discovered call-chain function bindings.
|
|
60
|
+
*/
|
|
61
|
+
const rewriteCallsInBody = ( bodyPath, descriptors, state ) => {
|
|
62
|
+
const callChainFunctions = new Map();
|
|
63
|
+
bodyPath.traverse( {
|
|
64
|
+
CallExpression: cPath => {
|
|
65
|
+
if ( !isIdentifierCallee( cPath ) ) {
|
|
66
|
+
return; // Only identifier callees are supported (skip member/dynamic)
|
|
67
|
+
}
|
|
68
|
+
const callee = cPath.node.callee;
|
|
69
|
+
|
|
70
|
+
// Rewrite imported calls (steps/shared/evaluators/flows)
|
|
71
|
+
for ( const { list, method, key } of descriptors ) {
|
|
72
|
+
const found = list.find( x => x.localName === callee.name );
|
|
73
|
+
if ( found ) {
|
|
74
|
+
const args = cPath.node.arguments;
|
|
75
|
+
cPath.replaceWith( createThisMethodCall( method, found[key], args ) );
|
|
76
|
+
state.rewrote = true;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Rewrite local call-chain function calls and track for recursive processing
|
|
82
|
+
const binding = cPath.scope.getBinding( callee.name );
|
|
83
|
+
if ( !binding ) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if ( !isFunctionLikeBinding( binding.path.node ) ) {
|
|
87
|
+
return; // Not a function-like binding
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Queue call-chain function for recursive processing
|
|
91
|
+
if ( !callChainFunctions.has( callee.name ) ) {
|
|
92
|
+
callChainFunctions.set( callee.name, binding.path );
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Bind `this` at callsite: fn(...) -> fn.call(this, ...)
|
|
96
|
+
cPath.replaceWith( bindThisAtCallSite( callee.name, cPath.node.arguments ) );
|
|
97
|
+
state.rewrote = true;
|
|
98
|
+
}
|
|
99
|
+
} );
|
|
100
|
+
return callChainFunctions;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Recursively process a call-chain function:
|
|
105
|
+
* - Ensures the function is a FunctionExpression (converts arrow when needed).
|
|
106
|
+
* - Rewrites calls inside the function using `rewriteCallsInBody`.
|
|
107
|
+
* - Follows nested call-chain functions depth-first while avoiding cycles via `processedFns`.
|
|
108
|
+
*
|
|
109
|
+
* @param {object} params - Params for processing a call-chain function.
|
|
110
|
+
* @param {string} params.name - Local identifier name in the current scope.
|
|
111
|
+
* @param {import('@babel/traverse').NodePath} params.bindingPath - Binding path of the function declaration.
|
|
112
|
+
* @param {{ rewrote: boolean }} params.state - Mutable state used to flag that edits were performed.
|
|
113
|
+
* @param {Array<{ list: Array<any>, method: string, key: string }>} params.descriptors - Import rewrite descriptors.
|
|
114
|
+
* @param {Set<string>} [params.processedFns] - Already processed names to avoid cycles.
|
|
115
|
+
*/
|
|
116
|
+
const processFunction = ( { name, bindingPath, state, descriptors, processedFns = new Set() } ) => {
|
|
117
|
+
// Avoid infinite loops for recursive/repeated references
|
|
118
|
+
if ( processedFns.has( name ) || bindingPath.removed ) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
processedFns.add( name );
|
|
122
|
+
|
|
123
|
+
if ( bindingPath.isVariableDeclarator() ) {
|
|
124
|
+
// Case 1: const foo = <function or arrow>
|
|
125
|
+
const initPath = bindingPath.get( 'init' );
|
|
126
|
+
// Arrow functions capture `this` lexically; normalize for dynamic `this`
|
|
127
|
+
normalizeArrowToFunctionPath( initPath, state );
|
|
128
|
+
// Rewrite calls in body; collect nested call-chain functions from this scope
|
|
129
|
+
const callChainFunctions = rewriteCallsInBody( initPath.get( 'body' ), descriptors, state );
|
|
130
|
+
// DFS: process nested call-chain functions (processedFns prevents cycles)
|
|
131
|
+
callChainFunctions.forEach( ( childBindingPath, childName ) => {
|
|
132
|
+
processFunction( { name: childName, bindingPath: childBindingPath, state, descriptors, processedFns } );
|
|
133
|
+
} );
|
|
134
|
+
} else if ( bindingPath.isFunctionDeclaration() ) {
|
|
135
|
+
// Case 2: function foo(...) { ... }
|
|
136
|
+
// Function declarations already have dynamic `this`; no normalization needed
|
|
137
|
+
const callChainFunctions = rewriteCallsInBody( bindingPath.get( 'body' ), descriptors, state );
|
|
138
|
+
// Continue DFS into any functions called from this declaration
|
|
139
|
+
callChainFunctions.forEach( ( childBindingPath, childName ) => {
|
|
140
|
+
processFunction( { name: childName, bindingPath: childBindingPath, state, descriptors, processedFns } );
|
|
141
|
+
} );
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
8
145
|
/**
|
|
9
146
|
* Rewrite calls to imported steps/workflows within `fn` object properties.
|
|
10
147
|
* Converts arrow fns to functions and replaces `StepX(...)` with
|
|
@@ -21,49 +158,36 @@ const traverse = traverseModule.default ?? traverseModule;
|
|
|
21
158
|
*/
|
|
22
159
|
export default function rewriteFnBodies( { ast, stepImports, sharedStepImports = [], evaluatorImports, flowImports } ) {
|
|
23
160
|
const state = { rewrote: false };
|
|
161
|
+
// Build rewrite descriptors once per traversal
|
|
162
|
+
const descriptors = [
|
|
163
|
+
{ list: stepImports, method: 'invokeStep', key: 'stepName' },
|
|
164
|
+
{ list: sharedStepImports, method: 'invokeSharedStep', key: 'stepName' },
|
|
165
|
+
{ list: evaluatorImports, method: 'invokeEvaluator', key: 'evaluatorName' },
|
|
166
|
+
{ list: flowImports, method: 'startWorkflow', key: 'workflowName' }
|
|
167
|
+
];
|
|
24
168
|
traverse( ast, {
|
|
25
169
|
ObjectProperty: path => {
|
|
26
|
-
//
|
|
170
|
+
// Only transform object properties named 'fn'
|
|
27
171
|
if ( !isIdentifier( path.node.key, { name: 'fn' } ) ) {
|
|
28
172
|
return;
|
|
29
173
|
}
|
|
30
174
|
|
|
31
175
|
const val = path.node.value;
|
|
32
176
|
|
|
33
|
-
//
|
|
34
|
-
if ( !
|
|
177
|
+
// Only functions (including arrows) are eligible
|
|
178
|
+
if ( !isFunction( val ) && !isArrowFunctionExpression( val ) ) {
|
|
35
179
|
return;
|
|
36
180
|
}
|
|
37
181
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
const func = toFunctionExpression( val );
|
|
41
|
-
path.get( 'value' ).replaceWith( func );
|
|
42
|
-
state.rewrote = true;
|
|
43
|
-
}
|
|
182
|
+
// Normalize arrow to function for correct dynamic `this`
|
|
183
|
+
normalizeArrowToFunctionPath( path.get( 'value' ), state );
|
|
44
184
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const descriptors = [
|
|
52
|
-
{ list: stepImports, method: 'invokeStep', key: 'stepName' },
|
|
53
|
-
{ list: sharedStepImports, method: 'invokeSharedStep', key: 'stepName' },
|
|
54
|
-
{ list: evaluatorImports, method: 'invokeEvaluator', key: 'evaluatorName' },
|
|
55
|
-
{ list: flowImports, method: 'startWorkflow', key: 'workflowName' }
|
|
56
|
-
];
|
|
57
|
-
for ( const { list, method, key } of descriptors ) {
|
|
58
|
-
const found = list.find( x => x.localName === callee.name );
|
|
59
|
-
if ( found ) {
|
|
60
|
-
const args = cPath.node.arguments;
|
|
61
|
-
cPath.replaceWith( createThisMethodCall( method, found[key], args ) );
|
|
62
|
-
state.rewrote = true;
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
185
|
+
// Rewrite the main workflow fn body and collect call-chain functions discovered within it
|
|
186
|
+
const callChainFunctions = rewriteCallsInBody( path.get( 'value.body' ), descriptors, state );
|
|
187
|
+
|
|
188
|
+
// Recursively rewrite call-chain functions and any functions they call
|
|
189
|
+
callChainFunctions.forEach( ( bindingPath, name ) => {
|
|
190
|
+
processFunction( { name, bindingPath, state, descriptors } );
|
|
67
191
|
} );
|
|
68
192
|
}
|
|
69
193
|
} );
|