Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-cycles-slide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/swc-plugin": patch
---

Apply SWC transformation on step functions returned from factory function
35 changes: 34 additions & 1 deletion packages/swc-plugin-workflow/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,40 @@ impl StepTransform {
stmt.visit_mut_children_with(self);
}
}
Stmt::Decl(Decl::Var(_)) => {
Stmt::Decl(Decl::Var(var_decl)) => {
// Check if any declarators contain arrow functions with object literal bodies
for declarator in &mut var_decl.decls {
if let Some(init) = &mut declarator.init {
if let Pat::Ident(binding) = &declarator.name {
let name = binding.id.sym.to_string();

// Check if the initializer is an arrow function with object literal body
if let Expr::Arrow(arrow_expr) = &mut **init {
match &mut *arrow_expr.body {
BlockStmtOrExpr::Expr(expr) => {
// Handle both direct object literals and parenthesized ones
let obj_lit_mut = match &mut **expr {
Expr::Object(obj) => Some(obj),
Expr::Paren(paren) => {
if let Expr::Object(obj) = &mut *paren.expr {
Some(obj)
} else {
None
}
}
_ => None,
};

if let Some(obj_lit) = obj_lit_mut {
self.process_object_properties_for_step_functions(obj_lit, &name);
}
}
_ => {}
}
}
}
}
}
stmt.visit_mut_children_with(self);
}
_ => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from 'fs/promises';

const myFactory = () => ({
myStep: async () => {
'use step';
await fs.mkdir('test');
},
});

export default myFactory;

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from 'fs/promises';
const myFactory = ()=>({
myStep: async ()=>{
await fs.mkdir('test');
}
});
export default myFactory;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { registerStepFunction } from "workflow/internal/private";
import fs from 'fs/promises';
/**__internal_workflows{"steps":{"input.js":{"myFactory/myStep":{"stepId":"step//input.js//myFactory/myStep"}}}}*/;
var myFactory$myStep = async ()=>{
await fs.mkdir('test');
};
const myFactory = ()=>({
myStep: myFactory$myStep
});
export default myFactory;
registerStepFunction("step//input.js//myFactory/myStep", myFactory$myStep);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**__internal_workflows{"steps":{"input.js":{"myFactory/myStep":{"stepId":"step//input.js//myFactory/myStep"}}}}*/;
const myFactory = ()=>({
myStep: globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//input.js//myFactory/myStep")
});
export default myFactory;
Loading