Skip to content

Commit 2ade117

Browse files
committed
Apply SWC transformation on step functions returned from factory function
1 parent 3ff5977 commit 2ade117

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

.changeset/metal-cycles-slide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/swc-plugin": patch
3+
---
4+
5+
Apply SWC transformation on step functions returned from factory function

packages/swc-plugin-workflow/transform/src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,40 @@ impl StepTransform {
786786
stmt.visit_mut_children_with(self);
787787
}
788788
}
789-
Stmt::Decl(Decl::Var(_)) => {
789+
Stmt::Decl(Decl::Var(var_decl)) => {
790+
// Check if any declarators contain arrow functions with object literal bodies
791+
for declarator in &mut var_decl.decls {
792+
if let Some(init) = &mut declarator.init {
793+
if let Pat::Ident(binding) = &declarator.name {
794+
let name = binding.id.sym.to_string();
795+
796+
// Check if the initializer is an arrow function with object literal body
797+
if let Expr::Arrow(arrow_expr) = &mut **init {
798+
match &mut *arrow_expr.body {
799+
BlockStmtOrExpr::Expr(expr) => {
800+
// Handle both direct object literals and parenthesized ones
801+
let obj_lit_mut = match &mut **expr {
802+
Expr::Object(obj) => Some(obj),
803+
Expr::Paren(paren) => {
804+
if let Expr::Object(obj) = &mut *paren.expr {
805+
Some(obj)
806+
} else {
807+
None
808+
}
809+
}
810+
_ => None,
811+
};
812+
813+
if let Some(obj_lit) = obj_lit_mut {
814+
self.process_object_properties_for_step_functions(obj_lit, &name);
815+
}
816+
}
817+
_ => {}
818+
}
819+
}
820+
}
821+
}
822+
}
790823
stmt.visit_mut_children_with(self);
791824
}
792825
_ => {

packages/swc-plugin-workflow/transform/tests/fixture/factory-with-step-method/output-client.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs/promises';
22
const myFactory = ()=>({
33
myStep: async ()=>{
4-
'use step';
54
await fs.mkdir('test');
65
}
76
});
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { registerStepFunction } from "workflow/internal/private";
12
import fs from 'fs/promises';
3+
/**__internal_workflows{"steps":{"input.js":{"myFactory/myStep":{"stepId":"step//input.js//myFactory/myStep"}}}}*/;
4+
var myFactory$myStep = async ()=>{
5+
await fs.mkdir('test');
6+
};
27
const myFactory = ()=>({
3-
myStep: async ()=>{
4-
'use step';
5-
await fs.mkdir('test');
6-
}
8+
myStep: myFactory$myStep
79
});
810
export default myFactory;
11+
registerStepFunction("step//input.js//myFactory/myStep", myFactory$myStep);
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import fs from 'fs/promises';
1+
/**__internal_workflows{"steps":{"input.js":{"myFactory/myStep":{"stepId":"step//input.js//myFactory/myStep"}}}}*/;
22
const myFactory = ()=>({
3-
myStep: async ()=>{
4-
'use step';
5-
await fs.mkdir('test');
6-
}
3+
myStep: globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//input.js//myFactory/myStep")
74
});
85
export default myFactory;

0 commit comments

Comments
 (0)