-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add effect-3.0.4 mod to remove gen adapter (#34)
- Loading branch information
1 parent
1283aff
commit bfc890c
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |