Skip to content

Commit 87ce1ab

Browse files
committed
make dfs | pack each expression in their own function
1 parent 9418250 commit 87ce1ab

File tree

1 file changed

+73
-62
lines changed

1 file changed

+73
-62
lines changed

babel-plugin-expose-private-functions-and-variables.js

+73-62
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = function ({types: t}) {
4646
});
4747
// collect private functions and replace them with `window.PACKAGE._._func = function (args) {...}`
4848
} else if (t.isFunctionDeclaration(node) && t.isIdentifier(node.id)) {
49-
if (node.id.name === '_zk') return;
49+
// if (node.id.name === '_zk') return;
5050
privateFuncs.add(node.id.name);
5151
funcCallCount.set(node.id.name, 0);
5252
path.get('body')[index].replaceWith(
@@ -117,75 +117,86 @@ module.exports = function ({types: t}) {
117117
);
118118
});
119119

120-
// replace private function calls to window.PACKAGE._.FUNC
121-
path.traverse({
122-
AssignmentExpression(assignPath) {
123-
const left = assignPath.node.left,
124-
right = assignPath.node.right;
125-
// case: FUNC.x = x -> window.PACKAGE._.FUNC.x = x
126-
if (t.isMemberExpression(left)) {
127-
const object = left.object;
128-
if (t.isIdentifier(object) && privateFuncs.has(object.name)) {
129-
funcCallCount.set(object.name, funcCallCount.get(object.name) + 1);
130-
assignPath.get("left.object").replaceWith(createNestedMemberExpression([object.name, ...dir]));
131-
}
132-
}
133-
// case: x = FUNC -> x = window.PACKAGE._.FUNC
134-
if (t.isIdentifier(right) && privateFuncs.has(right.name)) {
135-
funcCallCount.set(right.name, funcCallCount.get(right.name) + 1);
136-
assignPath.node.right = createNestedMemberExpression([right.name, ...dir]);
120+
function assExp(assignPath) {
121+
const left = assignPath.node.left,
122+
right = assignPath.node.right;
123+
// case: FUNC.x = x -> window.PACKAGE._.FUNC.x = x
124+
if (t.isMemberExpression(left)) {
125+
const object = left.object;
126+
if (t.isIdentifier(object) && privateFuncs.has(object.name)) {
127+
funcCallCount.set(object.name, funcCallCount.get(object.name) + 1);
128+
assignPath.get("left.object").replaceWith(createNestedMemberExpression([object.name, ...dir]));
137129
}
138-
},
139-
ConditionalExpression(condPath) {
140-
const { test, consequent, alternate } = condPath.node;
141-
// TODO: test ?
142-
// case: x ? FUNC : x -> x ? window.PACKAGE._.FUNC : x
143-
if (t.isIdentifier(consequent) && privateFuncs.has(consequent.name)) {
144-
funcCallCount.set(consequent.name, funcCallCount.get(consequent.name) + 1);
145-
condPath.get('consequent').replaceWith(createNestedMemberExpression([consequent.name, ...dir]));
146-
}
147-
// case: x ? x : FUNC -> x ? x : window.PACKAGE._.FUNC
148-
if (t.isIdentifier(alternate) && privateFuncs.has(alternate.name)) {
149-
funcCallCount.set(alternate.name, funcCallCount.get(alternate.name) + 1);
150-
condPath.get('alternate').replaceWith(createNestedMemberExpression([alternate.name, ...dir]));
151-
}
152-
},
153-
CallExpression(callPath) {
154-
const callee = callPath.node.callee;
155-
if (t.isIdentifier(callee)) {
156-
// case: FUNC() -> window.PACKAGE._.FUNC()
157-
// this case includes `FUNC() in ?: conditional`
158-
if (privateFuncs.has(callee.name)) {
159-
funcCallCount.set(callee.name, funcCallCount.get(callee.name) + 1);
160-
callPath.node.callee = createNestedMemberExpression([callee.name, ...dir]);
161-
}
162-
const args = callPath.get('arguments');
163-
args.forEach(arg => {
164-
// case: xxx(FUNC) -> xxx(window.PACKAGE._.FUNC)
165-
if (t.isIdentifier(arg.node) && privateFuncs.has(arg.node.name)) {
166-
funcCallCount.set(arg.node.name, funcCallCount.get(arg.node.name) + 1);
167-
arg.replaceWith(createNestedMemberExpression([arg.node.name, ...dir]));
168-
}
169-
});
130+
}
131+
// case: x = FUNC -> x = window.PACKAGE._.FUNC
132+
if (t.isIdentifier(right) && privateFuncs.has(right.name)) {
133+
funcCallCount.set(right.name, funcCallCount.get(right.name) + 1);
134+
assignPath.node.right = createNestedMemberExpression([right.name, ...dir]);
135+
}
136+
}
137+
138+
function condExp(condPath) {
139+
const { test, consequent, alternate } = condPath.node;
140+
// TODO: test ?
141+
// case: x ? FUNC : x -> x ? window.PACKAGE._.FUNC : x
142+
if (t.isIdentifier(consequent) && privateFuncs.has(consequent.name)) {
143+
funcCallCount.set(consequent.name, funcCallCount.get(consequent.name) + 1);
144+
condPath.get('consequent').replaceWith(createNestedMemberExpression([consequent.name, ...dir]));
145+
}
146+
// case: x ? x : FUNC -> x ? x : window.PACKAGE._.FUNC
147+
if (t.isIdentifier(alternate) && privateFuncs.has(alternate.name)) {
148+
funcCallCount.set(alternate.name, funcCallCount.get(alternate.name) + 1);
149+
condPath.get('alternate').replaceWith(createNestedMemberExpression([alternate.name, ...dir]));
150+
}
151+
}
152+
153+
function callExp(callPath) {
154+
const callee = callPath.node.callee;
155+
if (t.isIdentifier(callee)) {
156+
// case: FUNC() -> window.PACKAGE._.FUNC()
157+
// this case includes `FUNC() in ?: conditional`
158+
if (privateFuncs.has(callee.name)) {
159+
funcCallCount.set(callee.name, funcCallCount.get(callee.name) + 1);
160+
callPath.node.callee = createNestedMemberExpression([callee.name, ...dir]);
170161
}
171-
},
172-
ObjectExpression(objectPath) {
173-
objectPath.node.properties.forEach((property) => {
174-
const {key, value} = property;
175-
// case: x = { x: FUNC } -> x = { x: window.PACKAGE._.FUNC }
176-
// case: x.x = FUNC -> x.x = window.PACKAGE._.FUNC
177-
if (t.isIdentifier(value) && privateFuncs.has(value.name)) {
178-
funcCallCount.set(value.name, funcCallCount.get(value.name) + 1);
179-
property.value = createNestedMemberExpression([property.value.name, ...dir]);
162+
const args = callPath.get('arguments');
163+
args.forEach(arg => {
164+
// case: xxx(FUNC) -> xxx(window.PACKAGE._.FUNC)
165+
if (t.isIdentifier(arg.node) && privateFuncs.has(arg.node.name)) {
166+
funcCallCount.set(arg.node.name, funcCallCount.get(arg.node.name) + 1);
167+
arg.replaceWith(createNestedMemberExpression([arg.node.name, ...dir]));
180168
}
181169
});
182170
}
183-
});
171+
}
172+
173+
function ObjExp(objectPath) {
174+
objectPath.node.properties.forEach((property) => {
175+
const {key, value} = property;
176+
// case: x = { x: FUNC } -> x = { x: window.PACKAGE._.FUNC }
177+
// case: x.x = FUNC -> x.x = window.PACKAGE._.FUNC
178+
if (t.isIdentifier(value) && privateFuncs.has(value.name)) {
179+
funcCallCount.set(value.name, funcCallCount.get(value.name) + 1);
180+
property.value = createNestedMemberExpression([property.value.name, ...dir]);
181+
}
182+
});
183+
}
184+
185+
function dfs(path) {
186+
path.traverse({
187+
AssignmentExpression(assignPath) {assExp(assignPath);},
188+
ConditionalExpression(condPath) {condExp(condPath);},
189+
CallExpression(callPath) {callExp(callPath);},
190+
ObjectExpression(objectPath) {ObjExp(objectPath);}
191+
});
192+
}
193+
194+
// replace private function calls to window.PACKAGE._.FUNC
195+
dfs(path);
184196

185197
const callCountArray = Array.from(funcCallCount.entries()).map(([name, count]) =>
186198
t.objectProperty(t.identifier(name), t.numericLiteral(count))
187-
);
188-
const callCountVariable = t.variableDeclaration('var', [
199+
), callCountVariable = t.variableDeclaration('var', [
189200
t.variableDeclarator(
190201
t.identifier('dlwlrma'),
191202
t.objectExpression(callCountArray)

0 commit comments

Comments
 (0)