Skip to content

Commit 18c0cfb

Browse files
committed
Transform Object.assign to now use shared/assign
We need this to use the shared instance when Object.spread is used.
1 parent 42c6a76 commit 18c0cfb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const helperModuleImports = require('@babel/helper-module-imports');
11+
12+
module.exports = function autoImporter(babel) {
13+
function getAssignIdent(path, file, state) {
14+
if (state.id) {
15+
return state.id;
16+
}
17+
state.id = helperModuleImports.addDefault(path, 'shared/assign', {
18+
nameHint: 'assign',
19+
});
20+
return state.id;
21+
}
22+
23+
return {
24+
pre: function() {
25+
// map from module to generated identifier
26+
this.id = null;
27+
},
28+
29+
visitor: {
30+
CallExpression: function(path, file) {
31+
if (file.filename.indexOf('shared/assign') !== -1) {
32+
// Don't replace Object.assign if we're transforming shared/assign
33+
return;
34+
}
35+
if (path.get('callee').matchesPattern('Object.assign')) {
36+
// generate identifier and require if it hasn't been already
37+
const id = getAssignIdent(path, file, this);
38+
path.node.callee = id;
39+
}
40+
},
41+
42+
MemberExpression: function(path, file) {
43+
if (file.filename.indexOf('shared/assign') !== -1) {
44+
// Don't replace Object.assign if we're transforming shared/assign
45+
return;
46+
}
47+
if (path.matchesPattern('Object.assign')) {
48+
const id = getAssignIdent(path, file, this);
49+
path.replaceWith(id);
50+
}
51+
},
52+
},
53+
};
54+
};

scripts/rollup/build.js

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ const babelPlugins = [
126126
'@babel/plugin-transform-parameters',
127127
// TODO: Remove array destructuring from the source. Requires runtime.
128128
['@babel/plugin-transform-destructuring', {loose: true, useBuiltIns: true}],
129+
// Transform Object spread to shared/assign
130+
require('../babel/transform-object-assign'),
129131
];
130132

131133
const babelToES5Plugins = [

0 commit comments

Comments
 (0)