@metaobjectsdev/metadata 0.6.0 → 0.7.0-rc.2

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 (128) hide show
  1. package/README.md +54 -3
  2. package/dist/attr-schema-validate.js +7 -7
  3. package/dist/attr-schema-validate.js.map +1 -1
  4. package/dist/core/export-json.d.ts +6 -7
  5. package/dist/core/export-json.d.ts.map +1 -1
  6. package/dist/core/export-json.js +15 -17
  7. package/dist/core/export-json.js.map +1 -1
  8. package/dist/core/index.d.ts +4 -2
  9. package/dist/core/index.d.ts.map +1 -1
  10. package/dist/core/index.js +6 -2
  11. package/dist/core/index.js.map +1 -1
  12. package/dist/core/parser-yaml.d.ts.map +1 -1
  13. package/dist/core/parser-yaml.js +36 -11
  14. package/dist/core/parser-yaml.js.map +1 -1
  15. package/dist/core/yaml-desugar.d.ts.map +1 -1
  16. package/dist/core/yaml-desugar.js +54 -5
  17. package/dist/core/yaml-desugar.js.map +1 -1
  18. package/dist/core/yaml-positions-walker.d.ts +21 -0
  19. package/dist/core/yaml-positions-walker.d.ts.map +1 -0
  20. package/dist/core/yaml-positions-walker.js +75 -0
  21. package/dist/core/yaml-positions-walker.js.map +1 -0
  22. package/dist/core/yaml-positions.d.ts +19 -0
  23. package/dist/core/yaml-positions.d.ts.map +1 -0
  24. package/dist/core/yaml-positions.js +60 -0
  25. package/dist/core/yaml-positions.js.map +1 -0
  26. package/dist/errors.d.ts +32 -9
  27. package/dist/errors.d.ts.map +1 -1
  28. package/dist/errors.js +44 -5
  29. package/dist/errors.js.map +1 -1
  30. package/dist/index.d.ts +6 -3
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +9 -2
  33. package/dist/index.js.map +1 -1
  34. package/dist/json-path.d.ts +8 -0
  35. package/dist/json-path.d.ts.map +1 -0
  36. package/dist/json-path.js +39 -0
  37. package/dist/json-path.js.map +1 -0
  38. package/dist/loader/meta-data-loader.d.ts +47 -6
  39. package/dist/loader/meta-data-loader.d.ts.map +1 -1
  40. package/dist/loader/meta-data-loader.js +126 -8
  41. package/dist/loader/meta-data-loader.js.map +1 -1
  42. package/dist/loader/meta-data-source.d.ts +6 -2
  43. package/dist/loader/meta-data-source.d.ts.map +1 -1
  44. package/dist/loader/meta-data-source.js +10 -6
  45. package/dist/loader/meta-data-source.js.map +1 -1
  46. package/dist/loader/shortcuts.d.ts +9 -0
  47. package/dist/loader/shortcuts.d.ts.map +1 -0
  48. package/dist/loader/shortcuts.js +19 -0
  49. package/dist/loader/shortcuts.js.map +1 -0
  50. package/dist/loader/sources/directory-source.d.ts +15 -0
  51. package/dist/loader/sources/directory-source.d.ts.map +1 -0
  52. package/dist/loader/sources/directory-source.js +80 -0
  53. package/dist/loader/sources/directory-source.js.map +1 -0
  54. package/dist/loader/sources/file-source.d.ts +12 -0
  55. package/dist/loader/sources/file-source.d.ts.map +1 -0
  56. package/dist/loader/sources/file-source.js +46 -0
  57. package/dist/loader/sources/file-source.js.map +1 -0
  58. package/dist/loader/sources/index.d.ts +5 -0
  59. package/dist/loader/sources/index.d.ts.map +1 -0
  60. package/dist/loader/sources/index.js +5 -0
  61. package/dist/loader/sources/index.js.map +1 -0
  62. package/dist/loader/sources/uri-source.d.ts +9 -0
  63. package/dist/loader/sources/uri-source.d.ts.map +1 -0
  64. package/dist/loader/sources/uri-source.js +42 -0
  65. package/dist/loader/sources/uri-source.js.map +1 -0
  66. package/dist/loader/validation-passes.d.ts.map +1 -1
  67. package/dist/loader/validation-passes.js +92 -28
  68. package/dist/loader/validation-passes.js.map +1 -1
  69. package/dist/naming.d.ts +15 -2
  70. package/dist/naming.d.ts.map +1 -1
  71. package/dist/naming.js +20 -6
  72. package/dist/naming.js.map +1 -1
  73. package/dist/parser-core.d.ts +17 -4
  74. package/dist/parser-core.d.ts.map +1 -1
  75. package/dist/parser-core.js +371 -44
  76. package/dist/parser-core.js.map +1 -1
  77. package/dist/parser-json.d.ts.map +1 -1
  78. package/dist/parser-json.js +10 -2
  79. package/dist/parser-json.js.map +1 -1
  80. package/dist/persistence/source/validate-source-roles.js +2 -2
  81. package/dist/persistence/source/validate-source-roles.js.map +1 -1
  82. package/dist/semantic-diff.d.ts +5 -0
  83. package/dist/semantic-diff.d.ts.map +1 -0
  84. package/dist/semantic-diff.js +49 -0
  85. package/dist/semantic-diff.js.map +1 -0
  86. package/dist/shared/meta-data.d.ts +10 -0
  87. package/dist/shared/meta-data.d.ts.map +1 -1
  88. package/dist/shared/meta-data.js +23 -0
  89. package/dist/shared/meta-data.js.map +1 -1
  90. package/dist/source.d.ts +96 -0
  91. package/dist/source.d.ts.map +1 -0
  92. package/dist/source.js +38 -0
  93. package/dist/source.js.map +1 -0
  94. package/dist/subtype-rules.js +1 -1
  95. package/dist/subtype-rules.js.map +1 -1
  96. package/dist/super-resolve.d.ts +2 -0
  97. package/dist/super-resolve.d.ts.map +1 -1
  98. package/dist/super-resolve.js +1 -1
  99. package/dist/super-resolve.js.map +1 -1
  100. package/package.json +1 -1
  101. package/src/attr-schema-validate.ts +7 -7
  102. package/src/core/export-json.ts +15 -18
  103. package/src/core/index.ts +8 -2
  104. package/src/core/parser-yaml.ts +38 -11
  105. package/src/core/yaml-desugar.ts +58 -4
  106. package/src/core/yaml-positions-walker.ts +101 -0
  107. package/src/core/yaml-positions.ts +80 -0
  108. package/src/errors.ts +57 -8
  109. package/src/index.ts +28 -3
  110. package/src/json-path.ts +46 -0
  111. package/src/loader/meta-data-loader.ts +168 -10
  112. package/src/loader/meta-data-source.ts +10 -6
  113. package/src/loader/shortcuts.ts +31 -0
  114. package/src/loader/sources/directory-source.ts +90 -0
  115. package/src/{core → loader/sources}/file-source.ts +3 -3
  116. package/src/loader/sources/index.ts +6 -0
  117. package/src/loader/sources/uri-source.ts +44 -0
  118. package/src/loader/validation-passes.ts +96 -29
  119. package/src/naming.ts +39 -7
  120. package/src/parser-core.ts +412 -46
  121. package/src/parser-json.ts +11 -2
  122. package/src/persistence/source/validate-source-roles.ts +2 -2
  123. package/src/semantic-diff.ts +48 -0
  124. package/src/shared/meta-data.ts +28 -0
  125. package/src/source.ts +99 -0
  126. package/src/subtype-rules.ts +1 -1
  127. package/src/super-resolve.ts +3 -1
  128. package/src/core/file-meta-data-loader.ts +0 -89
