Skip to content

Commit

Permalink
layout, sorting and jsdoc added
Browse files Browse the repository at this point in the history
  • Loading branch information
JamsonChan committed Oct 10, 2024
1 parent b64016e commit e6c2e8e
Showing 1 changed file with 70 additions and 49 deletions.
119 changes: 70 additions & 49 deletions babel-plugin-expose-private-functions-and-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ module.exports = function ({types: t}) {
);
});

/**
* AssignmentExpression
* properties: left, right
* example: x = y
* left: x, right: y
*/
function assExp(assignPath) {
// AssignmentExpression (left, right)
// x = y -> left: x, right: y

dfs(assignPath.get('left'));
dfs(assignPath.get('right'));

Expand All @@ -136,25 +139,40 @@ module.exports = function ({types: t}) {
}
}

function memExp(memPath) {
// MemberExpression (object, property)
// x.y.z -> object: x.y, property: z
// [NOTE] property cannot replace with window.PACKAGE._.FUNC, so ignore

dfs(memPath.get('object'));
/**
* CallExpression
* properties: callee, arguments
* example: x(y...)
* callee: x, arguments: y...
*/
function callExp(callPath) {
dfs(callPath.get('callee'));

const object = memPath.node.object;
// case: FUNC.x -> window.PACKAGE._.FUNC.x
if (t.isIdentifier(object) && privateFuncs.has(object.name)) {
funcCallCount.set(object.name, funcCallCount.get(object.name) + 1);
memPath.get('object').replaceWith(createNestedMemberExpression([object.name, ...dir]));
const { callee } = callPath.node;
// case: FUNC() -> window.PACKAGE._.FUNC()
if (t.isIdentifier(callee) && privateFuncs.has(callee.name)) {
funcCallCount.set(callee.name, funcCallCount.get(callee.name) + 1);
callPath.node.callee = createNestedMemberExpression([callee.name, ...dir]);
}

const args = callPath.get('arguments');
args.forEach(arg => {
// dfs(arg); // FIXME: no effect, but needed, too slow, temp comment it
// case: xxx(FUNC...) -> xxx(window.PACKAGE._.FUNC...)
if (t.isIdentifier(arg.node) && privateFuncs.has(arg.node.name)) {
funcCallCount.set(arg.node.name, funcCallCount.get(arg.node.name) + 1);
arg.replaceWith(createNestedMemberExpression([arg.node.name, ...dir]));
}
});
}

/**
* ConditionalExpression
* properties: test, consequent, alternate
* example: x ? y : z
* test: x, consequent: y, alternate: z
*/
function condExp(condPath) {
// ConditionalExpression (test, consequent, alternate)
// x ? y : z -> test: x, consequent: y, alternate: z

dfs(condPath.get('test'));
dfs(condPath.get('consequent'));
dfs(condPath.get('alternate'));
Expand All @@ -177,10 +195,13 @@ module.exports = function ({types: t}) {
}
}

/**
* LogicalExpression
* properties: left, right
* example: x && y, x || y, x ?? y
* left: x, right: y
*/
function logicExp(logicPath) {
// LogicalExpression (left, right)
// x && y -> left: x, right: y

dfs(logicPath.get('left'));
dfs(logicPath.get('right'));

Expand All @@ -197,34 +218,30 @@ module.exports = function ({types: t}) {
}
}

function callExp(callPath) {
// CallExpression (callee, arguments)
// x(y...) -> callee: x, arguments: y...

dfs(callPath.get('callee'));
/**
* MemberExpression
* properties: object, property
* example: x.y.z
* object: x.y, property: z
* [NOTE] property cannot replace with window.PACKAGE._.FUNC, so ignore
*/
function memExp(memPath) {
dfs(memPath.get('object'));

const { callee } = callPath.node;
// case: FUNC() -> window.PACKAGE._.FUNC()
if (t.isIdentifier(callee) && privateFuncs.has(callee.name)) {
funcCallCount.set(callee.name, funcCallCount.get(callee.name) + 1);
callPath.node.callee = createNestedMemberExpression([callee.name, ...dir]);
const object = memPath.node.object;
// case: FUNC.x -> window.PACKAGE._.FUNC.x
if (t.isIdentifier(object) && privateFuncs.has(object.name)) {
funcCallCount.set(object.name, funcCallCount.get(object.name) + 1);
memPath.get('object').replaceWith(createNestedMemberExpression([object.name, ...dir]));
}

const args = callPath.get('arguments');
args.forEach(arg => {
// dfs(arg); // FIXME: no effect, but needed, too slow, temp comment it
// case: xxx(FUNC...) -> xxx(window.PACKAGE._.FUNC...)
if (t.isIdentifier(arg.node) && privateFuncs.has(arg.node.name)) {
funcCallCount.set(arg.node.name, funcCallCount.get(arg.node.name) + 1);
arg.replaceWith(createNestedMemberExpression([arg.node.name, ...dir]));
}
});
}

/**
* ObjectExpression
* properties: properties
* TODO: split properties into 3 types (ObjectMethod | ObjectProperty | SpreadElement) of functions to handle
*/
function objExp(objectPath) {
// ObjectExpression (properties)
// TODO: split properties into 3 types (ObjectMethod | ObjectProperty | SpreadElement) of functions to handle

objectPath.node.properties.forEach((property) => {
// TODO: dfs
const {value} = property;
Expand All @@ -238,21 +255,25 @@ module.exports = function ({types: t}) {
});
}

/**
* Traverse all nodes in current file
*/
function dfs(path) {
path.traverse({
AssignmentExpression(assignPath) {assExp(assignPath); },
MemberExpression(memPath) { memExp(memPath); },
LogicalExpression(logicPath) { logicExp(logicPath); },
ConditionalExpression(condPath) { condExp(condPath); },
CallExpression(callPath) { callExp(callPath); },
ObjectExpression(objectPath) { objExp(objectPath); }
AssignmentExpression(p) {assExp(p); },
CallExpression(p) { callExp(p); },
ConditionalExpression(p) { condExp(p); },
LogicalExpression(p) { logicExp(p); },
MemberExpression(p) { memExp(p); },
ObjectExpression(p) { objExp(p); }
});
}

// replace private function calls to window.PACKAGE._.FUNC
dfs(path);


// =============== [DEBUG INFO] show counter of private function calls ===============
const callCountArray = Array.from(funcCallCount.entries()).map(([name, count]) =>
t.objectProperty(t.identifier(name), t.numericLiteral(count))
), callCountVariable = t.variableDeclaration('var', [
Expand Down

0 comments on commit e6c2e8e

Please sign in to comment.