const fs = require("fs"); const path = require("path"); const parser = require("@babel/parser"); const traverse = require("@babel/traverse").default; const generate = require("@babel/generator").default; const t = require("@babel/types"); const prettier = require("prettier"); const projectRoot = path.resolve(__dirname, "..", ".."); const outputRoot = path.resolve(__dirname, "..", "output"); const inputPath = path.join(outputRoot, "index-CO3BwsT2.pretty.js"); const outputPath = path.join(outputRoot, "index-CO3BwsT2.normalized.js"); async function main() { console.log("Reading file..."); const source = fs.readFileSync(inputPath, "utf8"); console.log("Parsing AST..."); const ast = parser.parse(source, { sourceType: "module", plugins: ["jsx"], }); console.log("Normalizing patterns..."); traverse(ast, { // Convert !0 -> true, !1 -> false UnaryExpression(path) { if (path.node.operator === "!" && path.node.argument.type === "NumericLiteral") { if (path.node.argument.value === 0) { path.replaceWith(t.booleanLiteral(true)); } else if (path.node.argument.value === 1) { path.replaceWith(t.booleanLiteral(false)); } } // Convert void 0 -> undefined if (path.node.operator === "void" && path.node.argument.type === "NumericLiteral" && path.node.argument.value === 0) { path.replaceWith(t.identifier("undefined")); } }, // Simplify numeric literals (e.g., 1e3 -> 1000) NumericLiteral(path) { if (path.node.extra && path.node.extra.raw && path.node.extra.raw.includes('e')) { delete path.node.extra; } }, // identified React Hook aliasing (heuristic) VariableDeclarator(path) { // Look for: const t = n.useState; if ( path.node.init && path.node.init.type === "MemberExpression" && path.node.init.property.type === "Identifier" && path.node.init.property.name.startsWith("use") && path.node.id.type === "Identifier" && path.node.id.name.length <= 2 // minified name ) { const hookName = path.node.init.property.name; const oldName = path.node.id.name; const scope = path.scope; // Rename oldName to hookName in this scope // scope.rename(oldName, hookName); // Dangerous if hookName is already in use // For now, let's just log it or be very careful } } }); console.log("Generating code..."); const { code } = generate(ast, { retainLines: true, compact: false, }); console.log("Formatting with Prettier..."); const formatted = await prettier.format(code, { parser: "babel", printWidth: 100, trailingComma: "all", singleQuote: false, }); console.log(`Writing output to ${outputPath}...`); fs.writeFileSync(outputPath, formatted, "utf8"); console.log("Normalization complete!"); } main().catch((err) => { console.error(err); process.exit(1); });