@telorun/analyzer 0.8.1 → 0.10.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.
Files changed (54) hide show
  1. package/dist/analysis-registry.d.ts +5 -0
  2. package/dist/analysis-registry.d.ts.map +1 -1
  3. package/dist/analysis-registry.js +7 -2
  4. package/dist/analyzer.d.ts.map +1 -1
  5. package/dist/analyzer.js +5 -5
  6. package/dist/definition-registry.d.ts +13 -1
  7. package/dist/definition-registry.d.ts.map +1 -1
  8. package/dist/definition-registry.js +58 -2
  9. package/dist/dependency-graph.d.ts +1 -1
  10. package/dist/dependency-graph.d.ts.map +1 -1
  11. package/dist/dependency-graph.js +8 -2
  12. package/dist/flatten-for-analyzer.d.ts +30 -0
  13. package/dist/flatten-for-analyzer.d.ts.map +1 -0
  14. package/dist/flatten-for-analyzer.js +119 -0
  15. package/dist/index.d.ts +6 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +3 -0
  18. package/dist/loaded-types.d.ts +81 -0
  19. package/dist/loaded-types.d.ts.map +1 -0
  20. package/dist/loaded-types.js +1 -0
  21. package/dist/manifest-loader.d.ts +30 -9
  22. package/dist/manifest-loader.d.ts.map +1 -1
  23. package/dist/manifest-loader.js +197 -417
  24. package/dist/normalize-inline-resources.d.ts +1 -1
  25. package/dist/normalize-inline-resources.d.ts.map +1 -1
  26. package/dist/normalize-inline-resources.js +7 -2
  27. package/dist/parse-loaded-file.d.ts +12 -0
  28. package/dist/parse-loaded-file.d.ts.map +1 -0
  29. package/dist/parse-loaded-file.js +50 -0
  30. package/dist/position-metadata.d.ts +27 -0
  31. package/dist/position-metadata.d.ts.map +1 -0
  32. package/dist/position-metadata.js +88 -0
  33. package/dist/reference-field-map.d.ts +5 -0
  34. package/dist/reference-field-map.d.ts.map +1 -1
  35. package/dist/reference-field-map.js +9 -0
  36. package/dist/types.d.ts +6 -0
  37. package/dist/types.d.ts.map +1 -1
  38. package/dist/validate-references.d.ts.map +1 -1
  39. package/dist/validate-references.js +68 -1
  40. package/package.json +3 -3
  41. package/src/analysis-registry.ts +11 -2
  42. package/src/analyzer.ts +17 -5
  43. package/src/definition-registry.ts +76 -3
  44. package/src/dependency-graph.ts +9 -1
  45. package/src/flatten-for-analyzer.ts +134 -0
  46. package/src/index.ts +18 -0
  47. package/src/loaded-types.ts +86 -0
  48. package/src/manifest-loader.ts +230 -459
  49. package/src/normalize-inline-resources.ts +8 -1
  50. package/src/parse-loaded-file.ts +70 -0
  51. package/src/position-metadata.ts +106 -0
  52. package/src/reference-field-map.ts +13 -0
  53. package/src/types.ts +6 -0
  54. package/src/validate-references.ts +74 -1
