Created
December 21, 2018 03:11
-
-
Save hufeng/cc1e70f337ae64b171f5aed1f99245d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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