index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. exports.__esModule = true;
  3. var _symbol = require("babel-runtime/core-js/symbol");
  4. var _symbol2 = _interopRequireDefault(_symbol);
  5. exports.default = function (code, opts) {
  6. var stack = void 0;
  7. try {
  8. throw new Error();
  9. } catch (error) {
  10. if (error.stack) {
  11. stack = error.stack.split("\n").slice(1).join("\n");
  12. }
  13. }
  14. var _getAst = function getAst() {
  15. var ast = void 0;
  16. try {
  17. ast = babylon.parse(code, (0, _assign2.default)({
  18. allowReturnOutsideFunction: true,
  19. allowSuperOutsideMethod: true
  20. }, opts));
  21. ast = _babelTraverse2.default.removeProperties(ast);
  22. _babelTraverse2.default.cheap(ast, function (node) {
  23. node[FROM_TEMPLATE] = true;
  24. });
  25. } catch (err) {
  26. err.stack = err.stack + "from\n" + stack;
  27. throw err;
  28. }
  29. _getAst = function getAst() {
  30. return ast;
  31. };
  32. return ast;
  33. };
  34. return function () {
  35. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  36. args[_key] = arguments[_key];
  37. }
  38. return useTemplate(_getAst(), args);
  39. };
  40. };
  41. var _cloneDeep = require("lodash/cloneDeep");
  42. var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
  43. var _assign = require("lodash/assign");
  44. var _assign2 = _interopRequireDefault(_assign);
  45. var _has = require("lodash/has");
  46. var _has2 = _interopRequireDefault(_has);
  47. var _babelTraverse = require("babel-traverse");
  48. var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
  49. var _babylon = require("babylon");
  50. var babylon = _interopRequireWildcard(_babylon);
  51. var _babelTypes = require("babel-types");
  52. var t = _interopRequireWildcard(_babelTypes);
  53. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  54. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  55. var FROM_TEMPLATE = "_fromTemplate";
  56. var TEMPLATE_SKIP = (0, _symbol2.default)();
  57. function useTemplate(ast, nodes) {
  58. ast = (0, _cloneDeep2.default)(ast);
  59. var _ast = ast;
  60. var program = _ast.program;
  61. if (nodes.length) {
  62. (0, _babelTraverse2.default)(ast, templateVisitor, null, nodes);
  63. }
  64. if (program.body.length > 1) {
  65. return program.body;
  66. } else {
  67. return program.body[0];
  68. }
  69. }
  70. var templateVisitor = {
  71. noScope: true,
  72. enter: function enter(path, args) {
  73. var node = path.node;
  74. if (node[TEMPLATE_SKIP]) return path.skip();
  75. if (t.isExpressionStatement(node)) {
  76. node = node.expression;
  77. }
  78. var replacement = void 0;
  79. if (t.isIdentifier(node) && node[FROM_TEMPLATE]) {
  80. if ((0, _has2.default)(args[0], node.name)) {
  81. replacement = args[0][node.name];
  82. } else if (node.name[0] === "$") {
  83. var i = +node.name.slice(1);
  84. if (args[i]) replacement = args[i];
  85. }
  86. }
  87. if (replacement === null) {
  88. path.remove();
  89. }
  90. if (replacement) {
  91. replacement[TEMPLATE_SKIP] = true;
  92. path.replaceInline(replacement);
  93. }
  94. },
  95. exit: function exit(_ref) {
  96. var node = _ref.node;
  97. if (!node.loc) _babelTraverse2.default.clearNode(node);
  98. }
  99. };
  100. module.exports = exports["default"];