@openelement/compiler 1.0.0-alpha.15 → 1.0.0-alpha.16

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.
Files changed (51) hide show
  1. package/package.json +3 -2
  2. package/src/compiler.d.ts +8 -6
  3. package/src/compiler.js +7 -6
  4. package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +5 -30
  5. package/src/internal/compiler/compiled-artifact-class-candidates.js +18 -32
  6. package/src/internal/compiler/compiled-artifact-style-requests.d.ts +9 -7
  7. package/src/internal/compiler/compiled-artifact-style-requests.js +9 -7
  8. package/src/internal/compiler/plugin.d.ts +31 -50
  9. package/src/internal/compiler/plugin.js +41 -108
  10. package/src/internal/compiler/semantic-core/analyze-module.d.ts +1 -45
  11. package/src/internal/compiler/semantic-core/analyze-module.js +10 -405
  12. package/src/internal/compiler/semantic-core/attribute-classification.js +92 -0
  13. package/src/internal/compiler/semantic-core/compiler-diagnostics.js +2 -16
  14. package/src/internal/compiler/semantic-core/expression-lowering.js +469 -0
  15. package/src/internal/compiler/semantic-core/expression-registry.js +165 -0
  16. package/src/internal/compiler/semantic-core/function-frontend.js +1229 -0
  17. package/src/internal/compiler/semantic-core/island-policy-statement.js +52 -0
  18. package/src/internal/compiler/semantic-core/jsx-module-id.d.ts +4 -0
  19. package/src/internal/compiler/semantic-core/jsx-module-id.js +7 -0
  20. package/src/internal/compiler/semantic-core/lower-program.d.ts +51 -0
  21. package/src/internal/compiler/semantic-core/lower-program.js +186 -90
  22. package/src/internal/compiler/semantic-core/module-analysis.d.ts +28 -5
  23. package/src/internal/compiler/semantic-core/module-analysis.js +16 -17
  24. package/src/internal/compiler/semantic-core/parse-module.js +16 -16
  25. package/src/internal/compiler/semantic-core/scope-resolution.d.ts +208 -0
  26. package/src/internal/compiler/semantic-core/scope-resolution.js +1121 -0
  27. package/src/internal/compiler/semantic-core/server-component.d.ts +22 -7
  28. package/src/internal/compiler/semantic-core/server-component.js +163 -57
  29. package/src/internal/compiler/semantic-core/type-erasure.js +156 -0
  30. package/src/internal/compiler/semantic-ir/generated-element/compile.d.ts +61 -0
  31. package/src/internal/compiler/semantic-ir/generated-element/compile.js +137 -0
  32. package/src/internal/compiler/semantic-ir/generated-element/contract.d.ts +201 -0
  33. package/src/internal/compiler/semantic-ir/generated-element/contract.js +147 -0
  34. package/src/internal/compiler/semantic-ir/generated-element/emit.js +452 -0
  35. package/src/internal/compiler/semantic-ir/generated-element/site.js +1195 -0
  36. package/src/internal/compiler/semantic-ir/generated-leaf/claim-emission.js +1372 -0
  37. package/src/internal/compiler/semantic-ir/generated-leaf/codegen-literals.js +31 -0
  38. package/src/internal/compiler/semantic-ir/generated-leaf/component-emission.js +647 -0
  39. package/src/internal/compiler/semantic-ir/generated-leaf/emission.js +388 -0
  40. package/src/internal/compiler/semantic-ir/generated-leaf/generate.js +395 -0
  41. package/src/internal/compiler/semantic-ir/generated-leaf/instance-emission.js +865 -0
  42. package/src/internal/compiler/semantic-ir/generated-leaf/mount-emission.js +960 -0
  43. package/src/internal/compiler/semantic-ir/generated-leaf/server-emission.js +1011 -0
  44. package/src/internal/compiler/semantic-ir/generated-leaf/structure.js +714 -0
  45. package/src/internal/compiler/style-requests.d.ts +0 -8
  46. package/src/internal/compiler/style-requests.js +1 -14
  47. package/src/internal/compiler/semantic-core/compile.d.ts +0 -47
  48. package/src/internal/compiler/semantic-core/compile.js +0 -100
  49. package/src/internal/compiler/semantic-core/emit-module.js +0 -317
  50. package/src/internal/compiler/semantic-core/emit-program.js +0 -141
  51. package/src/internal/compiler/semantic-core/style-imports.js +0 -87
