| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | const helperModuleImports = require('@babel/helper-module-imports'); |
| 11 | |
| 12 | module.exports = function autoImporter(babel) { |
| 13 | function getAssignIdent(path, file, state) { |
| 14 | if (state.id) { |
| 15 | return state.id; |
| 16 | } |
| 17 | state.id = helperModuleImports.addDefault(path, 'shared/assign', { |
| 18 | nameHint: 'assign', |
| 19 | }); |
| 20 | return state.id; |
| 21 | } |
| 22 | |
| 23 | return { |
| 24 | pre: function () { |
| 25 | // map from module to generated identifier |
| 26 | this.id = null; |
| 27 | }, |
| 28 | |
| 29 | visitor: { |
| 30 | CallExpression: function (path, file) { |
| 31 | if (/shared(\/|\\)assign/.test(file.filename)) { |
| 32 | // Don't replace Object.assign if we're transforming shared/assign |
| 33 | return; |
| 34 | } |
| 35 | if (path.get('callee').matchesPattern('Object.assign')) { |
| 36 | // generate identifier and require if it hasn't been already |
| 37 | const id = getAssignIdent(path, file, this); |
| 38 | path.node.callee = id; |
| 39 | } |
| 40 | }, |
| 41 | |
| 42 | MemberExpression: function (path, file) { |
| 43 | if (/shared(\/|\\)assign/.test(file.filename)) { |
| 44 | // Don't replace Object.assign if we're transforming shared/assign |
| 45 | return; |
| 46 | } |
| 47 | if (path.matchesPattern('Object.assign')) { |
| 48 | const id = getAssignIdent(path, file, this); |
| 49 | path.replaceWith(id); |
| 50 | } |
| 51 | }, |
| 52 | }, |
| 53 | }; |
| 54 | }; |