@effected/yaml 0.1.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/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import { CollectionStyle, ScalarChomp, ScalarStyle, YamlAlias, YamlMap, YamlNode, YamlPair, YamlScalar, YamlSeq } from "./YamlNode.js";
2
+ import { YamlComposerErrorCode, YamlDiagnostic, YamlErrorCode, YamlLexErrorCode, YamlModifyErrorCode, YamlParseErrorCode, YamlStringifyErrorCode } from "./YamlDiagnostic.js";
3
+ import { Yaml, YamlParseError, YamlParseOptions, YamlStringifyError, YamlStringifyOptions } from "./Yaml.js";
4
+ import { YamlDirective, YamlDocument } from "./YamlDocument.js";
5
+ import { YamlEdit, YamlRange } from "./YamlEdit.js";
6
+ import { YamlFormat, YamlFormattingOptions, YamlModificationError } from "./YamlFormat.js";
7
+ import { YamlVisitor, YamlVisitorEvent } from "./YamlVisitor.js";
8
+
9
+ export { CollectionStyle, ScalarChomp, ScalarStyle, Yaml, YamlAlias, YamlComposerErrorCode, YamlDiagnostic, YamlDirective, YamlDocument, YamlEdit, YamlErrorCode, YamlFormat, YamlFormattingOptions, YamlLexErrorCode, YamlMap, YamlModificationError, YamlModifyErrorCode, YamlNode, YamlPair, YamlParseError, YamlParseErrorCode, YamlParseOptions, YamlRange, YamlScalar, YamlSeq, YamlStringifyError, YamlStringifyErrorCode, YamlStringifyOptions, YamlVisitor, YamlVisitorEvent };
@@ -0,0 +1,97 @@
1
+ import { YamlAlias, YamlMap, YamlScalar, YamlSeq } from "../../YamlNode.js";
2
+
3
+ //#region src/internal/composer/anchors.ts
4
+ /**
5
+ * Check if a pending anchor is being applied to an alias node (invalid in YAML 1.2 §3.2.2).
6
+ * Aliases represent references to existing anchored nodes and cannot have their own anchors.
7
+ *
8
+ * Uses `DuplicateAnchor` error code as a pragmatic reuse — semantically this is
9
+ * "anchor on alias" rather than "same anchor name defined twice", but adding a
10
+ * dedicated `AnchorOnAlias` code would be a public API change. The error message
11
+ * distinguishes the two cases for consumers inspecting the message text.
12
+ */
13
+ function checkAnchorOnAlias(pendingMeta, cst, state) {
14
+ if (pendingMeta.anchor !== void 0) state.errors.push({
15
+ code: "DuplicateAnchor",
16
+ message: `Anchor &${pendingMeta.anchor} cannot be applied to alias *${getAliasName(cst, state.text)}`,
17
+ offset: cst.offset,
18
+ length: cst.length
19
+ });
20
+ }
21
+ function makeAlias(cst, state) {
22
+ const name = getAliasName(cst, state.text);
23
+ if (!state.anchors.has(name)) state.errors.push({
24
+ code: "UndefinedAlias",
25
+ message: `Undefined alias: *${name}`,
26
+ offset: cst.offset,
27
+ length: cst.length
28
+ });
29
+ else {
30
+ state.aliasCount++;
31
+ if (state.aliasCount > state.options.maxAliasCount) state.errors.push({
32
+ code: "AliasCountExceeded",
33
+ message: `Alias count exceeded maximum of ${state.options.maxAliasCount}`,
34
+ offset: cst.offset,
35
+ length: cst.length
36
+ });
37
+ }
38
+ return new YamlAlias({
39
+ name,
40
+ offset: cst.offset,
41
+ length: cst.length
42
+ });
43
+ }
44
+ function registerAnchor(node, anchor, state, offset) {
45
+ if (state.anchors.has(anchor)) state.warnings.push({
46
+ code: "DuplicateAnchor",
47
+ message: `Duplicate anchor: &${anchor}`,
48
+ offset,
49
+ length: anchor.length + 1
50
+ });
51
+ state.anchors.set(anchor, node);
52
+ }
53
+ function getAnchorName(cst, text) {
54
+ if (text[cst.offset] === "&") return scanName(text, cst.offset + 1);
55
+ return cst.source;
56
+ }
57
+ function getAliasName(cst, text) {
58
+ if (text[cst.offset] === "*") return scanName(text, cst.offset + 1);
59
+ return cst.source;
60
+ }
61
+ function scanName(text, start) {
62
+ let end = start;
63
+ while (end < text.length) {
64
+ const ch = text[end];
65
+ if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "{" || ch === "}" || ch === "[" || ch === "]" || ch === "," || ch === void 0) break;
66
+ end++;
67
+ }
68
+ return text.slice(start, end);
69
+ }
70
+ /**
71
+ * Build an anchor map by walking the AST, collecting nodes that have anchors.
72
+ * Used to resolve aliases when extracting plain JavaScript values from
73
+ * parsed YAML documents.
74
+ */
75
+ function buildAnchorMap(node) {
76
+ const anchors = /* @__PURE__ */ new Map();
77
+ collectAnchors(node, anchors);
78
+ return anchors;
79
+ }
80
+ function collectAnchors(node, anchors) {
81
+ if (node === null) return;
82
+ if (node instanceof YamlScalar) {
83
+ if (node.anchor !== void 0) anchors.set(node.anchor, node);
84
+ } else if (node instanceof YamlMap) {
85
+ if (node.anchor !== void 0) anchors.set(node.anchor, node);
86
+ for (const pair of node.items) {
87
+ collectAnchors(pair.key, anchors);
88
+ collectAnchors(pair.value, anchors);
89
+ }
90
+ } else if (node instanceof YamlSeq) {
91
+ if (node.anchor !== void 0) anchors.set(node.anchor, node);
92
+ for (const item of node.items) collectAnchors(item, anchors);
93
+ }
94
+ }
95
+
96
+ //#endregion
97
+ export { buildAnchorMap, checkAnchorOnAlias, getAliasName, getAnchorName, makeAlias, registerAnchor, scanName };