1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = _default;
|
7 |
|
8 | function _default(_ref) {
|
9 | var t = _ref.types;
|
10 | return {
|
11 | visitor: {
|
12 | Program: {
|
13 | enter: function (path, _ref2) {
|
14 | var file = _ref2.file;
|
15 |
|
16 |
|
17 | if (path.scope.hasBinding('React')) {
|
18 | return;
|
19 | }
|
20 |
|
21 | var ourNode = t.importDeclaration([t.importDefaultSpecifier(t.identifier('React'))], t.stringLiteral('react'));
|
22 |
|
23 | file.set('ourPath', path.unshiftContainer('body', ourNode)[0]);
|
24 | },
|
25 | exit: function (_, _ref3) {
|
26 | var file = _ref3.file;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var ourPath = file.get('ourPath');
|
33 |
|
34 | if (ourPath && !file.get('hasJSX')) {
|
35 | if (!ourPath.removed) ourPath.remove();
|
36 | file.set('ourPath', undefined);
|
37 | }
|
38 | }
|
39 | },
|
40 | ImportDeclaration: function (path, _ref4) {
|
41 | var file = _ref4.file;
|
42 |
|
43 | if (path.node.specifiers.every(x => x.local.name !== 'React')) return;
|
44 |
|
45 |
|
46 | var ourPath = file.get('ourPath');
|
47 |
|
48 | if (ourPath && path !== ourPath) {
|
49 | if (!ourPath.removed) ourPath.remove();
|
50 | file.set('ourPath', undefined);
|
51 | }
|
52 | },
|
53 | JSXOpeningElement: function (_, _ref5) {
|
54 | var file = _ref5.file;
|
55 | file.set('hasJSX', true);
|
56 | },
|
57 | JSXOpeningFragment: function (_, _ref6) {
|
58 | var file = _ref6.file;
|
59 | file.set('hasJSX', true);
|
60 | }
|
61 | }
|
62 | };
|
63 | } |
\ | No newline at end of file |