1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
8 | var _core = require("@babel/core");
|
9 | var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
|
10 | var _api$assumption, _api$assumption2;
|
11 | api.assertVersion(7);
|
12 | const ignoreToPrimitiveHint = (_api$assumption = api.assumption("ignoreToPrimitiveHint")) != null ? _api$assumption : options.loose;
|
13 | const mutableTemplateObject = (_api$assumption2 = api.assumption("mutableTemplateObject")) != null ? _api$assumption2 : options.loose;
|
14 | let helperName = "taggedTemplateLiteral";
|
15 | if (mutableTemplateObject) helperName += "Loose";
|
16 | function buildConcatCallExpressions(items) {
|
17 | let avail = true;
|
18 | return items.reduce(function (left, right) {
|
19 | let canBeInserted = _core.types.isLiteral(right);
|
20 | if (!canBeInserted && avail) {
|
21 | canBeInserted = true;
|
22 | avail = false;
|
23 | }
|
24 | if (canBeInserted && _core.types.isCallExpression(left)) {
|
25 | left.arguments.push(right);
|
26 | return left;
|
27 | }
|
28 | return _core.types.callExpression(_core.types.memberExpression(left, _core.types.identifier("concat")), [right]);
|
29 | });
|
30 | }
|
31 | return {
|
32 | name: "transform-template-literals",
|
33 | visitor: {
|
34 | TaggedTemplateExpression(path) {
|
35 | const {
|
36 | node
|
37 | } = path;
|
38 | const {
|
39 | quasi
|
40 | } = node;
|
41 | const strings = [];
|
42 | const raws = [];
|
43 | let isStringsRawEqual = true;
|
44 | for (const elem of quasi.quasis) {
|
45 | const {
|
46 | raw,
|
47 | cooked
|
48 | } = elem.value;
|
49 | const value = cooked == null ? path.scope.buildUndefinedNode() : _core.types.stringLiteral(cooked);
|
50 | strings.push(value);
|
51 | raws.push(_core.types.stringLiteral(raw));
|
52 | if (raw !== cooked) {
|
53 | isStringsRawEqual = false;
|
54 | }
|
55 | }
|
56 | const helperArgs = [_core.types.arrayExpression(strings)];
|
57 | if (!isStringsRawEqual) {
|
58 | helperArgs.push(_core.types.arrayExpression(raws));
|
59 | }
|
60 | const tmp = path.scope.generateUidIdentifier("templateObject");
|
61 | path.scope.getProgramParent().push({
|
62 | id: _core.types.cloneNode(tmp)
|
63 | });
|
64 | path.replaceWith(_core.types.callExpression(node.tag, [_core.template.expression.ast`
|
65 | ${_core.types.cloneNode(tmp)} || (
|
66 | ${tmp} = ${this.addHelper(helperName)}(${helperArgs})
|
67 | )
|
68 | `, ...quasi.expressions]));
|
69 | },
|
70 | TemplateLiteral(path) {
|
71 | if (path.parent.type === "TSLiteralType") {
|
72 | return;
|
73 | }
|
74 | const nodes = [];
|
75 | const expressions = path.get("expressions");
|
76 | let index = 0;
|
77 | for (const elem of path.node.quasis) {
|
78 | if (elem.value.cooked) {
|
79 | nodes.push(_core.types.stringLiteral(elem.value.cooked));
|
80 | }
|
81 | if (index < expressions.length) {
|
82 | const expr = expressions[index++];
|
83 | const node = expr.node;
|
84 | if (!_core.types.isStringLiteral(node, {
|
85 | value: ""
|
86 | })) {
|
87 | nodes.push(node);
|
88 | }
|
89 | }
|
90 | }
|
91 | if (!_core.types.isStringLiteral(nodes[0]) && !(ignoreToPrimitiveHint && _core.types.isStringLiteral(nodes[1]))) {
|
92 | nodes.unshift(_core.types.stringLiteral(""));
|
93 | }
|
94 | let root = nodes[0];
|
95 | if (ignoreToPrimitiveHint) {
|
96 | for (let i = 1; i < nodes.length; i++) {
|
97 | root = _core.types.binaryExpression("+", root, nodes[i]);
|
98 | }
|
99 | } else if (nodes.length > 1) {
|
100 | root = buildConcatCallExpressions(nodes);
|
101 | }
|
102 | path.replaceWith(root);
|
103 | }
|
104 | }
|
105 | };
|
106 | });
|
107 |
|
108 |
|