Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuse codemods, include tag rename #8

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading