index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. var _api$assumption, _api$assumption2;
  10. api.assertVersion(7);
  11. const ignoreToPrimitiveHint = (_api$assumption = api.assumption("ignoreToPrimitiveHint")) != null ? _api$assumption : options.loose;
  12. const mutableTemplateObject = (_api$assumption2 = api.assumption("mutableTemplateObject")) != null ? _api$assumption2 : options.loose;
  13. let helperName = "taggedTemplateLiteral";
  14. if (mutableTemplateObject) 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 helperArgs = [_core.types.arrayExpression(strings)];
  56. if (!isStringsRawEqual) {
  57. helperArgs.push(_core.types.arrayExpression(raws));
  58. }
  59. const tmp = path.scope.generateUidIdentifier("templateObject");
  60. path.scope.getProgramParent().push({
  61. id: _core.types.cloneNode(tmp)
  62. });
  63. path.replaceWith(_core.types.callExpression(node.tag, [_core.template.expression.ast`
  64. ${_core.types.cloneNode(tmp)} || (
  65. ${tmp} = ${this.addHelper(helperName)}(${helperArgs})
  66. )
  67. `, ...quasi.expressions]));
  68. },
  69. TemplateLiteral(path) {
  70. if (path.parent.type === "TSLiteralType") {
  71. return;
  72. }
  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. if (!_core.types.isStringLiteral(nodes[0]) && !(ignoreToPrimitiveHint && _core.types.isStringLiteral(nodes[1]))) {
  91. nodes.unshift(_core.types.stringLiteral(""));
  92. }
  93. let root = nodes[0];
  94. if (ignoreToPrimitiveHint) {
  95. for (let i = 1; i < nodes.length; i++) {
  96. root = _core.types.binaryExpression("+", root, nodes[i]);
  97. }
  98. } else if (nodes.length > 1) {
  99. root = buildConcatCallExpressions(nodes);
  100. }
  101. path.replaceWith(root);
  102. }
  103. }
  104. };
  105. });
  106. exports.default = _default;