@telorun/analyzer 0.13.0 → 0.15.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/alias-resolver.d.ts +6 -0
- package/dist/alias-resolver.d.ts.map +1 -1
- package/dist/alias-resolver.js +8 -0
- package/dist/analysis-registry.d.ts +1 -0
- package/dist/analysis-registry.d.ts.map +1 -1
- package/dist/analyzer.d.ts +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +111 -5
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +66 -0
- package/dist/flatten-for-analyzer.d.ts.map +1 -1
- package/dist/flatten-for-analyzer.js +17 -0
- package/dist/manifest-visitor.d.ts +15 -0
- package/dist/manifest-visitor.d.ts.map +1 -1
- package/dist/manifest-visitor.js +73 -2
- package/dist/reference-field-map.js +16 -0
- package/dist/resolve-ref-sentinels.d.ts +15 -17
- package/dist/resolve-ref-sentinels.d.ts.map +1 -1
- package/dist/resolve-ref-sentinels.js +86 -40
- package/dist/resolve-throws-union.d.ts +10 -0
- package/dist/resolve-throws-union.d.ts.map +1 -1
- package/dist/resolve-throws-union.js +35 -7
- package/dist/validate-references.d.ts.map +1 -1
- package/dist/validate-references.js +108 -7
- package/package.json +5 -4
- package/src/alias-resolver.ts +9 -0
- package/src/analysis-registry.ts +1 -1
- package/src/analyzer.ts +130 -5
- package/src/builtins.ts +66 -0
- package/src/flatten-for-analyzer.ts +20 -0
- package/src/manifest-visitor.ts +91 -2
- package/src/reference-field-map.ts +14 -0
- package/src/resolve-ref-sentinels.ts +89 -44
- package/src/resolve-throws-union.ts +36 -8
- package/src/validate-references.ts +110 -8
|
@@ -5,43 +5,100 @@ import type { DefinitionRegistry } from "./definition-registry.js";
|
|
|
5
5
|
import { isRefEntry } from "./reference-field-map.js";
|
|
6
6
|
import { REF_RESOLUTION_SKIP_KINDS as SYSTEM_KINDS } from "./system-kinds.js";
|
|
7
7
|
|
|
8
|
+
/** Resolved ref shape written in place of a `!ref` sentinel. `alias` is set only for
|
|
9
|
+
* cross-module references (resolved into an imported library's exported instance). */
|
|
10
|
+
type ResolvedRef = { kind: string; name: string; alias?: string };
|
|
11
|
+
|
|
8
12
|
/**
|
|
9
13
|
* Walks every `x-telo-ref` slot in every non-system resource and rewrites
|
|
10
|
-
* `!ref <name>` sentinels in-place to `{kind
|
|
14
|
+
* `!ref <name>` sentinels in-place to `{kind, name}` (local) or
|
|
15
|
+
* `{kind, name, alias}` (cross-module).
|
|
11
16
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
+
* Reference grammar — the tag's source string is split on the FIRST dot:
|
|
18
|
+
* - `!ref writeLine` → local resource `writeLine`
|
|
19
|
+
* - `!ref Self.writeLine` → local resource `writeLine` (explicit self-qualifier)
|
|
20
|
+
* - `!ref Console.writeLine` → instance `writeLine` exported by the import aliased
|
|
21
|
+
* `Console`, resolved against the forwarded foreign set
|
|
17
22
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
23
|
+
* Aliases are PascalCase identifiers without dots and resource names carry no dots
|
|
24
|
+
* (enforced as a hard diagnostic), so the first-dot split is unambiguous. When the
|
|
25
|
+
* name doesn't resolve, the sentinel is left in place so `validateReferences` emits the
|
|
26
|
+
* `UNRESOLVED_REFERENCE` diagnostic with full context.
|
|
22
27
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* to the path-encoding rules of `resolveFieldEntries` — any new path
|
|
27
|
-
* marker would silently break this writer. Descending directly avoids
|
|
28
|
-
* that coupling.
|
|
28
|
+
* Forwarded foreign resources (an imported library's exported instances, carrying a
|
|
29
|
+
* `metadata.module` that isn't a root module) are resolution TARGETS only — they are not
|
|
30
|
+
* re-walked as sources here, since their own ref slots belong to their own module scope.
|
|
29
31
|
*/
|
|
30
32
|
export function resolveRefSentinels(
|
|
31
33
|
resources: ResourceManifest[],
|
|
32
34
|
registry: DefinitionRegistry,
|
|
33
35
|
aliases?: AliasResolver,
|
|
34
36
|
aliasesByModule?: Map<string, AliasResolver>,
|
|
37
|
+
// Extra foreign resources used only as cross-module resolution TARGETS (not mutated, not
|
|
38
|
+
// walked as sources). The kernel passes the analyzer-flattened set here so the runtime
|
|
39
|
+
// pass — which loads the entry module only — can still resolve `!ref Alias.name` against
|
|
40
|
+
// imported libraries' exported instances.
|
|
41
|
+
crossModuleTargets: ResourceManifest[] = [],
|
|
35
42
|
): void {
|
|
43
|
+
const moduleOf = (r: ResourceManifest): string | undefined =>
|
|
44
|
+
(r.metadata as { module?: string } | undefined)?.module;
|
|
45
|
+
// Forwarded exports are flagged by flattenForAnalyzer (`metadata.forwardedExport`); they're
|
|
46
|
+
// cross-module resolution targets only — never walked as local ref sources here.
|
|
47
|
+
const isForeign = (r: ResourceManifest): boolean =>
|
|
48
|
+
(r.metadata as { forwardedExport?: boolean } | undefined)?.forwardedExport === true;
|
|
49
|
+
|
|
50
|
+
// Local resources resolve a bare / `Self.`-qualified name; forwarded foreign exports
|
|
51
|
+
// resolve an `Alias.`-qualified name keyed by (module, name).
|
|
36
52
|
const byName = new Map<string, ResourceManifest>();
|
|
53
|
+
const byModuleName = new Map<string, ResourceManifest>();
|
|
37
54
|
for (const r of resources) {
|
|
38
|
-
if (r.metadata?.name
|
|
39
|
-
|
|
55
|
+
if (!r.metadata?.name || SYSTEM_KINDS.has(r.kind)) continue;
|
|
56
|
+
const name = r.metadata.name as string;
|
|
57
|
+
if (isForeign(r)) {
|
|
58
|
+
byModuleName.set(`${moduleOf(r)}\0${name}`, r);
|
|
59
|
+
} else {
|
|
60
|
+
byName.set(name, r);
|
|
40
61
|
}
|
|
41
62
|
}
|
|
63
|
+
for (const r of crossModuleTargets) {
|
|
64
|
+
if (!r.metadata?.name || SYSTEM_KINDS.has(r.kind) || !isForeign(r)) continue;
|
|
65
|
+
byModuleName.set(`${moduleOf(r)}\0${r.metadata.name as string}`, r);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const resolveTarget = (source: string): ResolvedRef | undefined => {
|
|
69
|
+
const dot = source.indexOf(".");
|
|
70
|
+
if (dot === -1) {
|
|
71
|
+
const t = byName.get(source);
|
|
72
|
+
return t ? { kind: t.kind as string, name: source } : undefined;
|
|
73
|
+
}
|
|
74
|
+
const alias = source.slice(0, dot);
|
|
75
|
+
const name = source.slice(dot + 1);
|
|
76
|
+
if (alias === "Self") {
|
|
77
|
+
const t = byName.get(name);
|
|
78
|
+
return t ? { kind: t.kind as string, name } : undefined;
|
|
79
|
+
}
|
|
80
|
+
const module = aliases?.moduleForAlias(alias);
|
|
81
|
+
if (module) {
|
|
82
|
+
const t = byModuleName.get(`${module}\0${name}`);
|
|
83
|
+
if (t) {
|
|
84
|
+
// The foreign instance's `kind` is authored in ITS module's scope (e.g.
|
|
85
|
+
// `Self.WriteLine`); canonicalize to a scope-independent `<module>.<Kind>` for the
|
|
86
|
+
// consumer's kind check. `Self.` maps to the owning module directly — the forwarded
|
|
87
|
+
// library's Library doc (hence its `Self` alias) isn't in the consumer's manifest
|
|
88
|
+
// set — while other alias prefixes resolve via that module's forwarded import scope.
|
|
89
|
+
const rawKind = t.kind as string;
|
|
90
|
+
const foreignKind = rawKind.startsWith("Self.")
|
|
91
|
+
? `${module}.${rawKind.slice("Self.".length)}`
|
|
92
|
+
: aliasesByModule?.get(module)?.resolveKind(rawKind) ?? rawKind;
|
|
93
|
+
return { kind: foreignKind, name, alias };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
};
|
|
42
98
|
|
|
43
99
|
for (const r of resources) {
|
|
44
100
|
if (!r.metadata?.name || !r.kind || SYSTEM_KINDS.has(r.kind)) continue;
|
|
101
|
+
if (isForeign(r)) continue;
|
|
45
102
|
|
|
46
103
|
const fieldMap =
|
|
47
104
|
aliases && aliasesByModule
|
|
@@ -51,45 +108,33 @@ export function resolveRefSentinels(
|
|
|
51
108
|
|
|
52
109
|
for (const [fieldPath, entry] of fieldMap) {
|
|
53
110
|
if (!isRefEntry(entry)) continue;
|
|
54
|
-
|
|
111
|
+
descend(r as Record<string, unknown>, fieldPath.split("."), resolveTarget);
|
|
55
112
|
}
|
|
56
113
|
}
|
|
57
114
|
}
|
|
58
115
|
|
|
59
|
-
/** Walks `obj` along `fieldPath` (dot notation with `[]` for arrays and
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* via `byName`. Mutates the parent container in place; no string-path
|
|
63
|
-
* round-trip. */
|
|
64
|
-
function replaceSentinelsAtPath(
|
|
65
|
-
obj: Record<string, unknown>,
|
|
66
|
-
fieldPath: string,
|
|
67
|
-
byName: Map<string, ResourceManifest>,
|
|
68
|
-
): void {
|
|
69
|
-
const parts = fieldPath.split(".");
|
|
70
|
-
descend(obj, parts, byName);
|
|
71
|
-
}
|
|
72
|
-
|
|
116
|
+
/** Walks `obj` along `fieldPath` parts (dot notation with `[]` for arrays and `{}` for
|
|
117
|
+
* additionalProperties-typed maps) and replaces any `!ref` sentinel at the terminal slot
|
|
118
|
+
* with its resolved `{kind, name, alias?}`. Mutates the parent container in place. */
|
|
73
119
|
function descend(
|
|
74
120
|
obj: unknown,
|
|
75
121
|
parts: string[],
|
|
76
|
-
|
|
122
|
+
resolve: (source: string) => ResolvedRef | undefined,
|
|
77
123
|
): void {
|
|
78
124
|
if (obj == null || typeof obj !== "object" || parts.length === 0) return;
|
|
79
125
|
const [head, ...rest] = parts;
|
|
80
126
|
|
|
81
|
-
// Map iteration: descend into every value of the current object.
|
|
82
127
|
if (head === "{}") {
|
|
83
128
|
const container = obj as Record<string, unknown>;
|
|
84
129
|
for (const key of Object.keys(container)) {
|
|
85
130
|
const child = container[key];
|
|
86
131
|
if (rest.length === 0) {
|
|
87
132
|
if (isRefSentinel(child)) {
|
|
88
|
-
const target =
|
|
89
|
-
if (target) container[key] =
|
|
133
|
+
const target = resolve(child.source);
|
|
134
|
+
if (target) container[key] = target;
|
|
90
135
|
}
|
|
91
136
|
} else {
|
|
92
|
-
descend(child, rest,
|
|
137
|
+
descend(child, rest, resolve);
|
|
93
138
|
}
|
|
94
139
|
}
|
|
95
140
|
return;
|
|
@@ -107,21 +152,21 @@ function descend(
|
|
|
107
152
|
if (rest.length === 0) {
|
|
108
153
|
const elem = val[i];
|
|
109
154
|
if (isRefSentinel(elem)) {
|
|
110
|
-
const target =
|
|
111
|
-
if (target) val[i] =
|
|
155
|
+
const target = resolve(elem.source);
|
|
156
|
+
if (target) val[i] = target;
|
|
112
157
|
}
|
|
113
158
|
} else {
|
|
114
|
-
descend(val[i], rest,
|
|
159
|
+
descend(val[i], rest, resolve);
|
|
115
160
|
}
|
|
116
161
|
}
|
|
117
162
|
} else {
|
|
118
163
|
if (rest.length === 0) {
|
|
119
164
|
if (isRefSentinel(val)) {
|
|
120
|
-
const target =
|
|
121
|
-
if (target) container[key] =
|
|
165
|
+
const target = resolve(val.source);
|
|
166
|
+
if (target) container[key] = target;
|
|
122
167
|
}
|
|
123
168
|
} else {
|
|
124
|
-
descend(val, rest,
|
|
169
|
+
descend(val, rest, resolve);
|
|
125
170
|
}
|
|
126
171
|
}
|
|
127
172
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ResourceDefinition, ResourceManifest } from "@telorun/sdk";
|
|
2
|
+
import { isTaggedSentinel } from "@telorun/templating";
|
|
2
3
|
import type { AliasResolver } from "./alias-resolver.js";
|
|
3
4
|
import type { DefinitionRegistry } from "./definition-registry.js";
|
|
4
5
|
|
|
@@ -6,6 +7,11 @@ export interface ThrowsCodeMeta {
|
|
|
6
7
|
data?: Record<string, any>;
|
|
7
8
|
}
|
|
8
9
|
|
|
10
|
+
/** Code a non-`InvokeError` failure surfaces as inside a `catch` block. Mirrors
|
|
11
|
+
* `PLAIN_ERROR_CODE` in `@telorun/run`'s `toSequenceError`: any invoke can throw
|
|
12
|
+
* a plain error, which the catch sees as `error.code === "INTERNAL_ERROR"`. */
|
|
13
|
+
export const PLAIN_ERROR_CODE = "INTERNAL_ERROR";
|
|
14
|
+
|
|
9
15
|
export interface ThrowsUnion {
|
|
10
16
|
/** Code → per-code metadata (data schema, etc). Keys are the declared codes. */
|
|
11
17
|
codes: Map<string, ThrowsCodeMeta>;
|
|
@@ -14,6 +20,12 @@ export interface ThrowsUnion {
|
|
|
14
20
|
* an unknown kind was encountered, or a cycle short-circuited resolution.
|
|
15
21
|
* Callers must treat unbounded unions as requiring a catch-all entry. */
|
|
16
22
|
unbounded: boolean;
|
|
23
|
+
/** True when the block can fail with a non-`InvokeError` (any `invoke:` step).
|
|
24
|
+
* Such a failure surfaces inside an enclosing `catch` as `PLAIN_ERROR_CODE`,
|
|
25
|
+
* so a `throw: { code: "${{ error.code }}" }` rethrow can propagate it. Not
|
|
26
|
+
* injected into `codes` — only seeds `enclosingTryCodes` at a try/catch site,
|
|
27
|
+
* leaving non-rethrow unions untouched. */
|
|
28
|
+
canThrowPlain?: boolean;
|
|
17
29
|
}
|
|
18
30
|
|
|
19
31
|
export interface ResolveCtx {
|
|
@@ -47,6 +59,7 @@ function unionInto(target: ThrowsUnion, src: ThrowsUnion): void {
|
|
|
47
59
|
if (!target.codes.has(code)) target.codes.set(code, meta);
|
|
48
60
|
}
|
|
49
61
|
if (src.unbounded) target.unbounded = true;
|
|
62
|
+
if (src.canThrowPlain) target.canThrowPlain = true;
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
function definitionFor(
|
|
@@ -173,7 +186,11 @@ function collectStepThrows(
|
|
|
173
186
|
ctx: ResolveCtx,
|
|
174
187
|
): ThrowsUnion {
|
|
175
188
|
if (step[invokeField]) {
|
|
176
|
-
|
|
189
|
+
// Any invoked resource can throw a non-InvokeError at runtime, which an
|
|
190
|
+
// enclosing catch surfaces as PLAIN_ERROR_CODE — record that possibility.
|
|
191
|
+
const u = cloneUnion(resolveStepInvokeThrows(step, invokeField, enclosingTryCodes, ctx));
|
|
192
|
+
u.canThrowPlain = true;
|
|
193
|
+
return u;
|
|
177
194
|
}
|
|
178
195
|
|
|
179
196
|
if (step.throw && typeof step.throw === "object") {
|
|
@@ -188,6 +205,10 @@ function collectStepThrows(
|
|
|
188
205
|
// out instead. Sequence-specific subtraction — the plan explicitly
|
|
189
206
|
// anchors this to Run.Sequence's try/catch schema shape.
|
|
190
207
|
const tryCodes = new Set(tryUnion.codes.keys());
|
|
208
|
+
// A plain (non-InvokeError) failure in the try block reaches the catch as
|
|
209
|
+
// `error.code === PLAIN_ERROR_CODE`, so a `throw: { code: error.code }`
|
|
210
|
+
// rethrow can propagate it — seed the set the catch resolves against.
|
|
211
|
+
if (tryUnion.canThrowPlain) tryCodes.add(PLAIN_ERROR_CODE);
|
|
191
212
|
propagated = collectStepArrayThrows(step.catch, invokeField, tryCodes, ctx);
|
|
192
213
|
// Unbounded in the try block still signals the caller to expect
|
|
193
214
|
// arbitrary codes to flow through the catch (e.g. via passthrough).
|
|
@@ -247,6 +268,7 @@ function cloneUnion(u: ThrowsUnion): ThrowsUnion {
|
|
|
247
268
|
const out = emptyUnion();
|
|
248
269
|
for (const [c, m] of u.codes) out.codes.set(c, m);
|
|
249
270
|
out.unbounded = u.unbounded;
|
|
271
|
+
if (u.canThrowPlain) out.canThrowPlain = true;
|
|
250
272
|
return out;
|
|
251
273
|
}
|
|
252
274
|
|
|
@@ -316,16 +338,22 @@ function resolveCodeExpression(
|
|
|
316
338
|
codeInput: unknown,
|
|
317
339
|
enclosingTryCodes: Set<string> | undefined,
|
|
318
340
|
): ThrowsUnion {
|
|
319
|
-
|
|
341
|
+
// A `!cel`-tagged sentinel and a `${{ … }}` string must resolve identically —
|
|
342
|
+
// normalize both to the inner CEL expression (or a bare literal code).
|
|
343
|
+
let expr: string;
|
|
344
|
+
if (isTaggedSentinel(codeInput)) {
|
|
345
|
+
if (codeInput.engine !== "cel") return { codes: new Map(), unbounded: true };
|
|
346
|
+
expr = codeInput.source.trim();
|
|
347
|
+
} else if (typeof codeInput === "string" && codeInput.length > 0) {
|
|
348
|
+
const match = codeInput.match(/^\s*\$\{\{\s*([\s\S]+?)\s*\}\}\s*$/);
|
|
349
|
+
if (!match) {
|
|
350
|
+
return { codes: new Map([[codeInput, {}]]), unbounded: false };
|
|
351
|
+
}
|
|
352
|
+
expr = match[1].trim();
|
|
353
|
+
} else {
|
|
320
354
|
return { codes: new Map(), unbounded: true };
|
|
321
355
|
}
|
|
322
356
|
|
|
323
|
-
const match = codeInput.match(/^\s*\$\{\{\s*([\s\S]+?)\s*\}\}\s*$/);
|
|
324
|
-
if (!match) {
|
|
325
|
-
return { codes: new Map([[codeInput, {}]]), unbounded: false };
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
const expr = match[1].trim();
|
|
329
357
|
const litMatch = expr.match(/^'([^']+)'$|^"([^"]+)"$/);
|
|
330
358
|
if (litMatch) {
|
|
331
359
|
const code = litMatch[1] ?? litMatch[2]!;
|
|
@@ -107,10 +107,46 @@ export function validateReferences(
|
|
|
107
107
|
// source, different sourceLine) keep separate fingerprints and still
|
|
108
108
|
// trip the diagnostic. `analyze()` enforces that every non-system
|
|
109
109
|
// manifest carries both positional fields — no defensive guard needed.
|
|
110
|
+
// Forwarded foreign exports (an imported library's exported instances, carrying a
|
|
111
|
+
// metadata.module that isn't a root module) are resolution TARGETS only: excluded from
|
|
112
|
+
// duplicate detection and local name resolution, and never walked as ref sources.
|
|
113
|
+
const moduleOf = (r: ResourceManifest): string | undefined =>
|
|
114
|
+
(r.metadata as { module?: string } | undefined)?.module;
|
|
115
|
+
// Forwarded exports are flagged by flattenForAnalyzer (`metadata.forwardedExport`); they're
|
|
116
|
+
// cross-module resolution targets only — excluded from duplicate detection and local name
|
|
117
|
+
// resolution, and never walked as ref sources.
|
|
118
|
+
const isForeign = (r: ResourceManifest): boolean =>
|
|
119
|
+
(r.metadata as { forwardedExport?: boolean } | undefined)?.forwardedExport === true;
|
|
120
|
+
// Forwarded exported instances keyed `${module}\0${name}` — the lookup that resolves
|
|
121
|
+
// whether a cross-module `!ref Alias.name` names a real exported instance.
|
|
122
|
+
const byModuleName = new Map<string, ResourceManifest>();
|
|
123
|
+
/** Modules whose import subtree was actually loaded in this analysis. A resolved
|
|
124
|
+
* `Telo.Import` carries `resolvedModuleName` (stamped only once the edge — and thus the
|
|
125
|
+
* imported module — resolved); forwarded exports carry `metadata.module`. Either marks
|
|
126
|
+
* the module loaded independent of how many instances it exports, so a loaded library
|
|
127
|
+
* that exports nothing still reports invalid cross-module refs, while partial single-file
|
|
128
|
+
* analysis (neither present) is skipped to avoid false `UNRESOLVED_REFERENCE`. */
|
|
129
|
+
const loadedModules = new Set<string>();
|
|
130
|
+
for (const r of resources) {
|
|
131
|
+
if (r.kind === "Telo.Import") {
|
|
132
|
+
const m = (r.metadata as { resolvedModuleName?: unknown } | undefined)?.resolvedModuleName;
|
|
133
|
+
if (typeof m === "string") loadedModules.add(m);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (!r.metadata?.name || SYSTEM_KINDS.has(r.kind) || !isForeign(r)) continue;
|
|
137
|
+
const m = moduleOf(r);
|
|
138
|
+
if (!m) continue;
|
|
139
|
+
byModuleName.set(`${m}\0${r.metadata.name as string}`, r);
|
|
140
|
+
loadedModules.add(m);
|
|
141
|
+
}
|
|
142
|
+
const moduleLoaded = (module: string): boolean => loadedModules.has(module);
|
|
143
|
+
const localResources = resources.filter((r) => !isForeign(r));
|
|
144
|
+
|
|
110
145
|
const byNameAll = new Map<string, ResourceManifest[]>();
|
|
111
146
|
const seen = new Set<string>();
|
|
112
147
|
for (const r of resources) {
|
|
113
|
-
if (!r.metadata?.name || SYSTEM_KINDS.has(r.kind) || r.kind === "Telo.Import")
|
|
148
|
+
if (!r.metadata?.name || SYSTEM_KINDS.has(r.kind) || r.kind === "Telo.Import" || isForeign(r))
|
|
149
|
+
continue;
|
|
114
150
|
const name = r.metadata.name as string;
|
|
115
151
|
// `analyze()` guarantees both fields are present on non-system manifests.
|
|
116
152
|
const meta = r.metadata as unknown as { source: string; sourceLine: number };
|
|
@@ -148,6 +184,33 @@ export function validateReferences(
|
|
|
148
184
|
});
|
|
149
185
|
}
|
|
150
186
|
}
|
|
187
|
+
// A resource name must contain no dot. The `!ref` resolver splits the tag's source on
|
|
188
|
+
// the first dot to separate an import alias from the resource name, so a dotted name
|
|
189
|
+
// would mis-resolve into a cross-module lookup. This is the load-bearing invariant of
|
|
190
|
+
// the reference grammar, so it is enforced here rather than left to the (unenforced)
|
|
191
|
+
// casing convention.
|
|
192
|
+
for (const [name, list] of byNameAll) {
|
|
193
|
+
if (!name.includes(".")) continue;
|
|
194
|
+
for (const r of list) {
|
|
195
|
+
const m = r.metadata as { source?: string; sourceLine?: number } | undefined;
|
|
196
|
+
const range =
|
|
197
|
+
typeof m?.sourceLine === "number"
|
|
198
|
+
? {
|
|
199
|
+
start: { line: m.sourceLine, character: 0 },
|
|
200
|
+
end: { line: m.sourceLine, character: Number.MAX_SAFE_INTEGER },
|
|
201
|
+
}
|
|
202
|
+
: undefined;
|
|
203
|
+
diagnostics.push({
|
|
204
|
+
severity: DiagnosticSeverity.Error,
|
|
205
|
+
code: "INVALID_RESOURCE_NAME",
|
|
206
|
+
source: SOURCE,
|
|
207
|
+
message: `${r.kind}/${name}: resource name must not contain '.' — in a '!ref' the '.' separates an import alias from the resource name`,
|
|
208
|
+
...(range ? { range } : {}),
|
|
209
|
+
data: { resource: { kind: r.kind, name }, filePath: m?.source, path: "metadata.name" },
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
151
214
|
// Single-resource map for the resolution / scope lookups below — when a
|
|
152
215
|
// collision exists, falling back to the first occurrence keeps the rest
|
|
153
216
|
// of the pass behaving the same as before the duplicate diagnostic was
|
|
@@ -161,7 +224,7 @@ export function validateReferences(
|
|
|
161
224
|
// enclosure (`inScope`) and the scope manifests visible to it — so this
|
|
162
225
|
// handler only validates, it does not re-walk.
|
|
163
226
|
visitManifest(
|
|
164
|
-
|
|
227
|
+
localResources,
|
|
165
228
|
registry,
|
|
166
229
|
{
|
|
167
230
|
onRef: (e) => {
|
|
@@ -178,14 +241,37 @@ export function validateReferences(
|
|
|
178
241
|
// string/inline ambiguity at the source.
|
|
179
242
|
if (isRefSentinel(val)) {
|
|
180
243
|
const refName = val.source;
|
|
244
|
+
const dot = refName.indexOf(".");
|
|
245
|
+
const aliasPrefix = dot > 0 ? refName.slice(0, dot) : undefined;
|
|
246
|
+
|
|
247
|
+
// Cross-module sentinel left unresolved by Phase 2.5 — it qualifies an import
|
|
248
|
+
// alias. If that module's exports are loaded in this analysis, the miss is real
|
|
249
|
+
// (name not in exports.resources, or a typo); if not (partial single-file
|
|
250
|
+
// analysis), skip rather than emit a false UNRESOLVED_REFERENCE.
|
|
251
|
+
if (aliasPrefix && aliasPrefix !== "Self" && aliases.hasAlias(aliasPrefix)) {
|
|
252
|
+
const module = aliases.moduleForAlias(aliasPrefix);
|
|
253
|
+
if (module && !moduleLoaded(module)) return;
|
|
254
|
+
diagnostics.push({
|
|
255
|
+
severity: DiagnosticSeverity.Error,
|
|
256
|
+
code: "UNRESOLVED_REFERENCE",
|
|
257
|
+
source: SOURCE,
|
|
258
|
+
message: `${resourceLabel}: reference at '${concretePath}' → '${refName}' is not exported by module '${module ?? aliasPrefix}' (add it to exports.resources)`,
|
|
259
|
+
data: { resource: resourceData, filePath, path: concretePath },
|
|
260
|
+
});
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Local reference (bare name or explicit `Self.`-qualified).
|
|
265
|
+
const localName = aliasPrefix === "Self" ? refName.slice(dot + 1) : refName;
|
|
181
266
|
const target =
|
|
182
|
-
byName.get(
|
|
267
|
+
byName.get(localName) ??
|
|
268
|
+
visibleScopeManifests.find((m) => m.metadata?.name === localName);
|
|
183
269
|
if (!target) {
|
|
184
270
|
diagnostics.push({
|
|
185
271
|
severity: DiagnosticSeverity.Error,
|
|
186
272
|
code: "UNRESOLVED_REFERENCE",
|
|
187
273
|
source: SOURCE,
|
|
188
|
-
message: `${resourceLabel}: reference at '${concretePath}' → resource '${
|
|
274
|
+
message: `${resourceLabel}: reference at '${concretePath}' → resource '${localName}' not found`,
|
|
189
275
|
data: { resource: resourceData, filePath, path: concretePath },
|
|
190
276
|
});
|
|
191
277
|
return;
|
|
@@ -248,6 +334,13 @@ export function validateReferences(
|
|
|
248
334
|
// Skip inline resources — Phase 2 normalization hasn't run yet.
|
|
249
335
|
if (isInlineResource(refVal)) return;
|
|
250
336
|
|
|
337
|
+
// Polymorphic ref slots (Application `targets`) accept object forms
|
|
338
|
+
// whose references live in nested slots rather than being a `{kind,
|
|
339
|
+
// name}` ref themselves — inline `{ invoke }` and gated `{ ref }`.
|
|
340
|
+
// Those nested refs are validated via their own field-map entries, so
|
|
341
|
+
// skip the item-level structural check here.
|
|
342
|
+
if (typeof refVal.kind !== "string" && ("invoke" in refVal || "ref" in refVal)) return;
|
|
343
|
+
|
|
251
344
|
// 1. Structural check
|
|
252
345
|
if (typeof refVal.kind !== "string" || typeof refVal.name !== "string") {
|
|
253
346
|
diagnostics.push({
|
|
@@ -273,9 +366,18 @@ export function validateReferences(
|
|
|
273
366
|
}
|
|
274
367
|
|
|
275
368
|
// 3. Resolution check — resource with this name must exist.
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
369
|
+
let exists: boolean;
|
|
370
|
+
if (typeof refVal.alias === "string" && refVal.alias !== "Self") {
|
|
371
|
+
// Cross-module ref resolved by Phase 2.5. Validate against the forwarded
|
|
372
|
+
// exports when loaded; in partial context (module not loaded) assume resolvable.
|
|
373
|
+
const module = aliases.moduleForAlias(refVal.alias);
|
|
374
|
+
exists =
|
|
375
|
+
!module || !moduleLoaded(module) || byModuleName.has(`${module}\0${refVal.name}`);
|
|
376
|
+
} else {
|
|
377
|
+
exists =
|
|
378
|
+
byName.has(refVal.name) ||
|
|
379
|
+
visibleScopeManifests.some((m) => m.metadata?.name === refVal.name);
|
|
380
|
+
}
|
|
279
381
|
if (!exists) {
|
|
280
382
|
diagnostics.push({
|
|
281
383
|
severity: DiagnosticSeverity.Error,
|
|
@@ -296,7 +398,7 @@ export function validateReferences(
|
|
|
296
398
|
// validate the field value against the resulting sub-schema. Driven off the base map
|
|
297
399
|
// (un-expanded) so each schema-from slot is seen as its own site.
|
|
298
400
|
visitManifest(
|
|
299
|
-
|
|
401
|
+
localResources,
|
|
300
402
|
registry,
|
|
301
403
|
{
|
|
302
404
|
onSchemaFrom: (e) => {
|