Skip to content

Commit

Permalink
Add effect-3.0.4 mod to remove gen adapter (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi committed Apr 25, 2024
1 parent 1283aff commit bfc890c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .changeset/slimy-years-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
"@effect/codemod": patch
---

Add effect-3.0.4 mod to remove gen adapter

NOTE: some edge cases are uncovered like:

```ts
yield* $([Effect.succeed(0), Effect.succeed(1)] as const, Effect.allWith())
```

that needs to be convered to:

```ts
yield* pipe([Effect.succeed(0), Effect.succeed(1)] as const, Effect.allWith())
```

Unfortunately not having type information in the mod tool renders impossible to decide if the `pipe` function is present or not.
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions public/codemods/effect-3.0.4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type cs from "jscodeshift"

export default function transformer(file: cs.FileInfo, api: cs.API) {
const j = api.jscodeshift
const root = j(file.source)

root.find(j.CallExpression).forEach((call) => {
if (call.node.callee.type === "MemberExpression" && call.node.callee.property.type === "Identifier" && call.node.callee.property.name === "gen") {
if (call.node.arguments.length > 0 && call.node.arguments.length <= 2) {
const arg = call.node.arguments[call.node.arguments.length - 1]
if (arg.type === "FunctionExpression" && arg.generator === true && arg.params.length === 1 && arg.params[0].type === "Identifier") {
const adapter = arg.params[0].name
arg.params = []
j(arg.body).find(j.YieldExpression).forEach((yieldExpr) => {
if (yieldExpr.node.argument?.type === "CallExpression") {
const call = yieldExpr.node.argument
if (call.callee.type === "Identifier" && call.callee.name === adapter) {
if (call.arguments.length === 1 && call.arguments[0].type !== "SpreadElement") {
yieldExpr.node.argument = call.arguments[0]
} else if (call.arguments.length > 1 && call.arguments[0].type !== "SpreadElement") {
yieldExpr.node.argument = j.callExpression(
j.memberExpression(
call.arguments[0],
j.identifier("pipe")
),
call.arguments.slice(1)
)
}
}
}
})
}
}
}
})

return root.toSource()
}

0 comments on commit bfc890c

Please sign in to comment.