builder.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = createTemplateBuilder;
  6. var _options = require("./options");
  7. var _string = _interopRequireDefault(require("./string"));
  8. var _literal = _interopRequireDefault(require("./literal"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var NO_PLACEHOLDER = (0, _options.validate)({
  11. placeholderPattern: false
  12. });
  13. function createTemplateBuilder(formatter, defaultOpts) {
  14. var templateFnCache = new WeakMap();
  15. var templateAstCache = new WeakMap();
  16. var cachedOpts = defaultOpts || (0, _options.validate)(null);
  17. return Object.assign(function (tpl) {
  18. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  19. args[_key - 1] = arguments[_key];
  20. }
  21. if (typeof tpl === "string") {
  22. if (args.length > 1) throw new Error("Unexpected extra params.");
  23. return extendedTrace((0, _string.default)(formatter, tpl, (0, _options.merge)(cachedOpts, (0, _options.validate)(args[0]))));
  24. } else if (Array.isArray(tpl)) {
  25. var builder = templateFnCache.get(tpl);
  26. if (!builder) {
  27. builder = (0, _literal.default)(formatter, tpl, cachedOpts);
  28. templateFnCache.set(tpl, builder);
  29. }
  30. return extendedTrace(builder(args));
  31. } else if (typeof tpl === "object" && tpl) {
  32. if (args.length > 0) throw new Error("Unexpected extra params.");
  33. return createTemplateBuilder(formatter, (0, _options.merge)(cachedOpts, (0, _options.validate)(tpl)));
  34. }
  35. throw new Error("Unexpected template param " + typeof tpl);
  36. }, {
  37. ast: function ast(tpl) {
  38. for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  39. args[_key2 - 1] = arguments[_key2];
  40. }
  41. if (typeof tpl === "string") {
  42. if (args.length > 1) throw new Error("Unexpected extra params.");
  43. return (0, _string.default)(formatter, tpl, (0, _options.merge)((0, _options.merge)(cachedOpts, (0, _options.validate)(args[0])), NO_PLACEHOLDER))();
  44. } else if (Array.isArray(tpl)) {
  45. var builder = templateAstCache.get(tpl);
  46. if (!builder) {
  47. builder = (0, _literal.default)(formatter, tpl, (0, _options.merge)(cachedOpts, NO_PLACEHOLDER));
  48. templateAstCache.set(tpl, builder);
  49. }
  50. return builder(args)();
  51. }
  52. throw new Error("Unexpected template param " + typeof tpl);
  53. }
  54. });
  55. }
  56. function extendedTrace(fn) {
  57. var rootStack = "";
  58. try {
  59. throw new Error();
  60. } catch (error) {
  61. if (error.stack) {
  62. rootStack = error.stack.split("\n").slice(3).join("\n");
  63. }
  64. }
  65. return function (arg) {
  66. try {
  67. return fn(arg);
  68. } catch (err) {
  69. err.stack += "\n =============\n" + rootStack;
  70. throw err;
  71. }
  72. };
  73. }