From c934eb3cbf17d4b3f81af5d1fafa48ba2d67f17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81?= Date: Tue, 17 Dec 2024 13:26:39 +0900 Subject: [PATCH] add MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ふぁ --- deobf2.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/deobf2.js b/deobf2.js index 2167a5f..082d9d5 100644 --- a/deobf2.js +++ b/deobf2.js @@ -137,9 +137,74 @@ const evaluateCallExpressions = { }, }; +const deadCodeElimination = { + ReturnStatement(path) { + let currentPath = path; + do { + const siblings = currentPath.getAllNextSiblings(); + siblings.forEach((sibling) => sibling.remove()); + currentPath = currentPath.parentPath; + } while (currentPath && t.isBlockStatement(currentPath.node)); + }, +}; + +const inlineArrayDestructuring = { + VariableDeclarator(path) { + const node = path.node; + if (t.isArrayExpression(node.init) && t.isArrayPattern(node.id)) { + const elements = node.init.elements; + const names = node.id.elements; + const newDeclarations = []; + + elements.forEach((element, index) => { + const name = names[index]; + if (name && element) { + newDeclarations.push( + t.variableDeclarator(t.identifier(name.name), element) + ); + } + }); + if (newDeclarations.length > 0) { + path.replaceWithMultiple(newDeclarations); + } + } + }, +}; + +const inlineObjectDestructuring = { + VariableDeclaration(path) { + const node = path.node; + if (node.declarations.length > 1) { + const newDeclarations = node.declarations.map((declarator) => + t.variableDeclaration(node.kind, [declarator]) + ); + path.replaceWithMultiple(newDeclarations); + } + }, +}; + +const removeUnusedVariables = { + Program(path) { + path.traverse({ + VariableDeclarator(variablePath) { + const binding = variablePath.scope.getBinding( + variablePath.node.id.name + ); + if (binding && !binding.referenced) { + variablePath.remove(); + } + }, + }); + }, +}; + traverse(AST, inlineDestructuredVariables); traverse(AST, evaluateMemberExpressions); traverse(AST, evaluateCallExpressions); +traverse(AST, deadCodeElimination); +traverse(AST, inlineArrayDestructuring); +traverse(AST, inlineObjectDestructuring); +traverse(AST, removeUnusedVariables); const final_code = generate(AST, beautify_opts).code;