Skip to content

Commit

Permalink
Fuse codemods, include tag rename (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored Feb 5, 2024
1 parent 0e1556c commit e8af4eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-files-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/codemod": patch
---

Fuse codemods, include tag renaming
49 changes: 0 additions & 49 deletions public/codemods/add-tag-identifier.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default function transformer(file: cs.FileInfo, api: cs.API) {
swapSchema(ast, j)
})

root.find(j.VariableDeclaration).forEach(ast => {
fixTagIdentifier(ast, j)
})

root.find(j.CallExpression).forEach(ast => {
if (
ast.value.typeParameters?.params.length === 3
Expand All @@ -24,6 +28,10 @@ export default function transformer(file: cs.FileInfo, api: cs.API) {
popNever(ast.value.typeParameters.params)
popNever(ast.value.typeParameters.params)
}

if (expressionHasName(ast.value.callee, "Tag")) {
expressionRename(ast.value.callee, "GenericTag")
}
})

return root.toSource()
Expand Down Expand Up @@ -87,6 +95,21 @@ const expressionHasName = (ast: k.ExpressionKind, name: string): boolean => {
}
}

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

const hasName = (reference: cs.ASTPath<cs.TSTypeReference>, name: string) => {
const initial = reference.value.typeName
const loop = (node: typeof initial): boolean => {
Expand All @@ -108,6 +131,30 @@ const hasName = (reference: cs.ASTPath<cs.TSTypeReference>, name: string) => {
return loop(initial)
}

const fixTagIdentifier = (
ast: cs.ASTPath<cs.VariableDeclaration>,
j: cs.API["jscodeshift"],
) => {
if (ast.value.declarations.length === 1) {
const declaration = ast.value.declarations[0]
if (
declaration.type === "VariableDeclarator"
&& declaration.init
&& declaration.init.type === "CallExpression"
) {
const init = declaration.init
const callee = init.callee
if (
expressionHasName(callee, "Tag")
&& init.arguments.length === 0
&& declaration.id.type === "Identifier"
) {
init.arguments.push(j.stringLiteral(`@services/${declaration.id.name}`))
}
}
}
}

//
// this is needed to resolve a bug in jscodeshift that
// forgets to traverse type parameters in call expressions
Expand Down

0 comments on commit e8af4eb

Please sign in to comment.