2016-03-30 06:02:35 +08:00
|
|
|
/**
|
2017-09-25 04:48:13 +08:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
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';
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-11-03 03:50:03 +08:00
|
|
|
state.id = file.addImport('object-assign', 'default', '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) {
|
|
|
|
|
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) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|