@@ -0,0 +1,134 @@
1
+ import type { ResourceManifest } from "@telorun/sdk";
2
+ import type { LoadedFile, LoadedGraph, LoadedModule } from "./loaded-types.js";
3
+ import { isModuleKind } from "./module-kinds.js";
4
+
5
+ /** Produce the flat manifest list `analyze()` consumes today.
6
+ *
7
+ * Combines the entry module's manifests with `Telo.Definition`,
8
+ * `Telo.Abstract`, and `Telo.Import` docs forwarded from imported libraries.
9
+ * Stamps three flavours of metadata along the way:
10
+ *
11
+ * - `metadata.source` and `metadata.sourceLine` — already on each manifest
12
+ * from `parseLoadedFile`, copied here unchanged.
13
+ * - `metadata.module` — the owning module's `Telo.Application` /
14
+ * `Telo.Library` `metadata.name`, applied to non-module manifests that
15
+ * don't already carry one.
16
+ * - `metadata.resolvedModuleName` / `metadata.resolvedNamespace` — for every
17
+ * `Telo.Import` manifest, looked up via `graph.importEdges` to find the
18
+ * target module's own `Telo.Library` identity. Without this, the
19
+ * analyzer's alias resolver and `validate-extends` fall back to
20
+ * path-derived identity and produce spurious diagnostics.
21
+ *
22
+ * Position metadata (`positionIndex`) is NOT stamped on manifests —
23
+ * callers look it up via `findPositions(graph, ...)` on the LoadedGraph. */
24
+ export function flattenForAnalyzer(graph: LoadedGraph): ResourceManifest[] {
25
+ const result: ResourceManifest[] = [];
26
+
27
+ const stampedEntry = collectModuleManifests(graph.entry);
28
+ result.push(...stampedEntry);
29
+
30
+ const seen = new Set<string>([graph.rootSource]);
31
+ const queue: string[] = [graph.rootSource];
32
+
33
+ while (queue.length > 0) {
34
+ const fromSource = queue.shift()!;
35
+ const edges = graph.importEdges.get(fromSource);
36
+ if (!edges) continue;
37
+
38
+ for (const edge of edges.values()) {
39
+ if (seen.has(edge.targetSource)) continue;
40
+ seen.add(edge.targetSource);
41
+ queue.push(edge.targetSource);
42
+
43
+ const targetModule = graph.modules.get(edge.targetSource);
44
+ if (!targetModule) continue;
45
+
46
+ const stamped = collectModuleManifests(targetModule);
47
+ for (const m of stamped) {
48
+ if (
49
+ m.kind === "Telo.Definition" ||
50
+ m.kind === "Telo.Abstract" ||
51
+ m.kind === "Telo.Import"
52
+ ) {
53
+ result.push(m);
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ // Stamp resolved import identity on every Telo.Import in the result by
60
+ // reading the edge's pre-resolved name/namespace — no re-derivation from
61
+ // manifest metadata. The edge is keyed by (owner-file, alias) which is
62
+ // exactly the (metadata.source, metadata.name) pair on each Telo.Import.
63
+ for (let i = 0; i < result.length; i++) {
64
+ const m = result[i];
65
+ if (m.kind !== "Telo.Import") continue;
66
+ const owner = (m.metadata as { source?: string } | undefined)?.source;
67
+ const alias = m.metadata?.name as string | undefined;
68
+ if (!owner || !alias) continue;
69
+ const edge = graph.importEdges.get(owner)?.get(alias);
70
+ if (!edge?.targetModuleName) continue;
71
+
72
+ const newMetadata: Record<string, unknown> = {
73
+ ...m.metadata,
74
+ resolvedModuleName: edge.targetModuleName,
75
+ resolvedNamespace: edge.targetNamespace,
76
+ };
77
+ result[i] = { ...m, metadata: newMetadata as ResourceManifest["metadata"] };
78
+ }
79
+
80
+ return result;
81
+ }
82
+
83
+ /** Project a LoadedModule (owner + partials) to a flat ResourceManifest[]
84
+ * with `metadata.module` stamped on non-module docs. The kernel's runtime
85
+ * entry load uses this to convert a `Loader.loadModule` result into the
86
+ * classic ResourceManifest[] shape it iterates over. Imports are not
87
+ * followed — the kernel's import-controller loads each import's module
88
+ * separately at runtime. */
89
+ export function flattenLoadedModule(mod: LoadedModule): ResourceManifest[] {
90
+ return collectModuleManifests(mod);
91
+ }
92
+
93
+ function collectModuleManifests(mod: LoadedModule): ResourceManifest[] {
94
+ const owner = stampFile(mod.owner, ownerModuleName(mod.owner));
95
+ const partials: ResourceManifest[] = [];
96
+ for (const p of mod.partials) {
97
+ partials.push(...stampFile(p, ownerModuleName(mod.owner)));
98
+ }
99
+ return [...owner, ...partials];
100
+ }
101
+
102
+ function ownerModuleName(file: LoadedFile): string | undefined {
103
+ for (const m of file.manifests) {
104
+ if (m && isModuleKind(m.kind)) {
105
+ const name = m.metadata?.name;
106
+ if (typeof name === "string") return name;
107
+ }
108
+ }
109
+ return undefined;
110
+ }
111
+
112
+ function stampFile(
113
+ file: LoadedFile,
114
+ ownerModule: string | undefined,
115
+ ): ResourceManifest[] {
116
+ const out: ResourceManifest[] = [];
117
+ for (let i = 0; i < file.manifests.length; i++) {
118
+ const m = file.manifests[i];
119
+ if (m === null || m === undefined) continue;
120
+ const { sourceLine } = file.positions[i];
121
+
122
+ const metadata: Record<string, unknown> = {
123
+ ...m.metadata,
124
+ source: file.source,
125
+ sourceLine,
126
+ };
127
+ if (ownerModule && !isModuleKind(m.kind) && !metadata.module) {
128
+ metadata.module = ownerModule;
129
+ }
130
+
131
+ out.push({ ...m, metadata: metadata as ResourceManifest["metadata"] });
132
+ }
133
+ return out;
134
+ }
package/src/index.ts CHANGED
@@ -1,8 +1,26 @@
1
1
  export { AnalysisRegistry } from "./analysis-registry.js";
2
2
  export { StaticAnalyzer } from "./analyzer.js";
3
+ export type {
4
+ GraphLoadError,
5
+ ImportEdge,
6
+ LoadedFile,
7
+ LoadedGraph,
8
+ LoadedModule,
9
+ ParseError,
10
+ } from "./loaded-types.js";
11
+ export { flattenForAnalyzer, flattenLoadedModule } from "./flatten-for-analyzer.js";
3
12
  export { Loader } from "./manifest-loader.js";
4
13
  export { isModuleKind, MODULE_KINDS } from "./module-kinds.js";
5
14
  export type { ModuleKind } from "./module-kinds.js";
15
+ export { parseLoadedFile } from "./parse-loaded-file.js";
16
+ export type { ParseOptions } from "./parse-loaded-file.js";
17
+ export {
18
+ buildDocumentPositions,
19
+ buildLineOffsets,
20
+ buildPositionIndex,
21
+ documentLineOffsets,
22
+ } from "./position-metadata.js";
23
+ export type { DocumentPosition } from "./position-metadata.js";
6
24
  export { HttpSource } from "./sources/http-source.js";
7
25
  export { RegistrySource } from "./sources/registry-source.js";
8
26
  export { DEFAULT_MANIFEST_FILENAME, DiagnosticSeverity } from "./types.js";
@@ -0,0 +1,86 @@
1
+ import type { ResourceManifest } from "@telorun/sdk";
2
+ import type { Document } from "yaml";
3
+ import type { DocumentPosition } from "./position-metadata.js";
4
+ import type { Range } from "./types.js";
5
+
6
+ /** One physical file's parsed result. Returned for the owner manifest, for
7
+ * each `include:` partial, and for each external import target.
8
+ *
9
+ * Identity rule: every map key, every cross-reference, every editor-side
10
+ * cache uses `source` — the URL the source adapter's `read()` returned. */
11
+ export interface LoadedFile {
12
+ /** Canonical identity. The URL the source adapter's `read()` returned —
13
+ * HTTPS for http/registry, an absolute path for local. */
14
+ source: string;
15
+ /** The URL the caller supplied (e.g. registry ref `std/javascript@0.3.0`).
16
+ * Differs from `source` only for adapter-resolved URLs. */
17
+ requestedUrl: string;
18
+ /** Raw text exactly as `read()` returned it. */
19
+ text: string;
20
+ /** Per-document parsed AST, in source order. */
21
+ documents: Document[];
22
+ /** Per-document JSON projection (`doc.toJSON()`). Aligned to `documents`. */
23
+ manifests: Array<ResourceManifest | null>;
24
+ /** Per-document `{sourceLine, positionIndex}`. Aligned to `documents`. */
25
+ positions: DocumentPosition[];
26
+ /** Document-level parse errors aggregated from `yaml.Document.errors`. */
27
+ parseErrors: ParseError[];
28
+ }
29
+
30
+ export interface ParseError {
31
+ documentIndex: number;
32
+ message: string;
33
+ /** Line/character of the failure, when the yaml parser provided one. */
34
+ range?: Range;
35
+ }
36
+
37
+ /** An owner file plus the partial files it includes. The unit
38
+ * `Loader.loadModule` returns. */
39
+ export interface LoadedModule {
40
+ owner: LoadedFile;
41
+ /** Each `include:` target as its own LoadedFile. Empty when no `include:`.
42
+ * Order matches the `include:` list (after glob expansion). */
43
+ partials: LoadedFile[];
44
+ }
45
+
46
+ /** Resolved Telo.Import edge: where the import points and what library
47
+ * identity it resolves to. Carrying name/namespace on the edge means
48
+ * `flattenForAnalyzer` can stamp `metadata.resolvedModuleName` /
49
+ * `resolvedNamespace` from this single source rather than re-deriving
50
+ * the target from manifest metadata, which would silently miss whenever
51
+ * a future projection forgets to stamp `metadata.source` consistently. */
52
+ export interface ImportEdge {
53
+ /** Canonical resolved URL of the target — a key into `modules`. */
54
+ targetSource: string;
55
+ /** Target library's `metadata.name`, or `null` when the target had no
56
+ * Telo.Library doc (an error case captured in `LoadedGraph.errors`). */
57
+ targetModuleName: string | null;
58
+ /** Target library's `metadata.namespace` (or `null` when unset). */
59
+ targetNamespace: string | null;
60
+ }
61
+
62
+ /** An entry plus every transitively-imported library. Returned by
63
+ * `Loader.loadGraph`. */
64
+ export interface LoadedGraph {
65
+ /** Canonical entry source — equals `entry.owner.source`. */
66
+ rootSource: string;
67
+ entry: LoadedModule;
68
+ /** Map keyed by `LoadedFile.source` (canonical resolved URL). Includes
69
+ * entry, partials, and every transitively reachable Telo.Import target +
70
+ * its partials. */
71
+ modules: Map<string, LoadedModule>;
72
+ /** Per-Telo.Import resolution. Keyed by the resolved URL of the file the
73
+ * Telo.Import was declared in, then by the import's PascalCase alias. */
74
+ importEdges: Map<string, Map<string, ImportEdge>>;
75
+ /** Surface-level errors that did not abort the graph load (e.g. an import
76
+ * whose target failed to fetch). */
77
+ errors: GraphLoadError[];
78
+ }
79
+
80
+ export interface GraphLoadError {
81
+ /** URL of the file that failed to load. */
82
+ url: string;
83
+ /** Source of the import that triggered the load, or null for the entry. */
84
+ fromSource: string | null;
85
+ error: Error;
86
+ }