-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform Object.assign to now use shared/assign
We need this to use the shared instance when Object.spread is used.
- Loading branch information
1 parent
42c6a76
commit 18c0cfb
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* 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 helperModuleImports = require('@babel/helper-module-imports'); | ||
|
||
module.exports = function autoImporter(babel) { | ||
function getAssignIdent(path, file, state) { | ||
if (state.id) { | ||
return state.id; | ||
} | ||
state.id = helperModuleImports.addDefault(path, 'shared/assign', { | ||
nameHint: 'assign', | ||
}); | ||
return state.id; | ||
} | ||
|
||
return { | ||
pre: function() { | ||
// map from module to generated identifier | ||
this.id = null; | ||
}, | ||
|
||
visitor: { | ||
CallExpression: function(path, file) { | ||
if (file.filename.indexOf('shared/assign') !== -1) { | ||
// Don't replace Object.assign if we're transforming shared/assign | ||
return; | ||
} | ||
if (path.get('callee').matchesPattern('Object.assign')) { | ||
// generate identifier and require if it hasn't been already | ||
const id = getAssignIdent(path, file, this); | ||
path.node.callee = id; | ||
} | ||
}, | ||
|
||
MemberExpression: function(path, file) { | ||
if (file.filename.indexOf('shared/assign') !== -1) { | ||
// Don't replace Object.assign if we're transforming shared/assign | ||
return; | ||
} | ||
if (path.matchesPattern('Object.assign')) { | ||
const id = getAssignIdent(path, file, this); | ||
path.replaceWith(id); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains 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