Skip to content

Commit

Permalink
filtered exported functions (export only private function)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamsonChan committed Oct 14, 2024
1 parent 9ce4634 commit 6cc1a4d
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions babel-plugin-expose-private-functions-and-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ module.exports = function ({types: t}) {
);
}

/** @internal */
function _isExportedFunction(node) {
return t.isExpressionStatement(node) &&
t.isAssignmentExpression(node.expression) &&
t.isMemberExpression(node.expression.left) &&
t.isIdentifier(node.expression.left.object) &&
node.expression.left.object.name === 'exports' &&
t.isIdentifier(node.expression.left.property) &&
t.isIdentifier(node.expression.right);
}

return {
visitor: {
Program: {
Expand All @@ -66,7 +77,8 @@ module.exports = function ({types: t}) {
_jsLoc = _dir.findIndex(x => x === 'js'),
_file = _dir[_dir.length - 1],
_privateVars = new Set(),
_privateFuncs = new Set();
_privateFuncs = new Set(),
_exportedFuncs = new Set();

// pass if [not in js folder] or [not ts file] or [is global.d.ts] or [is index.ts]
if (_jsLoc === -1 || !_file.endsWith('ts') || _file === 'global.d.ts' || _file === 'index.ts') return;
Expand All @@ -82,7 +94,7 @@ module.exports = function ({types: t}) {
// || _dirStr === 'window.za11y.dom_a11y._') return;

// visit all nodes
rootPath.node.body.forEach((node, index) => {
rootPath.node.body.forEach((node) => {
// collect private variables
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
Expand All @@ -92,9 +104,19 @@ module.exports = function ({types: t}) {
// collect private functions and replace them with `window.PACKAGE._._func = function (args) {...}`
} else if (t.isFunctionDeclaration(node) && t.isIdentifier(node.id)) {
_privateFuncs.add(node.id.name);
rootPath.get('body')[index].replaceWith(_createFunctionAssignmentExpression(_dir, node));
} else if (_isExportedFunction(node)) {
_exportedFuncs.add(node.expression.right.name);
}
});
_exportedFuncs.forEach(f => _privateFuncs.delete(f));

// replace private function declarations with `window.PACKAGE._._func`
rootPath.node.body.forEach((node, index) => {
if (t.isFunctionDeclaration(node)
&& t.isIdentifier(node.id)
&& _privateFuncs.has(node.id.name))
rootPath.get('body')[index].replaceWith(_createFunctionAssignmentExpression(_dir, node));
});

// insert check-exist if statements in the start of the file
for (let i = _dir.length; i >= 2; i--)
Expand Down Expand Up @@ -259,6 +281,23 @@ module.exports = function ({types: t}) {

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

// =============== [DEBUG INFO] show private function calls ===============
const arr = t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier('dlwlrma'),
t.arrayExpression(Array.from(_privateFuncs).map(x => t.stringLiteral(x)))
)
]);
rootPath.pushContainer('body', arr);

const brr = t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier('exportedFuncs'),
t.arrayExpression(Array.from(_exportedFuncs).map(x => t.stringLiteral(x)))
)
]);
rootPath.pushContainer('body', brr);
}
}
}
Expand Down

0 comments on commit 6cc1a4d

Please sign in to comment.