@@ -100,7 +100,7 @@ function validateNode(
100
100
  errors.push(
101
101
  new ParseError(
102
102
  `Common attr '${ca.name}' conflicts with per-type attr on ${typeKey}`,
103
- { code: "ERR_PROVIDER_ATTR_CONFLICT" },
103
+ { code: "ERR_PROVIDER_ATTR_CONFLICT", source: node.source },
104
104
  ),
105
105
  );
106
106
  reportedConflicts.add(typeKey);
@@ -126,7 +126,7 @@ function validateNode(
126
126
  errors.push(
127
127
  new ParseError(
128
128
  `${nodeLabel(node)} is missing required attribute '@${spec.name}'`,
129
- { code: "ERR_MISSING_REQUIRED_ATTR" },
129
+ { code: "ERR_MISSING_REQUIRED_ATTR", source: node.source },
130
130
  ),
131
131
  );
132
132
  }
@@ -145,7 +145,7 @@ function validateNode(
145
145
  const valueErrors = inst.validateValue(value);
146
146
  if (valueErrors.length > 0) {
147
147
  for (const ve of valueErrors) {
148
- errors.push(new ParseError(`${nodeLabel(node)} ${ve.message}`, { code: "ERR_BAD_ATTR_VALUE" }));
148
+ errors.push(new ParseError(`${nodeLabel(node)} ${ve.message}`, { code: "ERR_BAD_ATTR_VALUE", source: node.source }));
149
149
  }
150
150
  continue; // type wrong → skip allowedValues
151
151
  }
@@ -159,7 +159,7 @@ function validateNode(
159
159
  `${nodeLabel(node)} attribute '@${inst.name}' has value ` +
160
160
  `'${String(value)}' which is not one of the allowed values: ` +
161
161
  `${spec.allowedValues.map((v) => String(v)).join(", ")}`,
162
- { code: "ERR_BAD_ATTR_VALUE" },
162
+ { code: "ERR_BAD_ATTR_VALUE", source: node.source },
163
163
  ),
164
164
  );
165
165
  }
