@telorun/analyzer 0.12.1 → 1.0.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/analysis-registry.d.ts +13 -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 +154 -83
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +85 -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/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 +124 -0
- package/dist/manifest-visitor.d.ts.map +1 -0
- package/dist/manifest-visitor.js +181 -0
- package/dist/reference-field-map.js +16 -0
- 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/schema-compat.d.ts +10 -0
- package/dist/schema-compat.d.ts.map +1 -1
- package/dist/schema-compat.js +32 -0
- 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-references.d.ts.map +1 -1
- package/dist/validate-references.js +124 -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 +3 -3
- package/src/analysis-registry.ts +20 -0
- package/src/analyzer.ts +256 -168
- package/src/builtins.ts +85 -0
- package/src/cel-environment.ts +42 -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 +340 -0
- package/src/reference-field-map.ts +14 -0
- package/src/resolve-throws-union.ts +36 -8
- package/src/schema-compat.ts +32 -0
- package/src/validate-cel-context.ts +50 -0
- package/src/validate-references.ts +175 -211
- package/src/validate-unused-declarations.ts +95 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { extractAccessChains, INDEX_SEGMENT, walkCelExpressions } from "@telorun/templating";
|
|
2
|
+
import { DiagnosticSeverity } from "./types.js";
|
|
3
|
+
const SOURCE = "telo-analyzer";
|
|
4
|
+
/** Module-doc namespaces whose entries are consumed via `<ns>.<name>` CEL
|
|
5
|
+
* access. One table drives the whole check — adding a namespace is an entry,
|
|
6
|
+
* not a branch. */
|
|
7
|
+
const NAMESPACES = ["variables", "secrets", "ports"];
|
|
8
|
+
/**
|
|
9
|
+
* Warn about declared `variables` / `secrets` / `ports` entries that no CEL
|
|
10
|
+
* expression references. A declared-but-unconsumed entry is dead weight at
|
|
11
|
+
* best and misleading at worst (an unbound `ports` entry makes a runner
|
|
12
|
+
* advertise a port the app never listens on).
|
|
13
|
+
*
|
|
14
|
+
* Generic across all three namespaces. References are collected from every CEL
|
|
15
|
+
* expression (both `${{ … }}` and `!cel`, via `walkCelExpressions`) by
|
|
16
|
+
* extracting member-access chains: a `<ns>.<name>` chain marks `<name>` used.
|
|
17
|
+
* Dynamic access (`<ns>[expr]`, or the namespace passed whole, e.g.
|
|
18
|
+
* `keys(variables)`) yields a chain that stops at the namespace root — that
|
|
19
|
+
* can't be attributed to a name, so the whole namespace is conservatively
|
|
20
|
+
* suppressed to avoid false positives.
|
|
21
|
+
*
|
|
22
|
+
* Application-only: an Application's `variables` / `secrets` / `ports` flow
|
|
23
|
+
* exclusively through CEL (into resource fields, or into `Telo.Import` inputs),
|
|
24
|
+
* so unreferenced means dead. A `Telo.Library`'s `variables` / `secrets` are a
|
|
25
|
+
* public input contract consumed by its controllers — invisible to CEL
|
|
26
|
+
* analysis — so they are deliberately not flagged.
|
|
27
|
+
*/
|
|
28
|
+
export function validateUnusedDeclarations(manifests, celEnv) {
|
|
29
|
+
const moduleManifest = manifests.find((m) => m.kind === "Telo.Application");
|
|
30
|
+
if (!moduleManifest)
|
|
31
|
+
return [];
|
|
32
|
+
const declared = new Map();
|
|
33
|
+
for (const ns of NAMESPACES) {
|
|
34
|
+
const block = moduleManifest[ns];
|
|
35
|
+
if (block && typeof block === "object" && !Array.isArray(block)) {
|
|
36
|
+
const names = Object.keys(block);
|
|
37
|
+
if (names.length > 0)
|
|
38
|
+
declared.set(ns, names);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (declared.size === 0)
|
|
42
|
+
return [];
|
|
43
|
+
const used = new Map(NAMESPACES.map((ns) => [ns, new Set()]));
|
|
44
|
+
const suppressed = new Set();
|
|
45
|
+
for (const m of manifests) {
|
|
46
|
+
walkCelExpressions(m, "", (expr, _path, engineName) => {
|
|
47
|
+
if (engineName !== "cel")
|
|
48
|
+
return;
|
|
49
|
+
let ast;
|
|
50
|
+
try {
|
|
51
|
+
ast = celEnv.parse(expr).ast;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return; // syntax errors are reported by the CEL engine pass
|
|
55
|
+
}
|
|
56
|
+
for (const chain of extractAccessChains(ast)) {
|
|
57
|
+
const ns = chain[0];
|
|
58
|
+
if (!used.has(ns))
|
|
59
|
+
continue;
|
|
60
|
+
const member = chain[1];
|
|
61
|
+
// No static member after the namespace root — either the namespace is
|
|
62
|
+
// used whole (`keys(ports)` → ["ports"]) or accessed dynamically
|
|
63
|
+
// (`ports[x]` → ["ports", "[*]"]). Neither can be attributed to a
|
|
64
|
+
// declared name, so suppress the namespace rather than false-positive.
|
|
65
|
+
if (member === undefined || member === INDEX_SEGMENT)
|
|
66
|
+
suppressed.add(ns);
|
|
67
|
+
else
|
|
68
|
+
used.get(ns).add(member);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const diagnostics = [];
|
|
73
|
+
const filePath = moduleManifest.metadata?.source;
|
|
74
|
+
for (const [ns, names] of declared) {
|
|
75
|
+
if (suppressed.has(ns))
|
|
76
|
+
continue;
|
|
77
|
+
const seen = used.get(ns);
|
|
78
|
+
for (const name of names) {
|
|
79
|
+
if (seen.has(name))
|
|
80
|
+
continue;
|
|
81
|
+
diagnostics.push({
|
|
82
|
+
severity: DiagnosticSeverity.Warning,
|
|
83
|
+
code: "UNUSED_DECLARATION",
|
|
84
|
+
source: SOURCE,
|
|
85
|
+
message: `${ns}.${name} is declared but never referenced in any CEL expression.`,
|
|
86
|
+
data: { filePath, path: `${ns}.${name}` },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return diagnostics;
|
|
91
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/analyzer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Telo Analyzer - Static manifest validator for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"ajv-formats": "^3.0.1",
|
|
43
43
|
"jsonpath-plus": "^10.3.0",
|
|
44
44
|
"yaml": "^2.8.3",
|
|
45
|
-
"@telorun/templating": "0.
|
|
45
|
+
"@telorun/templating": "1.0.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@types/node": "^20.0.0",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"vitest": "^2.1.8"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@telorun/sdk": "0.
|
|
53
|
+
"@telorun/sdk": "1.0.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "tsc -p tsconfig.lib.json",
|
package/src/analysis-registry.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { AliasResolver } from "./alias-resolver.js";
|
|
|
3
3
|
import { KERNEL_BUILTINS } from "./builtins.js";
|
|
4
4
|
import { DefinitionRegistry } from "./definition-registry.js";
|
|
5
5
|
import { computeSuggestKind, computeValidUserFacingKinds } from "./kind-suggest.js";
|
|
6
|
+
import { visitManifest as runVisitManifest, type ManifestVisitor } from "./manifest-visitor.js";
|
|
6
7
|
import { isRefEntry, isScopeEntry } from "./reference-field-map.js";
|
|
7
8
|
import type { AnalysisContext } from "./types.js";
|
|
8
9
|
|
|
@@ -62,6 +63,25 @@ export class AnalysisRegistry {
|
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Walks a manifest's annotation sites (refs, scopes, schema-from, CEL) via
|
|
68
|
+
* the shared manifest visitor, bound to this registry's definitions and
|
|
69
|
+
* aliases. The public seam for hosts (editor overview graph, tooling) that
|
|
70
|
+
* need the same site discovery the analyzer's own passes use, without
|
|
71
|
+
* reaching into the internal DefinitionRegistry.
|
|
72
|
+
*/
|
|
73
|
+
visitManifest(
|
|
74
|
+
resources: ResourceManifest[],
|
|
75
|
+
visitor: ManifestVisitor,
|
|
76
|
+
opts?: { skipKinds?: ReadonlySet<string>; expand?: boolean; discoverNestedRefs?: boolean },
|
|
77
|
+
): void {
|
|
78
|
+
runVisitManifest(resources, this.defs, visitor, {
|
|
79
|
+
aliases: this.aliases,
|
|
80
|
+
aliasesByModule: this.aliasesByModule,
|
|
81
|
+
...opts,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
65
85
|
/**
|
|
66
86
|
* Returns the built-in kernel definitions. The underlying DefinitionRegistry already
|
|
67
87
|
* seeds these on construction; this method exposes them so callers (e.g. the kernel's
|