@telorun/analyzer 0.72.0 → 0.73.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/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +50 -13
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +71 -97
- package/dist/cel-scope-query.d.ts.map +1 -1
- package/dist/cel-scope-query.js +32 -11
- package/dist/cel-scope.d.ts.map +1 -1
- package/dist/cel-scope.js +1 -0
- package/dist/eval-paths.d.ts +36 -5
- package/dist/eval-paths.d.ts.map +1 -1
- package/dist/eval-paths.js +47 -6
- package/dist/extends-resolution.d.ts +12 -0
- package/dist/extends-resolution.d.ts.map +1 -1
- package/dist/extends-resolution.js +23 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/nearest-name.d.ts +22 -0
- package/dist/nearest-name.d.ts.map +1 -0
- package/dist/nearest-name.js +43 -0
- package/dist/release/destinations.d.ts +59 -0
- package/dist/release/destinations.d.ts.map +1 -0
- package/dist/release/destinations.js +75 -0
- package/dist/release/index.d.ts +6 -2
- package/dist/release/index.d.ts.map +1 -1
- package/dist/release/index.js +3 -1
- package/dist/release/ledger.d.ts +20 -11
- package/dist/release/ledger.d.ts.map +1 -1
- package/dist/release/ledger.js +43 -14
- package/dist/release/release-plan.d.ts +3 -2
- package/dist/release/release-plan.d.ts.map +1 -1
- package/dist/release/release-plan.js +12 -19
- package/dist/release/workspace-config.d.ts +114 -34
- package/dist/release/workspace-config.d.ts.map +1 -1
- package/dist/release/workspace-config.js +322 -38
- package/dist/release/workspace-schema.d.ts +41 -0
- package/dist/release/workspace-schema.d.ts.map +1 -0
- package/dist/release/workspace-schema.js +77 -0
- package/dist/schema-error-report.d.ts +7 -0
- package/dist/schema-error-report.d.ts.map +1 -1
- package/dist/schema-error-report.js +9 -4
- package/dist/telo-version.d.ts +1 -1
- package/dist/telo-version.js +1 -1
- package/dist/template-body.d.ts +10 -0
- package/dist/template-body.d.ts.map +1 -1
- package/dist/template-body.js +25 -0
- package/dist/validate-cel-context.d.ts +23 -8
- package/dist/validate-cel-context.d.ts.map +1 -1
- package/dist/validate-cel-context.js +48 -16
- package/dist/validate-exports.d.ts +28 -0
- package/dist/validate-exports.d.ts.map +1 -0
- package/dist/validate-exports.js +146 -0
- package/dist/validate-extends.d.ts.map +1 -1
- package/dist/validate-extends.js +37 -1
- package/dist/validate-provider-coherence.d.ts +4 -8
- package/dist/validate-provider-coherence.d.ts.map +1 -1
- package/dist/validate-provider-coherence.js +12 -123
- package/dist/validate-references.d.ts.map +1 -1
- package/dist/validate-references.js +141 -60
- package/dist/validate-template-body.d.ts +36 -0
- package/dist/validate-template-body.d.ts.map +1 -0
- package/dist/validate-template-body.js +246 -0
- package/package.json +3 -2
- package/src/analyzer.ts +64 -11
- package/src/builtins.ts +85 -97
- package/src/cel-scope-query.ts +28 -9
- package/src/cel-scope.ts +1 -0
- package/src/eval-paths.ts +53 -6
- package/src/extends-resolution.ts +24 -0
- package/src/index.ts +1 -0
- package/src/nearest-name.ts +47 -0
- package/src/release/destinations.ts +105 -0
- package/src/release/index.ts +33 -2
- package/src/release/ledger.ts +51 -21
- package/src/release/release-plan.ts +16 -26
- package/src/release/workspace-config.ts +483 -47
- package/src/release/workspace-schema.ts +99 -0
- package/src/schema-error-report.ts +16 -4
- package/src/telo-version.ts +1 -1
- package/src/template-body.ts +25 -0
- package/src/validate-cel-context.ts +57 -16
- package/src/validate-exports.ts +185 -0
- package/src/validate-extends.ts +43 -0
- package/src/validate-provider-coherence.ts +17 -127
- package/src/validate-references.ts +141 -60
- package/src/validate-template-body.ts +274 -0
- package/dist/validate-template-dispatch.d.ts +0 -27
- package/dist/validate-template-dispatch.d.ts.map +0 -1
- package/dist/validate-template-dispatch.js +0 -95
- package/src/validate-template-dispatch.ts +0 -99
|
@@ -15,10 +15,141 @@ import { DiagnosticSeverity, type AnalysisDiagnostic, type AnalysisContext } fro
|
|
|
15
15
|
import type { AliasResolver } from "./alias-resolver.js";
|
|
16
16
|
import type { DefinitionRegistry } from "./definition-registry.js";
|
|
17
17
|
import { moduleAliasScope } from "./module-alias-scope.js";
|
|
18
|
+
import { isModuleKind } from "./module-kinds.js";
|
|
18
19
|
import { isInjectedDeclaration } from "./resource-input.js";
|
|
19
20
|
|
|
20
21
|
const SOURCE = "telo-analyzer";
|
|
21
22
|
|
|
23
|
+
/** What a name IS on one side of a collision. The three shapes share the
|
|
24
|
+
* kernel's one namespace but read as different things to an author — an alias
|
|
25
|
+
* is not a resource, and a module's own name is not a declaration inside it —
|
|
26
|
+
* so calling all three "resource name" describes two of them wrongly. */
|
|
27
|
+
function describeNamed(m: ResourceManifest, name: string): string {
|
|
28
|
+
if (m.kind === "Telo.Import") return `import alias '${name}'`;
|
|
29
|
+
if (isModuleKind(m.kind)) return `this module's own name (${m.kind} '${name}')`;
|
|
30
|
+
return `resource ${m.kind}/${name}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* WHAT SHARES A NAME AT THE KERNEL.
|
|
35
|
+
*
|
|
36
|
+
* `registerManifest` keys on `metadata.name` alone, per module context, for
|
|
37
|
+
* EVERY kind — so an import alias, a kind definition and an ordinary resource
|
|
38
|
+
* are one namespace. This check used to model a narrower one: `Telo.Import` was
|
|
39
|
+
* excluded outright, on the stated grounds that an alias "lives in a separate
|
|
40
|
+
* namespace from resources", and `Telo.Definition` / `Telo.Abstract` fell out
|
|
41
|
+
* through the ref-validation skip set, which answers a different question. All
|
|
42
|
+
* three passed `telo check` and then died at boot with `ERR_DUPLICATE_RESOURCE`
|
|
43
|
+
* — an application named after one of its own imports (`metadata.name: Schedule`
|
|
44
|
+
* beside `Schedule: oci://…/scheduler`) being the shape that reaches an author,
|
|
45
|
+
* since nothing about the two lines looks like one name written twice.
|
|
46
|
+
*
|
|
47
|
+
* Grouped per DECLARING MODULE, which is what makes including imports sound: a
|
|
48
|
+
* library's own `Telo.Import` / `Telo.Definition` docs are forwarded into a
|
|
49
|
+
* consumer's flat set, and two modules sharing an alias (`Console` in an app and
|
|
50
|
+
* again in a library it imports) is ordinary rather than a collision. A module
|
|
51
|
+
* doc carries no `metadata.module` and IS its own scope — that is what puts an
|
|
52
|
+
* application's own name in the same group as its imports.
|
|
53
|
+
*
|
|
54
|
+
* Kept apart from the resolution lookup it used to share a map with: that one
|
|
55
|
+
* must hold resolution targets alone, so the two disagree about membership by
|
|
56
|
+
* design and merging them re-keyed every bare-name reference.
|
|
57
|
+
*/
|
|
58
|
+
function duplicateNameDiagnostics(
|
|
59
|
+
resources: ResourceManifest[],
|
|
60
|
+
isForeign: (r: ResourceManifest) => boolean,
|
|
61
|
+
moduleOf: (r: ResourceManifest) => string | undefined,
|
|
62
|
+
): AnalysisDiagnostic[] {
|
|
63
|
+
const diagnostics: AnalysisDiagnostic[] = [];
|
|
64
|
+
|
|
65
|
+
const moduleDocNames = new Set<string>();
|
|
66
|
+
for (const r of resources) {
|
|
67
|
+
if (isModuleKind(r.kind) && typeof r.metadata?.name === "string") {
|
|
68
|
+
moduleDocNames.add(r.metadata.name);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// A module doc belongs in the scope its OWN resources are in. Flattened, they
|
|
72
|
+
// carry `metadata.module` equal to its name, so it scopes to itself — which is
|
|
73
|
+
// what puts an application's name beside its imports. Unflattened (a hand-built
|
|
74
|
+
// fixture, a single-file editor analysis) nothing is stamped, so its resources
|
|
75
|
+
// sit in the unnamed scope and the doc has to join them there or a collision
|
|
76
|
+
// with one of them splits across two groups and goes unreported.
|
|
77
|
+
const stampedModules = new Set<string>();
|
|
78
|
+
for (const r of resources) {
|
|
79
|
+
const m = moduleOf(r);
|
|
80
|
+
if (m) stampedModules.add(m);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// scope → name → declarations, so no separator has to be safe against a name.
|
|
84
|
+
const byScope = new Map<string, Map<string, ResourceManifest[]>>();
|
|
85
|
+
const seen = new Set<string>();
|
|
86
|
+
for (const r of resources) {
|
|
87
|
+
if (!r.metadata?.name || isForeign(r)) continue;
|
|
88
|
+
const name = r.metadata.name as string;
|
|
89
|
+
const scope = isModuleKind(r.kind)
|
|
90
|
+
? (stampedModules.has(name) ? name : "")
|
|
91
|
+
: (moduleOf(r) ?? "");
|
|
92
|
+
// A scope with no module doc in this set is an imported library's, whose
|
|
93
|
+
// doc `selectModuleManifestsForAnalysis` drops — its internals are its own
|
|
94
|
+
// author's to fix, and its own `telo check` reports them.
|
|
95
|
+
if (scope !== "" && moduleDocNames.size > 0 && !moduleDocNames.has(scope)) continue;
|
|
96
|
+
// Dedup pipeline echoes — the same physical document emitted twice through
|
|
97
|
+
// an analyzer host's pipeline (telo studio walks a file reachable as both an
|
|
98
|
+
// entry module and an `include:` partial). Keyed on (kind, name, source,
|
|
99
|
+
// sourceLine), so two textually-distinct docs in one file keep separate
|
|
100
|
+
// fingerprints and still trip the diagnostic.
|
|
101
|
+
const meta = r.metadata as unknown as { source?: string; sourceLine?: number };
|
|
102
|
+
const fingerprint = `${r.kind} ${name} ${meta.source} ${meta.sourceLine}`;
|
|
103
|
+
if (seen.has(fingerprint)) continue;
|
|
104
|
+
seen.add(fingerprint);
|
|
105
|
+
let names = byScope.get(scope);
|
|
106
|
+
if (!names) byScope.set(scope, (names = new Map()));
|
|
107
|
+
const existing = names.get(name);
|
|
108
|
+
if (existing) existing.push(r);
|
|
109
|
+
else names.set(name, [r]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const names of byScope.values()) {
|
|
113
|
+
for (const [name, list] of names) {
|
|
114
|
+
if (list.length <= 1) continue;
|
|
115
|
+
const [first, ...rest] = list;
|
|
116
|
+
for (const dup of rest) {
|
|
117
|
+
// Two imports sharing an alias is ONE defect with its own diagnostic
|
|
118
|
+
// (`DUPLICATE_IMPORT_ALIAS`), which says what to do about it; reporting
|
|
119
|
+
// it again here would describe one mistake as two.
|
|
120
|
+
if (dup.kind === "Telo.Import" && first.kind === "Telo.Import") continue;
|
|
121
|
+
const dupMeta = dup.metadata as { source?: string; sourceLine?: number } | undefined;
|
|
122
|
+
// The precomputed range matters because editor hosts resolve positions
|
|
123
|
+
// via a `${file}::${kind}::${name}` lookup, which collides on duplicates.
|
|
124
|
+
const range =
|
|
125
|
+
typeof dupMeta?.sourceLine === "number"
|
|
126
|
+
? {
|
|
127
|
+
start: { line: dupMeta.sourceLine, character: 0 },
|
|
128
|
+
end: { line: dupMeta.sourceLine, character: Number.MAX_SAFE_INTEGER },
|
|
129
|
+
}
|
|
130
|
+
: undefined;
|
|
131
|
+
diagnostics.push({
|
|
132
|
+
severity: DiagnosticSeverity.Error,
|
|
133
|
+
code: "DUPLICATE_RESOURCE_NAME",
|
|
134
|
+
source: SOURCE,
|
|
135
|
+
message:
|
|
136
|
+
`${dup.kind}/${name}: ${describeNamed(dup, name)} collides with ` +
|
|
137
|
+
`${describeNamed(first, name)} declared earlier — the kernel registers both ` +
|
|
138
|
+
`under '${name}' in one namespace, so boot fails with ERR_DUPLICATE_RESOURCE. ` +
|
|
139
|
+
`Rename one of them.`,
|
|
140
|
+
...(range ? { range } : {}),
|
|
141
|
+
data: {
|
|
142
|
+
resource: { kind: dup.kind, name },
|
|
143
|
+
filePath: dupMeta?.source,
|
|
144
|
+
path: "metadata.name",
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return diagnostics;
|
|
151
|
+
}
|
|
152
|
+
|
|
22
153
|
/**
|
|
23
154
|
* Liskov substitutability at a kind constraint: is `resolved` (a canonical
|
|
24
155
|
* `<module>.<Kind>`) accepted where `targetKind` is required?
|
|
@@ -162,40 +293,16 @@ export function validateReferences(
|
|
|
162
293
|
const aliasesByModule = context.aliasesByModule;
|
|
163
294
|
if (!aliases || !registry) return diagnostics;
|
|
164
295
|
|
|
165
|
-
// Build outer resource lookup by name for resolution
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
// kernel refuses to start" surprises.)
|
|
296
|
+
// Build the outer resource lookup by name, for the resolution checks below.
|
|
297
|
+
// WHICH NAMES COLLIDE is a different question over a different namespace and
|
|
298
|
+
// is answered by `duplicateNameDiagnostics`; this map answers only "what does
|
|
299
|
+
// a bare `!ref <name>` resolve to", so it holds resolution TARGETS alone — an
|
|
300
|
+
// import alias and a kind definition are neither, and admitting them here
|
|
301
|
+
// would resolve a reference to something no ref slot can accept.
|
|
172
302
|
//
|
|
173
|
-
//
|
|
174
|
-
//
|
|
175
|
-
//
|
|
176
|
-
// colliding aliases vs. resource names is benign — the alias is only
|
|
177
|
-
// ever read as a kind prefix).
|
|
178
|
-
// Group manifests by name to detect collisions. Two subtleties:
|
|
179
|
-
//
|
|
180
|
-
// 1. Some analyzer hosts emit the SAME physical document twice through
|
|
181
|
-
// their pipeline — e.g. telo studio's `toAnalysisManifests` walks
|
|
182
|
-
// each workspace module's documents independently, and a file
|
|
183
|
-
// reachable from two angles (entry module + `include:` partial)
|
|
184
|
-
// shows up twice. The fingerprint includes `sourceLine` so identical
|
|
185
|
-
// docs (same kind, name, source, AND source line) collapse to one,
|
|
186
|
-
// while two textually-separate documents in the same file (different
|
|
187
|
-
// source lines) keep separate fingerprints and trip the diagnostic.
|
|
188
|
-
// 2. The diagnostic carries a precomputed `range` pointing at the
|
|
189
|
-
// duplicate's source line — editor hosts that resolve diagnostic
|
|
190
|
-
// positions via a `${file}::${kind}::${name}` lookup would otherwise
|
|
191
|
-
// collide on duplicates (Map.set overwrites) and place the squiggle
|
|
192
|
-
// ambiguously. The explicit `range` short-circuits that lookup.
|
|
193
|
-
// Dedup pipeline echoes — the same physical document emitted twice
|
|
194
|
-
// through an analyzer host's pipeline. Keyed on (kind, name, source,
|
|
195
|
-
// sourceLine), so two textually-distinct docs in the same file (same
|
|
196
|
-
// source, different sourceLine) keep separate fingerprints and still
|
|
197
|
-
// trip the diagnostic. `analyze()` enforces that every non-system
|
|
198
|
-
// manifest carries both positional fields — no defensive guard needed.
|
|
303
|
+
// The list per name survives because resolution falls back to the FIRST
|
|
304
|
+
// occurrence when a collision exists, which keeps the rest of the pass
|
|
305
|
+
// behaving as it did before duplicates were reported at all.
|
|
199
306
|
// Forwarded foreign exports (an imported library's exported instances, carrying a
|
|
200
307
|
// metadata.module that isn't a root module) are resolution TARGETS only: excluded from
|
|
201
308
|
// duplicate detection and local name resolution, and never walked as ref sources.
|
|
@@ -246,33 +353,7 @@ export function validateReferences(
|
|
|
246
353
|
if (existing) existing.push(r);
|
|
247
354
|
else byNameAll.set(name, [r]);
|
|
248
355
|
}
|
|
249
|
-
|
|
250
|
-
if (list.length <= 1) continue;
|
|
251
|
-
const [first, ...rest] = list;
|
|
252
|
-
const firstLabel = `${first.kind}/${name}`;
|
|
253
|
-
for (const dup of rest) {
|
|
254
|
-
const dupMeta = dup.metadata as { source?: string; sourceLine?: number } | undefined;
|
|
255
|
-
const range =
|
|
256
|
-
typeof dupMeta?.sourceLine === "number"
|
|
257
|
-
? {
|
|
258
|
-
start: { line: dupMeta.sourceLine, character: 0 },
|
|
259
|
-
end: { line: dupMeta.sourceLine, character: Number.MAX_SAFE_INTEGER },
|
|
260
|
-
}
|
|
261
|
-
: undefined;
|
|
262
|
-
diagnostics.push({
|
|
263
|
-
severity: DiagnosticSeverity.Error,
|
|
264
|
-
code: "DUPLICATE_RESOURCE_NAME",
|
|
265
|
-
source: SOURCE,
|
|
266
|
-
message: `${dup.kind}/${name}: resource name collides with ${firstLabel} declared earlier (kernel runtime would fail with ERR_DUPLICATE_RESOURCE)`,
|
|
267
|
-
...(range ? { range } : {}),
|
|
268
|
-
data: {
|
|
269
|
-
resource: { kind: dup.kind, name },
|
|
270
|
-
filePath: dupMeta?.source,
|
|
271
|
-
path: "metadata.name",
|
|
272
|
-
},
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
}
|
|
356
|
+
diagnostics.push(...duplicateNameDiagnostics(resources, isForeign, moduleOf));
|
|
276
357
|
// The dot rule that used to live here is now the strictest special case of
|
|
277
358
|
// the identifier grammar in `validate-identifier-names.ts` — a dot is one of
|
|
278
359
|
// several characters that make a name unreferenceable, and checking one of
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { nearestName } from "./nearest-name.js";
|
|
2
|
+
import type { ResourceManifest } from "@telorun/sdk";
|
|
3
|
+
import { isRefSentinel, isTaggedSentinel } from "@telorun/templating";
|
|
4
|
+
import type { AliasResolver, ModuleScopes } from "./alias-resolver.js";
|
|
5
|
+
import type { DefinitionRegistry } from "./definition-registry.js";
|
|
6
|
+
import { refSentinelTarget } from "./ref-sentinel-target.js";
|
|
7
|
+
import { isRefEntry, resolveFieldEntries, satisfiesValueBranch } from "./reference-field-map.js";
|
|
8
|
+
import { templateBodies } from "./template-body.js";
|
|
9
|
+
import { DiagnosticSeverity, DiagnosticTag, type AnalysisDiagnostic } from "./types.js";
|
|
10
|
+
|
|
11
|
+
const SOURCE = "telo-analyzer";
|
|
12
|
+
|
|
13
|
+
/** The four slots a `Telo.Definition` names its dispatch target in.
|
|
14
|
+
*
|
|
15
|
+
* WHAT THE TARGET'S CAPABILITY IS IS DELIBERATELY NOT CHECKED. The kernel tests
|
|
16
|
+
* METHOD PRESENCE at dispatch (`entry.instance?.invoke`), so a declared
|
|
17
|
+
* capability says nothing about whether a dispatch works — the same structural
|
|
18
|
+
* -versus-nominal split that keeps `use` uncross-constrained against a slot's
|
|
19
|
+
* target. A check here read `Run.Sequence`'s `capability: Telo.Runnable` and
|
|
20
|
+
* rejected `Ai.Buffered`, a shipping module whose sequence implements `invoke()`
|
|
21
|
+
* over an inputs/outputs contract and dispatches correctly. Trimming the refused
|
|
22
|
+
* set would only move that: any controller may expose a method its capability
|
|
23
|
+
* does not name, and a template body is exactly where that is done on purpose. */
|
|
24
|
+
const DISPATCH_SLOTS = ["invoke", "run", "provide", "mount"] as const;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A TEMPLATE BODY IS WRITTEN LIKE EVERY OTHER MANIFEST, AND CHECKED LIKE ONE.
|
|
28
|
+
*
|
|
29
|
+
* A `Telo.Definition` is in both reference skip sets — its body holds
|
|
30
|
+
* declarations of OTHER kinds, and its dispatch slots carry no `x-telo-ref` — so
|
|
31
|
+
* no reference pass reached it, and each rule about it was added per construct:
|
|
32
|
+
* `provide:` and `mount:` had a target check for the `{ kind, name }` object
|
|
33
|
+
* form, `!ref` had one that switched itself off for any definition with a
|
|
34
|
+
* CEL-named entry, and `invoke:` / `run:` had none. What was tagged was caught
|
|
35
|
+
* and what was a plain name was resolved by the kernel alone. This pass is the
|
|
36
|
+
* one reader of a body's reference surface, and it rests on one spelling:
|
|
37
|
+
*
|
|
38
|
+
* - every `resources:` entry is named by a LITERAL (`TEMPLATE_ENTRY_NAME_DYNAMIC`).
|
|
39
|
+
* Each instance of a template owns its children in a child context of its
|
|
40
|
+
* own, so a per-instance suffix (`self.name + '-query'`) buys nothing — and a
|
|
41
|
+
* `!ref` is looked up verbatim, so a CEL-named sibling is one nothing can name;
|
|
42
|
+
* - a dispatch slot is `!ref <entry>` naming an entry
|
|
43
|
+
* (`TEMPLATE_DISPATCH_UNKNOWN`); the legacy spellings are deprecated rather
|
|
44
|
+
* than refused, since published artifacts carry them. What the target's
|
|
45
|
+
* CAPABILITY is stays unchecked — see `DISPATCH_SLOTS`;
|
|
46
|
+
* - a reference slot INSIDE an entry follows the rule every other slot does —
|
|
47
|
+
* `!ref` or an inline declaration, never `{ kind, name }` or a bare string
|
|
48
|
+
* (`INVALID_REFERENCE_FORM`) — and a bare `!ref` names a sibling or a
|
|
49
|
+
* resource of the defining module (`TEMPLATE_REF_UNKNOWN`). Slots are found
|
|
50
|
+
* through the nested kind's own field map, resolved in the DEFINING module's
|
|
51
|
+
* alias scope, since that is where the body's kinds are written.
|
|
52
|
+
*
|
|
53
|
+
* Entry-module-scoped, like every other declaration check: a published
|
|
54
|
+
* dependency's template body is not the consumer's to fix. Browser-safe.
|
|
55
|
+
*/
|
|
56
|
+
export function validateTemplateBody(
|
|
57
|
+
manifests: ResourceManifest[],
|
|
58
|
+
registry: DefinitionRegistry,
|
|
59
|
+
aliases: AliasResolver,
|
|
60
|
+
aliasesByModule: Map<string, AliasResolver>,
|
|
61
|
+
rootModules: ReadonlySet<string>,
|
|
62
|
+
): AnalysisDiagnostic[] {
|
|
63
|
+
const out: AnalysisDiagnostic[] = [];
|
|
64
|
+
const scopes: ModuleScopes = { aliasesByModule, rootModules };
|
|
65
|
+
|
|
66
|
+
// A definition forwarded from an imported library is that library's to fix;
|
|
67
|
+
// the imports name them (the `validate-extends` rule).
|
|
68
|
+
const importedModules = new Set<string>();
|
|
69
|
+
// Every named instance, by declaring module — what a body's bare `!ref` may
|
|
70
|
+
// reach beside its siblings. A manifest with no stamp belongs to the entry.
|
|
71
|
+
const instancesByModule = new Map<string | undefined, Set<string>>();
|
|
72
|
+
for (const m of manifests) {
|
|
73
|
+
const name = m.metadata?.name;
|
|
74
|
+
if (typeof name !== "string") continue;
|
|
75
|
+
if (m.kind === "Telo.Import") {
|
|
76
|
+
const resolved = (m.metadata as { resolvedModuleName?: string }).resolvedModuleName;
|
|
77
|
+
if (resolved) importedModules.add(resolved);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (m.kind === "Telo.Definition" || m.kind === "Telo.Abstract") continue;
|
|
81
|
+
if (m.kind === "Telo.Library" || m.kind === "Telo.Application") continue;
|
|
82
|
+
const mod = (m.metadata as { module?: string } | undefined)?.module;
|
|
83
|
+
let set = instancesByModule.get(mod);
|
|
84
|
+
if (!set) instancesByModule.set(mod, (set = new Set()));
|
|
85
|
+
set.add(name);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
for (const m of manifests) {
|
|
89
|
+
if (m.kind !== "Telo.Definition") continue;
|
|
90
|
+
const meta = m.metadata as { name?: string; module?: string; source?: string } | undefined;
|
|
91
|
+
const name = meta?.name;
|
|
92
|
+
if (!name) continue;
|
|
93
|
+
if (meta?.module && importedModules.has(meta.module)) continue;
|
|
94
|
+
|
|
95
|
+
const bodies = (m as Record<string, unknown>).resources;
|
|
96
|
+
const resourceRef = { kind: m.kind, name };
|
|
97
|
+
const filePath = meta?.source;
|
|
98
|
+
const label = `${m.kind}/${name}`;
|
|
99
|
+
const report = (code: string, path: string, message: string, fix?: string) =>
|
|
100
|
+
out.push({
|
|
101
|
+
severity: DiagnosticSeverity.Error,
|
|
102
|
+
code,
|
|
103
|
+
source: SOURCE,
|
|
104
|
+
message: `${label}: ${message}`,
|
|
105
|
+
data: {
|
|
106
|
+
resource: resourceRef,
|
|
107
|
+
filePath,
|
|
108
|
+
path,
|
|
109
|
+
...(fix ? { fix: { replacement: fix } } : {}),
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
/** A spelling the kernel still READS. Warning-grade because refusing it
|
|
114
|
+
* would break every app pinning a version that carries it, and tagged so an
|
|
115
|
+
* editor strikes it through rather than merely colouring it — the
|
|
116
|
+
* `DEPRECATED_KIND` shape, for the same reason. */
|
|
117
|
+
const deprecate = (code: string, path: string, message: string) =>
|
|
118
|
+
out.push({
|
|
119
|
+
severity: DiagnosticSeverity.Warning,
|
|
120
|
+
code,
|
|
121
|
+
source: SOURCE,
|
|
122
|
+
message: `${label}: ${message}`,
|
|
123
|
+
tags: [DiagnosticTag.Deprecated],
|
|
124
|
+
data: { resource: resourceRef, filePath, path },
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const entries = Array.isArray(bodies) ? bodies : [];
|
|
128
|
+
const siblings: string[] = [];
|
|
129
|
+
let anyDynamic = false;
|
|
130
|
+
entries.forEach((entry, i) => {
|
|
131
|
+
const entryName = (entry as { metadata?: { name?: unknown } } | undefined)?.metadata?.name;
|
|
132
|
+
if (typeof entryName === "string" && !entryName.includes("${{")) {
|
|
133
|
+
siblings.push(entryName);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (entryName === undefined) return;
|
|
137
|
+
anyDynamic = true;
|
|
138
|
+
deprecate(
|
|
139
|
+
"DEPRECATED_TEMPLATE_ENTRY_NAME",
|
|
140
|
+
`resources[${i}].metadata.name`,
|
|
141
|
+
`a 'resources:' entry named by ${describeDynamic(entryName)} is deprecated — name it ` +
|
|
142
|
+
`with a literal and dispatch to it with '!ref'. Each instance of a template owns its ` +
|
|
143
|
+
`children in a child context of its own, so a per-instance suffix buys nothing, and a ` +
|
|
144
|
+
`'!ref' is looked up verbatim, so nothing can name a computed entry. While one is ` +
|
|
145
|
+
`present the target checks below are switched off for this kind.`,
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Siblings PLUS the declaring module's own resources, because that is what
|
|
150
|
+
// the runtime resolves at a reference slot: a body's `!ref` is stamped
|
|
151
|
+
// `{kind, name}` with an empty kind for a non-sibling, and Phase-5 injection
|
|
152
|
+
// dispatches by NAME and recovers the kind from the instance it finds — so
|
|
153
|
+
// `client: !ref moduleClient` in a body reaches the module-level client and
|
|
154
|
+
// works. (A step's `invoke:` is a different path that does NOT: see the
|
|
155
|
+
// `resources` note in `validate-template-body`'s own tests.)
|
|
156
|
+
const reachable = new Set([
|
|
157
|
+
...siblings,
|
|
158
|
+
...(instancesByModule.get(meta?.module) ?? []),
|
|
159
|
+
]);
|
|
160
|
+
|
|
161
|
+
// --- dispatch slots ---
|
|
162
|
+
for (const slot of DISPATCH_SLOTS) {
|
|
163
|
+
const value = (m as Record<string, unknown>)[slot];
|
|
164
|
+
if (value == null) continue;
|
|
165
|
+
if (!isRefSentinel(value)) {
|
|
166
|
+
deprecate(
|
|
167
|
+
"DEPRECATED_TEMPLATE_DISPATCH_FORM",
|
|
168
|
+
slot,
|
|
169
|
+
`'${slot}:' written as ${describeDispatchValue(value)} is deprecated — ` +
|
|
170
|
+
`write '!ref <entry>', the spelling every other reference uses. ` +
|
|
171
|
+
`Available: ${siblings.join(", ") || "(none)"}.`,
|
|
172
|
+
);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const source = value.source;
|
|
176
|
+
const target = source.startsWith("Self.") ? source.slice("Self.".length) : source;
|
|
177
|
+
if (siblings.includes(target)) continue;
|
|
178
|
+
// A dynamic sibling has already been reported, and might be the one
|
|
179
|
+
// meant — a second diagnostic about the same line would be noise.
|
|
180
|
+
if (anyDynamic) continue;
|
|
181
|
+
const suggestion = nearestName(target, siblings);
|
|
182
|
+
report(
|
|
183
|
+
"TEMPLATE_DISPATCH_UNKNOWN",
|
|
184
|
+
slot,
|
|
185
|
+
`'${slot}: !ref ${source}' names no entry in 'resources:'. ` +
|
|
186
|
+
`Available: ${siblings.join(", ") || "(none)"}.` +
|
|
187
|
+
(suggestion ? ` Did you mean '${suggestion}'?` : ""),
|
|
188
|
+
suggestion,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// --- reference slots inside each entry ---
|
|
193
|
+
for (const body of templateBodies(m, registry, aliases, scopes)) {
|
|
194
|
+
const kind = body.manifest.kind;
|
|
195
|
+
const fieldMap = registry.expandedFieldMapForResource(
|
|
196
|
+
{ ...body.manifest, metadata: { ...(body.manifest.metadata ?? {}), module: meta?.module } },
|
|
197
|
+
aliases,
|
|
198
|
+
aliasesByModule,
|
|
199
|
+
);
|
|
200
|
+
if (!fieldMap) continue;
|
|
201
|
+
for (const [fieldPath, entry] of fieldMap) {
|
|
202
|
+
if (!isRefEntry(entry)) continue;
|
|
203
|
+
for (const site of resolveFieldEntries(body.manifest, fieldPath)) {
|
|
204
|
+
const value = site.value;
|
|
205
|
+
if (value == null) continue;
|
|
206
|
+
const path = `${body.prefix}.${site.path}`;
|
|
207
|
+
if (isTaggedSentinel(value)) {
|
|
208
|
+
const target = refSentinelTarget(value);
|
|
209
|
+
if (!target) continue;
|
|
210
|
+
const { alias, name: refName } = target;
|
|
211
|
+
if (alias && alias !== "Self") continue;
|
|
212
|
+
if (reachable.has(refName)) continue;
|
|
213
|
+
const suggestion = nearestName(refName, [...reachable]);
|
|
214
|
+
report(
|
|
215
|
+
"TEMPLATE_REF_UNKNOWN",
|
|
216
|
+
path,
|
|
217
|
+
`'${site.path}: !ref ${value.source}' on the ${kind} entry names no sibling ` +
|
|
218
|
+
`'resources:' entry and no resource of this module. ` +
|
|
219
|
+
`Siblings: ${siblings.filter((s) => s !== body.manifest.metadata?.name).join(", ") || "(none)"}.` +
|
|
220
|
+
(suggestion ? ` Did you mean '${suggestion}'?` : ""),
|
|
221
|
+
suggestion,
|
|
222
|
+
);
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const hasValueBranch = (entry.valueBranches?.length ?? 0) > 0;
|
|
226
|
+
if (hasValueBranch && typeof value !== "object") continue;
|
|
227
|
+
if (satisfiesValueBranch(value, entry.valueBranches, registry)) continue;
|
|
228
|
+
if (typeof value === "string") {
|
|
229
|
+
if (value.includes("${{")) continue;
|
|
230
|
+
report(
|
|
231
|
+
"INVALID_REFERENCE_FORM",
|
|
232
|
+
path,
|
|
233
|
+
`string reference at '${site.path}' on the ${kind} entry → '${value}' is not ` +
|
|
234
|
+
`supported; write it as '!ref ${value}'`,
|
|
235
|
+
);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
239
|
+
const obj = value as Record<string, unknown>;
|
|
240
|
+
if (typeof obj.kind === "string" && obj.name !== undefined) {
|
|
241
|
+
report(
|
|
242
|
+
"INVALID_REFERENCE_FORM",
|
|
243
|
+
path,
|
|
244
|
+
`object reference '{ kind, name }' at '${site.path}' on the ${kind} entry is ` +
|
|
245
|
+
`not supported; write it as '!ref ${describeRefName(obj.name)}'`,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function describeDynamic(name: unknown): string {
|
|
258
|
+
if (isTaggedSentinel(name)) return `the expression '${name.source}'`;
|
|
259
|
+
if (typeof name === "string") return `the template '${name}'`;
|
|
260
|
+
return `a ${typeof name}`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function describeDispatchValue(value: unknown): string {
|
|
264
|
+
if (isTaggedSentinel(value)) return `a '!${value.engine}' expression`;
|
|
265
|
+
if (typeof value === "string") return `the string '${value}'`;
|
|
266
|
+
if (value && typeof value === "object" && "name" in value) return "the '{ kind, name }' object form";
|
|
267
|
+
return `a ${typeof value}`;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function describeRefName(name: unknown): string {
|
|
271
|
+
if (typeof name === "string") return name;
|
|
272
|
+
if (isTaggedSentinel(name)) return "<entry>";
|
|
273
|
+
return String(name);
|
|
274
|
+
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ResourceManifest } from "@telorun/sdk";
|
|
2
|
-
import { type AnalysisDiagnostic } from "./types.js";
|
|
3
|
-
/**
|
|
4
|
-
* A `!ref` at a definition's dispatch slot must name a sibling `resources:`
|
|
5
|
-
* entry.
|
|
6
|
-
*
|
|
7
|
-
* `!ref` is the one spelling an author — and the editor's rename, completion and
|
|
8
|
-
* go-to-definition — expects to be RESOLVED. But a `Telo.Definition` is in both
|
|
9
|
-
* `REF_VALIDATION_SKIP_KINDS` and `REF_RESOLUTION_SKIP_KINDS`, and the dispatch
|
|
10
|
-
* slots carry no `x-telo-ref` (the accepted targets are the definition's own
|
|
11
|
-
* template-internal entries, not resources of any module), so no reference pass
|
|
12
|
-
* reaches them. Introducing the tag at a slot nothing resolves would be worse
|
|
13
|
-
* than the string form it replaces: a typo that used to be an obvious runtime
|
|
14
|
-
* miss becomes a typo in a construct that advertises static resolution.
|
|
15
|
-
*
|
|
16
|
-
* Decidable only when every sibling name is LITERAL. A template routinely names
|
|
17
|
-
* its entries with CEL (`name: !cel "self.name + '-query'"`), and an expression
|
|
18
|
-
* could expand to the referenced name — so one dynamic sibling switches the
|
|
19
|
-
* check off for that definition rather than inventing a miss.
|
|
20
|
-
*
|
|
21
|
-
* Entry-module-scoped, like every other declaration check: a published
|
|
22
|
-
* dependency's template body is not the consumer's to fix.
|
|
23
|
-
*
|
|
24
|
-
* Browser-safe.
|
|
25
|
-
*/
|
|
26
|
-
export declare function validateTemplateDispatch(manifests: ResourceManifest[], rootModules: ReadonlySet<string>): AnalysisDiagnostic[];
|
|
27
|
-
//# sourceMappingURL=validate-template-dispatch.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validate-template-dispatch.d.ts","sourceRoot":"","sources":["../src/validate-template-dispatch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGrD,OAAO,EAAsB,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AASzE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,gBAAgB,EAAE,EAC7B,WAAW,EAAE,WAAW,CAAC,MAAM,CAAC,GAC/B,kBAAkB,EAAE,CAgDtB"}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { isRefSentinel } from "@telorun/templating";
|
|
2
|
-
import { distance } from "./levenshtein.js";
|
|
3
|
-
import { DiagnosticSeverity } from "./types.js";
|
|
4
|
-
const SOURCE = "telo-analyzer";
|
|
5
|
-
/** The four slots a `Telo.Definition` names its dispatch target in. Each takes
|
|
6
|
-
* the same grammar; which one is legal for a given capability is
|
|
7
|
-
* `validate-provider-coherence`'s question, not this one's. */
|
|
8
|
-
const DISPATCH_FIELDS = ["invoke", "run", "provide", "mount"];
|
|
9
|
-
/**
|
|
10
|
-
* A `!ref` at a definition's dispatch slot must name a sibling `resources:`
|
|
11
|
-
* entry.
|
|
12
|
-
*
|
|
13
|
-
* `!ref` is the one spelling an author — and the editor's rename, completion and
|
|
14
|
-
* go-to-definition — expects to be RESOLVED. But a `Telo.Definition` is in both
|
|
15
|
-
* `REF_VALIDATION_SKIP_KINDS` and `REF_RESOLUTION_SKIP_KINDS`, and the dispatch
|
|
16
|
-
* slots carry no `x-telo-ref` (the accepted targets are the definition's own
|
|
17
|
-
* template-internal entries, not resources of any module), so no reference pass
|
|
18
|
-
* reaches them. Introducing the tag at a slot nothing resolves would be worse
|
|
19
|
-
* than the string form it replaces: a typo that used to be an obvious runtime
|
|
20
|
-
* miss becomes a typo in a construct that advertises static resolution.
|
|
21
|
-
*
|
|
22
|
-
* Decidable only when every sibling name is LITERAL. A template routinely names
|
|
23
|
-
* its entries with CEL (`name: !cel "self.name + '-query'"`), and an expression
|
|
24
|
-
* could expand to the referenced name — so one dynamic sibling switches the
|
|
25
|
-
* check off for that definition rather than inventing a miss.
|
|
26
|
-
*
|
|
27
|
-
* Entry-module-scoped, like every other declaration check: a published
|
|
28
|
-
* dependency's template body is not the consumer's to fix.
|
|
29
|
-
*
|
|
30
|
-
* Browser-safe.
|
|
31
|
-
*/
|
|
32
|
-
export function validateTemplateDispatch(manifests, rootModules) {
|
|
33
|
-
const out = [];
|
|
34
|
-
for (const m of manifests) {
|
|
35
|
-
if (m.kind !== "Telo.Definition")
|
|
36
|
-
continue;
|
|
37
|
-
const meta = m.metadata;
|
|
38
|
-
const name = meta?.name;
|
|
39
|
-
if (!name)
|
|
40
|
-
continue;
|
|
41
|
-
if (meta?.module && !rootModules.has(meta.module))
|
|
42
|
-
continue;
|
|
43
|
-
const bodies = m.resources;
|
|
44
|
-
if (!Array.isArray(bodies))
|
|
45
|
-
continue;
|
|
46
|
-
const siblings = [];
|
|
47
|
-
let anyDynamic = false;
|
|
48
|
-
for (const entry of bodies) {
|
|
49
|
-
const entryName = entry?.metadata?.name;
|
|
50
|
-
if (typeof entryName === "string" && !entryName.includes("${{"))
|
|
51
|
-
siblings.push(entryName);
|
|
52
|
-
else if (entryName !== undefined)
|
|
53
|
-
anyDynamic = true;
|
|
54
|
-
}
|
|
55
|
-
if (anyDynamic)
|
|
56
|
-
continue;
|
|
57
|
-
for (const field of DISPATCH_FIELDS) {
|
|
58
|
-
const value = m[field];
|
|
59
|
-
if (!isRefSentinel(value))
|
|
60
|
-
continue;
|
|
61
|
-
const source = value.source;
|
|
62
|
-
const target = source.startsWith("Self.") ? source.slice("Self.".length) : source;
|
|
63
|
-
if (siblings.includes(target))
|
|
64
|
-
continue;
|
|
65
|
-
const suggestion = nearest(target, siblings);
|
|
66
|
-
out.push({
|
|
67
|
-
severity: DiagnosticSeverity.Error,
|
|
68
|
-
code: "TEMPLATE_DISPATCH_UNKNOWN",
|
|
69
|
-
source: SOURCE,
|
|
70
|
-
message: `${m.kind}/${name}: '${field}: !ref ${source}' names no entry in 'resources:'. ` +
|
|
71
|
-
`Available: ${siblings.join(", ") || "(none)"}.` +
|
|
72
|
-
(suggestion ? ` Did you mean '${suggestion}'?` : ""),
|
|
73
|
-
data: {
|
|
74
|
-
resource: { kind: m.kind, name },
|
|
75
|
-
filePath: meta?.source,
|
|
76
|
-
path: field,
|
|
77
|
-
...(suggestion ? { fix: { replacement: suggestion } } : {}),
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return out;
|
|
83
|
-
}
|
|
84
|
-
/** The closest sibling name, when one is close enough to be a typo rather than a
|
|
85
|
-
* different name. */
|
|
86
|
-
function nearest(target, candidates) {
|
|
87
|
-
let best;
|
|
88
|
-
for (const candidate of candidates) {
|
|
89
|
-
const d = distance(target, candidate);
|
|
90
|
-
if (!best || d < best.d)
|
|
91
|
-
best = { name: candidate, d };
|
|
92
|
-
}
|
|
93
|
-
const limit = Math.max(1, Math.floor(target.length / 3));
|
|
94
|
-
return best && best.d <= limit ? best.name : undefined;
|
|
95
|
-
}
|