@@ -180,7 +180,7 @@ function validateNode(
180
180
  errors.push(
181
181
  new ParseError(
182
182
  `${nodeLabel(node)} must declare at least one value in '@${FIELD_ATTR_VALUES}'.`,
183
- { code: "ERR_BAD_ATTR_VALUE" },
183
+ { code: "ERR_BAD_ATTR_VALUE", source: node.source },
184
184
  ),
185
185
  );
186
186
  } else {
@@ -194,14 +194,14 @@ function validateNode(
194
194
  `${nodeLabel(node)} attribute '@${FIELD_ATTR_VALUES}' member '${member}' ` +
195
195
  `is not a valid identifier (must match ${ENUM_MEMBER_PATTERN.source}). ` +
196
196
  `Non-identifier-safe member strings require a symbol↔value mapping (deferred).`,
197
- { code: "ERR_BAD_ATTR_VALUE" },
197
+ { code: "ERR_BAD_ATTR_VALUE", source: node.source },
198
198
  ),
199
199
  );
200
200
  } else if (seen.has(member)) {
201
201
  errors.push(
202
202
  new ParseError(
203
203
  `${nodeLabel(node)} attribute '@${FIELD_ATTR_VALUES}' has duplicate member '${member}'.`,
204
- { code: "ERR_BAD_ATTR_VALUE" },
204
+ { code: "ERR_BAD_ATTR_VALUE", source: node.source },
205
205
  ),
206
206
  );
207
207
  } else {
@@ -10,13 +10,12 @@
10
10
  // - Content errors (parse/validation failures) are collected in errors[] and
11
11
  // returned in the result — they do NOT throw. `json` is still produced from
12
12
  // whatever tree the Loader returned (Loader always returns a valid MetaData).
13
- // - I/O failures (missing/unreadable directory) are caught by
14
- // `loadFromDirectory` and returned in its `errors[]`; `loadAndExportJson`
15
- // surfaces them unchanged in `ExportResult.errors`. It does not throw for
16
- // directory or metadata problems.
13
+ // - I/O failures (missing/unreadable directory) surface as collected errors
14
+ // via MetaDataLoader.fromDirectory; `loadAndExportJson` surfaces them
15
+ // unchanged in `ExportResult.errors`. It does not throw for directory or
16
+ // metadata problems.
17
17
 
18
- import { FileMetaDataLoader } from "./file-meta-data-loader.js";
19
- import type { LoadOptions } from "../loader/meta-data-loader.js";
18
+ import { MetaDataLoader, type LoadOptions } from "../loader/meta-data-loader.js";
20
19
  import { canonicalSerialize } from "../serializer-json.js";
21
20
 
22
21
  // ---------------------------------------------------------------------------
@@ -39,28 +38,26 @@ export interface ExportResult {
39
38
  * Load all metadata under `dir` and export the entire model as one flattened
40
39
  * canonical-JSON document.
41
40
  *
42
- * Internally constructs a fresh `FileMetaDataLoader` (using the default registry
43
- * composed via `composeRegistry(coreProviders)`), calls `loadDirectory`, then serializes the
44
- * resulting tree with `canonicalSerialize`.
41
+ * Routes through `MetaDataLoader.fromDirectory` (using the default registry
42
+ * composed via `composeRegistry(coreProviders)` when none supplied), then
43
+ * serializes the resulting tree with `canonicalSerialize`.
45
44
  *
46
45
  * @param dir Absolute or relative path to the directory containing `meta.*.json` files.
47
- * @param opts Optional loader options forwarded to the `FileMetaDataLoader` constructor
48
- * (registry, freeze, strict). The `exclude` glob list can be
49
- * supplied as `opts.exclude` — see `FileMetaDataLoader.loadDirectory`.
46
+ * @param opts Optional loader options (registry, freeze, strict). The
47
+ * `exclude` glob list can be supplied as `opts.exclude`.
50
48
  */
51
49
  export async function loadAndExportJson(
52
50
  dir: string,
53
51
  opts?: LoadOptions & { exclude?: string[] },
54
52
  ): Promise<ExportResult> {
55
- const { exclude, ...loaderOpts } = opts ?? {};
56
- const loader = new FileMetaDataLoader(loaderOpts);
57
- // Only pass the exclude option when defined, to satisfy exactOptionalPropertyTypes.
58
- const dirOpts = exclude !== undefined ? { exclude } : undefined;
59
- const result = await loader.loadDirectory(dir, dirOpts);
53
+ const result = await MetaDataLoader.fromDirectory(dir, opts);
60
54
  const json = canonicalSerialize(result.root);
61
55
  return {
62
56
  json,
63
57
  errors: result.errors,
64
- warnings: result.warnings,
58
+ // FR5a: LoadResult.warnings is now LoaderWarning[]; ExportResult preserves
59
+ // its public string[] shape (callers print warnings as text). Extract the
60
+ // message for back-compat.
61
+ warnings: result.warnings.map((w) => w.message),
65
62
  };
66
63
  }
package/src/core/index.ts CHANGED
@@ -5,8 +5,14 @@
5
5
  // load-and-export convenience. The root `@metaobjectsdev/metadata` entry is
6
6
  // browser-safe and imports none of this. See the package README.
7
7
 
8
- export { FileSource } from "./file-source.js";
9
- export { FileMetaDataLoader } from "./file-meta-data-loader.js";
8
+ // Source impls the node:fs-backed MetaDataSource implementations. Live
9
+ // under `loader/sources/`; re-exported here so server-side consumers can pull
10
+ // them from the same `/core` entry that already houses the YAML parser.
11
+ export { FileSource } from "../loader/sources/file-source.js";
12
+ export { DirectorySource } from "../loader/sources/directory-source.js";
13
+ export type { DirectoryOptions } from "../loader/sources/directory-source.js";
14
+ export { UriSource } from "../loader/sources/uri-source.js";
15
+
10
16
  export { parseYaml } from "./parser-yaml.js";
11
17
  export { loadAndExportJson } from "./export-json.js";
12
18
  export type { ExportResult } from "./export-json.js";
@@ -1,15 +1,38 @@
1
1
  // Authoring YAML parser.
2
2
  //
3
- // parseYaml is a front-end: yaml.parse → desugar → the shared buildTree
4
- // (parser-core.ts). The desugar applies the four authoring-sugar rules so the
5
- // resulting typed tree is identical to the one the equivalent canonical JSON
6
- // produces.
3
+ // parseYaml is a front-end: parseYamlWithPositions → desugar → the shared
4
+ // buildTree (parser-core.ts). The desugar applies the four authoring-sugar
5
+ // rules so the resulting typed tree is identical to the one the equivalent
6
+ // canonical JSON produces.
7
+ //
8
+ // FR5b: the parse phase now preserves YAML source positions through the
9
+ // pipeline. parseYamlWithPositions attaches a Symbol-keyed position-by-key
10
+ // map onto every mapping object; desugar shallow-copies that property; and
11
+ // buildTree's `format: "yaml"` mode reads the position when stamping
12
+ // `node.source.yamlPosition` (see parser-core.ts).
7
13
 
8
- import { parse as parseYamlText } from "yaml";
9
14
  import { ParseError } from "../errors.js";
10
- import { buildTree, errOpts } from "../parser-core.js";
15
+ import { buildTree } from "../parser-core.js";
11
16
  import type { ParseOptions, ParseResult } from "../parser-core.js";
17
+ import type { ErrorSource } from "../source.js";
12
18
  import { desugar } from "./yaml-desugar.js";
19
+ import { parseYamlWithPositions } from "./yaml-positions-walker.js";
20
+
21
+ /** FR5a / ADR-0009 — build a YAML-source envelope rooted at "$".
22
+ * yamlPosition on the envelope is left undefined here; envelopes for
23
+ * THROWN parser errors carry positions only when the parse phase pushed
24
+ * the path into the live parser-core builder (see populateNodeSource in
25
+ * parser-core.ts). Errors raised before buildTree (e.g. yaml syntax
26
+ * failures, an empty desugar result) lack a node — `yamlPosition` is
27
+ * intentionally omitted, per the spec's "skip on desugar-synthesized
28
+ * nodes" decision. */
29
+ function yamlSource(sourceName: string | undefined): ErrorSource {
30
+ return {
31
+ format: "yaml",
32
+ files: [sourceName ?? "<unknown>"],
33
+ jsonPath: "$",
34
+ };
35
+ }
13
36
 
14
37
  export function parseYaml(content: string, opts: ParseOptions): ParseResult {
15
38
  // Strip UTF-8 BOM if present (consistent with parseJson).
@@ -20,11 +43,11 @@ export function parseYaml(content: string, opts: ParseOptions): ParseResult {
20
43
  // The loader's per-source try/catch collects the throw into LoadResult.errors.
21
44
  let parsed: unknown;
22
45
  try {
23
- parsed = parseYamlText(normalizedContent);
46
+ parsed = parseYamlWithPositions(normalizedContent).value;
24
47
  } catch (err) {
25
48
  throw new ParseError(
26
49
  `Invalid YAML: ${(err as Error).message}`,
27
- { ...errOpts(opts.sourceName), code: "ERR_MALFORMED_YAML" },
50
+ { code: "ERR_MALFORMED_YAML", source: yamlSource(opts.sourceName) },
28
51
  );
29
52
  }
30
53
 
@@ -37,11 +60,14 @@ export function parseYaml(content: string, opts: ParseOptions): ParseResult {
37
60
  const first = desugarErrors[0]!;
38
61
  throw new ParseError(
39
62
  first.message,
40
- { ...errOpts(opts.sourceName), code: first.code ?? "ERR_MALFORMED_YAML" },
63
+ { code: first.code ?? "ERR_MALFORMED_YAML", source: yamlSource(opts.sourceName) },
41
64
  );
42
65
  }
43
66
 
44
- const result = buildTree(canonical, opts);
67
+ // FR5b buildTree needs to know the source-format discriminant so
68
+ // populateNodeSource emits `format: "yaml"` envelopes (with the
69
+ // optional yamlPosition) instead of the default `format: "json"`.
70
+ const result = buildTree(canonical, { ...opts, sourceFormat: "yaml" });
45
71
 
46
72
  // Merge collected desugar errors ahead of buildTree's own collected errors.
47
73
  // Each CollectedError carries its own stable code when set (e.g.
@@ -50,13 +76,14 @@ export function parseYaml(content: string, opts: ParseOptions): ParseResult {
50
76
  const desugarParseErrors = desugarErrors.map(
51
77
  (e) =>
52
78
  new ParseError(e.message, {
53
- ...errOpts(opts.sourceName),
54
79
  code: e.code ?? "ERR_MALFORMED_YAML",
80
+ source: yamlSource(opts.sourceName),
55
81
  }),
56
82
  );
57
83
  return {
58
84
  root: result.root,
59
85
  warnings: result.warnings,
60
86
  errors: [...desugarParseErrors, ...result.errors],
87
+ envelopeWarnings: result.envelopeWarnings,
61
88
  };
62
89
  }
@@ -38,6 +38,11 @@ import {
38
38
  RESERVED_KEY_IS_ARRAY,
39
39
  TYPE_SUBTYPE_SEPARATOR,
40
40
  } from "../shared/structural.js";
41
+ import {
42
+ getPositionMap,
43
+ setPositionMap,
44
+ type PositionMap,
45
+ } from "./yaml-positions.js";
41
46
  import {
42
47
  ATTR_SUBTYPE_STRING,
43
48
  ATTR_SUBTYPE_CLASS,
@@ -99,6 +104,11 @@ function desugarNode(
99
104
  const rawKey = entries[0]!;
100
105
  const rawBody = (input as Record<string, unknown>)[rawKey];
101
106
 
107
+ // FR5b — capture the wrapper-level position-by-key map BEFORE re-keying.
108
+ // The author's raw key (with `[]` suffix and possibly omitted subType) is
109
+ // the lookup key; the desugar's canonical key is what we emit.
110
+ const wrapperPositions = getPositionMap(input);
111
+
102
112
  // Rule 4: a trailing "[]" on the key → isArray.
103
113
  let key = rawKey;
104
114
  let isArray = false;
@@ -111,7 +121,10 @@ function desugarNode(
111
121
  const canonicalKey = resolveKey(key, registry, errors, path);
112
122
 
113
123
  // Rule 2: a scalar body → { name: <scalar> }.
114
- const body = desugarBody(rawBody, registry, canonicalKey, errors, path);
124
+ // FR5b propagate the wrapper-key's position into the synthesized body
125
+ // when the input body was a scalar (no body-side positions to inherit).
126
+ const wrapperKeyPos = wrapperPositions?.[rawKey];
127
+ const body = desugarBody(rawBody, registry, canonicalKey, errors, path, wrapperKeyPos);
115
128
 
116
129
  // Rule 4 (cont.): stamp isArray onto the canonical body.
117
130
  if (isArray) body[RESERVED_KEY_IS_ARRAY] = true;
@@ -131,7 +144,16 @@ function desugarNode(
131
144
  }
132
145
  // A non-array `children` value is left untouched — buildTree reports it.
133
146
 
134
- return { [canonicalKey]: body };
147
+ // FR5b emit a wrapper-level position-by-key map for the canonical wrapper
148
+ // so buildTree's per-child iteration can read the position via the same
149
+ // lookup it uses for JSON input. The single key transformation is
150
+ // rawKey → canonicalKey (Rule 1 fuses the subType, Rule 4 strips `[]`).
151
+ const outWrapper: Record<string, unknown> = { [canonicalKey]: body };
152
+ if (wrapperKeyPos !== undefined) {
153
+ setPositionMap(outWrapper, { [canonicalKey]: wrapperKeyPos });
154
+ }
155
+
156
+ return outWrapper;
135
157
  }
136
158
 
137
159
  // Rule 1 — resolve a possibly-bare key to a fused `type.subType` token.
@@ -169,16 +191,30 @@ function desugarBody(
169
191
  canonicalKey: string,
170
192
  errors: CollectedError[],
171
193
  path: string,
194
+ /** FR5b — position of the WRAPPER key (the `field.string:` line). Used to
195
+ * back-fill `yamlPosition` on synthesized bodies (Rule 2's scalar lift) +
196
+ * empty bodies; for mapping bodies we use the body's own position-by-key
197
+ * map. */
198
+ wrapperKeyPos: { line: number; col: number } | undefined,
172
199
  ): Record<string, unknown> {
173
200
  if (
174
201
  typeof rawBody === "string" ||
175
202
  typeof rawBody === "number" ||
176
203
  typeof rawBody === "boolean"
177
204
  ) {
178
- return { [RESERVED_KEY_NAME]: rawBody };
205
+ // FR5b — the synthesized `{ name: rawBody }` has no YAML-side
206
+ // counterpart; we attribute the `name` slot to the wrapper-key's
207
+ // position (the only YAML position that meaningfully belongs to this
208
+ // synthesis).
209
+ const out: Record<string, unknown> = { [RESERVED_KEY_NAME]: rawBody };
210
+ if (wrapperKeyPos !== undefined) {
211
+ setPositionMap(out, { [RESERVED_KEY_NAME]: wrapperKeyPos });
212
+ }
213
+ return out;
179
214
  }
180
215
  if (rawBody === null || rawBody === undefined) {
181
216
  // An empty body (`field.string:` with nothing after) → an empty node.
217
+ // No body keys to position; the wrapper carries the node's position.
182
218
  return {};
183
219
  }
184
220
  if (Array.isArray(rawBody)) {
@@ -192,20 +228,38 @@ function desugarBody(
192
228
  // Rule D2 (type-coercion guard).
193
229
  const src = rawBody as Record<string, unknown>;
194
230
  const out: Record<string, unknown> = {};
231
+ // FR5b — translate the body's position-by-key map across the sigil-free
232
+ // rewrite. A bare `filterable` key in the source maps to `@filterable` in
233
+ // the canonical body; the YAML position belongs to BOTH names (the YAML
234
+ // author only wrote one). We re-key the position map to match the canonical
235
+ // body's keys so buildTree's per-attr inspection (FR5b follow-ups, e.g.
236
+ // ERR_BAD_ATTR_VALUE) can find the position via the canonical key.
237
+ const srcPositions = getPositionMap(src);
238
+ const outPositions: PositionMap = {};
239
+ let hasOutPositions = false;
195
240
  const schemaIndex = attrSchemaIndex(registry, canonicalKey);
196
241
  for (const key of Object.keys(src)) {
242
+ let outKey: string;
197
243
  if (RESERVED_KEYS.has(key) || key.startsWith(ATTR_PREFIX)) {
198
244
  out[key] = src[key];
245
+ outKey = key;
199
246
  // D2 also applies to author-written @-keys (the awkward form).
200
247
  const attrName = key.startsWith(ATTR_PREFIX) ? key.slice(ATTR_PREFIX.length) : "";
201
248
  if (attrName !== "" && !RESERVED_KEYS.has(attrName)) {
202
249
  checkCoercion(attrName, src[key], schemaIndex, errors, path);
203
250
  }
204
251
  } else {
205
- out[`${ATTR_PREFIX}${key}`] = src[key];
252
+ outKey = `${ATTR_PREFIX}${key}`;
253
+ out[outKey] = src[key];
206
254
  checkCoercion(key, src[key], schemaIndex, errors, path);
207
255
  }
256
+ const pos = srcPositions?.[key];
257
+ if (pos !== undefined) {
258
+ outPositions[outKey] = pos;
259
+ hasOutPositions = true;
260
+ }
208
261
  }
262
+ if (hasOutPositions) setPositionMap(out, outPositions);
209
263
  return out;
210
264
  }
211
265
 
@@ -0,0 +1,101 @@
1
+ // FR5b — YAML AST → JS walker that preserves source positions.
2
+ //
3
+ // This module is the only place inside @metaobjectsdev/metadata that
4
+ // imports the `yaml` package. It lives in core/ alongside parser-yaml.ts,
5
+ // and is reached only via that parser — never via src/index.ts. The
6
+ // browser-safety test guards this invariant.
7
+
8
+ import {
9
+ parseDocument,
10
+ isAlias,
11
+ isMap,
12
+ isScalar,
13
+ isSeq,
14
+ LineCounter,
15
+ type Document,
16
+ } from "yaml";
17
+
18
+ import {
19
+ setPositionMap,
20
+ type PositionMap,
21
+ } from "./yaml-positions.js";
22
+
23
+ /** Result of parsing YAML text with positions retained. */
24
+ export interface YamlParseResult {
25
+ /** The JS object (same shape as `yaml.parse(text)` returns), with
26
+ * position-by-key maps attached to every mapping. */
27
+ value: unknown;
28
+ /** The yaml library's LineCounter — exposed for callers that need to map
29
+ * additional ranges (e.g. surfacing errors raised by the YAML library
30
+ * itself). */
31
+ lineCounter: LineCounter;
32
+ }
33
+
34
+ /** Parse YAML text and return a JS object with positions attached.
35
+ *
36
+ * Mirrors the contract of `yaml.parse(text)` for the shapes the metaobjects
37
+ * authoring grammar uses (mappings, sequences, scalars). Aliases and tags
38
+ * are deferred via the underlying parseDocument call — i.e. they resolve as
39
+ * the library normally would.
40
+ *
41
+ * Throws on YAML syntax errors (same behavior as `yaml.parse`). */
42
+ export function parseYamlWithPositions(text: string): YamlParseResult {
43
+ const lineCounter = new LineCounter();
44
+ const doc = parseDocument(text, { lineCounter });
45
+ // Surface YAML syntax errors as a throw, matching `yaml.parse` behavior.
46
+ // (parseDocument collects them rather than throwing.)
47
+ if (doc.errors.length > 0) {
48
+ throw doc.errors[0]!;
49
+ }
50
+ const value = yamlNodeToJs(doc.contents, lineCounter, doc);
51
+ return { value, lineCounter };
52
+ }
53
+
54
+ // Walk a yaml AST node into a JS structure. For each YAMLMap, attach a
55
+ // position-by-key map onto the resulting JS object — the position of each
56
+ // key is the (line, col) of the KEY token in the YAML source.
57
+ function yamlNodeToJs(
58
+ node: unknown,
59
+ lineCounter: LineCounter,
60
+ doc: Document,
61
+ ): unknown {
62
+ if (node === null || node === undefined) return null;
63
+ if (isScalar(node)) {
64
+ // Honour the library's default scalar typing (numbers / booleans /
65
+ // strings / null all come through Scalar.value).
66
+ return node.value;
67
+ }
68
+ if (isAlias(node)) {
69
+ // Resolve an anchor alias (e.g. `*col` after `&col sku_code`) to its
70
+ // target value — same behaviour as the library's toJS().
71
+ const target = node.resolve(doc);
72
+ return yamlNodeToJs(target, lineCounter, doc);
73
+ }
74
+ if (isMap(node)) {
75
+ const out: Record<string, unknown> = {};
76
+ const positions: PositionMap = {};
77
+ let hasAnyPosition = false;
78
+ for (const pair of node.items) {
79
+ // Only string-keyed entries are valid in metaobjects authoring; ignore
80
+ // exotic keys (numeric / complex) — they'd already break the desugar.
81
+ if (!isScalar(pair.key)) continue;
82
+ const keyText = String(pair.key.value);
83
+ const valueJs = yamlNodeToJs(pair.value, lineCounter, doc);
84
+ out[keyText] = valueJs;
85
+ const keyRange = pair.key.range;
86
+ if (keyRange !== null && keyRange !== undefined) {
87
+ const pos = lineCounter.linePos(keyRange[0]);
88
+ positions[keyText] = { line: pos.line, col: pos.col };
89
+ hasAnyPosition = true;
90
+ }
91
+ }
92
+ if (hasAnyPosition) setPositionMap(out, positions);
93
+ return out;
94
+ }
95
+ if (isSeq(node)) {
96
+ return node.items.map((item) => yamlNodeToJs(item, lineCounter, doc));
97
+ }
98
+ // Tags / unsupported — fall back to null. The metaobjects authoring
99
+ // grammar does not use them.
100
+ return null;
101
+ }
@@ -0,0 +1,80 @@
1
+ // FR5b — YAML authoring source-position carrier (per ADR-0009).
2
+ //
3
+ // This module is split into two layers so the browser bundle (which
4
+ // imports from src/index.ts) stays free of the Node-only `yaml` package:
5
+ //
6
+ // - yaml-positions.ts (this file) — pure types + Symbol + accessors. NO
7
+ // `yaml` import. Imported by parser-core.ts so the source-on-node
8
+ // stamper can read positions when stamping `format: "yaml"` envelopes.
9
+ // - yaml-positions-walker.ts — depends on `yaml`. Imported only by
10
+ // parser-yaml.ts (and parser-yaml itself only ships server-side).
11
+ //
12
+ // Source-map carrier (per the FR5b spec's "open question" §2): a
13
+ // Symbol-keyed, non-enumerable property on the wrapper-mapping object. The
14
+ // symbol is the well-known cross-port key
15
+ // `Symbol.for("@metaobjectsdev/yamlPositionByKey")`, so any plugin that
16
+ // touches the canonical JS can read positions if it knows to look. The
17
+ // map's keys are the wrapper's own keys (e.g. "object.entity" for a
18
+ // wrapper `{ "object.entity": { ... } }` or "name" / "package" /
19
+ // "children" for the body keys of a node).
20
+ //
21
+ // Rationale for "symbol-keyed property" over a parallel sourcemap / wrapper
22
+ // type:
23
+ // - Invisible to JSON.stringify and Object.keys (non-enumerable).
24
+ // - No parallel data structure to keep in sync — the position rides with
25
+ // the node it describes.
26
+ // - No wrapper type — desugar still operates on plain JS objects, so the
27
+ // existing Rule 1–5 logic does not need a rewrite.
28
+ //
29
+ // On desugar-synthesized nodes (Rule 2's scalar-body lift): the synthesized
30
+ // body `{ name: rawScalar }` inherits the wrapper key's position from the
31
+ // parent's position map. On any other synthesis (Rule 4's isArray stamping,
32
+ // for example), the position survives because we shallow-copy via the
33
+ // existing desugar path.
34
+
35
+ /** Cross-port well-known symbol key for the position-by-key map. */
36
+ export const YAML_POSITION_BY_KEY = Symbol.for(
37
+ "@metaobjectsdev/yamlPositionByKey",
38
+ );
39
+
40
+ /** A YAML source position — 1-indexed line and column. */
41
+ export interface YamlPosition {
42
+ readonly line: number;
43
+ readonly col: number;
44
+ }
45
+
46
+ /** The position-by-key map attached to a mapping object. */
47
+ export type PositionMap = Record<string, YamlPosition>;
48
+
49
+ /** Read the position-by-key map from a JS object, if present.
50
+ * Returns undefined for primitives, arrays, null, and untagged objects. */
51
+ export function getPositionMap(obj: unknown): PositionMap | undefined {
52
+ if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
53
+ return undefined;
54
+ }
55
+ return (obj as { [YAML_POSITION_BY_KEY]?: PositionMap })[YAML_POSITION_BY_KEY];
56
+ }
57
+
58
+ /** Read the position for a specific key on a mapping object. */
59
+ export function getYamlPosition(
60
+ obj: unknown,
61
+ key: string,
62
+ ): YamlPosition | undefined {
63
+ const map = getPositionMap(obj);
64
+ return map?.[key];
65
+ }
66
+
67
+ /** Attach (or replace) the position-by-key map on a mapping object. The map
68
+ * property is non-enumerable so JSON.stringify and `for (const k in obj)`
69
+ * loops do not see it. */
70
+ export function setPositionMap(
71
+ obj: Record<string, unknown>,
72
+ positions: PositionMap,
73
+ ): void {
74
+ Object.defineProperty(obj, YAML_POSITION_BY_KEY, {
75
+ value: positions,
76
+ enumerable: false,
77
+ writable: true,
78
+ configurable: true,
79
+ });
80
+ }
package/src/errors.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  // Typed error classes for the metadata parser.
2
2
 
3
+ import type { ErrorSource, LoaderError, NodeContext } from "./source.js";
4
+
3
5
  /** Stable, language-neutral error codes — mirrors fixtures/conformance/ERROR-CODES.json. */
4
6
  // NOTE: The following codes are forward-declared (no emitting site in the current
5
7
  // TS parser/loader — the condition is not yet detected):
@@ -50,25 +52,72 @@ export const ERROR_CODES = [
50
52
  // ADR-0006 D2 — YAML type-coercion guard. Emitted by every port's YAML
51
53
  // loader when a coerced scalar mismatches the schema-declared type.
52
54
  "ERR_YAML_COERCION",
55
+ // FR5c — multi-file overlay merge produced a conflicting attribute value:
56
+ // two contributors set the same @attr to different non-empty values.
57
+ "ERR_MERGE_CONFLICT",
53
58
  "ERR_UNKNOWN",
54
59
  ] as const;
55
60
 
61
+ /** Warning codes — same envelope shape as errors but advisory. */
62
+ export const WARNING_CODES = [
63
+ // FR5c — two contributors declared the same node identically (no semantic
64
+ // change). Emitted at the overlay-merge boundary.
65
+ "WARN_DUPLICATE_DECLARATION",
66
+ // Pre-FR5c legacy: parser/validator messages still surface as plain
67
+ // strings; wrapped at the loader boundary into the envelope shape with
68
+ // this code. Retired as those sites are migrated to envelopes.
69
+ "WARN_LEGACY",
70
+ ] as const;
71
+ export type WarningCode = (typeof WARNING_CODES)[number];
72
+
56
73
  export type ErrorCode = (typeof ERROR_CODES)[number];
57
74
 
58
- export class ParseError extends Error {
59
- readonly source: string | undefined;
60
- readonly path: string | undefined; // logical path within the JSON, e.g. "metadata.children[2].field"
61
- readonly code: ErrorCode | undefined;
75
+ /**
76
+ * Loader error carrying the ADR-0009 LoaderError envelope.
77
+ *
78
+ * Public shape (FR5a):
79
+ * new ParseError(message, { code, source, suggestions?, fixture?, node? })
80
+ *
81
+ * - `code` and `source` are required.
82
+ * - `source` is the ErrorSource discriminated union (json/yaml/merged/resolved/
83
+ * database/code) — the same envelope every cross-language port emits.
84
+ * - `suggestions[]`, `fixture`, `node` are optional per ADR-0009 §RECOMMENDED;
85
+ * FR5a does not populate them, FR5b–FR5e may.
86
+ *
87
+ * Legacy fields (`path?: string`, `source?: string`) were superseded by the
88
+ * envelope's `jsonPath` and `files` and have been dropped — see CHANGELOG.
89
+ */
90
+ export class ParseError extends Error implements LoaderError {
91
+ readonly code: ErrorCode;
92
+ readonly source: ErrorSource;
93
+ readonly suggestions?: string[];
94
+ readonly fixture?: string;
95
+ readonly node?: NodeContext;
62
96
 
63
97
  constructor(
64
98
  message: string,
65
- opts?: { source?: string; path?: string; code?: ErrorCode },
99
+ opts: {
100
+ code: ErrorCode;
101
+ source: ErrorSource;
102
+ suggestions?: string[];
103
+ fixture?: string;
104
+ node?: NodeContext;
105
+ },
66
106
  ) {
67
107
  super(message);
68
108
  this.name = "ParseError";
69
- this.source = opts?.source;
70
- this.path = opts?.path;
71
- this.code = opts?.code;
109
+ this.code = opts.code;
110
+ this.source = opts.source;
111
+ // exactOptionalPropertyTypes: only assign when defined.
112
+ if (opts.suggestions !== undefined) {
113
+ (this as { suggestions?: string[] }).suggestions = opts.suggestions;
114
+ }
115
+ if (opts.fixture !== undefined) {
116
+ (this as { fixture?: string }).fixture = opts.fixture;
117
+ }
118
+ if (opts.node !== undefined) {
119
+ (this as { node?: NodeContext }).node = opts.node;
120
+ }
72
121
  }
73
122
  }
74
123