@effected/yaml 0.6.1 → 0.7.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/Yaml.js CHANGED
@@ -4,7 +4,7 @@ import { composeAllDocuments, composeFirstDocument } from "./internal/composer/d
4
4
  import { isFatalCode } from "./internal/diagnostics.js";
5
5
  import { StringifyDepthExceeded, StringifyFailure, stringifyValue } from "./internal/stringifier.js";
6
6
  import { YamlDiagnostic } from "./YamlDiagnostic.js";
7
- import { Effect, Option, Result, Schema, SchemaIssue, SchemaTransformation } from "effect";
7
+ import { Effect, Result, Schema, SchemaIssue, SchemaTransformation } from "effect";
8
8
 
9
9
  //#region src/Yaml.ts
10
10
  /**
@@ -126,7 +126,7 @@ var YamlStringifyOptions = class extends Schema.Class("YamlStringifyOptions")({
126
126
  *
127
127
  * @public
128
128
  */
129
- var YamlParseError = class extends Schema.TaggedErrorClass()("YamlParseError", {
129
+ var YamlParseError = class extends Schema.TaggedError()("YamlParseError", {
130
130
  diagnostics: Schema.Array(YamlDiagnostic),
131
131
  input: Schema.String
132
132
  }) {
@@ -144,7 +144,7 @@ var YamlParseError = class extends Schema.TaggedErrorClass()("YamlParseError", {
144
144
  *
145
145
  * @public
146
146
  */
147
- var YamlStringifyError = class extends Schema.TaggedErrorClass()("YamlStringifyError", {
147
+ var YamlStringifyError = class extends Schema.TaggedError()("YamlStringifyError", {
148
148
  diagnostics: Schema.Array(YamlDiagnostic),
149
149
  value: Schema.Unknown
150
150
  }) {
@@ -516,8 +516,8 @@ var Yaml = class Yaml {
516
516
  */
517
517
  static fromString(options) {
518
518
  return Schema.String.pipe(Schema.decodeTo(Schema.Unknown, SchemaTransformation.transformOrFail({
519
- decode: (input) => Yaml.parse(input, options).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue(Option.some(input), { message: error.message }))),
520
- encode: (value) => stringifyOrFail(value).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue(Option.some(value), { message: error.message })))
519
+ decode: (input) => Yaml.parse(input, options).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, input))),
520
+ encode: (value) => stringifyOrFail(value).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, value)))
521
521
  })));
522
522
  }
523
523
  /**
@@ -536,12 +536,12 @@ var Yaml = class Yaml {
536
536
  */
537
537
  static allFromString(options) {
538
538
  return Schema.String.pipe(Schema.decodeTo(Schema.Array(Schema.Unknown), SchemaTransformation.transformOrFail({
539
- decode: (input) => Yaml.parseAll(input, options).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue(Option.some(input), { message: error.message }))),
539
+ decode: (input) => Yaml.parseAll(input, options).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, input))),
540
540
  encode: (values) => Effect.gen(function* () {
541
541
  if (values.length === 0) return "";
542
542
  const parts = [];
543
543
  for (let index = 0; index < values.length; index++) {
544
- const yaml = yield* stringifyOrFail(values[index]).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue(Option.some(values), { message: error.message })));
544
+ const yaml = yield* stringifyOrFail(values[index]).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, values)));
545
545
  parts.push(index > 0 ? `---\n${yaml}` : yaml);
546
546
  }
547
547
  return parts.join("");
package/YamlDocument.js CHANGED
@@ -4,7 +4,7 @@ import { isFatalCode } from "./internal/diagnostics.js";
4
4
  import { StringifyDepthExceeded, StringifyFailure, stringifyDocument } from "./internal/stringifier.js";
5
5
  import { YamlDiagnostic } from "./YamlDiagnostic.js";
6
6
  import { YamlParseError, YamlStringifyError } from "./Yaml.js";
7
- import { Effect, Option, Schema, SchemaIssue, SchemaTransformation } from "effect";
7
+ import { Effect, Schema, SchemaIssue, SchemaTransformation } from "effect";
8
8
 
9
9
  //#region src/YamlDocument.ts
