Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shoonia committed Oct 25, 2023
1 parent 1e93780 commit 82f735d
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions plugins/babel.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const deepMatch = (source, patter) => {
if (
typeof source === 'object' &&
typeof patter === 'object'
) {
return Object.keys(patter).every((key) => {
return deepMatch(source[key], patter[key]);
});
const isMatch = (a, b) => {
if (a === null) {
return b === null;
}

return source === patter;
if (Array.isArray(a)) {
return Array.isArray(b) && b.every((p, i) => isMatch(a[i], p));
}

if (typeof a === 'object') {
return typeof b === 'object' && Object.keys(b).every((i) => isMatch(a[i], b[i]));
}

return a === b;
};

const components = new Set([
'Modal',
'ModalPortal',
]);

/** @returns {import('@babel/types').MemberExpression} */
const createObjectAssign = () => ({
/**
* @param {string} prop
* @returns {import('@babel/types').MemberExpression}
*/
const objectMember = (prop) => ({
type: 'MemberExpression',
computed: false,
object: {
Expand All @@ -26,7 +32,7 @@ const createObjectAssign = () => ({
},
property: {
type: 'Identifier',
name: 'assign',
name: prop,
},
});

Expand All @@ -37,7 +43,7 @@ const maybePolyfill = {
};

/** @type {import('@babel/types').MemberExpression} */
const objectAssign = createObjectAssign();
const objectAssign = objectMember('assign');

/** @type {import('@babel/types').LogicalExpression} */
const objectAssignTs = {
Expand Down Expand Up @@ -79,12 +85,12 @@ const plugin = () => {
LogicalExpression(path) {
const { node } = path;

if (deepMatch(node, maybePolyfill)) {
if (isMatch(node, maybePolyfill)) {
if (
deepMatch(node.left, objectAssign) ||
deepMatch(node.left, objectAssignTs)
isMatch(node.left, objectAssign) ||
isMatch(node.left, objectAssignTs)
) {
path.replaceWith(createObjectAssign());
path.replaceWith(objectMember('assign'));
}
}
},
Expand All @@ -94,7 +100,7 @@ const plugin = () => {
const { node } = path;

if (
deepMatch(node, propTypes) &&
isMatch(node, propTypes) &&
components.has(node.left.object.name)
) {
path.remove();
Expand Down

0 comments on commit 82f735d

Please sign in to comment.