You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.6 KiB

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