From 18c0cfb9b0211bf88330e11258fcbc8efe1e2f5b Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 23 Feb 2022 19:02:37 -0500 Subject: [PATCH] Transform Object.assign to now use shared/assign We need this to use the shared instance when Object.spread is used. --- scripts/babel/transform-object-assign.js | 54 ++++++++++++++++++++++++ scripts/rollup/build.js | 2 + 2 files changed, 56 insertions(+) create mode 100644 scripts/babel/transform-object-assign.js diff --git a/scripts/babel/transform-object-assign.js b/scripts/babel/transform-object-assign.js new file mode 100644 index 0000000000000..9b20675f39f13 --- /dev/null +++ b/scripts/babel/transform-object-assign.js @@ -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); + } + }, + }, + }; +}; diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 5c45368681281..c42edbe7eea64 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -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 = [