massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / node_modules / ajv / dist / compile / codegen / code.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.regexpCode = exports.getProperty = exports.safeStringify = exports.stringify = exports.strConcat = exports.addCodeArg = exports.str = exports._ = exports.nil = exports._Code = exports.Name = exports.IDENTIFIER = exports._CodeOrName = void 0;
4 class _CodeOrName {
5 }
6 exports._CodeOrName = _CodeOrName;
7 exports.IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
8 class Name extends _CodeOrName {
9     constructor(s) {
10         super();
11         if (!exports.IDENTIFIER.test(s))
12             throw new Error("CodeGen: name must be a valid identifier");
13         this.str = s;
14     }
15     toString() {
16         return this.str;
17     }
18     emptyStr() {
19         return false;
20     }
21     get names() {
22         return { [this.str]: 1 };
23     }
24 }
25 exports.Name = Name;
26 class _Code extends _CodeOrName {
27     constructor(code) {
28         super();
29         this._items = typeof code === "string" ? [code] : code;
30     }
31     toString() {
32         return this.str;
33     }
34     emptyStr() {
35         if (this._items.length > 1)
36             return false;
37         const item = this._items[0];
38         return item === "" || item === '""';
39     }
40     get str() {
41         var _a;
42         return ((_a = this._str) !== null && _a !== void 0 ? _a : (this._str = this._items.reduce((s, c) => `${s}${c}`, "")));
43     }
44     get names() {
45         var _a;
46         return ((_a = this._names) !== null && _a !== void 0 ? _a : (this._names = this._items.reduce((names, c) => {
47             if (c instanceof Name)
48                 names[c.str] = (names[c.str] || 0) + 1;
49             return names;
50         }, {})));
51     }
52 }
53 exports._Code = _Code;
54 exports.nil = new _Code("");
55 function _(strs, ...args) {
56     const code = [strs[0]];
57     let i = 0;
58     while (i < args.length) {
59         addCodeArg(code, args[i]);
60         code.push(strs[++i]);
61     }
62     return new _Code(code);
63 }
64 exports._ = _;
65 const plus = new _Code("+");
66 function str(strs, ...args) {
67     const expr = [safeStringify(strs[0])];
68     let i = 0;
69     while (i < args.length) {
70         expr.push(plus);
71         addCodeArg(expr, args[i]);
72         expr.push(plus, safeStringify(strs[++i]));
73     }
74     optimize(expr);
75     return new _Code(expr);
76 }
77 exports.str = str;
78 function addCodeArg(code, arg) {
79     if (arg instanceof _Code)
80         code.push(...arg._items);
81     else if (arg instanceof Name)
82         code.push(arg);
83     else
84         code.push(interpolate(arg));
85 }
86 exports.addCodeArg = addCodeArg;
87 function optimize(expr) {
88     let i = 1;
89     while (i < expr.length - 1) {
90         if (expr[i] === plus) {
91             const res = mergeExprItems(expr[i - 1], expr[i + 1]);
92             if (res !== undefined) {
93                 expr.splice(i - 1, 3, res);
94                 continue;
95             }
96             expr[i++] = "+";
97         }
98         i++;
99     }
100 }
101 function mergeExprItems(a, b) {
102     if (b === '""')
103         return a;
104     if (a === '""')
105         return b;
106     if (typeof a == "string") {
107         if (b instanceof Name || a[a.length - 1] !== '"')
108             return;
109         if (typeof b != "string")
110             return `${a.slice(0, -1)}${b}"`;
111         if (b[0] === '"')
112             return a.slice(0, -1) + b.slice(1);
113         return;
114     }
115     if (typeof b == "string" && b[0] === '"' && !(a instanceof Name))
116         return `"${a}${b.slice(1)}`;
117     return;
118 }
119 function strConcat(c1, c2) {
120     return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str `${c1}${c2}`;
121 }
122 exports.strConcat = strConcat;
123 // TODO do not allow arrays here
124 function interpolate(x) {
125     return typeof x == "number" || typeof x == "boolean" || x === null
126         ? x
127         : safeStringify(Array.isArray(x) ? x.join(",") : x);
128 }
129 function stringify(x) {
130     return new _Code(safeStringify(x));
131 }
132 exports.stringify = stringify;
133 function safeStringify(x) {
134     return JSON.stringify(x)
135         .replace(/\u2028/g, "\\u2028")
136         .replace(/\u2029/g, "\\u2029");
137 }
138 exports.safeStringify = safeStringify;
139 function getProperty(key) {
140     return typeof key == "string" && exports.IDENTIFIER.test(key) ? new _Code(`.${key}`) : _ `[${key}]`;
141 }
142 exports.getProperty = getProperty;
143 function regexpCode(rx) {
144     return new _Code(rx.toString());
145 }
146 exports.regexpCode = regexpCode;
147 //# sourceMappingURL=code.js.map