@@ -0,0 +1,1121 @@
1
+ import ts from "typescript";
2
+ //#region src/internal/compiler/semantic-core/scope-resolution.ts
3
+ /**
4
+ * M03-I-1 — lexical scope resolution (Alpha16a, compiler-private).
5
+ *
6
+ * Design authority: `docs/architecture/m03-scope-resolution.md` (M03-A). This
7
+ * module is **not** exported from `packages/compiler/src/compiler.ts` or any
8
+ * package subpath, and it is not wired into the production compile path: A1 is
9
+ * the resolver + its contract + its tests. A4 owns the production frontend.
10
+ *
11
+ * What it implements, mapped to the design sections:
12
+ *
13
+ * - §3 binding identity split from classification: `ScopeId`, `BindingId`,
14
+ * `DeclarationKind`, `ValueKind`, `BindingOrigin`, `DeclarationRecord`,
15
+ * `ResolvedBinding`; declaration merging and redeclaration diagnostics.
16
+ * - §4 lexical scope semantics: `var` hoisting, block scoping, catch, `for`
17
+ * per-iteration bindings, switch sharing one scope, class outer/inner
18
+ * bindings, named function/class expression self-name binding, parameter
19
+ * ordering, and the coded fail-closed diagnostics for constructs outside the
20
+ * model.
21
+ * - §4.1 TDZ is a **runtime state**: a read always binds lexically; a static
22
+ * TDZ diagnostic is emitted only inside the provable bound documented below.
23
+ * - §5 four independent axes: `reads` (which bindings an expression reads),
24
+ * `captures` (a read that crosses a function boundary), `dependencies` (the
25
+ * reads a tracked slot must subscribe to, selected by mode AND value kind),
26
+ * and `mode`.
27
+ * - §6/§6.1 host `ImportClassifier`: syntax is authoritative for the module's
28
+ * own declarations; a host-supplied classifier answers module-symbol
29
+ * questions (`classifySymbol` → `classifyCall` / `classifyMember`); no
30
+ * `ts.Program` and no type checker is used as the mechanism.
31
+ *
32
+ * Design-bound decisions (recorded here so review can check them):
33
+ *
34
+ * 1. **Span/diagnostic types are declared in this module**, with the same
35
+ * field shape as `semantic-ir/types.ts`'s `IrSpan`/`IrDiagnostic`. Reusing
36
+ * the spike's types would make `semantic-core` depend on a module excluded
37
+ * from the published package; the adapter that feeds the IR passes these
38
+ * records through field-for-field.
39
+ * 2. **TDZ provable bound** (the only source of `OEIR_TDZ_READ`): the read is
40
+ * in the same statement-list scope as the `let`/`const`/`class` declaration
41
+ * (no nested block, no nested function), the read is not inside a
42
+ * control-flow construct or a nested class (`if`/loop/`switch`/`try`/
43
+ * conditional/logical short-circuit/label/class), and the read's
44
+ * (statement, declarator) slot is at or before the declaring
45
+ * (statement, declarator) slot. Everything else — closures created before
46
+ * initialization, calls whose ordering cannot be proven, reads in nested
47
+ * scopes — stays non-diagnostic (§4.1).
48
+ * 3. **Capture rule**: a read is a capture when it sits inside more function
49
+ * boundaries than the declaration (§5 "does the read cross a function
50
+ * boundary"). Builtin globals are never captures. Module-scope reads from
51
+ * inside a component closure therefore count as captures; a consumer that
52
+ * only wants component-local captures filters on `origin.form`.
53
+ * 4. **Builtin globals** are a small, documented ECMAScript allowlist. A name
54
+ * outside it is unresolved (`OEIR_UNRESOLVED_BINDING`) instead of being
55
+ * silently treated as a local (§4). Hosts extend the list through
56
+ * `ResolveModuleOptions.globals`; the resolver itself never guesses from a
57
+ * spelling and never lets ambient state in.
58
+ * 5. **Provisional diagnostic codes** the design names behaviour for but does
59
+ * not spell: `OEIR_TDZ_READ` (provable TDZ), `OEIR_TYPE_ONLY_VALUE_READ`,
60
+ * `OEIR_UNCLASSIFIED_BINDING` (tracked slot whose binding kind the host did
61
+ * not classify), `OEIR_UNSUPPORTED_DECLARATION` (namespace/enum/import=),
62
+ * `OEIR_UNSUPPORTED_LABEL`.
63
+ * 6. **Value-kind defaults for local declarations**: a local binding is
64
+ * `plain` by syntax unless the §6.1 factory chain classifies its
65
+ * initializer, or its initializer is a plain identifier alias of an already
66
+ * classified binding (kind and import provenance are copied). A call whose
67
+ * callee is a local/global value stays `plain` — the resolver never guesses
68
+ * a factory from a spelling; a call whose callee is a module symbol the host
69
+ * could not classify is `unknown`/`unresolved` and fails closed only where a
70
+ * kind is required. This is a syntax resolver, not a checker: a local helper
71
+ * that returns a signal is invisible to it by construction.
72
+ * 7. **`ResolvedBinding.role`** carries the §6 `SymbolRole` next to `value`
73
+ * (`ValueKind`) so a factory import is known to be a factory while it is
74
+ * *not* the reactive value the factory produces. §3's field list does not
75
+ * name the role, but §6 makes it an observable classification result.
76
+ * 8. **Builtin globals are not part of `bindings`**: they are synthesized on
77
+ * first read (`origin: 'builtin'`, no declaration records, zero span) and
78
+ * `bindings` lists authored declarations only. The §3 invariant "a binding
79
+ * has at least one declaration record" therefore applies to authored
80
+ * bindings.
81
+ * 9. **Two diagnostic surfaces, one per pass**: `resolveModule(...).diagnostics`
82
+ * carries declaration/import/unsupported-construct findings (nothing that
83
+ * requires deciding whether a read resolves); `resolveExpression(...)`
84
+ * returns the per-expression diagnostics (unresolved reads, provable TDZ,
85
+ * type-only value reads, unclassified tracked slots). The production
86
+ * frontend resolves exactly the expressions it lowers, so it sees every
87
+ * diagnostic that applies to authored code it accepts, and no consumer is
88
+ * required to walk the whole module to get them.
89
+ */
90
+ /** Stable key for a `BindingId` (identity is never serialized, lookups need one). */
91
+ function bindingKey(id) {
92
+ return `${id.scope.moduleId}#${id.scope.path}#${id.name}#${id.declarationOrder}`;
93
+ }
94
+ /** The default ECMAScript global allowlist (hosts extend it via `globals`). */
95
+ const BUILTIN_GLOBALS = /* @__PURE__ */ new Set([
96
+ "undefined",
97
+ "NaN",
98
+ "Infinity",
99
+ "globalThis",
100
+ "Object",
101
+ "Function",
102
+ "Boolean",
103
+ "Symbol",
104
+ "Error",
105
+ "AggregateError",
106
+ "EvalError",
107
+ "RangeError",
108
+ "ReferenceError",
109
+ "SyntaxError",
110
+ "TypeError",
111
+ "URIError",
112
+ "Number",
113
+ "BigInt",
114
+ "Math",
115
+ "Date",
116
+ "String",
117
+ "RegExp",
118
+ "Array",
119
+ "Int8Array",
120
+ "Uint8Array",
121
+ "Uint8ClampedArray",
122
+ "Int16Array",
123
+ "Uint16Array",
124
+ "Int32Array",
125
+ "Uint32Array",
126
+ "Float32Array",
127
+ "Float64Array",
128
+ "BigInt64Array",
129
+ "BigUint64Array",
130
+ "Map",
131
+ "Set",
132
+ "WeakMap",
133
+ "WeakSet",
134
+ "WeakRef",
135
+ "FinalizationRegistry",
136
+ "ArrayBuffer",
137
+ "SharedArrayBuffer",
138
+ "DataView",
139
+ "Atomics",
140
+ "JSON",
141
+ "Promise",
142
+ "Reflect",
143
+ "Proxy",
144
+ "Intl",
145
+ "WebAssembly",
146
+ "parseInt",
147
+ "parseFloat",
148
+ "isNaN",
149
+ "isFinite",
150
+ "decodeURI",
151
+ "decodeURIComponent",
152
+ "encodeURI",
153
+ "encodeURIComponent",
154
+ "escape",
155
+ "unescape"
156
+ ]);
157
+ const SCOPE_FUNCTION_KINDS = /* @__PURE__ */ new Set([
158
+ ts.SyntaxKind.FunctionDeclaration,
159
+ ts.SyntaxKind.FunctionExpression,
160
+ ts.SyntaxKind.ArrowFunction,
161
+ ts.SyntaxKind.MethodDeclaration,
162
+ ts.SyntaxKind.Constructor,
163
+ ts.SyntaxKind.GetAccessor,
164
+ ts.SyntaxKind.SetAccessor
165
+ ]);
166
+ function isScopeFunctionNode(node) {
167
+ return SCOPE_FUNCTION_KINDS.has(node.kind);
168
+ }
169
+ const LEXICAL_KINDS = /* @__PURE__ */ new Set([
170
+ "let",
171
+ "const",
172
+ "class"
173
+ ]);
174
+ const REACTIVE_KINDS = /* @__PURE__ */ new Set([
175
+ "signal",
176
+ "computed",
177
+ "prop"
178
+ ]);
179
+ /** The tracked-slot modes of §5: the binding kind is required there (§6.1). */
180
+ const TRACKED_MODES = /* @__PURE__ */ new Set([
181
+ "inline",
182
+ "derived",
183
+ "per-entry"
184
+ ]);
185
+ function emptySpan(file) {
186
+ return {
187
+ file,
188
+ start: 0,
189
+ end: 0,
190
+ line: 1,
191
+ column: 1
192
+ };
193
+ }
194
+ function unwrapExpression(node) {
195
+ let current = node;
196
+ for (;;) {
197
+ if (ts.isParenthesizedExpression(current) || ts.isAsExpression(current) || ts.isSatisfiesExpression(current) || ts.isNonNullExpression(current) || ts.isTypeAssertionExpression(current)) {
198
+ current = current.expression;
199
+ continue;
200
+ }
201
+ return current;
202
+ }
203
+ }
204
+ function isVarDeclarationList(list) {
205
+ return (list.flags & ts.NodeFlags.BlockScoped) === 0;
206
+ }
207
+ function lexicalKindOf(list) {
208
+ return (list.flags & ts.NodeFlags.Const) !== 0 ? "const" : "let";
209
+ }
210
+ function isNewTarget(node) {
211
+ return ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.NewKeyword && node.name.text === "target";
212
+ }
213
+ var ScopeResolver = class {
214
+ sf;
215
+ file;
216
+ options;
217
+ diagnostics = [];
218
+ scopes = [];
219
+ authoredBindings = [];
220
+ bindingViews = /* @__PURE__ */ new Map();
221
+ builtins = /* @__PURE__ */ new Map();
222
+ declarationBinding = /* @__PURE__ */ new Map();
223
+ propsParameters = /* @__PURE__ */ new Set();
224
+ declaredGlobals;
225
+ globalScope;
226
+ moduleScope;
227
+ constructor(sf, options) {
228
+ this.sf = sf;
229
+ this.file = sf.fileName;
230
+ this.options = options;
231
+ this.declaredGlobals = /* @__PURE__ */ new Set([...BUILTIN_GLOBALS, ...options.globals ?? []]);
232
+ for (const component of options.components ?? []) {
233
+ const props = component.parameters[0];
234
+ if (props !== void 0 && ts.isIdentifier(props.name)) this.propsParameters.add(props);
235
+ }
236
+ this.globalScope = this.createScope("global", emptySpan(this.file), null, null, false);
237
+ this.moduleScope = this.createScope("module", this.spanOfNode(sf), sf, this.globalScope, false);
238
+ }
239
+ run() {
240
+ this.setStatementList(this.moduleScope, this.sf, this.sf.statements);
241
+ this.declareImports();
242
+ this.hoistVars(this.sf, this.moduleScope);
243
+ this.predeclareStatementList(this.moduleScope, this.sf.statements);
244
+ for (const statement of this.sf.statements) this.walkStatement(statement, this.moduleScope);
245
+ this.scanDynamicScope();
246
+ for (const component of this.options.components ?? []) this.scanComponent(component);
247
+ return this.facade();
248
+ }
249
+ spanOfRange(start, end) {
250
+ const position = this.sf.getLineAndCharacterOfPosition(start);
251
+ return {
252
+ file: this.file,
253
+ start,
254
+ end,
255
+ line: position.line + 1,
256
+ column: position.character + 1
257
+ };
258
+ }
259
+ spanOfNode(node) {
260
+ return this.spanOfRange(node.getStart(this.sf), node.getEnd());
261
+ }
262
+ report(code, message, node) {
263
+ this.diagnostics.push({
264
+ severity: "error",
265
+ code,
266
+ message,
267
+ span: this.spanOfNode(node),
268
+ origin: "source"
269
+ });
270
+ }
271
+ createScope(kind, span, bodyOwner, parent, functionBoundary) {
272
+ const parentRecord = parent?.record ?? null;
273
+ const depth = parentRecord === null ? kind === "global" ? -1 : 0 : parentRecord.depth + 1;
274
+ const functionDepth = parentRecord === null ? 0 : parentRecord.functionDepth + (functionBoundary ? 1 : 0);
275
+ const path = kind === "global" ? "global" : kind === "module" ? "0" : parent === null ? "0" : `${parent.record.id.path}.${parent.nextChildOrdinal++}`;
276
+ const record = {
277
+ id: {
278
+ moduleId: this.file,
279
+ path
280
+ },
281
+ kind,
282
+ parent: parentRecord?.id ?? null,
283
+ span,
284
+ functionBoundary,
285
+ depth,
286
+ functionDepth
287
+ };
288
+ this.scopes.push(record);
289
+ const node = {
290
+ record,
291
+ parent,
292
+ children: [],
293
+ byName: /* @__PURE__ */ new Map(),
294
+ byId: /* @__PURE__ */ new Map(),
295
+ statements: null,
296
+ statementIndex: /* @__PURE__ */ new Map(),
297
+ exclude: [],
298
+ nextChildOrdinal: 0,
299
+ bodyOwner
300
+ };
301
+ parent?.children.push(node);
302
+ return node;
303
+ }
304
+ setStatementList(scope, owner, statements) {
305
+ scope.statements = statements;
306
+ scope.bodyOwner = owner;
307
+ statements.forEach((statement, index) => scope.statementIndex.set(statement, index));
308
+ }
309
+ /** Innermost scope containing a source position (exclusions respected). */
310
+ scopeAt(position) {
311
+ let scope = this.moduleScope;
312
+ for (;;) {
313
+ const next = scope.children.find((child) => child.record.span.start <= position && position < child.record.span.end && !child.exclude.some((range) => range.start <= position && position < range.end));
314
+ if (next === void 0) return scope;
315
+ scope = next;
316
+ }
317
+ }
318
+ newBinding(scope, name, declarationOrder, kind, nameNode, initializing, slot, importInfo) {
319
+ const span = this.spanOfNode(nameNode);
320
+ const id = {
321
+ scope: scope.record.id,
322
+ name,
323
+ declarationOrder
324
+ };
325
+ const binding = {
326
+ id,
327
+ declarations: [{
328
+ kind,
329
+ span,
330
+ initializing
331
+ }],
332
+ value: "plain",
333
+ origin: { form: "local" },
334
+ declarationSpan: span,
335
+ classifiedBy: "syntax",
336
+ role: null,
337
+ scope,
338
+ declarationSlot: slot,
339
+ hasImplementation: kind === "function" && initializing,
340
+ importInfo
341
+ };
342
+ scope.byName.set(name, id);
343
+ scope.byId.set(bindingKey(id), binding);
344
+ this.authoredBindings.push(binding);
345
+ this.recordDeclarationBinding(nameNode, binding);
346
+ return binding;
347
+ }
348
+ recordDeclarationBinding(nameNode, binding) {
349
+ if (ts.isIdentifier(nameNode)) this.declarationBinding.set(nameNode, binding);
350
+ }
351
+ addDeclaration(binding, kind, nameNode, initializing) {
352
+ binding.declarations.push({
353
+ kind,
354
+ span: this.spanOfNode(nameNode),
355
+ initializing
356
+ });
357
+ if (kind === "function" && initializing) binding.hasImplementation = true;
358
+ this.recordDeclarationBinding(nameNode, binding);
359
+ }
360
+ /**
361
+ * Declare one name, applying §3's merge table. Returns the binding identity
362
+ * this declaration contributes to (a recovered duplicate is a new identity).
363
+ */
364
+ declareBinding(scope, name, kind, nameNode, initializing, slot, importInfo) {
365
+ const existingId = scope.byName.get(name);
366
+ const existing = existingId === void 0 ? void 0 : scope.byId.get(bindingKey(existingId));
367
+ if (existing === void 0) return this.newBinding(scope, name, 0, kind, nameNode, initializing, slot, importInfo);
368
+ const kinds = new Set(existing.declarations.map((declaration) => declaration.kind));
369
+ const existingImport = kinds.has("import");
370
+ const existingLexical = [...kinds].some((declarationKind) => LEXICAL_KINDS.has(declarationKind));
371
+ const lexicalNew = LEXICAL_KINDS.has(kind);
372
+ const importNew = kind === "import";
373
+ let recovery = null;
374
+ if (existingImport && !importNew) recovery = "import-conflict";
375
+ else if (importNew) recovery = "import-conflict";
376
+ else if (lexicalNew && existingLexical) recovery = "duplicate";
377
+ else if (lexicalNew && !existingLexical) recovery = "conflict";
378
+ else if (!lexicalNew && existingLexical) recovery = "conflict";
379
+ else if (kind === "param" && kinds.has("param")) recovery = "duplicate";
380
+ else if (kind === "function" && initializing && existing.hasImplementation) recovery = "conflict";
381
+ if (recovery === null || recovery === "conflict") {
382
+ this.addDeclaration(existing, kind, nameNode, initializing);
383
+ if (recovery === "conflict") this.report("OEIR_DECLARATION_CONFLICT", `declaration of '${name}' conflicts with an existing ${[...kinds].join("+")} declaration in the same scope`, nameNode);
384
+ return existing;
385
+ }
386
+ const duplicate = this.newBinding(scope, name, existing.id.declarationOrder + 1, kind, nameNode, initializing, slot, importInfo);
387
+ this.report(recovery === "duplicate" ? "OEIR_DUPLICATE_DECLARATION" : "OEIR_IMPORT_CONFLICT", recovery === "duplicate" ? `duplicate ${kind} declaration of '${name}' in the same scope` : `module-scope import '${name}' conflicts with a local declaration`, nameNode);
388
+ return duplicate;
389
+ }
390
+ /** Declare every name a binding pattern introduces (destructuring, §4). */
391
+ declarePattern(scope, name, kind, initializing, slot, propParam = false) {
392
+ if (ts.isIdentifier(name)) {
393
+ const binding = this.declareBinding(scope, name.text, kind, name, initializing, slot, null);
394
+ if (propParam) {
395
+ binding.origin = { form: "prop-param" };
396
+ binding.value = "prop";
397
+ binding.classifiedBy = "syntax";
398
+ }
399
+ return;
400
+ }
401
+ if (ts.isObjectBindingPattern(name)) {
402
+ for (const element of name.elements) this.declarePattern(scope, element.name, kind, true, slot);
403
+ return;
404
+ }
405
+ if (ts.isArrayBindingPattern(name)) for (const element of name.elements) {
406
+ if (ts.isOmittedExpression(element)) continue;
407
+ this.declarePattern(scope, element.name, kind, true, slot);
408
+ }
409
+ }
410
+ declareImports() {
411
+ for (const statement of this.sf.statements) {
412
+ if (!ts.isImportDeclaration(statement)) continue;
413
+ if (!ts.isStringLiteral(statement.moduleSpecifier)) continue;
414
+ const specifier = statement.moduleSpecifier.text;
415
+ const clause = statement.importClause;
416
+ if (clause === void 0) continue;
417
+ const clauseTypeOnly = clause.isTypeOnly ?? false;
418
+ if (clause.name !== void 0) this.declareImportName(clause.name, specifier, null, "default", clauseTypeOnly);
419
+ const bindings = clause.namedBindings;
420
+ if (bindings !== void 0 && ts.isNamedImports(bindings)) for (const element of bindings.elements) this.declareImportName(element.name, specifier, element.propertyName?.text ?? element.name.text, "named", clauseTypeOnly || (element.isTypeOnly ?? false));
421
+ else if (bindings !== void 0 && ts.isNamespaceImport(bindings)) this.declareImportName(bindings.name, specifier, null, "namespace", clauseTypeOnly);
422
+ }
423
+ }
424
+ declareImportName(local, specifier, importedName, form, typeOnly) {
425
+ const importInfo = {
426
+ specifier,
427
+ importedName,
428
+ form,
429
+ typeOnly
430
+ };
431
+ const binding = this.declareBinding(this.moduleScope, local.text, "import", local, true, null, importInfo);
432
+ binding.origin = {
433
+ form: "import",
434
+ specifier,
435
+ importedName
436
+ };
437
+ binding.importInfo = importInfo;
438
+ const classifier = this.options.importClassifier;
439
+ const role = typeOnly ? "type" : classifier?.classifySymbol(specifier, importedName);
440
+ const hostValue = typeOnly ? void 0 : classifier?.classifyValue(specifier, importedName);
441
+ if (typeOnly) {
442
+ binding.value = "unknown";
443
+ binding.classifiedBy = "syntax";
444
+ binding.role = "type";
445
+ return;
446
+ }
447
+ if (hostValue !== void 0) {
448
+ binding.value = hostValue;
449
+ binding.classifiedBy = "host-classifier";
450
+ } else if (role === "factory" || role === "namespace") {
451
+ binding.value = "plain";
452
+ binding.classifiedBy = "host-classifier";
453
+ } else if (role === "type") {
454
+ binding.value = "unknown";
455
+ binding.classifiedBy = "host-classifier";
456
+ } else {
457
+ binding.value = "unknown";
458
+ binding.classifiedBy = role === void 0 ? "unresolved" : "host-classifier";
459
+ }
460
+ binding.role = role ?? (hostValue !== void 0 ? "value" : null);
461
+ }
462
+ /** Declare every `var` declaration of a function/module body in its var scope. */
463
+ hoistVars(root, target) {
464
+ const visit = (node) => {
465
+ if (isScopeFunctionNode(node) || ts.isClassLike(node)) return;
466
+ if (ts.isVariableDeclarationList(node)) {
467
+ if (isVarDeclarationList(node)) for (const declaration of node.declarations) this.declarePattern(target, declaration.name, "var", false, null);
468
+ }
469
+ ts.forEachChild(node, visit);
470
+ };
471
+ visit(root);
472
+ }
473
+ predeclareStatementList(scope, statements) {
474
+ statements.forEach((statement, index) => {
475
+ this.predeclareStatement(scope, statement, {
476
+ statementIndex: index,
477
+ declaratorIndex: 0
478
+ });
479
+ });
480
+ }
481
+ predeclareStatement(scope, statement, slot) {
482
+ if (ts.isFunctionDeclaration(statement)) {
483
+ if (statement.name !== void 0) this.declareBinding(scope, statement.name.text, "function", statement.name, statement.body !== void 0, slot, null);
484
+ return;
485
+ }
486
+ if (ts.isClassDeclaration(statement)) {
487
+ if (statement.name !== void 0) this.declareBinding(scope, statement.name.text, "class", statement.name, true, slot, null);
488
+ return;
489
+ }
490
+ if (ts.isVariableStatement(statement)) {
491
+ const list = statement.declarationList;
492
+ if (isVarDeclarationList(list)) return;
493
+ const kind = lexicalKindOf(list);
494
+ list.declarations.forEach((declaration, index) => {
495
+ this.declarePattern(scope, declaration.name, kind, declaration.initializer !== void 0, {
496
+ statementIndex: slot.statementIndex,
497
+ declaratorIndex: index
498
+ });
499
+ });
500
+ return;
501
+ }
502
+ if (ts.isEnumDeclaration(statement)) {
503
+ this.report("OEIR_UNSUPPORTED_DECLARATION", "namespace/enum merging is outside the function-component model; fail closed", statement);
504
+ return;
505
+ }
506
+ if (ts.isModuleDeclaration(statement)) {
507
+ this.report("OEIR_UNSUPPORTED_DECLARATION", "namespace/enum merging is outside the function-component model; fail closed", statement);
508
+ return;
509
+ }
510
+ if (ts.isImportEqualsDeclaration(statement)) {
511
+ this.report("OEIR_UNSUPPORTED_DECLARATION", "import-equals is outside the function-component model; fail closed", statement);
512
+ return;
513
+ }
514
+ if (ts.isExportAssignment(statement) && statement.isExportEquals) this.report("OEIR_UNSUPPORTED_DECLARATION", "export-equals is outside the function-component model; fail closed", statement);
515
+ }
516
+ walkStatement(statement, scope) {
517
+ if (ts.isVariableStatement(statement)) {
518
+ const list = statement.declarationList;
519
+ for (const declaration of list.declarations) {
520
+ const binding = this.declarationBinding.get(declaration.name);
521
+ if (binding !== void 0) this.classifyInitializer(binding, declaration.initializer);
522
+ this.walkExpression(scope, declaration.initializer);
523
+ this.walkBindingNameExpressions(scope, declaration.name);
524
+ }
525
+ return;
526
+ }
527
+ if (ts.isFunctionDeclaration(statement)) {
528
+ this.buildFunctionScope(statement, scope);
529
+ return;
530
+ }
531
+ if (ts.isClassDeclaration(statement)) {
532
+ this.buildClassScope(statement, scope);
533
+ return;
534
+ }
535
+ if (ts.isBlock(statement)) {
536
+ this.buildBlock(statement, scope);
537
+ return;
538
+ }
539
+ if (ts.isExpressionStatement(statement)) {
540
+ this.walkExpression(scope, statement.expression);
541
+ return;
542
+ }
543
+ if (ts.isReturnStatement(statement) || ts.isThrowStatement(statement)) {
544
+ this.walkExpression(scope, statement.expression);
545
+ return;
546
+ }
547
+ if (ts.isIfStatement(statement)) {
548
+ this.walkExpression(scope, statement.expression);
549
+ this.walkEmbeddedStatement(statement.thenStatement, scope);
550
+ if (statement.elseStatement !== void 0) this.walkEmbeddedStatement(statement.elseStatement, scope);
551
+ return;
552
+ }
553
+ if (ts.isWhileStatement(statement) || ts.isDoStatement(statement)) {
554
+ this.walkExpression(scope, statement.expression);
555
+ this.walkEmbeddedStatement(statement.statement, scope);
556
+ return;
557
+ }
558
+ if (ts.isForStatement(statement)) {
559
+ this.buildForScope(statement, scope);
560
+ return;
561
+ }
562
+ if (ts.isForOfStatement(statement) || ts.isForInStatement(statement)) {
563
+ this.buildForScope(statement, scope);
564
+ return;
565
+ }
566
+ if (ts.isSwitchStatement(statement)) {
567
+ this.buildSwitchScope(statement, scope);
568
+ return;
569
+ }
570
+ if (ts.isTryStatement(statement)) {
571
+ this.buildBlock(statement.tryBlock, scope);
572
+ if (statement.catchClause !== void 0) this.buildCatchScope(statement.catchClause, scope);
573
+ if (statement.finallyBlock !== void 0) this.buildBlock(statement.finallyBlock, scope);
574
+ return;
575
+ }
576
+ if (ts.isLabeledStatement(statement)) {
577
+ this.report("OEIR_UNSUPPORTED_LABEL", "labels are outside the function-component model; fail closed", statement.label);
578
+ this.walkEmbeddedStatement(statement.statement, scope);
579
+ return;
580
+ }
581
+ if (ts.isWithStatement(statement)) {
582
+ this.report("OEIR_DYNAMIC_SCOPE", "with introduces dynamic scope; forbidden by the static resolution model", statement);
583
+ this.walkExpression(scope, statement.expression);
584
+ this.walkEmbeddedStatement(statement.statement, scope);
585
+ return;
586
+ }
587
+ if (ts.isExportAssignment(statement)) {
588
+ if (!statement.isExportEquals) this.walkExpression(scope, statement.expression);
589
+ return;
590
+ }
591
+ }
592
+ walkEmbeddedStatement(statement, scope) {
593
+ if (ts.isBlock(statement)) {
594
+ this.buildBlock(statement, scope);
595
+ return;
596
+ }
597
+ this.walkStatement(statement, scope);
598
+ }
599
+ buildBlock(block, parent) {
600
+ const scope = this.createScope("block", this.spanOfNode(block), block, parent, false);
601
+ this.setStatementList(scope, block, block.statements);
602
+ this.predeclareStatementList(scope, block.statements);
603
+ for (const statement of block.statements) this.walkStatement(statement, scope);
604
+ }
605
+ /** Start of a function scope: after decorators (evaluated outside the body). */
606
+ functionScopeStart(node) {
607
+ if (ts.canHaveDecorators(node)) {
608
+ const decorators = ts.getDecorators(node);
609
+ const last = decorators?.[decorators.length - 1];
610
+ if (last !== void 0) return last.getEnd();
611
+ }
612
+ return node.getStart(this.sf);
613
+ }
614
+ buildFunctionScope(node, parent) {
615
+ const scope = this.createScope("function", this.spanOfRange(this.functionScopeStart(node), node.getEnd()), node, parent, true);
616
+ if (ts.isFunctionExpression(node) && node.name !== void 0) this.declareBinding(scope, node.name.text, "function", node.name, true, null, null);
617
+ for (const parameter of node.parameters) {
618
+ const propParam = ts.isIdentifier(parameter.name) && this.propsParameters.has(parameter);
619
+ if (ts.isIdentifier(parameter.name) && parameter.name.text === "this") continue;
620
+ this.declarePattern(scope, parameter.name, "param", true, null, propParam);
621
+ this.walkBindingNameExpressions(scope, parameter.name);
622
+ this.walkExpression(scope, parameter.initializer);
623
+ }
624
+ const body = node.body;
625
+ if (body === void 0) return;
626
+ if (ts.isBlock(body)) {
627
+ this.setStatementList(scope, body, body.statements);
628
+ this.hoistVars(body, scope);
629
+ this.predeclareStatementList(scope, body.statements);
630
+ for (const statement of body.statements) this.walkStatement(statement, scope);
631
+ return;
632
+ }
633
+ this.walkExpression(scope, body);
634
+ }
635
+ buildStaticBlockScope(node, parent) {
636
+ const scope = this.createScope("function", this.spanOfNode(node), node, parent, true);
637
+ this.setStatementList(scope, node.body, node.body.statements);
638
+ this.hoistVars(node.body, scope);
639
+ this.predeclareStatementList(scope, node.body.statements);
640
+ for (const statement of node.body.statements) this.walkStatement(statement, scope);
641
+ }
642
+ buildForScope(node, parent) {
643
+ const scope = this.createScope("for", this.spanOfNode(node), node, parent, false);
644
+ const initializer = node.initializer;
645
+ if (initializer !== void 0 && ts.isVariableDeclarationList(initializer)) {
646
+ if (!isVarDeclarationList(initializer)) {
647
+ const kind = lexicalKindOf(initializer);
648
+ for (const declaration of initializer.declarations) this.declarePattern(scope, declaration.name, kind, true, null);
649
+ }
650
+ for (const declaration of initializer.declarations) {
651
+ this.walkExpression(scope, declaration.initializer);
652
+ this.walkBindingNameExpressions(scope, declaration.name);
653
+ }
654
+ } else if (initializer !== void 0) this.walkExpression(scope, initializer);
655
+ if (ts.isForStatement(node)) {
656
+ this.walkExpression(scope, node.condition);
657
+ this.walkExpression(scope, node.incrementor);
658
+ this.walkEmbeddedStatement(node.statement, scope);
659
+ return;
660
+ }
661
+ const right = node.expression;
662
+ scope.exclude.push({
663
+ start: right.getStart(this.sf),
664
+ end: right.getEnd()
665
+ });
666
+ this.walkExpression(parent, right);
667
+ this.walkEmbeddedStatement(node.statement, scope);
668
+ }
669
+ buildSwitchScope(node, parent) {
670
+ const caseBlock = node.caseBlock;
671
+ const start = caseBlock.getStart(this.sf);
672
+ const scope = this.createScope("switch", this.spanOfRange(start, node.getEnd()), node, parent, false);
673
+ this.walkExpression(parent, node.expression);
674
+ for (const clause of caseBlock.clauses) {
675
+ if (ts.isCaseClause(clause)) this.walkExpression(scope, clause.expression);
676
+ clause.statements.forEach((statement, index) => {
677
+ this.predeclareStatement(scope, statement, {
678
+ statementIndex: index,
679
+ declaratorIndex: 0
680
+ });
681
+ });
682
+ }
683
+ for (const clause of caseBlock.clauses) for (const statement of clause.statements) this.walkStatement(statement, scope);
684
+ }
685
+ buildCatchScope(node, parent) {
686
+ const scope = this.createScope("catch", this.spanOfNode(node), node, parent, false);
687
+ if (node.variableDeclaration !== void 0) {
688
+ this.declarePattern(scope, node.variableDeclaration.name, "catch", true, null);
689
+ this.walkBindingNameExpressions(scope, node.variableDeclaration.name);
690
+ }
691
+ this.buildBlock(node.block, scope);
692
+ }
693
+ buildClassScope(node, parent) {
694
+ for (const decorator of ts.getDecorators(node) ?? []) this.walkExpression(parent, decorator.expression);
695
+ for (const clause of node.heritageClauses ?? []) for (const type of clause.types) this.walkExpression(parent, type.expression);
696
+ const start = node.members.pos;
697
+ const scope = this.createScope("class", this.spanOfRange(start, node.getEnd()), node, parent, false);
698
+ if (node.name !== void 0) this.declareBinding(scope, node.name.text, "class-name", node.name, true, null, null);
699
+ for (const member of node.members) {
700
+ if (ts.isPropertyDeclaration(member)) {
701
+ for (const decorator of ts.getDecorators(member) ?? []) this.walkExpression(scope, decorator.expression);
702
+ this.walkExpression(scope, member.initializer);
703
+ continue;
704
+ }
705
+ if (ts.isClassStaticBlockDeclaration(member)) {
706
+ this.buildStaticBlockScope(member, scope);
707
+ continue;
708
+ }
709
+ if (ts.isMethodDeclaration(member) || ts.isConstructorDeclaration(member) || ts.isGetAccessorDeclaration(member) || ts.isSetAccessorDeclaration(member)) this.buildFunctionScope(member, scope);
710
+ }
711
+ }
712
+ /** Build scopes for function/class expressions nested anywhere in an expression. */
713
+ walkExpression(scope, node) {
714
+ if (node === void 0) return;
715
+ if (isScopeFunctionNode(node)) {
716
+ this.buildFunctionScope(node, scope);
717
+ return;
718
+ }
719
+ if (ts.isClassExpression(node)) {
720
+ this.buildClassScope(node, scope);
721
+ return;
722
+ }
723
+ ts.forEachChild(node, (child) => {
724
+ if (!ts.isPartOfTypeNode(child)) this.walkExpression(scope, child);
725
+ });
726
+ }
727
+ /** Build scopes for expressions embedded in binding patterns (defaults). */
728
+ walkBindingNameExpressions(scope, name) {
729
+ if (ts.isIdentifier(name)) return;
730
+ if (ts.isObjectBindingPattern(name)) {
731
+ for (const element of name.elements) {
732
+ if (element.propertyName !== void 0 && ts.isComputedPropertyName(element.propertyName)) this.walkExpression(scope, element.propertyName.expression);
733
+ this.walkBindingNameExpressions(scope, element.name);
734
+ this.walkExpression(scope, element.initializer);
735
+ }
736
+ return;
737
+ }
738
+ if (ts.isArrayBindingPattern(name)) for (const element of name.elements) {
739
+ if (ts.isOmittedExpression(element)) continue;
740
+ this.walkBindingNameExpressions(scope, element.name);
741
+ this.walkExpression(scope, element.initializer);
742
+ }
743
+ }
744
+ classifyInitializer(binding, initializer) {
745
+ if (initializer === void 0) return;
746
+ if (binding.origin.form !== "local") return;
747
+ const value = unwrapExpression(initializer);
748
+ if (ts.isCallExpression(value)) {
749
+ const answer = this.classifyFactoryCall(value);
750
+ if (answer.kind === "classified") {
751
+ binding.value = answer.call;
752
+ binding.classifiedBy = "host-classifier";
753
+ return;
754
+ }
755
+ if (answer.kind === "unanswered") {
756
+ const callee = unwrapExpression(value.expression);
757
+ if (ts.isIdentifier(callee)) {
758
+ const target = this.lookupAuthored(this.scopeAt(callee.getStart(this.sf)), callee.text);
759
+ if ((target?.importInfo == null ? void 0 : this.options.importClassifier?.classifyValue(target.importInfo.specifier, target.importInfo.importedName)) === "plain" || target?.role === "value") {
760
+ binding.value = "plain";
761
+ binding.classifiedBy = "host-classifier";
762
+ return;
763
+ }
764
+ }
765
+ binding.value = "unknown";
766
+ binding.classifiedBy = "unresolved";
767
+ return;
768
+ }
769
+ }
770
+ if (ts.isIdentifier(value)) {
771
+ const target = this.lookupAuthored(this.scopeAt(value.getStart(this.sf)), value.text);
772
+ if (target !== void 0) {
773
+ binding.value = target.value;
774
+ binding.classifiedBy = target.classifiedBy;
775
+ if (target.importInfo !== null) binding.importInfo = target.importInfo;
776
+ if (target.role !== null) binding.role = target.role;
777
+ }
778
+ }
779
+ }
780
+ /**
781
+ * §6.1: `classifySymbol` → `classifyCall` for a direct import;
782
+ * `classifyMember` → `classifyCall` for a namespace member. Nothing is ever
783
+ * inferred from an identifier spelling.
784
+ */
785
+ classifyFactoryCall(call) {
786
+ const classifier = this.options.importClassifier;
787
+ const callee = unwrapExpression(call.expression);
788
+ if (ts.isIdentifier(callee)) {
789
+ const target = this.lookupAuthored(this.scopeAt(callee.getStart(this.sf)), callee.text);
790
+ if (target === void 0 || target.importInfo === null) return { kind: "unclassified" };
791
+ if (target.role !== "factory") return { kind: "unanswered" };
792
+ const result = classifier?.classifyCall(target.importInfo.specifier, target.importInfo.importedName);
793
+ if (result === void 0) return { kind: "unanswered" };
794
+ return {
795
+ kind: "classified",
796
+ call: result.call
797
+ };
798
+ }
799
+ if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression)) {
800
+ const object = callee.expression;
801
+ const target = this.lookupAuthored(this.scopeAt(object.getStart(this.sf)), object.text);
802
+ if (target === void 0 || target.importInfo === null) return { kind: "unclassified" };
803
+ if (target.importInfo.form !== "namespace") return { kind: "unanswered" };
804
+ const member = callee.name.text;
805
+ if (classifier?.classifyMember?.(target.importInfo.specifier, member) !== "factory") return { kind: "unanswered" };
806
+ const result = classifier?.classifyCall(target.importInfo.specifier, member);
807
+ if (result === void 0) return { kind: "unanswered" };
808
+ return {
809
+ kind: "classified",
810
+ call: result.call
811
+ };
812
+ }
813
+ return { kind: "unclassified" };
814
+ }
815
+ lookupAuthored(scope, name) {
816
+ let current = scope;
817
+ while (current !== null) {
818
+ const id = current.byName.get(name);
819
+ if (id !== void 0) return current.byId.get(bindingKey(id));
820
+ current = current.parent;
821
+ }
822
+ }
823
+ lookup(scope, name) {
824
+ return this.lookupAuthored(scope, name) ?? this.builtinBinding(name);
825
+ }
826
+ builtinBinding(name) {
827
+ if (!this.declaredGlobals.has(name)) return void 0;
828
+ const existing = this.builtins.get(name);
829
+ if (existing !== void 0) return existing;
830
+ const span = emptySpan(this.file);
831
+ const binding = {
832
+ id: {
833
+ scope: this.globalScope.record.id,
834
+ name,
835
+ declarationOrder: 0
836
+ },
837
+ declarations: [],
838
+ value: "plain",
839
+ origin: {
840
+ form: "builtin",
841
+ name
842
+ },
843
+ declarationSpan: span,
844
+ classifiedBy: "syntax",
845
+ role: null,
846
+ scope: this.globalScope,
847
+ declarationSlot: null,
848
+ hasImplementation: false,
849
+ importInfo: null
850
+ };
851
+ this.builtins.set(name, binding);
852
+ return binding;
853
+ }
854
+ /** `eval` anywhere in the module is dynamic scope; `with` is caught per statement. */
855
+ scanDynamicScope() {
856
+ const visit = (node) => {
857
+ if (ts.isIdentifier(node) && node.text === "eval" && this.isValueReference(node) && this.lookupAuthored(this.scopeAt(node.getStart(this.sf)), "eval") === void 0) this.report("OEIR_DYNAMIC_SCOPE", "eval introduces dynamic scope; forbidden by the static resolution model", node);
858
+ ts.forEachChild(node, (child) => {
859
+ if (!ts.isPartOfTypeNode(child)) visit(child);
860
+ });
861
+ };
862
+ visit(this.sf);
863
+ }
864
+ /**
865
+ * A function component has no `this`/`arguments`/`super`/`new.target`; their
866
+ * presence is diagnostic (§4). Class subtrees are skipped: a class member's
867
+ * `this`/`arguments`/`super` are legal JavaScript, and class authoring is
868
+ * outside this frontend rather than scope-resolved here.
869
+ */
870
+ scanComponent(component) {
871
+ const visit = (node) => {
872
+ if (ts.isClassLike(node)) return;
873
+ if (node.kind === ts.SyntaxKind.ThisKeyword) {
874
+ this.report("OEIR_UNSUPPORTED_THIS", "a function component has no `this`; use the props parameter", node);
875
+ return;
876
+ }
877
+ if (node.kind === ts.SyntaxKind.SuperKeyword) {
878
+ this.report("OEIR_UNSUPPORTED_SUPER", "`super` is not part of the function-component model", node);
879
+ return;
880
+ }
881
+ if (isNewTarget(node)) {
882
+ this.report("OEIR_UNSUPPORTED_NEW_TARGET", "`new.target` is not part of the function-component model", node);
883
+ return;
884
+ }
885
+ if (ts.isIdentifier(node) && node.text === "arguments" && this.isValueReference(node)) {
886
+ this.report("OEIR_UNSUPPORTED_ARGUMENTS", "`arguments` is not part of the function-component model", node);
887
+ return;
888
+ }
889
+ ts.forEachChild(node, (child) => {
890
+ if (!ts.isPartOfTypeNode(child)) visit(child);
891
+ });
892
+ };
893
+ visit(component);
894
+ }
895
+ /**
896
+ * True when an identifier occurrence in an expression is a *value read*
897
+ * (not a property name, declaration name, label, JSX attribute name, ...).
898
+ * JSX component tags (`<Row/>`) are reads; intrinsic tags (`<div>`) are not.
899
+ */
900
+ isValueReference(identifier) {
901
+ const parent = identifier.parent;
902
+ if (parent === void 0) return false;
903
+ if (ts.isPropertyAccessExpression(parent)) return parent.expression === identifier;
904
+ if (ts.isQualifiedName(parent)) return false;
905
+ if (ts.isPropertyAssignment(parent)) return parent.name !== identifier;
906
+ if (ts.isShorthandPropertyAssignment(parent)) return parent.name === identifier;
907
+ if (ts.isBindingElement(parent)) return false;
908
+ if (ts.isTypeParameterDeclaration(parent)) return false;
909
+ if (ts.isVariableDeclaration(parent)) return parent.name !== identifier;
910
+ if (ts.isParameter(parent)) return parent.name !== identifier;
911
+ if (ts.isFunctionDeclaration(parent) || ts.isFunctionExpression(parent) || ts.isClassDeclaration(parent) || ts.isClassExpression(parent) || ts.isMethodDeclaration(parent) || ts.isPropertyDeclaration(parent) || ts.isGetAccessorDeclaration(parent) || ts.isSetAccessorDeclaration(parent)) return false;
912
+ if (ts.isImportClause(parent) || ts.isImportSpecifier(parent) || ts.isNamespaceImport(parent) || ts.isImportEqualsDeclaration(parent) || ts.isExportSpecifier(parent) || ts.isNamespaceExport(parent)) return false;
913
+ if (ts.isLabeledStatement(parent)) return false;
914
+ if (ts.isBreakStatement(parent) || ts.isContinueStatement(parent)) return false;
915
+ if (ts.isJsxAttribute(parent)) return false;
916
+ if (ts.isJsxNamespacedName(parent)) return false;
917
+ if (ts.isMetaProperty(parent)) return false;
918
+ if (ts.isJsxOpeningElement(parent) || ts.isJsxSelfClosingElement(parent) || ts.isJsxClosingElement(parent)) {
919
+ if (parent.tagName !== identifier) return false;
920
+ return /^[A-Z]/.test(identifier.text);
921
+ }
922
+ return true;
923
+ }
924
+ /** §5 captures: the read sits inside more function boundaries than the binding. */
925
+ isCapture(readScope, binding) {
926
+ if (binding.origin.form === "builtin") return false;
927
+ return readScope.record.functionDepth > binding.scope.record.functionDepth;
928
+ }
929
+ /** Control flow / nested execution that makes the straight-line bound unprovable. */
930
+ isTdzBarrier(node) {
931
+ if (ts.isIfStatement(node) || ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node) || ts.isWhileStatement(node) || ts.isDoStatement(node) || ts.isSwitchStatement(node) || ts.isCaseClause(node) || ts.isDefaultClause(node) || ts.isTryStatement(node) || ts.isCatchClause(node) || ts.isLabeledStatement(node) || ts.isWithStatement(node) || isScopeFunctionNode(node)) return true;
932
+ if (ts.isConditionalExpression(node)) return true;
933
+ if (ts.isBinaryExpression(node)) {
934
+ const kind = node.operatorToken.kind;
935
+ return kind === ts.SyntaxKind.AmpersandAmpersandToken || kind === ts.SyntaxKind.BarBarToken || kind === ts.SyntaxKind.QuestionQuestionToken;
936
+ }
937
+ return false;
938
+ }
939
+ /**
940
+ * A static TDZ diagnostic is emitted ONLY inside this provable bound: a
941
+ * same-scope straight-line read at or before the declaring (statement,
942
+ * declarator) slot. Everything uncertain stays non-diagnostic (§4.1).
943
+ */
944
+ tdzDiagnostic(identifier, binding) {
945
+ const slot = binding.declarationSlot;
946
+ if (slot === null) return null;
947
+ const scope = binding.scope;
948
+ if (scope.statements === null) return null;
949
+ if (!binding.declarations.map((declaration) => declaration.kind).every((kind) => kind === "let" || kind === "const" || kind === "class")) return null;
950
+ if (this.scopeAt(identifier.getStart(this.sf)) !== scope) return null;
951
+ const owner = scope.bodyOwner;
952
+ if (owner === null) return null;
953
+ let current = identifier;
954
+ let declarator = null;
955
+ for (;;) {
956
+ const parent = current.parent;
957
+ if (parent === void 0) return null;
958
+ if (parent === owner) break;
959
+ if (this.isTdzBarrier(parent)) return null;
960
+ if (ts.isVariableDeclaration(parent)) declarator = parent;
961
+ current = parent;
962
+ }
963
+ if (!ts.isStatement(current)) return null;
964
+ const statementIndex = scope.statementIndex.get(current);
965
+ if (statementIndex === void 0) return null;
966
+ let readDeclaratorIndex = 0;
967
+ let readStatementIndex = statementIndex;
968
+ if (declarator !== null) {
969
+ const list = declarator.parent;
970
+ if (ts.isVariableDeclarationList(list) && ts.isVariableStatement(list.parent)) {
971
+ const statement = list.parent;
972
+ const index = scope.statementIndex.get(statement);
973
+ if (index === void 0) return null;
974
+ readStatementIndex = index;
975
+ readDeclaratorIndex = list.declarations.indexOf(declarator);
976
+ }
977
+ }
978
+ if (!(readStatementIndex < slot.statementIndex || readStatementIndex === slot.statementIndex && readDeclaratorIndex <= slot.declaratorIndex)) return null;
979
+ return {
980
+ severity: "error",
981
+ code: "OEIR_TDZ_READ",
982
+ message: `block-scoped '${binding.id.name}' is read before its declaration initializes it (provable same-block straight-line read; TDZ otherwise stays a runtime state)`,
983
+ span: this.spanOfNode(identifier),
984
+ origin: "source"
985
+ };
986
+ }
987
+ resolveExpression(node, mode) {
988
+ const reads = [];
989
+ const seen = /* @__PURE__ */ new Set();
990
+ const diagnostics = [];
991
+ const unresolvedReads = [];
992
+ const dependencies = [];
993
+ const captures = [];
994
+ const resolvedByKey = /* @__PURE__ */ new Map();
995
+ const visit = (current) => {
996
+ if (ts.isIdentifier(current) && this.isValueReference(current)) {
997
+ const start = current.getStart(this.sf);
998
+ const readScope = this.scopeAt(start);
999
+ const binding = this.lookup(readScope, current.text);
1000
+ if (binding === void 0) {
1001
+ const span = this.spanOfNode(current);
1002
+ unresolvedReads.push({
1003
+ name: current.text,
1004
+ span
1005
+ });
1006
+ diagnostics.push({
1007
+ severity: "error",
1008
+ code: "OEIR_UNRESOLVED_BINDING",
1009
+ message: `unresolved identifier '${current.text}'`,
1010
+ span,
1011
+ origin: "source"
1012
+ });
1013
+ return;
1014
+ }
1015
+ if (binding.importInfo?.typeOnly === true || binding.role === "type") diagnostics.push({
1016
+ severity: "error",
1017
+ code: "OEIR_TYPE_ONLY_VALUE_READ",
1018
+ message: `type-only binding '${current.text}' is read in a value position`,
1019
+ span: this.spanOfNode(current),
1020
+ origin: "source"
1021
+ });
1022
+ const tdz = this.tdzDiagnostic(current, binding);
1023
+ if (tdz !== null) diagnostics.push(tdz);
1024
+ const expressionStart = node.getStart(this.sf);
1025
+ if (binding.declarationSpan.start >= expressionStart && binding.declarationSpan.start < node.getEnd()) return;
1026
+ const key = bindingKey(binding.id);
1027
+ if (!seen.has(key)) {
1028
+ seen.add(key);
1029
+ resolvedByKey.set(key, binding);
1030
+ reads.push({
1031
+ name: current.text,
1032
+ binding: binding.id,
1033
+ value: binding.value,
1034
+ span: this.spanOfNode(current),
1035
+ capture: this.isCapture(readScope, binding)
1036
+ });
1037
+ }
1038
+ return;
1039
+ }
1040
+ ts.forEachChild(current, (child) => {
1041
+ if (!ts.isPartOfTypeNode(child)) visit(child);
1042
+ });
1043
+ };
1044
+ visit(node);
1045
+ if (TRACKED_MODES.has(mode)) for (const read of reads) {
1046
+ const binding = resolvedByKey.get(bindingKey(read.binding));
1047
+ if (binding === void 0) continue;
1048
+ if (REACTIVE_KINDS.has(binding.value)) {
1049
+ dependencies.push(binding.id);
1050
+ continue;
1051
+ }
1052
+ if (binding.value === "unknown") diagnostics.push({
1053
+ severity: "error",
1054
+ code: "OEIR_UNCLASSIFIED_BINDING",
1055
+ message: `binding '${binding.id.name}' has an unknown value kind in a tracked slot; ${describeOrigin(binding.origin)}`,
1056
+ span: read.span,
1057
+ origin: "source"
1058
+ });
1059
+ }
1060
+ for (const read of reads) if (read.capture) captures.push(read.binding);
1061
+ return {
1062
+ mode,
1063
+ reads,
1064
+ captures,
1065
+ dependencies,
1066
+ unresolvedReads,
1067
+ diagnostics
1068
+ };
1069
+ }
1070
+ resolveIdentifier(node) {
1071
+ const scope = this.scopeAt(node.getStart(this.sf));
1072
+ const binding = this.lookupAuthored(scope, node.text) ?? this.builtinBinding(node.text) ?? void 0;
1073
+ return binding === void 0 ? void 0 : this.viewOf(binding);
1074
+ }
1075
+ viewOf(binding) {
1076
+ const existing = this.bindingViews.get(binding);
1077
+ if (existing !== void 0) return existing;
1078
+ const declarations = [...binding.declarations].sort((left, right) => left.span.start - right.span.start);
1079
+ const view = Object.freeze({
1080
+ id: binding.id,
1081
+ declarations: Object.freeze(declarations),
1082
+ value: binding.value,
1083
+ origin: binding.origin,
1084
+ declarationSpan: declarations[0]?.span ?? binding.declarationSpan,
1085
+ classifiedBy: binding.classifiedBy,
1086
+ role: binding.role
1087
+ });
1088
+ this.bindingViews.set(binding, view);
1089
+ return view;
1090
+ }
1091
+ facade() {
1092
+ const bindings = this.authoredBindings.map((binding) => this.viewOf(binding));
1093
+ return {
1094
+ moduleId: this.file,
1095
+ file: this.file,
1096
+ sourceFile: this.sf,
1097
+ diagnostics: Object.freeze([...this.diagnostics]),
1098
+ scopes: Object.freeze([...this.scopes]),
1099
+ bindings: Object.freeze(bindings),
1100
+ resolveExpression: (node, mode) => this.resolveExpression(node, mode),
1101
+ resolveIdentifier: (node) => this.resolveIdentifier(node)
1102
+ };
1103
+ }
1104
+ };
1105
+ function describeOrigin(origin) {
1106
+ if (origin.form === "import") return `import '${origin.importedName === null ? "default" : origin.importedName}' from '${origin.specifier}' was not classified by the host`;
1107
+ if (origin.form === "builtin") return `builtin global '${origin.name}' has no reactive kind`;
1108
+ if (origin.form === "prop-param") return "the props parameter";
1109
+ return "the binding has no host classification";
1110
+ }
1111
+ /**
1112
+ * Resolve the lexical scopes, declarations, reads, captures and dependencies of
1113
+ * one module. Pure: the caller hands the parsed source file and the host
1114
+ * classifier; the resolver never touches a `ts.Program`, the filesystem or
1115
+ * ambient state.
1116
+ */
1117
+ function resolveModule(sourceFile, options = {}) {
1118
+ return new ScopeResolver(sourceFile, options).run();
1119
+ }
1120
+ //#endregion
1121
+ export { bindingKey, resolveModule };