10
10
  /**
@@ -80,8 +80,8 @@ var YamlDocument = class YamlDocument extends Schema.Class("YamlDocument")({
80
80
  */
81
81
  static schema(options) {
82
82
  return Schema.String.pipe(Schema.decodeTo(Schema.instanceOf(YamlDocument), SchemaTransformation.transformOrFail({
83
- decode: (input) => YamlDocument.parse(input, options).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue(Option.some(input), { message: error.message }))),
84
- encode: (doc) => doc.stringify().pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue(Option.some(doc), { message: error.message })))
83
+ decode: (input) => YamlDocument.parse(input, options).pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, input))),
84
+ encode: (doc) => doc.stringify().pipe(Effect.mapError((error) => new SchemaIssue.InvalidValue({ message: error.message }, doc)))
85
85
  })));
86
86
  }
87
87
  /**
package/YamlFormat.js CHANGED
@@ -47,7 +47,7 @@ var YamlFormattingOptions = class extends Schema.Class("YamlFormattingOptions")(
47
47
  *
48
48
  * @public
49
49
  */
50
- var YamlModificationError = class extends Schema.TaggedErrorClass()("YamlModificationError", {
50
+ var YamlModificationError = class extends Schema.TaggedError()("YamlModificationError", {
51
51
  path: Schema.Array(Schema.Union([Schema.String, Schema.Number])),
52
52
  diagnostics: Schema.Array(YamlDiagnostic)
53
53
  }) {
@@ -508,7 +508,7 @@ function buildPairs(items, pairs, text) {
508
508
  const nullKey = new YamlScalar({
509
509
  value: null,
510
510
  style: "plain",
511
- offset: 0,
511
+ offset: valueSepOffset,
512
512
  length: 0
513
513
  });
514
514
  pairs.push(new YamlPair({
@@ -520,7 +520,7 @@ function buildPairs(items, pairs, text) {
520
520
  const nullKey = new YamlScalar({
521
521
  value: null,
522
522
  style: "plain",
523
- offset: 0,
523
+ offset: valueSepOffset,
524
524
  length: 0
525
525
  });
526
526
  pairs.push(new YamlPair({
@@ -265,10 +265,11 @@ function foldFlowLines(text) {
265
265
  }
266
266
  const isLast = i === lines.length - 1;
267
267
  const trimmed = isLast ? line.trimStart() : line.trim();
268
- if (trimmed === "") if (isLast) {
269
- if (result.length === 0 || result[result.length - 1] !== "\n") result += " ";
270
- } else result += "\n";
271
- else {
268
+ if (trimmed === "") {
269
+ if (isLast) {
270
+ if (result.length === 0 || result[result.length - 1] !== "\n") result += " ";
271
+ } else result += "\n";
272
+ } else {
272
273
  if (result.length > 0 && result[result.length - 1] !== "\n") result += " ";
273
274
  result += trimmed;
274
275
  }
@@ -681,8 +682,10 @@ function decodeBlockScalar(raw, fullText, nodeOffset) {
681
682
  } else break;
682
683
  }
683
684
  while (i < raw.length && raw[i] !== "\n" && raw[i] !== "\r") i++;
684
- if (i < raw.length) if (raw[i] === "\r" && raw[i + 1] === "\n") i += 2;
685
- else i++;
685
+ if (i < raw.length) {
686
+ if (raw[i] === "\r" && raw[i + 1] === "\n") i += 2;
687
+ else i++;
688
+ }
686
689
  let contentIndent = explicitIndent;
687
690
  let foundContent = explicitIndent > 0;
688
691
  if (explicitIndent > 0 && fullText !== void 0 && nodeOffset !== void 0) {
@@ -745,8 +748,10 @@ function decodeBlockScalar(raw, fullText, nodeOffset) {
745
748
  trailingNewlines.length = 0;
746
749
  lines.push(" ".repeat(spaces - contentIndent));
747
750
  } else trailingNewlines.push("");
748
- if (i < raw.length) if (raw[i] === "\r" && i + 1 < raw.length && raw[i + 1] === "\n") i += 2;
749
- else i++;
751
+ if (i < raw.length) {
752
+ if (raw[i] === "\r" && i + 1 < raw.length && raw[i + 1] === "\n") i += 2;
753
+ else i++;
754
+ }
750
755
  continue;
751
756
  }
752
757
  if (spaces < contentIndent) break;
@@ -756,8 +761,10 @@ function decodeBlockScalar(raw, fullText, nodeOffset) {
756
761
  const contentStart = i;
757
762
  while (i < raw.length && raw[i] !== "\n" && raw[i] !== "\r") i++;
758
763
  lines.push(extra + raw.slice(contentStart, i));
759
- if (i < raw.length) if (raw[i] === "\r" && i + 1 < raw.length && raw[i + 1] === "\n") i += 2;
760
- else i++;
764
+ if (i < raw.length) {
765
+ if (raw[i] === "\r" && i + 1 < raw.length && raw[i + 1] === "\n") i += 2;
766
+ else i++;
767
+ }
761
768
  }
762
769
  let value;
763
770
  if (isFolded) {
@@ -773,9 +780,10 @@ function decodeBlockScalar(raw, fullText, nodeOffset) {
773
780
  prevMoreIndented = isMoreIndented;
774
781
  hadContent = true;
775
782
  } else {
776
- if (result[result.length - 1] === "\n") if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
777
- else result += ln;
778
- else if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
783
+ if (result[result.length - 1] === "\n") {
784
+ if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
785
+ else result += ln;
786
+ } else if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
779
787
  else result += ` ${ln}`;
780
788
  prevMoreIndented = isMoreIndented;
781
789
  }
@@ -809,8 +817,10 @@ function validateBlockScalarLeadingEmpties(cst, state) {
809
817
  const raw = cst.source;
810
818
  let i = 0;
811
819
  while (i < raw.length && raw[i] !== "\n" && raw[i] !== "\r") i++;
812
- if (i < raw.length) if (raw[i] === "\r" && raw[i + 1] === "\n") i += 2;
813
- else i++;
820
+ if (i < raw.length) {
821
+ if (raw[i] === "\r" && raw[i + 1] === "\n") i += 2;
822
+ else i++;
823
+ }
814
824
  const emptyIndents = [];
815
825
  while (i < raw.length) {
816
826
  const lineStart = i;
@@ -824,8 +834,10 @@ function validateBlockScalarLeadingEmpties(cst, state) {
824
834
  indent: spaces,
825
835
  offsetInRaw: lineStart
826
836
  });
827
- if (i < raw.length) if (raw[i] === "\r" && raw[i + 1] === "\n") i += 2;
828
- else i++;
837
+ if (i < raw.length) {
838
+ if (raw[i] === "\r" && raw[i + 1] === "\n") i += 2;
839
+ else i++;
840
+ }
829
841
  continue;
830
842
  }
831
843
  const contentIndent = spaces;
@@ -286,12 +286,14 @@ function parseBlockMappingInner(state, indent) {
286
286
  continue;
287
287
  }
288
288
  if (token.kind === "block-map-key") {
289
+ if (token.column < indent && children.length > 0) break;
289
290
  sawExplicitKey = true;
290
291
  const leaf = consumeLeafToken(state);
291
292
  if (leaf) children.push(leaf);
292
293
  continue;
293
294
  }
294
295
  if (token.kind === "block-map-value") {
296
+ if (token.column < indent && children.length > 0) break;
295
297
  const leaf = consumeLeafToken(state);
296
298
  if (leaf) children.push(leaf);
297
299
  children.push(...parseBlockValue(state, indent, sawExplicitKey));
@@ -357,7 +359,7 @@ function parseBlockMappingInner(state, indent) {
357
359
  /**
358
360
  * Parse the value part after a ":" in a block mapping.
359
361
  */
360
- function parseBlockValue(state, _parentIndent, explicitKey = false) {
362
+ function parseBlockValue(state, parentIndent, explicitKey = false) {
361
363
  const nodes = [];
362
364
  while (!atEnd(state)) {
363
365
  const token = peek(state);
@@ -385,6 +387,10 @@ function parseBlockValue(state, _parentIndent, explicitKey = false) {
385
387
  nodes.push(parseBlockSequence(state, seqIndent));
386
388
  break;
387
389
  }
390
+ if (explicitKey && token.kind === "scalar" && !isBlockScalarToken(state) && hasImplicitMapAhead(state, parentIndent)) {
391
+ nodes.push(parseImplicitBlockMapping(state, parentIndent));
392
+ break;
393
+ }
388
394
  if (token.kind === "scalar" || token.kind === "anchor" || token.kind === "alias" || token.kind === "tag") {
389
395
  const leaf = consumeLeafToken(state);
390
396
  if (leaf) nodes.push(leaf);
@@ -540,11 +546,13 @@ function parseImplicitBlockMappingInner(state, seqIndent) {
540
546
  if (!token) break;
541
547
  if (isDocumentBoundary(token)) break;
542
548
  if (token.kind === "block-seq-entry" && token.column <= seqIndent) break;
549
+ if (token.kind === "block-map-key" && token.column <= seqIndent) break;
543
550
  if (isTrivia(token)) {
544
551
  children.push(...consumeTrivia(state));
545
552
  continue;
546
553
  }
547
554
  if (token.kind === "block-map-value") {
555
+ if (token.column <= seqIndent && children.length > 0) break;
548
556
  const leaf = consumeLeafToken(state);
549
557
  if (leaf) children.push(leaf);
550
558
  children.push(...parseBlockValue(state, seqIndent));
package/internal/lexer.js CHANGED
@@ -216,14 +216,15 @@ function createScanner(text) {
216
216
  let value = "";
217
217
  while (pos < text.length) {
218
218
  const ch = peek();
219
- if (ch === "'") if (peek(1) === "'") {
220
- value += "'";
221
- advance(2);
222
- } else {
223
- advance();
224
- return makeToken("scalar", value, start, sLine, sCol, pos - start);
225
- }
226
- else if (ch === "\n" || ch === "\r" && peek(1) === "\n") {
219
+ if (ch === "'") {
220
+ if (peek(1) === "'") {
221
+ value += "'";
222
+ advance(2);
223
+ } else {
224
+ advance();
225
+ return makeToken("scalar", value, start, sLine, sCol, pos - start);
226
+ }
227
+ } else if (ch === "\n" || ch === "\r" && peek(1) === "\n") {
227
228
  value += " ";
228
229
  if (ch === "\r") advance(2);
229
230
  else advance();
@@ -402,13 +403,17 @@ function createScanner(text) {
402
403
  hadHeaderWhitespace = true;
403
404
  advance();
404
405
  }
405
- if (pos < text.length && !isNewline(peek()) && peek() !== "") if (peek() === "#" && hadHeaderWhitespace) while (pos < text.length && !isNewline(peek())) advance();
406
- else {
407
- while (pos < text.length && !isNewline(peek())) advance();
408
- return makeToken("error", text.slice(start, pos), start, sLine, sCol);
406
+ if (pos < text.length && !isNewline(peek()) && peek() !== "") {
407
+ if (peek() === "#" && hadHeaderWhitespace) while (pos < text.length && !isNewline(peek())) advance();
408
+ else {
409
+ while (pos < text.length && !isNewline(peek())) advance();
410
+ return makeToken("error", text.slice(start, pos), start, sLine, sCol);
411
+ }
412
+ }
413
+ if (pos < text.length && isNewline(peek())) {
414
+ if (peek() === "\r" && peek(1) === "\n") advance(2);
415
+ else advance();
409
416
  }
410
- if (pos < text.length && isNewline(peek())) if (peek() === "\r" && peek(1) === "\n") advance(2);
411
- else advance();
412
417
  let contentIndent = explicitIndent;
413
418
  let foundContent = explicitIndent > 0;
414
419
  if (explicitIndent > 0) {
@@ -564,9 +569,10 @@ function createScanner(text) {
564
569
  prevMoreIndented = isMoreIndented;
565
570
  hadContent = true;
566
571
  } else {
567
- if (result[result.length - 1] === "\n") if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
568
- else result += ln;
569
- else if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
572
+ if (result[result.length - 1] === "\n") {
573
+ if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
574
+ else result += ln;
575
+ } else if (isMoreIndented || prevMoreIndented) result += `\n${ln}`;
570
576
  else result += ` ${ln}`;
571
577
  prevMoreIndented = isMoreIndented;
572
578
  }
@@ -98,10 +98,12 @@ function requiresQuoting(s, ignoreType = false) {
98
98
  if (!ignoreType && wouldBeResolved(s)) return true;
99
99
  const first = s[0];
100
100
  if (first === " " || first === " ") return true;
101
- if (first !== void 0 && INDICATOR_CHARS.has(first)) if (first === ":" || first === "?" || first === "-") {
102
- const second = s[1];
103
- if (s.length === 1 || second === " " || second === " ") return true;
104
- } else return true;
101
+ if (first !== void 0 && INDICATOR_CHARS.has(first)) {
102
+ if (first === ":" || first === "?" || first === "-") {
103
+ const second = s[1];
104
+ if (s.length === 1 || second === " " || second === " ") return true;
105
+ } else return true;
106
+ }
105
107
  if (s.startsWith("---") || s.startsWith("...")) return true;
106
108
  if (s.includes(": ") || s.includes(": ") || s.endsWith(":")) return true;
107
109
  if (s.includes(" #") || s.includes(" #")) return true;
@@ -132,14 +134,15 @@ function renderDoubleQuoted(s, canonical = false) {
132
134
  let result = "";
133
135
  for (let i = 0; i < escaped.length; i++) {
134
136
  const code = escaped.charCodeAt(i);
135
- if (isControlChar(code)) if (code === 0) result += "\\0";
136
- else if (code === 7) result += "\\a";
137
- else if (code === 8) result += "\\b";
138
- else if (code === 11) result += "\\v";
139
- else if (code === 12) result += "\\f";
140
- else if (code === 27) result += "\\e";
141
- else result += `\\x${code.toString(16).padStart(2, "0")}`;
142
- else if (canonical && code > 126) {
137
+ if (isControlChar(code)) {
138
+ if (code === 0) result += "\\0";
139
+ else if (code === 7) result += "\\a";
140
+ else if (code === 8) result += "\\b";
141
+ else if (code === 11) result += "\\v";
142
+ else if (code === 12) result += "\\f";
143
+ else if (code === 27) result += "\\e";
144
+ else result += `\\x${code.toString(16).padStart(2, "0")}`;
145
+ } else if (canonical && code > 126) {
143
146
  if (code >= 55296 && code <= 56319 && i + 1 < escaped.length) {
144
147
  const low = escaped.charCodeAt(i + 1);
145
148
  if (low >= 56320 && low <= 57343) {
@@ -534,9 +537,10 @@ function stringifyScalarNodeLines(node, ctx) {
534
537
  return [parts.join(" ")];
535
538
  }
536
539
  let lines;
537
- if (val === null || val === void 0) if (isEmpty) lines = [""];
538
- else lines = ["null"];
539
- else if (typeof val === "boolean") lines = [val ? "true" : "false"];
540
+ if (val === null || val === void 0) {
541
+ if (isEmpty) lines = [""];
542
+ else lines = ["null"];
543
+ } else if (typeof val === "boolean") lines = [val ? "true" : "false"];
540
544
  else if (typeof val === "number") lines = [node.raw !== void 0 ? node.raw : renderNumber(val)];
541
545
  else if (typeof val === "string") lines = renderString(val, style, " ".repeat(ctx.indent), !!node.tag, ctx.forceDefaultStyles, node.chomp, ctx.parentPosition, ctx.quoteStyle).split("\n");
542
546
  else lines = [renderDoubleQuoted(String(val))];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effected/yaml",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "private": false,
5
5
  "description": "Zero-dependency YAML parsing, editing and formatting as Effect schemas.",
6
6
  "keywords": [
@@ -40,7 +40,7 @@
40
40
  "./package.json": "./package.json"
41
41
  },
42
42
  "peerDependencies": {
43
- "effect": "4.0.0-beta.101"
43
+ "effect": "4.0.0-beta.107"
44
44
  },
45
45
  "engines": {
46
46
  "node": ">=24.11.0"