UNPKG

1.57 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7exports.default = function (_ref) {
8 var t = _ref.types;
9
10 return {
11 visitor: {
12 Program: {
13 enter: function enter(path, _ref2) {
14 var file = _ref2.file;
15
16 file.set(OBJECT_ASSIGN, false);
17 },
18 exit: function exit(path, _ref3) {
19 var file = _ref3.file,
20 opts = _ref3.opts;
21
22 if (typeof opts === 'string') {
23 throw new Error('Configuring module specifier with a string is no longer supported. Configure with { "moduleSpecifier": "' + opts + '" } instead of "' + opts + '".');
24 }
25
26 var _opts$moduleSpecifier = opts.moduleSpecifier,
27 moduleSpecifier = _opts$moduleSpecifier === undefined ? 'object-assign' : _opts$moduleSpecifier;
28
29
30 if (!file.get(OBJECT_ASSIGN) && !path.scope.hasBinding(moduleSpecifier)) {
31 return;
32 }
33
34 var declar = t.importDeclaration([t.importDefaultSpecifier(file.get(OBJECT_ASSIGN))], t.stringLiteral(moduleSpecifier));
35
36 path.node.body.unshift(declar);
37 }
38 },
39
40 CallExpression: function CallExpression(path, _ref4) {
41 var file = _ref4.file;
42
43 if (path.get('callee').matchesPattern('Object.assign')) {
44
45 if (!file.get(OBJECT_ASSIGN)) {
46 file.set(OBJECT_ASSIGN, path.scope.generateUidIdentifier('objectAssign'));
47 }
48
49 path.node.callee = file.get(OBJECT_ASSIGN);
50 }
51 }
52 }
53 };
54};
55
56var OBJECT_ASSIGN = 'ObjectAssign';
\No newline at end of file