UNPKG

1.2 kBJavaScriptView Raw
1const OBJECT_ASSIGN = 'ObjectAssign';
2
3export default function ({types: t}) {
4 return {
5 visitor: {
6 Program: {
7 enter(path, {file}) {
8 file.set(OBJECT_ASSIGN, false);
9 },
10
11 exit(path, {file, opts}) {
12 if (typeof opts === 'string') {
13 throw new Error(`Configuring module specifier with a string is no longer supported. Configure with { "moduleSpecifier": "${opts}" } instead of "${opts}".`);
14 }
15
16 const { moduleSpecifier = 'object-assign' } = opts;
17
18 if (!file.get(OBJECT_ASSIGN) && !path.scope.hasBinding(moduleSpecifier)) {
19 return;
20 }
21
22 const declar = t.importDeclaration([
23 t.importDefaultSpecifier(file.get(OBJECT_ASSIGN)),
24 ], t.stringLiteral(moduleSpecifier));
25
26 path.node.body.unshift(declar);
27 }
28 },
29
30 CallExpression(path, {file}) {
31 if (path.get('callee').matchesPattern('Object.assign')) {
32
33 if (!file.get(OBJECT_ASSIGN)) {
34 file.set(OBJECT_ASSIGN, path.scope.generateUidIdentifier('objectAssign'));
35 }
36
37 path.node.callee = file.get(OBJECT_ASSIGN);
38 }
39 }
40 }
41 };
42}