source: trip-planner-front/node_modules/@babel/template/lib/builder.js@ 571e0df

Last change on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = createTemplateBuilder;
7
8var _options = require("./options");
9
10var _string = require("./string");
11
12var _literal = require("./literal");
13
14const NO_PLACEHOLDER = (0, _options.validate)({
15 placeholderPattern: false
16});
17
18function 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
62function 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}
Note: See TracBrowser for help on using the repository browser.