Skip to content

Commit

Permalink
Add codemod for async (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored Feb 5, 2024
1 parent f861455 commit 0e1556c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-candles-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/codemod": patch
---

Add codemod to turn async<a,b,c>() into async<c,b,a>() considering never as default
25 changes: 25 additions & 0 deletions public/codemods/swap-type-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ export default function transformer(file: cs.FileInfo, api: cs.API) {
swapSchema(ast, j)
})

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)
}
})

return root.toSource()
}

Expand Down Expand Up @@ -62,6 +73,20 @@ const popNever = (params: Array<k.TSTypeKind>) => {
}
}

const expressionHasName = (ast: k.ExpressionKind, name: string): boolean => {
switch (ast.type) {
case "Identifier": {
return ast.name === name
}
case "MemberExpression": {
return expressionHasName(ast.property, name)
}
default: {
return false
}
}
}

const hasName = (reference: cs.ASTPath<cs.TSTypeReference>, name: string) => {
const initial = reference.value.typeName
const loop = (node: typeof initial): boolean => {
Expand Down

0 comments on commit 0e1556c

Please sign in to comment.