Skip to content

Commit

Permalink
Minify arrowFunctionName
Browse files Browse the repository at this point in the history
  • Loading branch information
echo094 committed Sep 7, 2024
1 parent f3e84b0 commit c6d5e11
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/plugin/jsconfuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,75 @@ const deAntiTooling = {
},
}

function checkArrowWrap(path) {
if (path.node?.name !== 'arguments') {
return null
}
if (!path.parentPath.isSpreadElement()) {
return null
}
const call = path.parentPath.parentPath
if (path.parentPath.listKey !== 'arguments' || !call.isCallExpression()) {
return null
}
if (call.key !== 'argument' || !call.parentPath.isReturnStatement()) {
return null
}
const func_name = call.node.callee?.name
if (!func_name) {
return null
}
let wrap = call.getFunctionParent()
if (wrap.key !== 'init') {
return null
}
wrap = wrap.parentPath
const wrap_name = wrap.node.id?.name
wrap = wrap.parentPath
if (
wrap.listKey !== 'body' ||
wrap.key !== 0 ||
wrap.container.length !== 2
) {
return null
}
const str = generator(wrap.container[1]).code
if (str.indexOf(wrap_name) === -1) {
return null
}
wrap = wrap.getFunctionParent()
const arrow_name = wrap.node?.id?.name
if (!arrow_name || wrap.node.params?.[0]?.name !== func_name) {
return null
}
return {
name: arrow_name,
path: wrap,
}
}

const deMinifyArrow = {
Identifier(path) {
let obj = checkArrowWrap(path)
if (!obj) {
return
}
console.log(`Find arrowFunctionName: ${obj.name}`)
let binding = obj.path.parentPath.scope.bindings[obj.name]
for (const ref of binding.referencePaths) {
if (ref.key !== 'callee') {
console.warn(`Unexpected ref of arrowFunctionName: ${obj.name}`)
continue
}
const repl_path = ref.parentPath
repl_path.replaceWith(repl_path.node.arguments[0])
}
binding.scope.crawl()
binding = obj.path.parentPath.scope.bindings[obj.name]
if (!binding.references) [obj.path.remove()]
},
}

module.exports = function (code) {
let ast
try {
Expand All @@ -70,6 +139,8 @@ module.exports = function (code) {
}
// AntiTooling
traverse(ast, deAntiTooling)
// Minify
traverse(ast, deMinifyArrow)
code = generator(ast, {
comments: false,
jsescOption: { minimal: true },
Expand Down

0 comments on commit c6d5e11

Please sign in to comment.