2016-03-30 06:02:35 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-03-30 06:02:35 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-03-30 06:02:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2019-08-09 08:46:35 +08:00
|
|
|
const helperModuleImports = require('@babel/helper-module-imports');
|
|
|
|
|
|
2016-03-30 06:02:35 +08:00
|
|
|
module.exports = function autoImporter(babel) {
|
2016-04-08 07:24:20 +08:00
|
|
|
function getAssignIdent(path, file, state) {
|
2017-11-03 03:50:03 +08:00
|
|
|
if (state.id) {
|
|
|
|
|
return state.id;
|
2016-04-08 07:24:20 +08:00
|
|
|
}
|
2022-02-24 08:34:24 +08:00
|
|
|
state.id = helperModuleImports.addDefault(path, 'shared/assign', {
|
2019-08-09 08:46:35 +08:00
|
|
|
nameHint: 'assign',
|
|
|
|
|
});
|
2016-04-08 07:24:20 +08:00
|
|
|
return state.id;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 06:02:35 +08:00
|
|
|
return {
|
|
|
|
|
pre: function() {
|
|
|
|
|
// map from module to generated identifier
|
|
|
|
|
this.id = null;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
visitor: {
|
|
|
|
|
CallExpression: function(path, file) {
|
2022-04-09 02:47:31 +08:00
|
|
|
if (/shared(\/|\\)assign/.test(file.filename)) {
|
2022-02-24 08:34:24 +08:00
|
|
|
// Don't replace Object.assign if we're transforming shared/assign
|
2020-02-29 03:14:09 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-03-30 06:02:35 +08:00
|
|
|
if (path.get('callee').matchesPattern('Object.assign')) {
|
|
|
|
|
// generate identifier and require if it hasn't been already
|
2017-12-11 23:52:46 +08:00
|
|
|
const id = getAssignIdent(path, file, this);
|
2016-04-08 07:24:20 +08:00
|
|
|
path.node.callee = id;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
MemberExpression: function(path, file) {
|
2022-04-09 02:47:31 +08:00
|
|
|
if (/shared(\/|\\)assign/.test(file.filename)) {
|
2022-02-24 08:34:24 +08:00
|
|
|
// Don't replace Object.assign if we're transforming shared/assign
|
2020-02-29 03:14:09 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-04-08 07:24:20 +08:00
|
|
|
if (path.matchesPattern('Object.assign')) {
|
2017-12-11 23:52:46 +08:00
|
|
|
const id = getAssignIdent(path, file, this);
|
2016-04-08 07:24:20 +08:00
|
|
|
path.replaceWith(id);
|
2016-03-30 06:02:35 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|