|
| 1 | +#!/usr/bin/env node |
| 2 | +import { readdir, readFile, stat, writeFile } from "node:fs/promises"; |
| 3 | +import { join, relative } from "node:path"; |
| 4 | +import type { ImportDeclaration, Node } from "@oxc-project/types"; |
| 5 | +import MagicString from "magic-string"; |
| 6 | +import { parseSync } from "oxc-parser"; |
| 7 | +import { walk } from "oxc-walker"; |
| 8 | +import { isCallExpressionWithName } from "./ast/core.ts"; |
| 9 | +import { findImportBySource } from "./ast/imports.ts"; |
| 10 | +import { isJSXElementWithName } from "./ast/jsx-helpers.ts"; |
| 11 | + |
| 12 | +async function* walkFiles(dir: string, extension: string): AsyncGenerator<string> { |
| 13 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 14 | + |
| 15 | + for (const entry of entries) { |
| 16 | + const path = join(dir, entry.name); |
| 17 | + if (entry.isDirectory()) { |
| 18 | + yield* walkFiles(path, extension); |
| 19 | + continue; |
| 20 | + } |
| 21 | + if (entry.isFile() && entry.name.endsWith(extension)) { |
| 22 | + yield path; |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function isRenderElement(node: Node, code: string): boolean { |
| 28 | + return isJSXElementWithName(node, code, "Render"); |
| 29 | +} |
| 30 | + |
| 31 | +function returnsRenderComponent(callback: Node, code: string): boolean { |
| 32 | + let hasRenderReturn = false; |
| 33 | + |
| 34 | + walk(callback, { |
| 35 | + enter(node: Node) { |
| 36 | + if (node.type === "ReturnStatement" && "argument" in node && node.argument) { |
| 37 | + if (isRenderElement(node.argument, code)) { |
| 38 | + hasRenderReturn = true; |
| 39 | + return; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (isRenderElement(node, code)) { |
| 44 | + hasRenderReturn = true; |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + return hasRenderReturn; |
| 50 | +} |
| 51 | + |
| 52 | +function detectsRenderComponentUsage(sourceCode: string): boolean { |
| 53 | + try { |
| 54 | + const ast = parseSync("temp.tsx", sourceCode); |
| 55 | + let hasRenderComponent = false; |
| 56 | + |
| 57 | + walk(ast.program, { |
| 58 | + enter(node: Node) { |
| 59 | + if (!isCallExpressionWithName(node, sourceCode, "component$")) return; |
| 60 | + if (!("arguments" in node)) return; |
| 61 | + |
| 62 | + const callback = node.arguments[0]; |
| 63 | + if (!callback) return; |
| 64 | + if ( |
| 65 | + callback.type !== "ArrowFunctionExpression" && |
| 66 | + callback.type !== "FunctionExpression" |
| 67 | + ) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (returnsRenderComponent(callback, sourceCode)) { |
| 72 | + hasRenderComponent = true; |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + return hasRenderComponent; |
| 78 | + } catch { |
| 79 | + return false; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function injectAsChildTypesIntoComponent( |
| 84 | + node: Node, |
| 85 | + content: string, |
| 86 | + s: MagicString |
| 87 | +): boolean { |
| 88 | + if (node.type !== "ExportNamedDeclaration") return false; |
| 89 | + if (!("declaration" in node) || !node.declaration) return false; |
| 90 | + |
| 91 | + const declaration = node.declaration; |
| 92 | + if (declaration.type !== "VariableDeclaration") return false; |
| 93 | + if (!("declarations" in declaration)) return false; |
| 94 | + |
| 95 | + let hasChanges = false; |
| 96 | + |
| 97 | + for (const declarator of declaration.declarations) { |
| 98 | + if (declarator.type !== "VariableDeclarator") continue; |
| 99 | + if (!("id" in declarator) || !declarator.id || declarator.id.type !== "Identifier") |
| 100 | + continue; |
| 101 | + |
| 102 | + const id = declarator.id as Node & { |
| 103 | + typeAnnotation?: { typeAnnotation: Node }; |
| 104 | + }; |
| 105 | + if (!("typeAnnotation" in id) || !id.typeAnnotation) continue; |
| 106 | + |
| 107 | + const typeAnnotation = id.typeAnnotation; |
| 108 | + if (!("typeAnnotation" in typeAnnotation)) continue; |
| 109 | + |
| 110 | + const typeNode = typeAnnotation.typeAnnotation; |
| 111 | + if (!typeNode) continue; |
| 112 | + |
| 113 | + const typeStr = content.slice(typeNode.start, typeNode.end); |
| 114 | + if (!typeStr.includes("Component<")) continue; |
| 115 | + |
| 116 | + const match = typeStr.match(/Component<([^>]+)>/); |
| 117 | + if (!match) continue; |
| 118 | + |
| 119 | + const propsType = match[1]; |
| 120 | + if (propsType.includes("AsChildTypes")) continue; |
| 121 | + |
| 122 | + const componentTypeEnd = typeNode.start + typeStr.lastIndexOf(">"); |
| 123 | + s.appendLeft(componentTypeEnd, " & AsChildTypes"); |
| 124 | + hasChanges = true; |
| 125 | + } |
| 126 | + |
| 127 | + return hasChanges; |
| 128 | +} |
| 129 | + |
| 130 | +function findToolsImport( |
| 131 | + ast: ReturnType<typeof parseSync>, |
| 132 | + content: string |
| 133 | +): Node | null { |
| 134 | + return findImportBySource(ast, content, "@qds.dev/tools"); |
| 135 | +} |
| 136 | + |
| 137 | +function injectAsChildTypesImport( |
| 138 | + ast: ReturnType<typeof parseSync>, |
| 139 | + content: string, |
| 140 | + s: MagicString, |
| 141 | + toolsImportNode: Node | null |
| 142 | +): void { |
| 143 | + if (toolsImportNode) { |
| 144 | + const importDecl = toolsImportNode as ImportDeclaration; |
| 145 | + if (importDecl.specifiers && importDecl.specifiers.length > 0) { |
| 146 | + const lastSpecifier = importDecl.specifiers[importDecl.specifiers.length - 1]; |
| 147 | + s.appendLeft(lastSpecifier.end, ", type AsChildTypes"); |
| 148 | + } |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + const firstImport = ast.program.body.find( |
| 153 | + (node: Node) => node.type === "ImportDeclaration" |
| 154 | + ); |
| 155 | + if (firstImport) { |
| 156 | + s.appendLeft( |
| 157 | + firstImport.start, |
| 158 | + 'import type { AsChildTypes } from "@qds.dev/tools";\n' |
| 159 | + ); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +async function transformTypeFile(dtsPath: string, sourcePath: string): Promise<boolean> { |
| 164 | + const content = await readFile(dtsPath, "utf-8"); |
| 165 | + if (content.includes("AsChildTypes")) return false; |
| 166 | + |
| 167 | + try { |
| 168 | + const sourceCode = await readFile(sourcePath, "utf-8"); |
| 169 | + if (!detectsRenderComponentUsage(sourceCode)) return false; |
| 170 | + } catch { |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + const ast = parseSync(dtsPath, content); |
| 176 | + const s = new MagicString(content); |
| 177 | + let hasChanges = false; |
| 178 | + |
| 179 | + walk(ast.program, { |
| 180 | + enter(node: Node) { |
| 181 | + if (injectAsChildTypesIntoComponent(node, content, s)) { |
| 182 | + hasChanges = true; |
| 183 | + } |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + if (!hasChanges) return false; |
| 188 | + |
| 189 | + const toolsImportNode = findToolsImport(ast, content); |
| 190 | + injectAsChildTypesImport(ast, content, s, toolsImportNode); |
| 191 | + |
| 192 | + await writeFile(dtsPath, s.toString(), "utf-8"); |
| 193 | + return true; |
| 194 | + } catch (error) { |
| 195 | + console.error(`Error processing ${dtsPath}:`, error); |
| 196 | + return false; |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +async function main() { |
| 201 | + const sourceDir = process.argv[2] || "./src"; |
| 202 | + const declDir = process.argv[3] || "./lib-types"; |
| 203 | + |
| 204 | + console.log(`🔍 Scanning ${sourceDir} for source files...`); |
| 205 | + |
| 206 | + let processedCount = 0; |
| 207 | + let changedCount = 0; |
| 208 | + |
| 209 | + for await (const sourcePath of walkFiles(sourceDir, ".tsx")) { |
| 210 | + const relativePath = relative(sourceDir, sourcePath); |
| 211 | + const dtsPath = join(declDir, relativePath.replace(/\.tsx$/, ".d.ts")); |
| 212 | + |
| 213 | + try { |
| 214 | + await stat(dtsPath); |
| 215 | + } catch { |
| 216 | + continue; |
| 217 | + } |
| 218 | + |
| 219 | + processedCount++; |
| 220 | + const changed = await transformTypeFile(dtsPath, sourcePath); |
| 221 | + if (changed) { |
| 222 | + changedCount++; |
| 223 | + console.log(`✓ Transformed ${dtsPath}`); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + console.log( |
| 228 | + `\n✨ Processed ${processedCount} files, transformed ${changedCount} files` |
| 229 | + ); |
| 230 | +} |
| 231 | + |
| 232 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 233 | + main().catch((error) => { |
| 234 | + console.error("Error:", error); |
| 235 | + process.exit(1); |
| 236 | + }); |
| 237 | +} |
| 238 | + |
| 239 | +export { |
| 240 | + detectsRenderComponentUsage, |
| 241 | + findToolsImport, |
| 242 | + injectAsChildTypesImport, |
| 243 | + injectAsChildTypesIntoComponent, |
| 244 | + isRenderElement, |
| 245 | + returnsRenderComponent, |
| 246 | + transformTypeFile, |
| 247 | + walkFiles |
| 248 | +}; |
0 commit comments