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