index.js 3.3 KB

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