From 7049d9037d353956bf1fd830f4528ab897c04da6 Mon Sep 17 00:00:00 2001 From: shoonia Date: Sun, 22 Oct 2023 22:20:52 +0300 Subject: [PATCH 1/2] Refactoring --- plugins/babel.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/plugins/babel.js b/plugins/babel.js index 295b903..9a98607 100644 --- a/plugins/babel.js +++ b/plugins/babel.js @@ -16,22 +16,25 @@ const components = new Set([ 'ModalPortal', ]); +/** @returns {import('@babel/types').MemberExpression} */ +const createObjectAssign = () => ({ + type: 'MemberExpression', + computed: false, + object: { + type: 'Identifier', + name: 'Object', + }, + property: { + type: 'Identifier', + name: 'assign', + }, +}); + /** @type {import('@babel/types').LogicalExpression} */ const objectAssign = { operator: '||', right: { type: 'FunctionExpression' }, - left: { - type: 'MemberExpression', - computed: false, - object: { - type: 'Identifier', - name: 'Object', - }, - property: { - type: 'Identifier', - name: 'assign', - }, - }, + left: createObjectAssign(), }; /** @type {import('@babel/types').AssignmentExpression} */ @@ -59,7 +62,7 @@ const plugin = () => { const { node } = path; if (deepMatch(node, objectAssign)) { - path.replaceWith(node.left); + path.replaceWith(createObjectAssign()); } }, From 01a13a61a931b4aa0781d7d3afdb7cb3251dbc07 Mon Sep 17 00:00:00 2001 From: shoonia Date: Sun, 22 Oct 2023 22:36:19 +0300 Subject: [PATCH 2/2] Remove TS polyfill --- plugins/babel.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/plugins/babel.js b/plugins/babel.js index 9a98607..6538d70 100644 --- a/plugins/babel.js +++ b/plugins/babel.js @@ -31,10 +31,28 @@ const createObjectAssign = () => ({ }); /** @type {import('@babel/types').LogicalExpression} */ -const objectAssign = { +const maybePolyfill = { operator: '||', right: { type: 'FunctionExpression' }, - left: createObjectAssign(), +}; + +/** @type {import('@babel/types').MemberExpression} */ +const objectAssign = createObjectAssign(); + +/** @type {import('@babel/types').LogicalExpression} */ +const objectAssignTs = { + type: 'LogicalExpression', + operator: '&&', + left: { type: 'ThisExpression' }, + right: { + type: 'MemberExpression', + computed: false, + object: { type: 'ThisExpression' }, + property: { + type: 'Identifier', + name: '__assign', + }, + }, }; /** @type {import('@babel/types').AssignmentExpression} */ @@ -61,8 +79,13 @@ const plugin = () => { LogicalExpression(path) { const { node } = path; - if (deepMatch(node, objectAssign)) { - path.replaceWith(createObjectAssign()); + if (deepMatch(node, maybePolyfill)) { + if ( + deepMatch(node.left, objectAssign) || + deepMatch(node.left, objectAssignTs) + ) { + path.replaceWith(createObjectAssign()); + } } },