@unframework.dev/target-angular 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/rules.js ADDED
@@ -0,0 +1,332 @@
1
+ import { narrowedWhole } from "./narrowing.js";
2
+ import { outputName } from "./outputs.js";
3
+ import { formOf, isSignal, memberOf, moduleMember, slotQuery, unreachable } from "./plan.js";
4
+ import { mapCode, parseCodeSource, parseExpression, writtenValue } from "@unframework.dev/codegen";
5
+ /**
6
+ * The rules for the class's code: every member through `this.`, a signal read by calling it,
7
+ * a template ref's element through `nativeElement` (`undefined` before the view renders it, as
8
+ * `viewChild` gives), a state written through `set` or `update`, an event emitted through its
9
+ * output, and `nextTick()` through the class's own helper (`nextTick` names it).
10
+ */
11
+ function classRules(plan, nextTick) {
12
+ return {
13
+ binding: (reference, binding, written) => wholeRef(plan, binding, written) ? `this.${memberOf(plan, binding)}` : classRead(plan, binding, narrowedWhole(reference), written),
14
+ slot: (reference) => slotPresence(plan, reference.slot),
15
+ write: (write, binding, parts) => {
16
+ const form = formOf(plan, binding);
17
+ switch (form) {
18
+ case "signal":
19
+ case "linked":
20
+ case "model":
21
+ if (write.operator === "=") return `this.${binding.name}.set(${parts.value})`;
22
+ return !parts.target.endsWith("!") && (parts.value === void 0 || readsOnlySignals(parts.value)) ? `this.${binding.name}.update((${binding.name}) => ${writtenValue(write, {
23
+ ...parts,
24
+ target: binding.name
25
+ })})` : `this.${binding.name}.set(${writtenValue(write, parts)})`;
26
+ case "field": return;
27
+ case "input":
28
+ case "computed":
29
+ case "once":
30
+ case "value":
31
+ case "query":
32
+ case "method":
33
+ case "arrow":
34
+ case "loop":
35
+ case "emit":
36
+ case "context":
37
+ case "instance":
38
+ case "component":
39
+ case "module":
40
+ case "result":
41
+ case "resultValue": throw new Error(`\`${binding.name}\` is written, which the analyser rejects (UF2011).`);
42
+ default: return unreachable(form);
43
+ }
44
+ },
45
+ emit: (emit, _binding, parts) => emitCall(`this.${outputName(emit.event)}`, eventOf(plan, emit), parts.arguments),
46
+ api: () => {
47
+ if (nextTick === void 0) throw new Error("`nextTick` is called, and no helper declared.");
48
+ return `this.${nextTick}`;
49
+ }
50
+ };
51
+ }
52
+ /**
53
+ * Whether class code reads only literals and the component's signals and methods (`this.step()`,
54
+ * `this.picked()!.id`): no name, which TypeScript may narrow where the code is and not in a
55
+ * callback, and no field (`this.last`).
56
+ */
57
+ function readsOnlySignals(code) {
58
+ return closed(parseExpression(code));
59
+ }
60
+ /** Whether a node of class code reads only literals, signals and methods (see `readsOnlySignals`). */
61
+ function closed(value, parent, key = "") {
62
+ if (Array.isArray(value)) return value.every((item) => closed(item, parent, key));
63
+ if (!isNode(value)) return true;
64
+ switch (value.type) {
65
+ case "Identifier": return (key === "property" || key === "key") && parent?.computed === false;
66
+ case "ThisExpression": return false;
67
+ case "CallExpression": {
68
+ const callee = value.callee;
69
+ return (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "ThisExpression" || closed(callee, value, "callee")) && closed(value.arguments, value);
70
+ }
71
+ default: return Object.entries(value).every(([child, node]) => child === "parent" || typeof node !== "object" || closed(node, value, child));
72
+ }
73
+ }
74
+ /**
75
+ * Class code with a template ref's element read as Angular code reads it. The source tests the
76
+ * ref's value (`field.value?.focus()`), which the class reads as `this.field()?.nativeElement`,
77
+ * whose element is never nullish once the query has one (`this.field()?.nativeElement.focus()`).
78
+ * And the source's value is `T | null`, where the query's read is `T | undefined`: so a read used
79
+ * as a value (stored, passed, returned, compared) is `this.field()?.nativeElement ?? null`, which
80
+ * type-checks wherever the source's does and compares with `null` as it does. A read that is only
81
+ * tested (`if (…)`, `!…`, the left side of `&&`, `||` or `??`, `… == null`), asserted (`!`) or
82
+ * read through (`.focus()`) needs none. Only code between literals changes: `this` is no name the
83
+ * source may use, so the text is the rules' own.
84
+ */
85
+ function tidied(code, kind) {
86
+ if (!code.includes("?.nativeElement")) return code;
87
+ return withNullElements(code.includes("?.nativeElement?.") ? mapCode(code, { other: (text) => text.replace(/(\(\)\?\.nativeElement)\?\./g, "$1.") }, kind) : code, kind);
88
+ }
89
+ /** Class code with each template ref's read used as a value ending `?? null` (see `tidied`). */
90
+ function withNullElements(code, kind) {
91
+ const edits = [];
92
+ const visit = (value, parent, key) => {
93
+ if (Array.isArray(value)) {
94
+ for (const item of value) visit(item, parent, key);
95
+ return;
96
+ }
97
+ if (!isNode(value)) return;
98
+ if (value.type === "ChainExpression" && isElementRead(value.expression)) {
99
+ if (parent === void 0 || !testsOnly(parent, key, value)) edits.push(value);
100
+ return;
101
+ }
102
+ for (const [child, node] of Object.entries(value)) if (child !== "type" && child !== "parent" && typeof node === "object") visit(node, value, child);
103
+ };
104
+ visit(parseCodeSource(code, kind).root, void 0, "");
105
+ let result = code;
106
+ for (const { start, end } of edits.toSorted((a, b) => b.start - a.start)) result = `${result.slice(0, start)}(${result.slice(start, end)} ?? null)${result.slice(end)}`;
107
+ return result;
108
+ }
109
+ /** `this.field()?.nativeElement`: a template ref's element, read in the class. */
110
+ function isElementRead(node) {
111
+ if (node.type !== "MemberExpression" || node.computed || !node.optional) return false;
112
+ const object = node.object;
113
+ if (node.property.name !== "nativeElement" || object.type !== "CallExpression") return false;
114
+ const callee = object.callee;
115
+ return object.arguments.length === 0 && callee.type === "MemberExpression" && callee.object.type === "ThisExpression";
116
+ }
117
+ /** Whether a value in `parent`'s `key` is only tested, asserted or read through, never kept. */
118
+ function testsOnly(parent, key, node) {
119
+ switch (parent.type) {
120
+ case "IfStatement":
121
+ case "WhileStatement":
122
+ case "DoWhileStatement":
123
+ case "ForStatement":
124
+ case "ConditionalExpression": return key === "test";
125
+ case "UnaryExpression": return parent.operator === "!";
126
+ case "LogicalExpression": return parent.left === node;
127
+ case "BinaryExpression": {
128
+ if (parent.operator !== "==" && parent.operator !== "!=") return false;
129
+ const other = parent.left === node ? parent.right : parent.left;
130
+ return other.type === "Literal" && other.value === null || other.type === "Identifier" && other.name === "undefined";
131
+ }
132
+ case "TSNonNullExpression":
133
+ case "MemberExpression":
134
+ case "CallExpression": return key === "expression" || key === "object" || key === "callee";
135
+ case "ExpressionStatement": return true;
136
+ default: return false;
137
+ }
138
+ }
139
+ function isNode(value) {
140
+ return typeof value === "object" && value !== null && typeof value.type === "string" && typeof value.start === "number";
141
+ }
142
+ /**
143
+ * A read of a binding in the class's code; `narrowed` where a condition around it shows it present,
144
+ * which TypeScript does not narrow a call by (./narrowing.ts): `this.selected()!`, and a template
145
+ * ref's `this.field()!.nativeElement` (an assertion inside `?.nativeElement` would leave the chain
146
+ * optional).
147
+ */
148
+ function classRead(plan, binding, narrowed = false, written = "") {
149
+ const form = formOf(plan, binding);
150
+ switch (form) {
151
+ case "input":
152
+ case "signal":
153
+ case "linked":
154
+ case "computed":
155
+ case "once":
156
+ case "model":
157
+ case "instance": return `this.${binding.name}()${narrowed ? "!" : ""}`;
158
+ case "context": return `this.${binding.name}()${narrowed ? "!" : ""}${refMember(written)}`;
159
+ case "query": return `this.${binding.name}()${narrowed ? "!." : "?."}nativeElement`;
160
+ case "value":
161
+ case "method":
162
+ case "arrow":
163
+ case "field": return `this.${binding.name}`;
164
+ case "module": return binding.name;
165
+ case "result": return `this.${memberOf(plan, binding)}()${narrowed ? "!" : ""}`;
166
+ case "resultValue": return `this.${memberOf(plan, binding)}`;
167
+ case "loop":
168
+ case "emit":
169
+ case "component": throw new Error(`\`${binding.name}\` is read in the class, where it is not in scope.`);
170
+ default: return unreachable(form);
171
+ }
172
+ }
173
+ /**
174
+ * The rules for the template's expressions: what the template reads through a `@let` (a signal's
175
+ * value, which Angular's type checker narrows as TypeScript narrows a local) or as a member (a
176
+ * constant, a method) by its own name; in a list's `track`, which may read only its item, its
177
+ * index and the component's members (NG8009), a signal from the class.
178
+ */
179
+ function templateRules(plan, signals = /* @__PURE__ */ new Set()) {
180
+ const written = new Set([...signals].map(({ span }) => `${span.start}:${span.end}`));
181
+ return {
182
+ binding: (reference, binding, text, site) => written.has(`${reference.span.start}:${reference.span.end}`) ? `this.${binding.name}` : templateRead(plan, binding, site, text),
183
+ slot: (reference) => slotPresence(plan, reference.slot),
184
+ import: (reference) => plan.recipes.get(reference.name) ?? reference.name
185
+ };
186
+ }
187
+ function templateRead(plan, binding, site, written) {
188
+ const form = formOf(plan, binding);
189
+ switch (form) {
190
+ case "input":
191
+ case "signal":
192
+ case "linked":
193
+ case "computed":
194
+ case "once":
195
+ case "model": return site === "key" ? `this.${binding.name}()` : binding.name;
196
+ case "value":
197
+ case "method":
198
+ case "arrow":
199
+ case "module": return site === "key" ? `this.${moduleMember(plan, binding)}` : moduleMember(plan, binding);
200
+ case "result": return site === "key" ? `this.${memberOf(plan, binding)}()` : binding.name;
201
+ case "resultValue": return site === "key" ? `this.${memberOf(plan, binding)}` : memberOf(plan, binding);
202
+ case "context": return `${site === "key" ? `this.${binding.name}()` : binding.name}${refMember(written)}`;
203
+ case "loop": return binding.name;
204
+ case "component": return JSON.stringify(binding.name);
205
+ case "query":
206
+ case "field":
207
+ case "emit":
208
+ case "instance": throw new Error(`\`${binding.name}\` is read in the template, which the analyser rejects.`);
209
+ default: return unreachable(form);
210
+ }
211
+ }
212
+ /**
213
+ * The rules for a listener's template statement (`(click)="pick(contact, index)"`): an input and
214
+ * a list's item or index through the template's own names, a state through the class (`this.x()`:
215
+ * a `@let` holds the value of the last render, which another listener of the same event may
216
+ * already have changed), the handler's event as `$event`, an event through its output (through
217
+ * `this.` where a list's variable shadows it or JavaScript reads its name as a keyword).
218
+ */
219
+ function statementRules(plan) {
220
+ return {
221
+ binding: (reference, binding, written) => statementRead(plan, binding, narrowedWhole(reference), written),
222
+ event: (reference) => `$event.${reference.member}`,
223
+ emit: (emit, _binding, parts) => {
224
+ const name = outputName(emit.event);
225
+ return emitCall(plan.loopNames.has(name) || !readsAsName(name) ? `this.${name}` : name, eventOf(plan, emit), parts.arguments);
226
+ }
227
+ };
228
+ }
229
+ /**
230
+ * Whether JavaScript reads a name as a name where it starts an expression (`picked.emit()`), not a
231
+ * keyword (`delete`, `new`): an event may take a reserved word that Angular's expression language
232
+ * reads as a name (UF2008 rejects its own keywords), but the printer handles a template statement
233
+ * as JavaScript, so such an output is read through `this.` (`this.delete.emit(id)`), which both
234
+ * read as the member.
235
+ */
236
+ function readsAsName(name) {
237
+ let known = NAMES.get(name);
238
+ if (known === void 0) {
239
+ try {
240
+ parseExpression(`${name}.emit()`);
241
+ known = true;
242
+ } catch {
243
+ known = false;
244
+ }
245
+ NAMES.set(name, known);
246
+ }
247
+ return known;
248
+ }
249
+ const NAMES = /* @__PURE__ */ new Map();
250
+ /**
251
+ * A read of a binding in a template statement; `narrowed` as in {@link classRead}, which an
252
+ * input, read through the template's own name, needs not.
253
+ */
254
+ function statementRead(plan, binding, narrowed = false, written = "") {
255
+ const form = formOf(plan, binding);
256
+ switch (form) {
257
+ case "input":
258
+ case "value":
259
+ case "method":
260
+ case "arrow":
261
+ case "loop": return binding.name;
262
+ case "module": return moduleMember(plan, binding);
263
+ case "resultValue": return memberOf(plan, binding);
264
+ case "result": return `this.${memberOf(plan, binding)}()${narrowed ? "!" : ""}`;
265
+ case "signal":
266
+ case "linked":
267
+ case "computed":
268
+ case "once":
269
+ case "model": return `this.${binding.name}()${narrowed ? "!" : ""}`;
270
+ case "context": return `this.${binding.name}()${narrowed ? "!" : ""}${refMember(written)}`;
271
+ case "query":
272
+ case "field":
273
+ case "emit":
274
+ case "instance":
275
+ case "component": throw new Error(`\`${binding.name}\` is read in a template statement.`);
276
+ default: return unreachable(form);
277
+ }
278
+ }
279
+ /**
280
+ * Whether a reference reads a ref itself rather than its value (`provide(CountKey, count)`, a key
281
+ * of a ref, ADR-0054): the signal, which `refObject` wraps.
282
+ */
283
+ function wholeRef(plan, binding, written) {
284
+ const form = formOf(plan, binding);
285
+ return (form === "signal" || form === "linked" || form === "computed" || form === "model" || form === "result") && !written.endsWith(".value");
286
+ }
287
+ /**
288
+ * What an injected value's read keeps after its name: a key of a ref is read through its value
289
+ * (`count.value`), which `refObject`'s getter gives (ADR-0054); any other value is read whole.
290
+ */
291
+ function refMember(written) {
292
+ const dot = written.indexOf(".");
293
+ return dot === -1 ? "" : written.slice(dot);
294
+ }
295
+ /**
296
+ * A slot's presence: whether its directive's query holds a template. The default slot without
297
+ * props has no directive, and its presence is unsupported (`default-slot-presence`, UF4001, an
298
+ * error): its output, which nothing runs, reads `true`, so the file still compiles. The template
299
+ * reads the query from the class too, so no template variable of its name hides it.
300
+ */
301
+ function slotPresence(plan, slot) {
302
+ const props = plan.component.slots?.slots.find((each) => each.name === slot)?.props;
303
+ return slot === "default" && props === void 0 ? "true" : `this.${slotQuery(slot)}()`;
304
+ }
305
+ /** Whether the template reads a binding through a `@let`: a signal's value. */
306
+ function readThroughLet(plan, binding) {
307
+ return isSignal(formOf(plan, binding));
308
+ }
309
+ function eventOf(plan, emit) {
310
+ const event = plan.events.get(emit.event);
311
+ if (!event) throw new Error(`\`${emit.event}\` is emitted and not declared.`);
312
+ return event;
313
+ }
314
+ /**
315
+ * An emit through an output, which emits one value (ADR-0047): nothing for an event without
316
+ * members, the value for one required member, and a tuple of exactly the arguments given
317
+ * otherwise (the mount adapter spreads it back).
318
+ */
319
+ function emitCall(output, event, args) {
320
+ const [only, ...more] = event.parameters;
321
+ if (only === void 0) return `${output}.emit()`;
322
+ if (more.length === 0 && !only.optional) return `${output}.emit(${args[0] ?? ""})`;
323
+ return `${output}.emit([${args.join(", ")}])`;
324
+ }
325
+ /** An output's value type by the same rule: `void`, the member's type, or the named tuple. */
326
+ function outputType(event) {
327
+ const [only, ...more] = event.parameters;
328
+ if (only === void 0) return "void";
329
+ if (more.length === 0 && !only.optional) return only.type.code;
330
+ return `[${event.parameters.map(({ name, optional, type }) => `${name}${optional ? "?" : ""}: ${type.code}`).join(", ")}]`;
331
+ }
332
+ export { classRead, classRules, emitCall, outputType, readThroughLet, statementRead, statementRules, templateRules, tidied };
package/dist/seeded.js ADDED
@@ -0,0 +1,284 @@
1
+ import { bindingById } from "./plan.js";
2
+ import { arrayElement, defaultedType, propType, withoutNullish } from "./types.js";
3
+ import { parseExpression } from "@unframework.dev/codegen";
4
+ const BOOLEAN_OPERATORS = /* @__PURE__ */ new Set([
5
+ "==",
6
+ "!=",
7
+ "===",
8
+ "!==",
9
+ "<",
10
+ "<=",
11
+ ">",
12
+ ">=",
13
+ "in",
14
+ "instanceof"
15
+ ]);
16
+ /** The arithmetic operators but `+`: a number of numbers, a bigint of bigints. */
17
+ const ARITHMETIC = /* @__PURE__ */ new Set([
18
+ "-",
19
+ "*",
20
+ "/",
21
+ "%",
22
+ "**",
23
+ "&",
24
+ "|",
25
+ "^",
26
+ "<<",
27
+ ">>"
28
+ ]);
29
+ /** The string methods whose result is the type named, on a string. */
30
+ const STRING_METHODS = new Map([
31
+ ...[
32
+ "trim",
33
+ "trimStart",
34
+ "trimEnd",
35
+ "toUpperCase",
36
+ "toLowerCase",
37
+ "toLocaleUpperCase",
38
+ "toLocaleLowerCase",
39
+ "slice",
40
+ "substring",
41
+ "padStart",
42
+ "padEnd",
43
+ "repeat",
44
+ "replace",
45
+ "replaceAll",
46
+ "concat",
47
+ "charAt",
48
+ "normalize",
49
+ "toString"
50
+ ].map((name) => [name, "string"]),
51
+ ...[
52
+ "indexOf",
53
+ "lastIndexOf",
54
+ "charCodeAt",
55
+ "localeCompare",
56
+ "search"
57
+ ].map((name) => [name, "number"]),
58
+ ...[
59
+ "includes",
60
+ "startsWith",
61
+ "endsWith"
62
+ ].map((name) => [name, "boolean"]),
63
+ ["split", "string[]"]
64
+ ]);
65
+ /** The number methods, all of which make a string. */
66
+ const NUMBER_METHODS = /* @__PURE__ */ new Set([
67
+ "toFixed",
68
+ "toPrecision",
69
+ "toExponential",
70
+ "toString",
71
+ "toLocaleString"
72
+ ]);
73
+ /**
74
+ * The array methods whose result the array's type fixes: by name, or, for `ARRAY`, a new array
75
+ * of the receiver's element type.
76
+ */
77
+ const ARRAY = "";
78
+ const ARRAY_METHODS = /* @__PURE__ */ new Map([
79
+ ["join", "string"],
80
+ ["includes", "boolean"],
81
+ ["some", "boolean"],
82
+ ["every", "boolean"],
83
+ ["indexOf", "number"],
84
+ ["lastIndexOf", "number"],
85
+ ["findIndex", "number"],
86
+ ["slice", ARRAY],
87
+ ["toSorted", ARRAY],
88
+ ["toReversed", ARRAY],
89
+ ["concat", ARRAY]
90
+ ]);
91
+ /** The calls of the pure globals whose result has one type, by callee. */
92
+ const GLOBAL_CALLS = new Map([
93
+ ...[
94
+ "Number",
95
+ "parseInt",
96
+ "parseFloat",
97
+ "Math.abs",
98
+ "Math.ceil",
99
+ "Math.floor",
100
+ "Math.max",
101
+ "Math.min",
102
+ "Math.pow",
103
+ "Math.round",
104
+ "Math.sign",
105
+ "Math.sqrt",
106
+ "Math.trunc",
107
+ "Math.log",
108
+ "Math.hypot"
109
+ ].map((name) => [name, "number"]),
110
+ ...["String", "JSON.stringify"].map((name) => [name, "string"]),
111
+ ...[
112
+ "Boolean",
113
+ "Array.isArray",
114
+ "Number.isInteger",
115
+ "Number.isFinite",
116
+ "Number.isNaN",
117
+ "Number.isSafeInteger"
118
+ ].map((name) => [name, "boolean"]),
119
+ ["Object.keys", "string[]"]
120
+ ]);
121
+ /**
122
+ * The type a `let` that starts with this value has, as TypeScript infers it (literals widened),
123
+ * when its text makes it certain; otherwise `undefined`.
124
+ */
125
+ function valueType(code, plan, declarations, seen = /* @__PURE__ */ new Set()) {
126
+ const text = (node) => code.code.slice(node.start, node.end);
127
+ const typeOf = (node) => {
128
+ switch (node.type) {
129
+ case "ParenthesizedExpression": return typeOf(node.expression);
130
+ case "Literal": switch (typeof node.value) {
131
+ case "string": return "string";
132
+ case "number": return "number";
133
+ case "boolean": return "boolean";
134
+ case "bigint": return "bigint";
135
+ default: return node.bigint === void 0 ? void 0 : "bigint";
136
+ }
137
+ case "TemplateLiteral": return "string";
138
+ case "UnaryExpression": switch (node.operator) {
139
+ case "!":
140
+ case "delete": return "boolean";
141
+ case "typeof": return "string";
142
+ case "+": return "number";
143
+ case "-":
144
+ case "~": return numeric(typeOf(node.argument));
145
+ default: return;
146
+ }
147
+ case "BinaryExpression": {
148
+ const operator = node.operator;
149
+ if (BOOLEAN_OPERATORS.has(operator)) return "boolean";
150
+ const left = typeOf(node.left);
151
+ const right = typeOf(node.right);
152
+ if (operator === "+") {
153
+ if (left === "string" || right === "string") return "string";
154
+ return left === right ? numeric(left) : void 0;
155
+ }
156
+ if (operator === ">>>") return left === "number" && right === "number" ? left : void 0;
157
+ return ARITHMETIC.has(operator) && left === right ? numeric(left) : void 0;
158
+ }
159
+ case "LogicalExpression": {
160
+ const left = typeOf(node.left);
161
+ const right = typeOf(node.right);
162
+ if (left === void 0 || right === void 0) return void 0;
163
+ const present = withoutNullish(left, declarations);
164
+ switch (node.operator) {
165
+ case "??": return union(present, right);
166
+ case "||": return [
167
+ "string",
168
+ "number",
169
+ "bigint"
170
+ ].includes(present) ? union(present, right) : void 0;
171
+ default: return;
172
+ }
173
+ }
174
+ case "ConditionalExpression": {
175
+ const consequent = typeOf(node.consequent);
176
+ const alternate = typeOf(node.alternate);
177
+ return consequent === void 0 || alternate === void 0 ? void 0 : union(consequent, alternate);
178
+ }
179
+ case "TSAsExpression":
180
+ case "TSTypeAssertion": {
181
+ const annotation = node.typeAnnotation;
182
+ return annotation.type === "TSTypeReference" && text(annotation) === "const" ? void 0 : text(annotation);
183
+ }
184
+ case "TSSatisfiesExpression": return typeOf(node.expression);
185
+ case "TSNonNullExpression": {
186
+ const type = typeOf(node.expression);
187
+ return type === void 0 ? void 0 : withoutNullish(type, declarations);
188
+ }
189
+ case "ArrayExpression": {
190
+ const elements = node.elements.map((element) => element === null ? void 0 : element.type === "SpreadElement" ? spreadElement(typeOf(element.argument)) : typeOf(element));
191
+ const [first] = elements;
192
+ return first !== void 0 && elements.every((element) => element === first) ? `${grouped(first)}[]` : void 0;
193
+ }
194
+ case "CallExpression": return callType(node);
195
+ case "Identifier":
196
+ case "MemberExpression": {
197
+ const start = code.span.start + node.start;
198
+ const end = code.span.start + node.end;
199
+ const reference = code.refs.find((each) => each.span.start === start && each.span.end === end);
200
+ if (reference?.kind === "Binding") return bindingType(reference.binding, plan, declarations, seen);
201
+ if (node.type !== "MemberExpression" || node.optional) return void 0;
202
+ const receiver = typeOf(node.object);
203
+ if (receiver === void 0) return void 0;
204
+ const property = node.property;
205
+ if (!node.computed) return property.name === "length" && (receiver === "string" || arrayElement(receiver) !== void 0) ? "number" : void 0;
206
+ const element = arrayElement(receiver);
207
+ return element !== void 0 && typeOf(property) === "number" ? `${grouped(element)} | undefined` : void 0;
208
+ }
209
+ default: return;
210
+ }
211
+ };
212
+ /** A call's type: a method of a string, a number or an array, or a pure global's. */
213
+ const callType = (node) => {
214
+ if (node.optional) return void 0;
215
+ const callee = node.callee;
216
+ const global = GLOBAL_CALLS.get(text(callee));
217
+ if (global !== void 0) {
218
+ const start = code.span.start + callee.start;
219
+ return code.refs.some((reference) => reference.kind === "Global" && reference.span.start === start) ? global : void 0;
220
+ }
221
+ if (callee.type !== "MemberExpression" || callee.computed || callee.optional) return void 0;
222
+ const receiver = typeOf(callee.object);
223
+ const method = callee.property.name;
224
+ if (receiver === "string") return STRING_METHODS.get(method);
225
+ if (receiver === "number") return NUMBER_METHODS.has(method) ? "string" : void 0;
226
+ const element = receiver === void 0 ? void 0 : arrayElement(receiver);
227
+ if (element === void 0) return void 0;
228
+ const result = ARRAY_METHODS.get(method);
229
+ return result === ARRAY ? `${grouped(element)}[]` : result;
230
+ };
231
+ return typeOf(parseExpression(code.code));
232
+ }
233
+ /** `number` or `bigint` as it is, or none. */
234
+ function numeric(type) {
235
+ return type === "number" || type === "bigint" ? type : void 0;
236
+ }
237
+ /** The type of what a spread of a value of this type adds to an array: its element's. */
238
+ function spreadElement(type) {
239
+ return type === void 0 ? void 0 : arrayElement(type);
240
+ }
241
+ /** Two types as one: the same type once, or their union. */
242
+ function union(first, second) {
243
+ return same(first, second) ? first : `${parenthesised(first)} | ${parenthesised(second)}`;
244
+ }
245
+ /** Whether two types are written alike, whitespace apart. */
246
+ function same(first, second) {
247
+ return first.replace(/\s+/g, "") === second.replace(/\s+/g, "");
248
+ }
249
+ /** A type in parentheses where `[]` would bind into it (a union, a function or a conditional). */
250
+ function grouped(type) {
251
+ return /[|&]|=>|\bextends\b|^\s*new\b|^\s*keyof\b|^\s*readonly\b/.test(type) ? `(${type})` : type;
252
+ }
253
+ /** The type of a binding's value, when its declaration makes it certain. */
254
+ function bindingType(id, plan, declarations, seen) {
255
+ if (seen.has(id)) return void 0;
256
+ const next = /* @__PURE__ */ new Set([...seen, id]);
257
+ const binding = bindingById(plan, id);
258
+ const { component } = plan;
259
+ if (binding.kind === "prop") {
260
+ const prop = component.props.find((each) => each.binding === id);
261
+ if (prop === void 0) return void 0;
262
+ const type = propType(prop, component);
263
+ if (!prop.optional) return type;
264
+ return prop.default === void 0 ? `${parenthesised(type)} | undefined` : defaultedType(prop, component, declarations);
265
+ }
266
+ const item = component.setup.find((each) => "binding" in each && each.binding === id);
267
+ switch (item?.kind) {
268
+ case "State":
269
+ if (item.type) return item.initial ? item.type.code : `${parenthesised(item.type.code)} | undefined`;
270
+ return item.initial && valueType(item.initial, plan, declarations, next);
271
+ case "Derived":
272
+ if (item.type) return item.type.code;
273
+ return item.getter.expression ? valueType(item.getter.body, plan, declarations, next) : void 0;
274
+ case "Const": return item.type?.code ?? valueType(item.value, plan, declarations, next);
275
+ case "Variable": return item.type?.code ?? (item.initial && valueType(item.initial, plan, declarations, next));
276
+ case "Id": return "string";
277
+ default: return;
278
+ }
279
+ }
280
+ /** A type in parentheses where `| undefined` would bind into it (a function or conditional type). */
281
+ function parenthesised(type) {
282
+ return /=>|\bextends\b|^\s*new\b/.test(type) ? `(${type})` : type;
283
+ }
284
+ export { parenthesised, valueType };