index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. exports.default = function (_ref) {
  6. var t = _ref.types;
  7. function isString(node) {
  8. return t.isLiteral(node) && typeof node.value === "string";
  9. }
  10. function buildBinaryExpression(left, right) {
  11. return t.binaryExpression("+", left, right);
  12. }
  13. return {
  14. visitor: {
  15. TaggedTemplateExpression: function TaggedTemplateExpression(path, state) {
  16. var node = path.node;
  17. var quasi = node.quasi;
  18. var args = [];
  19. var strings = [];
  20. var raw = [];
  21. for (var _iterator = quasi.quasis, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  22. var _ref2;
  23. if (_isArray) {
  24. if (_i >= _iterator.length) break;
  25. _ref2 = _iterator[_i++];
  26. } else {
  27. _i = _iterator.next();
  28. if (_i.done) break;
  29. _ref2 = _i.value;
  30. }
  31. var elem = _ref2;
  32. strings.push(t.stringLiteral(elem.value.cooked));
  33. raw.push(t.stringLiteral(elem.value.raw));
  34. }
  35. strings = t.arrayExpression(strings);
  36. raw = t.arrayExpression(raw);
  37. var templateName = "taggedTemplateLiteral";
  38. if (state.opts.loose) templateName += "Loose";
  39. var templateObject = state.file.addTemplateObject(templateName, strings, raw);
  40. args.push(templateObject);
  41. args = args.concat(quasi.expressions);
  42. path.replaceWith(t.callExpression(node.tag, args));
  43. },
  44. TemplateLiteral: function TemplateLiteral(path, state) {
  45. var nodes = [];
  46. var expressions = path.get("expressions");
  47. for (var _iterator2 = path.node.quasis, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
  48. var _ref3;
  49. if (_isArray2) {
  50. if (_i2 >= _iterator2.length) break;
  51. _ref3 = _iterator2[_i2++];
  52. } else {
  53. _i2 = _iterator2.next();
  54. if (_i2.done) break;
  55. _ref3 = _i2.value;
  56. }
  57. var elem = _ref3;
  58. nodes.push(t.stringLiteral(elem.value.cooked));
  59. var expr = expressions.shift();
  60. if (expr) {
  61. if (state.opts.spec && !expr.isBaseType("string") && !expr.isBaseType("number")) {
  62. nodes.push(t.callExpression(t.identifier("String"), [expr.node]));
  63. } else {
  64. nodes.push(expr.node);
  65. }
  66. }
  67. }
  68. nodes = nodes.filter(function (n) {
  69. return !t.isLiteral(n, { value: "" });
  70. });
  71. if (!isString(nodes[0]) && !isString(nodes[1])) {
  72. nodes.unshift(t.stringLiteral(""));
  73. }
  74. if (nodes.length > 1) {
  75. var root = buildBinaryExpression(nodes.shift(), nodes.shift());
  76. for (var _iterator3 = nodes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
  77. var _ref4;
  78. if (_isArray3) {
  79. if (_i3 >= _iterator3.length) break;
  80. _ref4 = _iterator3[_i3++];
  81. } else {
  82. _i3 = _iterator3.next();
  83. if (_i3.done) break;
  84. _ref4 = _i3.value;
  85. }
  86. var node = _ref4;
  87. root = buildBinaryExpression(root, node);
  88. }
  89. path.replaceWith(root);
  90. } else {
  91. path.replaceWith(nodes[0]);
  92. }
  93. }
  94. }
  95. };
  96. };
  97. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  98. module.exports = exports["default"];