index.js
3.67 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _core = require("@babel/core");
var _default = (0, _helperPluginUtils.declare)((api, options) => {
api.assertVersion(7);
const {
loose
} = options;
let helperName = "taggedTemplateLiteral";
if (loose) helperName += "Loose";
function buildConcatCallExpressions(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 {
name: "transform-template-literals",
visitor: {
TaggedTemplateExpression(path) {
const {
node
} = path;
const {
quasi
} = node;
const strings = [];
const raws = [];
let isStringsRawEqual = true;
for (const elem of quasi.quasis) {
const {
raw,
cooked
} = elem.value;
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)};
${_core.types.cloneNode(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;
for (const elem of path.node.quasis) {
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 = buildConcatCallExpressions(nodes);
}
path.replaceWith(root);
}
}
};
});
exports.default = _default;