File size: 1,253 Bytes
cc651f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use strict";

exports.__esModule = true;
exports.default = void 0;

var _util = require("./util");

function defaultTemplate({
  template
}, opts, {
  imports,
  interfaces,
  componentName,
  props,
  jsx,
  exports
}) {
  const plugins = ['jsx'];

  if (opts.typescript) {
    plugins.push('typescript');
  }

  const typeScriptTpl = template.smart({
    plugins
  });
  return typeScriptTpl.ast`${imports}

${interfaces}

function ${componentName}(${props}) {
  return ${jsx};
}
${exports}
  `;
}

const plugin = (api, opts) => ({
  visitor: {
    Program(path) {
      const {
        types: t
      } = api;
      const template = opts.template || defaultTemplate;
      const body = template(api, opts, {
        componentName: t.identifier(opts.state.componentName),
        interfaces: (0, _util.getInterface)(api, opts),
        props: (0, _util.getProps)(api, opts),
        imports: (0, _util.getImport)(api, opts),
        exports: (0, _util.getExport)(api, opts),
        jsx: path.node.body[0].expression
      });

      if (Array.isArray(body)) {
        path.node.body = body;
      } else {
        path.node.body = [body];
      }

      path.replaceWith(path.node);
    }

  }
});

var _default = plugin;
exports.default = _default;