2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.ValueScope = exports.ValueScopeName = exports.Scope = exports.varKinds = exports.UsedValueState = void 0;
4 const code_1 = require("./code");
5 class ValueError extends Error {
7 super(`CodeGen: "code" for ${name} not defined`);
8 this.value = name.value;
12 (function (UsedValueState) {
13 UsedValueState[UsedValueState["Started"] = 0] = "Started";
14 UsedValueState[UsedValueState["Completed"] = 1] = "Completed";
15 })(UsedValueState = exports.UsedValueState || (exports.UsedValueState = {}));
17 const: new code_1.Name("const"),
18 let: new code_1.Name("let"),
19 var: new code_1.Name("var"),
22 constructor({ prefixes, parent } = {}) {
24 this._prefixes = prefixes;
25 this._parent = parent;
27 toName(nameOrPrefix) {
28 return nameOrPrefix instanceof code_1.Name ? nameOrPrefix : this.name(nameOrPrefix);
31 return new code_1.Name(this._newName(prefix));
34 const ng = this._names[prefix] || this._nameGroup(prefix);
35 return `${prefix}${ng.index++}`;
39 if (((_b = (_a = this._parent) === null || _a === void 0 ? void 0 : _a._prefixes) === null || _b === void 0 ? void 0 : _b.has(prefix)) || (this._prefixes && !this._prefixes.has(prefix))) {
40 throw new Error(`CodeGen: prefix "${prefix}" is not allowed in this scope`);
42 return (this._names[prefix] = { prefix, index: 0 });
45 exports.Scope = Scope;
46 class ValueScopeName extends code_1.Name {
47 constructor(prefix, nameStr) {
51 setValue(value, { property, itemIndex }) {
53 this.scopePath = (0, code_1._) `.${new code_1.Name(property)}[${itemIndex}]`;
56 exports.ValueScopeName = ValueScopeName;
57 const line = (0, code_1._) `\n`;
58 class ValueScope extends Scope {
62 this._scope = opts.scope;
63 this.opts = { ...opts, _n: opts.lines ? line : code_1.nil };
69 return new ValueScopeName(prefix, this._newName(prefix));
71 value(nameOrPrefix, value) {
73 if (value.ref === undefined)
74 throw new Error("CodeGen: ref must be passed in value");
75 const name = this.toName(nameOrPrefix);
76 const { prefix } = name;
77 const valueKey = (_a = value.key) !== null && _a !== void 0 ? _a : value.ref;
78 let vs = this._values[prefix];
80 const _name = vs.get(valueKey);
85 vs = this._values[prefix] = new Map();
87 vs.set(valueKey, name);
88 const s = this._scope[prefix] || (this._scope[prefix] = []);
89 const itemIndex = s.length;
90 s[itemIndex] = value.ref;
91 name.setValue(value, { property: prefix, itemIndex });
94 getValue(prefix, keyOrRef) {
95 const vs = this._values[prefix];
98 return vs.get(keyOrRef);
100 scopeRefs(scopeName, values = this._values) {
101 return this._reduceValues(values, (name) => {
102 if (name.scopePath === undefined)
103 throw new Error(`CodeGen: name "${name}" has no value`);
104 return (0, code_1._) `${scopeName}${name.scopePath}`;
107 scopeCode(values = this._values, usedValues, getCode) {
108 return this._reduceValues(values, (name) => {
109 if (name.value === undefined)
110 throw new Error(`CodeGen: name "${name}" has no value`);
111 return name.value.code;
112 }, usedValues, getCode);
114 _reduceValues(values, valueCode, usedValues = {}, getCode) {
115 let code = code_1.nil;
116 for (const prefix in values) {
117 const vs = values[prefix];
120 const nameSet = (usedValues[prefix] = usedValues[prefix] || new Map());
121 vs.forEach((name) => {
122 if (nameSet.has(name))
124 nameSet.set(name, UsedValueState.Started);
125 let c = valueCode(name);
127 const def = this.opts.es5 ? exports.varKinds.var : exports.varKinds.const;
128 code = (0, code_1._) `${code}${def} ${name} = ${c};${this.opts._n}`;
130 else if ((c = getCode === null || getCode === void 0 ? void 0 : getCode(name))) {
131 code = (0, code_1._) `${code}${c}${this.opts._n}`;
134 throw new ValueError(name);
136 nameSet.set(name, UsedValueState.Completed);
142 exports.ValueScope = ValueScope;
143 //# sourceMappingURL=scope.js.map