|
| 1 | +import type { |
| 2 | + Edit, |
| 3 | + Kinds, |
| 4 | + Range, |
| 5 | + Rule, |
| 6 | + SgNode, |
| 7 | + SgRoot, |
| 8 | + TypesMap, |
| 9 | +} from "@codemod.com/jssg-types/main"; |
| 10 | +import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call"; |
| 11 | +import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement"; |
| 12 | +import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path"; |
| 13 | +import { removeBinding } from "@nodejs/codemod-utils/ast-grep/remove-binding"; |
| 14 | +import { removeLines } from "@nodejs/codemod-utils/ast-grep/remove-lines"; |
| 15 | + |
| 16 | +type BindingToReplace = { |
| 17 | + rule: Rule<TypesMap>; |
| 18 | + node: SgNode<TypesMap, Kinds<TypesMap>>; |
| 19 | + binding: string; |
| 20 | + replaceFn: (arg: string) => string; |
| 21 | +}; |
| 22 | + |
| 23 | +const updates = [ |
| 24 | + { |
| 25 | + oldBind: "$.print", |
| 26 | + replaceFn: (arg: string) => `console.log(${arg})`, |
| 27 | + }, |
| 28 | + { |
| 29 | + oldBind: "$.puts", |
| 30 | + replaceFn: (arg: string) => `console.log(${arg})`, |
| 31 | + }, |
| 32 | + { |
| 33 | + oldBind: "$.debug", |
| 34 | + replaceFn: (arg: string) => `console.error(${arg})`, |
| 35 | + }, |
| 36 | + { |
| 37 | + oldBind: "$.error", |
| 38 | + replaceFn: (arg: string) => `console.error(${arg})`, |
| 39 | + }, |
| 40 | +]; |
| 41 | + |
| 42 | +/* |
| 43 | + * Transforms `util.print($$$ARG)` usage to `console.log($$$ARG)`. |
| 44 | + * Transforms `util.puts($$$ARG)` usage to `console.log($$$ARG)`. |
| 45 | + * Transforms `util.debug($$$ARG)` usage to `console.error($$$ARG)`. |
| 46 | + * Transforms `util.error($$$ARG)` usage to `console.error($$$ARG)`. |
| 47 | + * |
| 48 | + * Steps: |
| 49 | + * |
| 50 | + * Locate all `util.print|puts|debug|error` import imports, noting the replacement rule, import node, and binding name. |
| 51 | + * |
| 52 | + * For each binding, replace calls to util.print|puts|debug|error($$$ARG) with the new console.log|error format, |
| 53 | + * and determine if the import line should be updated or removed. |
| 54 | + * |
| 55 | + * Apply all changes, removing or updating the import line as needed. |
| 56 | + */ |
| 57 | +export default function transform(root: SgRoot): string | null { |
| 58 | + const rootNode = root.root(); |
| 59 | + const edits: Edit[] = []; |
| 60 | + const linesToRemove: Range[] = []; |
| 61 | + const bindsToReplace: BindingToReplace[] = []; |
| 62 | + |
| 63 | + const nodeRequires = getNodeRequireCalls(root, "util"); |
| 64 | + const nodeImports = getNodeImportStatements(root, "util"); |
| 65 | + const importRequireStatement = [...nodeRequires, ...nodeImports]; |
| 66 | + |
| 67 | + if (!importRequireStatement.length) return null; |
| 68 | + |
| 69 | + for (const node of importRequireStatement) { |
| 70 | + for (const update of updates) { |
| 71 | + const bind = resolveBindingPath(node, update.oldBind); |
| 72 | + |
| 73 | + // if `fn` function ins't coming from `node:util` |
| 74 | + if (!bind) continue; |
| 75 | + |
| 76 | + bindsToReplace.push({ |
| 77 | + rule: { |
| 78 | + pattern: `${bind}($$$ARG)`, |
| 79 | + }, |
| 80 | + node, |
| 81 | + binding: bind, |
| 82 | + replaceFn: update.replaceFn, |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + for (const bind of bindsToReplace) { |
| 88 | + const matches = rootNode.findAll({ |
| 89 | + rule: bind.rule, |
| 90 | + }); |
| 91 | + |
| 92 | + for (const match of matches) { |
| 93 | + const args = match.getMultipleMatches("ARG"); |
| 94 | + |
| 95 | + const argsStr = args |
| 96 | + .map((arg) => { |
| 97 | + const text = arg.text(); |
| 98 | + if (text === ",") { |
| 99 | + // if arg is a comman, add a space at end |
| 100 | + return text.padEnd(2, " "); |
| 101 | + } |
| 102 | + return text; |
| 103 | + }) |
| 104 | + .join(""); |
| 105 | + |
| 106 | + const replace = match.replace(bind.replaceFn(argsStr)); |
| 107 | + edits.push(replace); |
| 108 | + |
| 109 | + const result = removeBinding(bind.node, bind.binding.split(".").at(0)); |
| 110 | + |
| 111 | + if (result?.lineToRemove) { |
| 112 | + linesToRemove.push(result.lineToRemove); |
| 113 | + } |
| 114 | + |
| 115 | + if (result?.edit) { |
| 116 | + edits.push(result.edit); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (!edits.length) return; |
| 122 | + |
| 123 | + const sourceCode = rootNode.commitEdits(edits); |
| 124 | + |
| 125 | + return removeLines(sourceCode, linesToRemove); |
| 126 | +} |
0 commit comments