Skip to content

Commit

Permalink
Refactoring of the patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
shoonia committed Oct 20, 2023
1 parent 9c8807d commit a61ff60
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions plugins/babel.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ const deepMatch = (source, patter) => {
return source === patter;
};

/** @returns {import('@babel/core').PluginObj} */
module.exports = () => {
const componentNames = new Set([
'Modal',
'ModalPortal',
]);
const components = new Set([
'Modal',
'ModalPortal',
]);

/** @type {import('@babel/types').MemberExpression} */
const objectAssign = {
/** @type {import('@babel/types').LogicalExpression} */
const objectAssign = {
operator: '||',
right: { type: 'FunctionExpression' },
left: {
type: 'MemberExpression',
computed: false,
object: {
Expand All @@ -32,33 +33,34 @@ module.exports = () => {
type: 'Identifier',
name: 'assign',
},
};
},
};

/** @type {import('@babel/types').MemberExpression} */
const propTypes = {
/** @type {import('@babel/types').AssignmentExpression} */
const propTypes = {
operator: '=',
right: { type: 'ObjectExpression' },
left: {
type: 'MemberExpression',
computed: false,
object: {
type: 'Identifier',
},
object: { type: 'Identifier' },
property: {
type: 'Identifier',
name: 'propTypes',
},
};
},
};

/** @returns {import('@babel/core').PluginObj} */
module.exports = () => {
return {
name: 'minimizer',
visitor: {
// Remove `Object.assign` polyfill
LogicalExpression(path) {
const { node } = path;

if (
node.operator === '||' &&
node.right.type === 'FunctionExpression' &&
deepMatch(node.left, objectAssign)
) {
if (deepMatch(node, objectAssign)) {
path.replaceWith(node.left);
}
},
Expand All @@ -68,10 +70,8 @@ module.exports = () => {
const { node } = path;

if (
node.operator === '=' &&
node.right.type === 'ObjectExpression' &&
deepMatch(node.left, propTypes) &&
componentNames.has(node.left.object.name)
deepMatch(node, propTypes) &&
components.has(node.left.object.name)
) {
path.remove();
}
Expand Down

0 comments on commit a61ff60

Please sign in to comment.