Skip to content

Instantly share code, notes, and snippets.

@hufeng
Created December 21, 2018 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hufeng/cc1e70f337ae64b171f5aed1f99245d7 to your computer and use it in GitHub Desktop.
Save hufeng/cc1e70f337ae64b171f5aed1f99245d7 to your computer and use it in GitHub Desktop.
/**
* 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 declare = require('@babel/helper-plugin-utils').declare;
const addDefault = require('@babel/helper-module-imports').addDefault;
module.exports = declare(api => {
api.assertVersion(7);
function getAssignIdent(path, state) {
if (state.id) {
return state.id;
}
state.id = addDefault(path, 'object-assign', {nameHint: 'assign'});
return state.id;
}
return {
pre: function() {
// map from module to generated identifier
this.id = null;
},
visitor: {
CallExpression: function(path) {
if (path.get('callee').matchesPattern('Object.assign')) {
// generate identifier and require if it hasn't been already
const id = getAssignIdent(path, this);
path.node.callee = id;
}
},
MemberExpression: function(path) {
if (path.matchesPattern('Object.assign')) {
const id = getAssignIdent(path, this);
path.replaceWith(id);
}
},
},
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment