@telorun/analyzer 0.12.0 → 0.13.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/README.md +2 -2
- package/dist/analysis-registry.d.ts +12 -0
- package/dist/analysis-registry.d.ts.map +1 -1
- package/dist/analysis-registry.js +15 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +131 -85
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +25 -0
- package/dist/cel-environment.d.ts +1 -1
- package/dist/cel-environment.d.ts.map +1 -1
- package/dist/cel-environment.js +40 -2
- package/dist/definition-registry.d.ts +12 -1
- package/dist/definition-registry.d.ts.map +1 -1
- package/dist/definition-registry.js +20 -1
- package/dist/dependency-graph.d.ts.map +1 -1
- package/dist/dependency-graph.js +41 -62
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/kernel-globals.d.ts +1 -1
- package/dist/kernel-globals.d.ts.map +1 -1
- package/dist/kernel-globals.js +19 -1
- package/dist/manifest-visitor.d.ts +109 -0
- package/dist/manifest-visitor.d.ts.map +1 -0
- package/dist/manifest-visitor.js +110 -0
- package/dist/reference-field-map.d.ts +1 -0
- package/dist/reference-field-map.d.ts.map +1 -1
- package/dist/reference-field-map.js +1 -1
- package/dist/schema-compat.d.ts +14 -0
- package/dist/schema-compat.d.ts.map +1 -1
- package/dist/schema-compat.js +38 -2
- package/dist/validate-cel-context.d.ts +14 -0
- package/dist/validate-cel-context.d.ts.map +1 -1
- package/dist/validate-cel-context.js +38 -0
- package/dist/validate-nested-inline.d.ts +30 -0
- package/dist/validate-nested-inline.d.ts.map +1 -0
- package/dist/validate-nested-inline.js +129 -0
- package/dist/validate-references.d.ts.map +1 -1
- package/dist/validate-references.js +117 -160
- package/dist/validate-unused-declarations.d.ts +25 -0
- package/dist/validate-unused-declarations.d.ts.map +1 -0
- package/dist/validate-unused-declarations.js +91 -0
- package/package.json +2 -2
- package/src/analysis-registry.ts +20 -0
- package/src/analyzer.ts +217 -158
- package/src/builtins.ts +25 -0
- package/src/cel-environment.ts +42 -1
- package/src/definition-registry.ts +20 -1
- package/src/dependency-graph.ts +37 -52
- package/src/index.ts +11 -0
- package/src/kernel-globals.ts +22 -1
- package/src/manifest-visitor.ts +251 -0
- package/src/reference-field-map.ts +1 -1
- package/src/schema-compat.ts +38 -2
- package/src/validate-cel-context.ts +50 -0
- package/src/validate-nested-inline.ts +158 -0
- package/src/validate-references.ts +168 -211
- package/src/validate-unused-declarations.ts +95 -0
- package/dist/adapters/http-adapter.d.ts +0 -10
- package/dist/adapters/http-adapter.d.ts.map +0 -1
- package/dist/adapters/http-adapter.js +0 -18
- package/dist/adapters/node-adapter.d.ts +0 -17
- package/dist/adapters/node-adapter.d.ts.map +0 -1
- package/dist/adapters/node-adapter.js +0 -71
- package/dist/adapters/registry-adapter.d.ts +0 -15
- package/dist/adapters/registry-adapter.d.ts.map +0 -1
- package/dist/adapters/registry-adapter.js +0 -53
|
@@ -219,3 +219,41 @@ function navigatePath(obj, segments) {
|
|
|
219
219
|
}
|
|
220
220
|
return cur;
|
|
221
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Walk a JSON Schema tree and collect all `x-telo-context` annotations,
|
|
224
|
+
* returning them as `{ scope, schema }` pairs using JSONPath-style scopes —
|
|
225
|
+
* the same format the analyzer uses for CEL context validation.
|
|
226
|
+
*
|
|
227
|
+
* Result is sorted by scope specificity (longer scope first) so that the
|
|
228
|
+
* per-expression resolver's first-match-wins logic picks the most-specific
|
|
229
|
+
* context. Without this, a broader ancestor scope (e.g. `$.resources[*]`)
|
|
230
|
+
* could shadow a narrower descendant scope whose activation differs.
|
|
231
|
+
*/
|
|
232
|
+
export function extractContextsFromSchema(schema, path = "$") {
|
|
233
|
+
const all = collectContexts(schema, path);
|
|
234
|
+
return all.sort((a, b) => b.scope.length - a.scope.length);
|
|
235
|
+
}
|
|
236
|
+
function collectContexts(schema, path) {
|
|
237
|
+
if (!schema || typeof schema !== "object")
|
|
238
|
+
return [];
|
|
239
|
+
const results = [];
|
|
240
|
+
if (schema["x-telo-context"]) {
|
|
241
|
+
results.push({ scope: path, schema: schema["x-telo-context"] });
|
|
242
|
+
}
|
|
243
|
+
if (schema.properties) {
|
|
244
|
+
for (const [key, value] of Object.entries(schema.properties)) {
|
|
245
|
+
results.push(...collectContexts(value, `${path}.${key}`));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (schema.items && typeof schema.items === "object") {
|
|
249
|
+
results.push(...collectContexts(schema.items, `${path}[*]`));
|
|
250
|
+
}
|
|
251
|
+
for (const key of ["oneOf", "anyOf", "allOf"]) {
|
|
252
|
+
if (Array.isArray(schema[key])) {
|
|
253
|
+
for (const subschema of schema[key]) {
|
|
254
|
+
results.push(...collectContexts(subschema, path));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return results;
|
|
259
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ResourceManifest } from "@telorun/sdk";
|
|
2
|
+
import { type AnalysisDiagnostic } from "./types.js";
|
|
3
|
+
/** Minimal view of a definition needed to validate an inline resource's config. */
|
|
4
|
+
export interface InlineDefinitionLookup {
|
|
5
|
+
(kind: string): {
|
|
6
|
+
schema?: Record<string, any>;
|
|
7
|
+
} | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Validates inline resources nested inside a resource body against their kind's
|
|
11
|
+
* config schema. The per-resource walk in `analyze()` validates a resource's
|
|
12
|
+
* own top-level config; inline resources at `x-telo-ref` slots reachable only
|
|
13
|
+
* through a local `$ref` (notably `Run.Sequence`'s `steps[].invoke`, hidden
|
|
14
|
+
* behind `#/$defs/step`) never reach the reference field map, so they would
|
|
15
|
+
* otherwise escape schema validation — e.g. `invoke: { kind: Console.ReadLine,
|
|
16
|
+
* prompt: "…" }`, where `prompt` belongs in the step's `inputs`, not the config.
|
|
17
|
+
*
|
|
18
|
+
* Walks the manifest data together with its definition schema, resolving local
|
|
19
|
+
* `$ref`s (so step trees of arbitrary depth are covered). At each `x-telo-ref`
|
|
20
|
+
* slot holding an inline resource, the inline's config is validated against its
|
|
21
|
+
* own kind's schema, then recursed into so inline resources nested inside
|
|
22
|
+
* inline resources are covered.
|
|
23
|
+
*
|
|
24
|
+
* Non-mutating: reads `manifest` and emits diagnostics anchored to its identity
|
|
25
|
+
* and a concrete dotted path matching the position-index key format;
|
|
26
|
+
* `rewriteSyntheticOrigins` reroutes those on inline-extracted (synthetic)
|
|
27
|
+
* manifests back to the root doc.
|
|
28
|
+
*/
|
|
29
|
+
export declare function validateNestedInlineResources(manifest: ResourceManifest, rootSchema: Record<string, any>, lookupDefinition: InlineDefinitionLookup): AnalysisDiagnostic[];
|
|
30
|
+
//# sourceMappingURL=validate-nested-inline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-nested-inline.d.ts","sourceRoot":"","sources":["../src/validate-nested-inline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAQrD,OAAO,EAAsB,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAIzE,mFAAmF;AACnF,MAAM,WAAW,sBAAsB;IACrC,CAAC,IAAI,EAAE,MAAM,GAAG;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,GAAG,SAAS,CAAC;CAC9D;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,6BAA6B,CAC3C,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC/B,gBAAgB,EAAE,sBAAsB,GACvC,kBAAkB,EAAE,CAoHtB"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { collectRefs, isInlineResource } from "./reference-field-map.js";
|
|
2
|
+
import { collectProperties, resolveRef, substituteCelFields, validateAgainstSchema, } from "./schema-compat.js";
|
|
3
|
+
import { DiagnosticSeverity } from "./types.js";
|
|
4
|
+
const SOURCE = "telo-analyzer";
|
|
5
|
+
/**
|
|
6
|
+
* Validates inline resources nested inside a resource body against their kind's
|
|
7
|
+
* config schema. The per-resource walk in `analyze()` validates a resource's
|
|
8
|
+
* own top-level config; inline resources at `x-telo-ref` slots reachable only
|
|
9
|
+
* through a local `$ref` (notably `Run.Sequence`'s `steps[].invoke`, hidden
|
|
10
|
+
* behind `#/$defs/step`) never reach the reference field map, so they would
|
|
11
|
+
* otherwise escape schema validation — e.g. `invoke: { kind: Console.ReadLine,
|
|
12
|
+
* prompt: "…" }`, where `prompt` belongs in the step's `inputs`, not the config.
|
|
13
|
+
*
|
|
14
|
+
* Walks the manifest data together with its definition schema, resolving local
|
|
15
|
+
* `$ref`s (so step trees of arbitrary depth are covered). At each `x-telo-ref`
|
|
16
|
+
* slot holding an inline resource, the inline's config is validated against its
|
|
17
|
+
* own kind's schema, then recursed into so inline resources nested inside
|
|
18
|
+
* inline resources are covered.
|
|
19
|
+
*
|
|
20
|
+
* Non-mutating: reads `manifest` and emits diagnostics anchored to its identity
|
|
21
|
+
* and a concrete dotted path matching the position-index key format;
|
|
22
|
+
* `rewriteSyntheticOrigins` reroutes those on inline-extracted (synthetic)
|
|
23
|
+
* manifests back to the root doc.
|
|
24
|
+
*/
|
|
25
|
+
export function validateNestedInlineResources(manifest, rootSchema, lookupDefinition) {
|
|
26
|
+
const diagnostics = [];
|
|
27
|
+
const resource = { kind: manifest.kind, name: manifest.metadata?.name };
|
|
28
|
+
const filePath = manifest.metadata?.source;
|
|
29
|
+
function validateInline(inline, path) {
|
|
30
|
+
const kind = inline.kind;
|
|
31
|
+
const def = lookupDefinition(kind);
|
|
32
|
+
// Unknown kind: these `$ref`-hidden slots are invisible to the field-map
|
|
33
|
+
// driven reference checks too, so nothing else would flag it — report here.
|
|
34
|
+
if (!def) {
|
|
35
|
+
diagnostics.push({
|
|
36
|
+
severity: DiagnosticSeverity.Error,
|
|
37
|
+
code: "UNDEFINED_KIND",
|
|
38
|
+
source: SOURCE,
|
|
39
|
+
message: `${resource.kind}/${resource.name}: inline ${kind} at '${path}': No Telo.Definition found for kind '${kind}'.`,
|
|
40
|
+
data: { resource, filePath, path: `${path}.kind` },
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
// Kind exists but declares no config schema (e.g. a pure Telo.Type): no
|
|
45
|
+
// config to validate and no schema-declared slots to nest resources in.
|
|
46
|
+
if (!def.schema)
|
|
47
|
+
return;
|
|
48
|
+
const schema = def.schema;
|
|
49
|
+
// `kind` / `metadata` are implicit on every resource; inject them so a
|
|
50
|
+
// strict `additionalProperties: false` config schema doesn't reject them.
|
|
51
|
+
const effectiveSchema = schema.additionalProperties === false
|
|
52
|
+
? {
|
|
53
|
+
...schema,
|
|
54
|
+
properties: {
|
|
55
|
+
kind: { type: "string" },
|
|
56
|
+
metadata: { type: "object" },
|
|
57
|
+
...schema.properties,
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
: schema;
|
|
61
|
+
// Inline resources omit `metadata` — it is synthesized when the kernel
|
|
62
|
+
// registers them (and by `normalizeInlineResources` for extracted slots,
|
|
63
|
+
// which assigns a derived `metadata.name`). Config schemas conventionally
|
|
64
|
+
// declare `required: ["metadata", …]` with `metadata.name` required, so add
|
|
65
|
+
// a placeholder before validating to mirror the post-registration shape.
|
|
66
|
+
const existingMeta = inline.metadata && typeof inline.metadata === "object"
|
|
67
|
+
? inline.metadata
|
|
68
|
+
: {};
|
|
69
|
+
const data = { ...inline, metadata: { name: "__inline__", ...existingMeta } };
|
|
70
|
+
const substituted = substituteCelFields(data, effectiveSchema, effectiveSchema);
|
|
71
|
+
for (const issue of validateAgainstSchema(substituted, effectiveSchema)) {
|
|
72
|
+
diagnostics.push({
|
|
73
|
+
severity: DiagnosticSeverity.Error,
|
|
74
|
+
code: "SCHEMA_VIOLATION",
|
|
75
|
+
source: SOURCE,
|
|
76
|
+
message: `${resource.kind}/${resource.name}: inline ${kind} at '${path}': ${issue.message}`,
|
|
77
|
+
data: { resource, filePath, path: issue.path ? `${path}.${issue.path}` : path },
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
// Recurse into the inline body against its own schema so deeper inline
|
|
81
|
+
// resources (e.g. an inline Run.Sequence's own steps) are validated too.
|
|
82
|
+
walk(inline, schema, schema, path);
|
|
83
|
+
}
|
|
84
|
+
function walk(data, schema, schemaRoot, path) {
|
|
85
|
+
if (!schema || typeof schema !== "object")
|
|
86
|
+
return;
|
|
87
|
+
const resolved = resolveRef(schema, schemaRoot);
|
|
88
|
+
// Reference slot: the value is either a named reference (`{kind, name}`,
|
|
89
|
+
// validated as its own manifest) or an inline resource to validate here.
|
|
90
|
+
if (collectRefs(resolved).length > 0) {
|
|
91
|
+
if (data &&
|
|
92
|
+
typeof data === "object" &&
|
|
93
|
+
!Array.isArray(data) &&
|
|
94
|
+
isInlineResource(data)) {
|
|
95
|
+
validateInline(data, path);
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (Array.isArray(data)) {
|
|
100
|
+
const itemSchema = resolved.items;
|
|
101
|
+
if (!itemSchema)
|
|
102
|
+
return;
|
|
103
|
+
for (let i = 0; i < data.length; i++) {
|
|
104
|
+
walk(data[i], itemSchema, schemaRoot, `${path}[${i}]`);
|
|
105
|
+
}
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (data && typeof data === "object") {
|
|
109
|
+
const props = collectProperties(resolved);
|
|
110
|
+
const additional = resolved.additionalProperties &&
|
|
111
|
+
typeof resolved.additionalProperties === "object" &&
|
|
112
|
+
!Array.isArray(resolved.additionalProperties)
|
|
113
|
+
? resolved.additionalProperties
|
|
114
|
+
: undefined;
|
|
115
|
+
// Descend only where the schema declares structure. Freeform fields
|
|
116
|
+
// (`additionalProperties: true`, e.g. step `inputs`) carry caller data
|
|
117
|
+
// that may coincidentally look like `{kind: …}`; not descending there
|
|
118
|
+
// keeps the inline-resource detection anchored to real ref slots.
|
|
119
|
+
for (const [key, value] of Object.entries(data)) {
|
|
120
|
+
const propSchema = props[key] ?? additional;
|
|
121
|
+
if (!propSchema)
|
|
122
|
+
continue;
|
|
123
|
+
walk(value, propSchema, schemaRoot, path ? `${path}.${key}` : key);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
walk(manifest, rootSchema, rootSchema, "");
|
|
128
|
+
return diagnostics;
|
|
129
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-references.d.ts","sourceRoot":"","sources":["../src/validate-references.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"validate-references.d.ts","sourceRoot":"","sources":["../src/validate-references.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAMrD,OAAO,EAAsB,KAAK,kBAAkB,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAC;AA4C/F;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,gBAAgB,EAAE,EAC7B,OAAO,EAAE,eAAe,GACvB,kBAAkB,EAAE,CA0YtB"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isRefSentinel } from "@telorun/templating";
|
|
2
|
-
import {
|
|
2
|
+
import { visitManifest } from "./manifest-visitor.js";
|
|
3
|
+
import { isInlineResource, resolveFieldEntries, resolveFieldValues } from "./reference-field-map.js";
|
|
3
4
|
import { navigateJsonPointer } from "./schema-compat.js";
|
|
4
5
|
import { REF_VALIDATION_SKIP_KINDS as SYSTEM_KINDS } from "./system-kinds.js";
|
|
5
6
|
import { DiagnosticSeverity } from "./types.js";
|
|
@@ -147,142 +148,36 @@ export function validateReferences(resources, context) {
|
|
|
147
148
|
const byName = new Map();
|
|
148
149
|
for (const [name, list] of byNameAll)
|
|
149
150
|
byName.set(name, list[0]);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const raw = resolveFieldValues(r, fieldPath)
|
|
171
|
-
.flatMap((v) => (Array.isArray(v) ? v : [v]))
|
|
172
|
-
.filter((v) => !!v && typeof v === "object");
|
|
173
|
-
const pointers = Array.isArray(entry.scope) ? entry.scope : [entry.scope];
|
|
174
|
-
for (const pointer of pointers) {
|
|
175
|
-
scopeManifestsByPointer.set(pointer, raw);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
const scopePrefixes = Array.from(scopeManifestsByPointer.keys()).map((p) => p.replace(/^\//, "").replace(/\//g, "."));
|
|
179
|
-
for (const [fieldPath, entry] of fieldMap) {
|
|
180
|
-
if (!isRefEntry(entry))
|
|
181
|
-
continue;
|
|
182
|
-
const inScope = scopePrefixes.some((prefix) => fieldPath === prefix ||
|
|
183
|
-
fieldPath.startsWith(prefix + ".") ||
|
|
184
|
-
fieldPath.startsWith(prefix + "["));
|
|
185
|
-
// Scope manifests visible to this ref path.
|
|
186
|
-
const visibleScopeManifests = [];
|
|
187
|
-
if (inScope) {
|
|
188
|
-
for (const [pointer, manifests] of scopeManifestsByPointer) {
|
|
189
|
-
const prefix = pointer.replace(/^\//, "").replace(/\//g, ".");
|
|
190
|
-
if (fieldPath === prefix ||
|
|
191
|
-
fieldPath.startsWith(prefix + ".") ||
|
|
192
|
-
fieldPath.startsWith(prefix + "[")) {
|
|
193
|
-
visibleScopeManifests.push(...manifests);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
for (const { value: val, path: concretePath } of resolveFieldEntries(r, fieldPath)) {
|
|
198
|
-
if (!val)
|
|
199
|
-
continue;
|
|
200
|
-
// `!ref <name>` sentinel — bare resource name marked at parse time as a
|
|
201
|
-
// reference. Look it up against the slot's x-telo-ref constraint exactly
|
|
202
|
-
// like the legacy bare-string path; the only difference is the value's
|
|
203
|
-
// shape (a TaggedSentinel rather than a raw string), which removed the
|
|
204
|
-
// string/inline ambiguity at the source.
|
|
205
|
-
if (isRefSentinel(val)) {
|
|
206
|
-
const refName = val.source;
|
|
207
|
-
const target = byName.get(refName) ?? visibleScopeManifests.find((m) => m.metadata?.name === refName);
|
|
208
|
-
if (!target) {
|
|
209
|
-
diagnostics.push({
|
|
210
|
-
severity: DiagnosticSeverity.Error,
|
|
211
|
-
code: "UNRESOLVED_REFERENCE",
|
|
212
|
-
source: SOURCE,
|
|
213
|
-
message: `${resourceLabel}: reference at '${concretePath}' → resource '${refName}' not found`,
|
|
214
|
-
data: { resource: resourceData, filePath, path: concretePath },
|
|
215
|
-
});
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
const kindErrors = checkKind(target.kind, entry, registry, aliases);
|
|
219
|
-
if (kindErrors.length > 0) {
|
|
220
|
-
diagnostics.push({
|
|
221
|
-
severity: DiagnosticSeverity.Error,
|
|
222
|
-
code: "REFERENCE_KIND_MISMATCH",
|
|
223
|
-
source: SOURCE,
|
|
224
|
-
message: `${resourceLabel}: reference at '${concretePath}' → ${kindErrors.join("; ")}`,
|
|
225
|
-
data: { resource: resourceData, filePath, path: concretePath },
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
// Name-only reference (plain string) — look up by name to validate.
|
|
231
|
-
// Qualified references use "Kind.Name" format (e.g. "Http.Api.PaymentApi");
|
|
232
|
-
// extract the resource name from the last dot segment.
|
|
233
|
-
if (typeof val === "string") {
|
|
234
|
-
const lastDot = val.lastIndexOf(".");
|
|
235
|
-
const refName = lastDot > 0 ? val.slice(lastDot + 1) : val;
|
|
236
|
-
const refKindPrefix = lastDot > 0 ? val.slice(0, lastDot) : undefined;
|
|
237
|
-
const target = byName.get(refName) ?? visibleScopeManifests.find((m) => m.metadata?.name === refName);
|
|
238
|
-
if (!target) {
|
|
239
|
-
// Cross-module reference: "Alias.ResourceName" (single dot, bare alias prefix).
|
|
240
|
-
// The resource lives in the imported module's scope and can't be validated here.
|
|
241
|
-
// Multi-dot prefixes like "Alias.Kind.Name" are local resources with qualified
|
|
242
|
-
// kinds — those must be validated.
|
|
243
|
-
if (refKindPrefix && !refKindPrefix.includes(".") && aliases.hasAlias(refKindPrefix)) {
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
diagnostics.push({
|
|
247
|
-
severity: DiagnosticSeverity.Error,
|
|
248
|
-
code: "UNRESOLVED_REFERENCE",
|
|
249
|
-
source: SOURCE,
|
|
250
|
-
message: `${resourceLabel}: reference at '${concretePath}' → resource '${val}' not found`,
|
|
251
|
-
data: { resource: resourceData, filePath, path: concretePath },
|
|
252
|
-
});
|
|
253
|
-
continue;
|
|
254
|
-
}
|
|
255
|
-
const kindErrors = checkKind(target.kind, entry, registry, aliases);
|
|
256
|
-
if (kindErrors.length > 0) {
|
|
257
|
-
diagnostics.push({
|
|
258
|
-
severity: DiagnosticSeverity.Error,
|
|
259
|
-
code: "REFERENCE_KIND_MISMATCH",
|
|
260
|
-
source: SOURCE,
|
|
261
|
-
message: `${resourceLabel}: reference at '${concretePath}' → ${kindErrors.join("; ")}`,
|
|
262
|
-
data: { resource: resourceData, filePath, path: concretePath },
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
if (typeof val !== "object")
|
|
268
|
-
continue;
|
|
269
|
-
const refVal = val;
|
|
270
|
-
// Skip inline resources — Phase 2 normalization hasn't run yet.
|
|
271
|
-
if (isInlineResource(refVal))
|
|
272
|
-
continue;
|
|
273
|
-
// 1. Structural check
|
|
274
|
-
if (typeof refVal.kind !== "string" || typeof refVal.name !== "string") {
|
|
151
|
+
// Phase 3 — per-ref validation. The walker supplies each ref site already
|
|
152
|
+
// resolved against the schema-from-expanded field map, with its source
|
|
153
|
+
// enclosure (`inScope`) and the scope manifests visible to it — so this
|
|
154
|
+
// handler only validates, it does not re-walk.
|
|
155
|
+
visitManifest(resources, registry, {
|
|
156
|
+
onRef: (e) => {
|
|
157
|
+
const r = e.source;
|
|
158
|
+
const resourceLabel = `${r.kind}/${r.metadata.name}`;
|
|
159
|
+
const resourceData = { kind: r.kind, name: r.metadata.name };
|
|
160
|
+
const filePath = r.metadata?.source;
|
|
161
|
+
const { value: val, concretePath, entry, visibleScopeManifests } = e;
|
|
162
|
+
// `!ref <name>` sentinel — bare resource name marked at parse time as a
|
|
163
|
+
// reference. Look it up against the slot's x-telo-ref constraint exactly
|
|
164
|
+
// like the legacy bare-string path; the only difference is the value's
|
|
165
|
+
// shape (a TaggedSentinel rather than a raw string), which removed the
|
|
166
|
+
// string/inline ambiguity at the source.
|
|
167
|
+
if (isRefSentinel(val)) {
|
|
168
|
+
const refName = val.source;
|
|
169
|
+
const target = byName.get(refName) ?? visibleScopeManifests.find((m) => m.metadata?.name === refName);
|
|
170
|
+
if (!target) {
|
|
275
171
|
diagnostics.push({
|
|
276
172
|
severity: DiagnosticSeverity.Error,
|
|
277
|
-
code: "
|
|
173
|
+
code: "UNRESOLVED_REFERENCE",
|
|
278
174
|
source: SOURCE,
|
|
279
|
-
message: `${resourceLabel}: reference at '${concretePath}'
|
|
175
|
+
message: `${resourceLabel}: reference at '${concretePath}' → resource '${refName}' not found`,
|
|
280
176
|
data: { resource: resourceData, filePath, path: concretePath },
|
|
281
177
|
});
|
|
282
|
-
|
|
178
|
+
return;
|
|
283
179
|
}
|
|
284
|
-
|
|
285
|
-
const kindErrors = checkKind(refVal.kind, entry, registry, aliases);
|
|
180
|
+
const kindErrors = checkKind(target.kind, entry, registry, aliases);
|
|
286
181
|
if (kindErrors.length > 0) {
|
|
287
182
|
diagnostics.push({
|
|
288
183
|
severity: DiagnosticSeverity.Error,
|
|
@@ -292,38 +187,100 @@ export function validateReferences(resources, context) {
|
|
|
292
187
|
data: { resource: resourceData, filePath, path: concretePath },
|
|
293
188
|
});
|
|
294
189
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
// Name-only reference (plain string) — look up by name to validate.
|
|
193
|
+
// Qualified references use "Kind.Name" format (e.g. "Http.Api.PaymentApi");
|
|
194
|
+
// extract the resource name from the last dot segment.
|
|
195
|
+
if (typeof val === "string") {
|
|
196
|
+
const lastDot = val.lastIndexOf(".");
|
|
197
|
+
const refName = lastDot > 0 ? val.slice(lastDot + 1) : val;
|
|
198
|
+
const refKindPrefix = lastDot > 0 ? val.slice(0, lastDot) : undefined;
|
|
199
|
+
const target = byName.get(refName) ?? visibleScopeManifests.find((m) => m.metadata?.name === refName);
|
|
200
|
+
if (!target) {
|
|
201
|
+
// Cross-module reference: "Alias.ResourceName" (single dot, bare alias prefix).
|
|
202
|
+
// The resource lives in the imported module's scope and can't be validated here.
|
|
203
|
+
// Multi-dot prefixes like "Alias.Kind.Name" are local resources with qualified
|
|
204
|
+
// kinds — those must be validated.
|
|
205
|
+
if (refKindPrefix && !refKindPrefix.includes(".") && aliases.hasAlias(refKindPrefix)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
299
208
|
diagnostics.push({
|
|
300
209
|
severity: DiagnosticSeverity.Error,
|
|
301
210
|
code: "UNRESOLVED_REFERENCE",
|
|
302
211
|
source: SOURCE,
|
|
303
|
-
message: `${resourceLabel}: reference at '${concretePath}' → resource '${
|
|
212
|
+
message: `${resourceLabel}: reference at '${concretePath}' → resource '${val}' not found`,
|
|
213
|
+
data: { resource: resourceData, filePath, path: concretePath },
|
|
214
|
+
});
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const kindErrors = checkKind(target.kind, entry, registry, aliases);
|
|
218
|
+
if (kindErrors.length > 0) {
|
|
219
|
+
diagnostics.push({
|
|
220
|
+
severity: DiagnosticSeverity.Error,
|
|
221
|
+
code: "REFERENCE_KIND_MISMATCH",
|
|
222
|
+
source: SOURCE,
|
|
223
|
+
message: `${resourceLabel}: reference at '${concretePath}' → ${kindErrors.join("; ")}`,
|
|
304
224
|
data: { resource: resourceData, filePath, path: concretePath },
|
|
305
225
|
});
|
|
306
226
|
}
|
|
227
|
+
return;
|
|
307
228
|
}
|
|
308
|
-
|
|
309
|
-
|
|
229
|
+
if (typeof val !== "object")
|
|
230
|
+
return;
|
|
231
|
+
const refVal = val;
|
|
232
|
+
// Skip inline resources — Phase 2 normalization hasn't run yet.
|
|
233
|
+
if (isInlineResource(refVal))
|
|
234
|
+
return;
|
|
235
|
+
// 1. Structural check
|
|
236
|
+
if (typeof refVal.kind !== "string" || typeof refVal.name !== "string") {
|
|
237
|
+
diagnostics.push({
|
|
238
|
+
severity: DiagnosticSeverity.Error,
|
|
239
|
+
code: "INVALID_REFERENCE",
|
|
240
|
+
source: SOURCE,
|
|
241
|
+
message: `${resourceLabel}: reference at '${concretePath}' must have string 'kind' and 'name' fields`,
|
|
242
|
+
data: { resource: resourceData, filePath, path: concretePath },
|
|
243
|
+
});
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
// 2. Kind check
|
|
247
|
+
const kindErrors = checkKind(refVal.kind, entry, registry, aliases);
|
|
248
|
+
if (kindErrors.length > 0) {
|
|
249
|
+
diagnostics.push({
|
|
250
|
+
severity: DiagnosticSeverity.Error,
|
|
251
|
+
code: "REFERENCE_KIND_MISMATCH",
|
|
252
|
+
source: SOURCE,
|
|
253
|
+
message: `${resourceLabel}: reference at '${concretePath}' → ${kindErrors.join("; ")}`,
|
|
254
|
+
data: { resource: resourceData, filePath, path: concretePath },
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
// 3. Resolution check — resource with this name must exist.
|
|
258
|
+
const exists = byName.has(refVal.name) ||
|
|
259
|
+
visibleScopeManifests.some((m) => m.metadata?.name === refVal.name);
|
|
260
|
+
if (!exists) {
|
|
261
|
+
diagnostics.push({
|
|
262
|
+
severity: DiagnosticSeverity.Error,
|
|
263
|
+
code: "UNRESOLVED_REFERENCE",
|
|
264
|
+
source: SOURCE,
|
|
265
|
+
message: `${resourceLabel}: reference at '${concretePath}' → resource '${refVal.name}' not found`,
|
|
266
|
+
data: { resource: resourceData, filePath, path: concretePath },
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
}, { aliases, aliasesByModule, skipKinds: SYSTEM_KINDS, expand: true });
|
|
310
271
|
// Phase 3b — x-telo-schema-from validation.
|
|
311
272
|
// For each field with a schemaFrom path expression, resolve the anchor ref to get the
|
|
312
273
|
// concrete kind, navigate the JSON Pointer into that kind's definition schema, and
|
|
313
|
-
// validate the field value against the resulting sub-schema.
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
for (const [fieldPath, entry] of fieldMap) {
|
|
324
|
-
if (!isSchemaFromEntry(entry))
|
|
325
|
-
continue;
|
|
326
|
-
const { schemaFrom } = entry;
|
|
274
|
+
// validate the field value against the resulting sub-schema. Driven off the base map
|
|
275
|
+
// (un-expanded) so each schema-from slot is seen as its own site.
|
|
276
|
+
visitManifest(resources, registry, {
|
|
277
|
+
onSchemaFrom: (e) => {
|
|
278
|
+
const r = e.source;
|
|
279
|
+
const fieldPath = e.fieldPath;
|
|
280
|
+
const resourceLabel = `${r.kind}/${r.metadata.name}`;
|
|
281
|
+
const resourceData = { kind: r.kind, name: r.metadata.name };
|
|
282
|
+
const filePath = r.metadata?.source;
|
|
283
|
+
const { schemaFrom } = e.entry;
|
|
327
284
|
const isAbsolute = schemaFrom.startsWith("/");
|
|
328
285
|
const expr = isAbsolute ? schemaFrom.slice(1) : schemaFrom;
|
|
329
286
|
const slashIdx = expr.indexOf("/");
|
|
@@ -335,7 +292,7 @@ export function validateReferences(resources, context) {
|
|
|
335
292
|
message: `${resourceLabel}: x-telo-schema-from "${schemaFrom}" must contain at least one "/" to separate anchor from JSON Pointer`,
|
|
336
293
|
data: { resource: resourceData, filePath, path: fieldPath },
|
|
337
294
|
});
|
|
338
|
-
|
|
295
|
+
return;
|
|
339
296
|
}
|
|
340
297
|
const anchorName = expr.slice(0, slashIdx);
|
|
341
298
|
const jsonPointer = "/" + expr.slice(slashIdx + 1);
|
|
@@ -360,7 +317,7 @@ export function validateReferences(resources, context) {
|
|
|
360
317
|
message: `${resourceLabel}: x-telo-schema-from at '${fieldPath}' → cannot resolve alias '${anchorName}'`,
|
|
361
318
|
data: { resource: resourceData, filePath, path: fieldPath },
|
|
362
319
|
});
|
|
363
|
-
|
|
320
|
+
return;
|
|
364
321
|
}
|
|
365
322
|
const targetDef = registry.resolve(targetKind);
|
|
366
323
|
if (!targetDef?.schema) {
|
|
@@ -371,7 +328,7 @@ export function validateReferences(resources, context) {
|
|
|
371
328
|
message: `${resourceLabel}: x-telo-schema-from at '${fieldPath}' → kind '${targetKind}' has no schema`,
|
|
372
329
|
data: { resource: resourceData, filePath, path: fieldPath },
|
|
373
330
|
});
|
|
374
|
-
|
|
331
|
+
return;
|
|
375
332
|
}
|
|
376
333
|
const subSchema = navigateJsonPointer(targetDef.schema, jsonPointer);
|
|
377
334
|
if (subSchema === undefined) {
|
|
@@ -382,7 +339,7 @@ export function validateReferences(resources, context) {
|
|
|
382
339
|
message: `${resourceLabel}: x-telo-schema-from at '${fieldPath}' → kind '${targetKind}' has no schema path '${jsonPointer}'`,
|
|
383
340
|
data: { resource: resourceData, filePath, path: fieldPath },
|
|
384
341
|
});
|
|
385
|
-
|
|
342
|
+
return;
|
|
386
343
|
}
|
|
387
344
|
for (const { value: fieldValue, path: concretePath } of resolveFieldEntries(r, fieldPath)) {
|
|
388
345
|
if (fieldValue == null)
|
|
@@ -398,7 +355,7 @@ export function validateReferences(resources, context) {
|
|
|
398
355
|
});
|
|
399
356
|
}
|
|
400
357
|
}
|
|
401
|
-
|
|
358
|
+
return;
|
|
402
359
|
}
|
|
403
360
|
// Derive the anchor path in the resource config.
|
|
404
361
|
let anchorPath;
|
|
@@ -413,7 +370,7 @@ export function validateReferences(resources, context) {
|
|
|
413
370
|
}
|
|
414
371
|
const anchorValues = resolveFieldValues(r, anchorPath);
|
|
415
372
|
if (anchorValues.length === 0)
|
|
416
|
-
|
|
373
|
+
return; // anchor field not set — nothing to validate
|
|
417
374
|
const fieldEntries = resolveFieldEntries(r, fieldPath);
|
|
418
375
|
for (let i = 0; i < fieldEntries.length; i++) {
|
|
419
376
|
const { value: fieldValue, path: concretePath } = fieldEntries[i];
|
|
@@ -460,7 +417,7 @@ export function validateReferences(resources, context) {
|
|
|
460
417
|
});
|
|
461
418
|
}
|
|
462
419
|
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
420
|
+
},
|
|
421
|
+
}, { aliases, aliasesByModule, skipKinds: SYSTEM_KINDS, expand: false });
|
|
465
422
|
return diagnostics;
|
|
466
423
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Environment } from "@marcbachmann/cel-js";
|
|
2
|
+
import type { ResourceManifest } from "@telorun/sdk";
|
|
3
|
+
import { type AnalysisDiagnostic } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Warn about declared `variables` / `secrets` / `ports` entries that no CEL
|
|
6
|
+
* expression references. A declared-but-unconsumed entry is dead weight at
|
|
7
|
+
* best and misleading at worst (an unbound `ports` entry makes a runner
|
|
8
|
+
* advertise a port the app never listens on).
|
|
9
|
+
*
|
|
10
|
+
* Generic across all three namespaces. References are collected from every CEL
|
|
11
|
+
* expression (both `${{ … }}` and `!cel`, via `walkCelExpressions`) by
|
|
12
|
+
* extracting member-access chains: a `<ns>.<name>` chain marks `<name>` used.
|
|
13
|
+
* Dynamic access (`<ns>[expr]`, or the namespace passed whole, e.g.
|
|
14
|
+
* `keys(variables)`) yields a chain that stops at the namespace root — that
|
|
15
|
+
* can't be attributed to a name, so the whole namespace is conservatively
|
|
16
|
+
* suppressed to avoid false positives.
|
|
17
|
+
*
|
|
18
|
+
* Application-only: an Application's `variables` / `secrets` / `ports` flow
|
|
19
|
+
* exclusively through CEL (into resource fields, or into `Telo.Import` inputs),
|
|
20
|
+
* so unreferenced means dead. A `Telo.Library`'s `variables` / `secrets` are a
|
|
21
|
+
* public input contract consumed by its controllers — invisible to CEL
|
|
22
|
+
* analysis — so they are deliberately not flagged.
|
|
23
|
+
*/
|
|
24
|
+
export declare function validateUnusedDeclarations(manifests: ResourceManifest[], celEnv: Environment): AnalysisDiagnostic[];
|
|
25
|
+
//# sourceMappingURL=validate-unused-declarations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-unused-declarations.d.ts","sourceRoot":"","sources":["../src/validate-unused-declarations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,EAAE,KAAK,kBAAkB,EAAsB,MAAM,YAAY,CAAC;AASzE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,gBAAgB,EAAE,EAC7B,MAAM,EAAE,WAAW,GAClB,kBAAkB,EAAE,CA2DtB"}
|