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