Skip to content

Commit

Permalink
Transform Object.assign to now use shared/assign
Browse files Browse the repository at this point in the history
We need this to use the shared instance when Object.spread is used.
  • Loading branch information
sebmarkbage committed Feb 24, 2022
1 parent 42c6a76 commit 18c0cfb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
54 changes: 54 additions & 0 deletions scripts/babel/transform-object-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const helperModuleImports = require('@babel/helper-module-imports');

module.exports = function autoImporter(babel) {
function getAssignIdent(path, file, state) {
if (state.id) {
return state.id;
}
state.id = helperModuleImports.addDefault(path, 'shared/assign', {
nameHint: 'assign',
});
return state.id;
}

return {
pre: function() {
// map from module to generated identifier
this.id = null;
},

visitor: {
CallExpression: function(path, file) {
if (file.filename.indexOf('shared/assign') !== -1) {
// Don't replace Object.assign if we're transforming shared/assign
return;
}
if (path.get('callee').matchesPattern('Object.assign')) {
// generate identifier and require if it hasn't been already
const id = getAssignIdent(path, file, this);
path.node.callee = id;
}
},

MemberExpression: function(path, file) {
if (file.filename.indexOf('shared/assign') !== -1) {
// Don't replace Object.assign if we're transforming shared/assign
return;
}
if (path.matchesPattern('Object.assign')) {
const id = getAssignIdent(path, file, this);
path.replaceWith(id);
}
},
},
};
};
2 changes: 2 additions & 0 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ const babelPlugins = [
'@babel/plugin-transform-parameters',
// TODO: Remove array destructuring from the source. Requires runtime.
['@babel/plugin-transform-destructuring', {loose: true, useBuiltIns: true}],
// Transform Object spread to shared/assign
require('../babel/transform-object-assign'),
];

const babelToES5Plugins = [
Expand Down

0 comments on commit 18c0cfb

Please sign in to comment.