Skip to content

Commit

Permalink
swap params for some common constructors (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Feb 8, 2024
1 parent 66d50bb commit 631d125
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-toys-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/codemod": patch
---

swap params for some common constructors
60 changes: 52 additions & 8 deletions public/codemods/minor-2.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ export default function transformer(file: cs.FileInfo, api: cs.API) {
})

root.find(j.CallExpression).forEach(ast => {
if (
ast.value.typeParameters?.params.length === 3
&& expressionHasName(ast.value.callee, "async")
) {
ast.value.typeParameters.params.reverse()
popNever(ast.value.typeParameters.params)
popNever(ast.value.typeParameters.params)
}
swapFunctionCall(ast, "async", 3)
swapFunctionCall(ast, "asyncEffect", 3)
swapFunctionCall(ast, "asyncEither", 3)
swapFunctionCall(ast, "asyncInterrupt", 3)
swapFunctionCall(ast, "asyncOption", 3)
swapFunctionCall(ast, "asyncScoped", 3)

swapMethodCall(ast, "Deferred", "make", 2)
swapMethodCall(ast, "Deferred", "makeAs", 2)
swapMethodCall(ast, "Deferred", "unsafeMake", 2)

if (expressionHasName(ast.value.callee, "Tag")) {
expressionRename(ast.value.callee, "GenericTag")
Expand Down Expand Up @@ -82,6 +84,39 @@ const swapSchema = (
}
}

const swapFunctionCall = (
ast: cs.ASTPath<cs.CallExpression>,
name: string,
size: number,
) => {
if (
ast.value.typeParameters?.params.length === size
&& expressionHasName(ast.value.callee, name)
) {
ast.value.typeParameters.params.reverse()
for (let i = 0; i < size - 1; i++) {
popNever(ast.value.typeParameters.params)
}
}
}

const swapMethodCall = (
ast: cs.ASTPath<cs.CallExpression>,
object: string,
name: string,
size: number,
) => {
if (
ast.value.typeParameters?.params.length === size
&& expressionHasPropAccess(ast.value.callee, object, name)
) {
ast.value.typeParameters.params.reverse()
for (let i = 0; i < size - 1; i++) {
popNever(ast.value.typeParameters.params)
}
}
}

const popNever = (params: Array<k.TSTypeKind>) => {
if (
params.length > 0
Expand Down Expand Up @@ -150,6 +185,15 @@ const expressionHasName = (ast: k.ExpressionKind, name: string): boolean => {
}
}

const expressionHasPropAccess = (
ast: k.ExpressionKind,
object: string,
prop: string,
): boolean =>
ast.type === "MemberExpression"
&& ast.object.type === "Identifier" && ast.object.name === object
&& ast.property.type === "Identifier" && ast.property.name === prop

const expressionRename = (ast: k.ExpressionKind, name: string): void => {
switch (ast.type) {
case "Identifier": {
Expand Down

0 comments on commit 631d125

Please sign in to comment.