From 064f9ef21ea1de616faea908011cbce9ffe4f512 Mon Sep 17 00:00:00 2001 From: Michael Arnaldi Date: Mon, 5 Feb 2024 19:04:16 +0100 Subject: [PATCH] Fuse codemods, include tag rename --- public/codemods/add-tag-identifier.ts | 49 ------------------- .../{swap-type-params.ts => minor-2.3.ts} | 47 ++++++++++++++++++ 2 files changed, 47 insertions(+), 49 deletions(-) delete mode 100644 public/codemods/add-tag-identifier.ts rename public/codemods/{swap-type-params.ts => minor-2.3.ts} (74%) diff --git a/public/codemods/add-tag-identifier.ts b/public/codemods/add-tag-identifier.ts deleted file mode 100644 index 2a6847e..0000000 --- a/public/codemods/add-tag-identifier.ts +++ /dev/null @@ -1,49 +0,0 @@ -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.VariableDeclaration).forEach(ast => { - writeTagIdentifier(ast, j) - }) - - return root.toSource() -} - -const writeTagIdentifier = ( - ast: cs.ASTPath, - 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 - const isTag = (node: typeof callee): boolean => { - switch (node.type) { - case "Identifier": { - return node.name === "Tag" - } - case "MemberExpression": { - return isTag(node.property) - } - default: { - return false - } - } - } - if ( - isTag(callee) - && init.arguments.length === 0 - && declaration.id.type === "Identifier" - ) { - init.arguments.push(j.stringLiteral(`@services/${declaration.id.name}`)) - } - } - } -} diff --git a/public/codemods/swap-type-params.ts b/public/codemods/minor-2.3.ts similarity index 74% rename from public/codemods/swap-type-params.ts rename to public/codemods/minor-2.3.ts index 4af5bd6..18b8b7d 100644 --- a/public/codemods/swap-type-params.ts +++ b/public/codemods/minor-2.3.ts @@ -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 @@ -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() @@ -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, name: string) => { const initial = reference.value.typeName const loop = (node: typeof initial): boolean => { @@ -108,6 +131,30 @@ const hasName = (reference: cs.ASTPath, name: string) => { return loop(initial) } +const fixTagIdentifier = ( + ast: cs.ASTPath, + 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