aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/plugin-transform-template-literals/lib/index.js
blob: 672aeaec78578137036fdd119ccfefa0610d9c3f (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _helperPluginUtils() {
  const data = require("@babel/helper-plugin-utils");

  _helperPluginUtils = function _helperPluginUtils() {
    return data;
  };

  return data;
}

function _core() {
  const data = require("@babel/core");

  _core = function _core() {
    return data;
  };

  return data;
}

var _default = (0, _helperPluginUtils().declare)((api, options) => {
  api.assertVersion(7);
  const loose = options.loose;
  let helperName = "taggedTemplateLiteral";
  if (loose) helperName += "Loose";

  function buildConcatCallExressions(items) {
    let avail = true;
    return items.reduce(function (left, right) {
      let canBeInserted = _core().types.isLiteral(right);

      if (!canBeInserted && avail) {
        canBeInserted = true;
        avail = false;
      }

      if (canBeInserted && _core().types.isCallExpression(left)) {
        left.arguments.push(right);
        return left;
      }

      return _core().types.callExpression(_core().types.memberExpression(left, _core().types.identifier("concat")), [right]);
    });
  }

  return {
    visitor: {
      TaggedTemplateExpression(path) {
        const node = path.node;
        const quasi = node.quasi;
        const strings = [];
        const raws = [];
        let isStringsRawEqual = true;
        var _arr = quasi.quasis;

        for (var _i = 0; _i < _arr.length; _i++) {
          const elem = _arr[_i];
          const _elem$value = elem.value,
                raw = _elem$value.raw,
                cooked = _elem$value.cooked;
          const value = cooked == null ? path.scope.buildUndefinedNode() : _core().types.stringLiteral(cooked);
          strings.push(value);
          raws.push(_core().types.stringLiteral(raw));

          if (raw !== cooked) {
            isStringsRawEqual = false;
          }
        }

        const scope = path.scope.getProgramParent();
        const templateObject = scope.generateUidIdentifier("templateObject");
        const helperId = this.addHelper(helperName);
        const callExpressionInput = [_core().types.arrayExpression(strings)];

        if (!isStringsRawEqual) {
          callExpressionInput.push(_core().types.arrayExpression(raws));
        }

        const lazyLoad = _core().template.ast`
          function ${templateObject}() {
            const data = ${_core().types.callExpression(helperId, callExpressionInput)};
            ${templateObject} = function() { return data };
            return data;
          } 
        `;
        scope.path.unshiftContainer("body", lazyLoad);
        path.replaceWith(_core().types.callExpression(node.tag, [_core().types.callExpression(_core().types.cloneNode(templateObject), []), ...quasi.expressions]));
      },

      TemplateLiteral(path) {
        const nodes = [];
        const expressions = path.get("expressions");
        let index = 0;
        var _arr2 = path.node.quasis;

        for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
          const elem = _arr2[_i2];

          if (elem.value.cooked) {
            nodes.push(_core().types.stringLiteral(elem.value.cooked));
          }

          if (index < expressions.length) {
            const expr = expressions[index++];
            const node = expr.node;

            if (!_core().types.isStringLiteral(node, {
              value: ""
            })) {
              nodes.push(node);
            }
          }
        }

        const considerSecondNode = !loose || !_core().types.isStringLiteral(nodes[1]);

        if (!_core().types.isStringLiteral(nodes[0]) && considerSecondNode) {
          nodes.unshift(_core().types.stringLiteral(""));
        }

        let root = nodes[0];

        if (loose) {
          for (let i = 1; i < nodes.length; i++) {
            root = _core().types.binaryExpression("+", root, nodes[i]);
          }
        } else if (nodes.length > 1) {
          root = buildConcatCallExressions(nodes);
        }

        path.replaceWith(root);
      }

    }
  };
});

exports.default = _default;