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;
6 exports._CodeOrName = _CodeOrName;
7 exports.IDENTIFIER = /^[a-z$_][a-z$_0-9]*$/i;
8 class Name extends _CodeOrName {
11 if (!exports.IDENTIFIER.test(s))
12 throw new Error("CodeGen: name must be a valid identifier");
22 return { [this.str]: 1 };
26 class _Code extends _CodeOrName {
29 this._items = typeof code === "string" ? [code] : code;
35 if (this._items.length > 1)
37 const item = this._items[0];
38 return item === "" || item === '""';
42 return ((_a = this._str) !== null && _a !== void 0 ? _a : (this._str = this._items.reduce((s, c) => `${s}${c}`, "")));
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;
53 exports._Code = _Code;
54 exports.nil = new _Code("");
55 function _(strs, ...args) {
56 const code = [strs[0]];
58 while (i < args.length) {
59 addCodeArg(code, args[i]);
62 return new _Code(code);
65 const plus = new _Code("+");
66 function str(strs, ...args) {
67 const expr = [safeStringify(strs[0])];
69 while (i < args.length) {
71 addCodeArg(expr, args[i]);
72 expr.push(plus, safeStringify(strs[++i]));
75 return new _Code(expr);
78 function addCodeArg(code, arg) {
79 if (arg instanceof _Code)
80 code.push(...arg._items);
81 else if (arg instanceof Name)
84 code.push(interpolate(arg));
86 exports.addCodeArg = addCodeArg;
87 function optimize(expr) {
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);
101 function mergeExprItems(a, b) {
106 if (typeof a == "string") {
107 if (b instanceof Name || a[a.length - 1] !== '"')
109 if (typeof b != "string")
110 return `${a.slice(0, -1)}${b}"`;
112 return a.slice(0, -1) + b.slice(1);
115 if (typeof b == "string" && b[0] === '"' && !(a instanceof Name))
116 return `"${a}${b.slice(1)}`;
119 function strConcat(c1, c2) {
120 return c2.emptyStr() ? c1 : c1.emptyStr() ? c2 : str `${c1}${c2}`;
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
127 : safeStringify(Array.isArray(x) ? x.join(",") : x);
129 function stringify(x) {
130 return new _Code(safeStringify(x));
132 exports.stringify = stringify;
133 function safeStringify(x) {
134 return JSON.stringify(x)
135 .replace(/\u2028/g, "\\u2028")
136 .replace(/\u2029/g, "\\u2029");
138 exports.safeStringify = safeStringify;
139 function getProperty(key) {
140 return typeof key == "string" && exports.IDENTIFIER.test(key) ? new _Code(`.${key}`) : _ `[${key}]`;
142 exports.getProperty = getProperty;
143 function regexpCode(rx) {
144 return new _Code(rx.toString());
146 exports.regexpCode = regexpCode;
147 //# sourceMappingURL=code.js.map