Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️🏗 Allow dead-code-eliminating config.urls #37998

Merged
merged 17 commits into from
Apr 5, 2022
Prev Previous commit
Next Next commit
transform
  • Loading branch information
alanorozco committed Apr 1, 2022
commit 4429ba3f5a9d784d701621bab2ee381737b6bb53
36 changes: 29 additions & 7 deletions build-system/babel-plugins/babel-plugin-deep-pure/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
const pureFnName = 'pure';

/** @return {import('@babel/core').PluginObj} */
module.exports = function () {
alanorozco marked this conversation as resolved.
Show resolved Hide resolved
/** @param {import('@babel/core').NodePath} path */
function setIsPure(path) {
path.addComment('leading', ' #__PURE__ ');
}

/**
* @param {import('@babel/core').NodePath<import('@babel/types').CallExpression>} path
* @return {boolean}
*/
function replacePureFnCallExpression(path) {
if (
path.node.arguments.length !== 1 ||
!path.get('callee').isIdentifier({name: pureFnName})
) {
return false;
}
path.replaceWith(path.node.arguments[0]);
return true;
}

return {
name: 'deep-pure',
visitor: {
CallExpression(path) {
if (
path.node.arguments.length === 1 &&
path.get('callee').isIdentifier({name: 'pure'})
) {
path.replaceWith(path.node.arguments[0]);
if (replacePureFnCallExpression(path)) {
path.traverse({
NewExpression: setIsPure,
MemberExpression: setIsPure,
CallExpression: setIsPure,
CallExpression(path) {
if (!replacePureFnCallExpression(path)) {
setIsPure(path);
}
},
MemberExpression(path) {
throw path.buildCodeFrameError(
`${pureFnName}() expressions cannot contain member expressions`
);
},
});
}
},
Expand Down