Skip to content

Commit c6d5e11

Browse files
committed
Minify arrowFunctionName
1 parent f3e84b0 commit c6d5e11

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/plugin/jsconfuser.js

+71
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,75 @@ const deAntiTooling = {
6060
},
6161
}
6262

63+
function checkArrowWrap(path) {
64+
if (path.node?.name !== 'arguments') {
65+
return null
66+
}
67+
if (!path.parentPath.isSpreadElement()) {
68+
return null
69+
}
70+
const call = path.parentPath.parentPath
71+
if (path.parentPath.listKey !== 'arguments' || !call.isCallExpression()) {
72+
return null
73+
}
74+
if (call.key !== 'argument' || !call.parentPath.isReturnStatement()) {
75+
return null
76+
}
77+
const func_name = call.node.callee?.name
78+
if (!func_name) {
79+
return null
80+
}
81+
let wrap = call.getFunctionParent()
82+
if (wrap.key !== 'init') {
83+
return null
84+
}
85+
wrap = wrap.parentPath
86+
const wrap_name = wrap.node.id?.name
87+
wrap = wrap.parentPath
88+
if (
89+
wrap.listKey !== 'body' ||
90+
wrap.key !== 0 ||
91+
wrap.container.length !== 2
92+
) {
93+
return null
94+
}
95+
const str = generator(wrap.container[1]).code
96+
if (str.indexOf(wrap_name) === -1) {
97+
return null
98+
}
99+
wrap = wrap.getFunctionParent()
100+
const arrow_name = wrap.node?.id?.name
101+
if (!arrow_name || wrap.node.params?.[0]?.name !== func_name) {
102+
return null
103+
}
104+
return {
105+
name: arrow_name,
106+
path: wrap,
107+
}
108+
}
109+
110+
const deMinifyArrow = {
111+
Identifier(path) {
112+
let obj = checkArrowWrap(path)
113+
if (!obj) {
114+
return
115+
}
116+
console.log(`Find arrowFunctionName: ${obj.name}`)
117+
let binding = obj.path.parentPath.scope.bindings[obj.name]
118+
for (const ref of binding.referencePaths) {
119+
if (ref.key !== 'callee') {
120+
console.warn(`Unexpected ref of arrowFunctionName: ${obj.name}`)
121+
continue
122+
}
123+
const repl_path = ref.parentPath
124+
repl_path.replaceWith(repl_path.node.arguments[0])
125+
}
126+
binding.scope.crawl()
127+
binding = obj.path.parentPath.scope.bindings[obj.name]
128+
if (!binding.references) [obj.path.remove()]
129+
},
130+
}
131+
63132
module.exports = function (code) {
64133
let ast
65134
try {
@@ -70,6 +139,8 @@ module.exports = function (code) {
70139
}
71140
// AntiTooling
72141
traverse(ast, deAntiTooling)
142+
// Minify
143+
traverse(ast, deMinifyArrow)
73144
code = generator(ast, {
74145
comments: false,
75146
jsescOption: { minimal: true },

0 commit comments

Comments
 (0)