All files / parsers yml.ts

87.96% Statements 95/108
77.04% Branches 47/61
100% Functions 10/10
94.56% Lines 87/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 1696x   6x   6x         8x 8x 8x           5x 5x 5x     5x 5x 5x 5x 5x         1x       1x       3x 3x 3x           3x               10x 8x 8x 8x 5x 5x   8x   2x 1x 1x 1x 1x 3x     1x 1x 3x 3x 3x   1x     1x 1x 1x 1x 2x 2x     1x 1x 1x 2x 2x 2x 2x 2x 2x   1x 1x 1x 1x 1x   1x                   2x 2x         2x 2x 2x 6x 6x     2x 2x 5x 5x 5x 5x 5x     2x     6x     481x     7x 3x     4x 4x 1x       3x 1x     2x       2x 2x      
import * as fs from "fs";
 
import { parse, parseDocument, stringify } from "yaml";
import type { Pair, YAMLMap, YAMLSeq } from "yaml/types";
import { Scalar as ScalarCtor } from "yaml/types";
 
import type { CustomParser } from "./index";
 
function getKeyString(keyNode: unknown): string | undefined {
  Iif (keyNode == null) return undefined;
  if (typeof (keyNode as { value?: unknown }).value !== "undefined") {
    return String((keyNode as { value: unknown }).value);
  }
  return typeof keyNode === "string" ? keyNode : undefined;
}
 
function copyComments(source: Pair, target: Pair): void {
  Iif (source.comment != null) target.comment = source.comment;
  if (source.commentBefore != null) target.commentBefore = source.commentBefore;
  const srcVal = source.value as
    | { comment?: string | null; commentBefore?: string | null }
    | undefined;
  const tgtVal = target.value;
  if (srcVal && tgtVal && typeof tgtVal === "object" && tgtVal !== null) {
    const t = tgtVal as { comment?: string | null; commentBefore?: string | null };
    if (srcVal.comment != null) t.comment = srcVal.comment;
    Iif (srcVal.commentBefore != null) t.commentBefore = srcVal.commentBefore;
  }
}
 
function isYamlMap(node: unknown): node is YAMLMap {
  return node != null && typeof node === "object" && Array.isArray((node as YAMLMap).items);
}
 
function isYamlSeq(node: unknown): node is YAMLSeq {
  return node != null && typeof node === "object" && Array.isArray((node as YAMLSeq).items);
}
 
function seqItemValueKey(item: unknown): string {
  Iif (item == null) return String(item);
  const n = item as { value?: unknown; toJSON?: () => unknown };
  if (typeof n.value !== "undefined") return JSON.stringify(n.value);
  Iif (typeof n.toJSON === "function") return JSON.stringify(n.toJSON());
  return JSON.stringify(item);
}
 
function primitiveValueKey(v: unknown): string {
  return JSON.stringify(v);
}
 
function createValueWithComments(
  schema: { createNode: (v: unknown) => unknown; createPair: (k: unknown, v: unknown) => Pair },
  oldNode: unknown,
  newValue: unknown,
): unknown {
  if (newValue === null || typeof newValue !== "object") {
    const node = new ScalarCtor(newValue);
    const old = oldNode as { comment?: string | null; commentBefore?: string | null } | undefined;
    if (old) {
      if (old.comment != null) node.comment = old.comment;
      Iif (old.commentBefore != null) node.commentBefore = old.commentBefore;
    }
    return node;
  }
  if (Array.isArray(newValue)) {
    const oldSeq = isYamlSeq(oldNode) ? (oldNode as YAMLSeq) : null;
    const oldItemsByValue = new Map<string, unknown>();
    if (oldSeq && oldSeq.items) {
      for (const item of oldSeq.items) {
        oldItemsByValue.set(seqItemValueKey(item), item);
      }
    }
    const newSeq = schema.createNode([]) as YAMLSeq;
    for (const el of newValue) {
      const oldItem = oldItemsByValue.get(primitiveValueKey(el));
      const itemNode = createValueWithComments(schema, oldItem, el);
      newSeq.add(itemNode);
    }
    return newSeq;
  }
  // newValue is a plain object; preserve comments from old map if present
  const oldMap = isYamlMap(oldNode) ? (oldNode as YAMLMap) : null;
  const oldPairsByKey = new Map<string, Pair>();
  if (oldMap) {
    for (const pair of (oldMap.items || []) as Pair[]) {
      const keyStr = getKeyString(pair.key);
      if (keyStr !== undefined) oldPairsByKey.set(keyStr, pair);
    }
  }
  const newMap = schema.createNode({}) as YAMLMap;
  const obj = newValue as Record<string, unknown>;
  for (const k of Object.keys(obj)) {
    const oldPair = oldPairsByKey.get(k);
    const childOldNode = oldPair ? oldPair.value : undefined;
    const childNewValue = createValueWithComments(schema, childOldNode, obj[k]);
    const newPair = schema.createPair(k, childNewValue);
    if (oldPair) copyComments(oldPair, newPair);
    newMap.add(newPair);
  }
  const result = newMap as { comment?: string | null; commentBefore?: string | null };
  const oldVal = oldNode as { comment?: string | null; commentBefore?: string | null } | undefined;
  if (oldVal && result) {
    Iif (oldVal.comment != null) result.comment = oldVal.comment;
    Iif (oldVal.commentBefore != null) result.commentBefore = oldVal.commentBefore;
  }
  return result;
}
 
function replaceContentsPreservingComments(
  doc: {
    contents: unknown;
    schema: { createNode: (v: unknown) => unknown; createPair: (k: unknown, v: unknown) => Pair };
  },
  newContent: Record<string, unknown>,
): void {
  const oldRoot = doc.contents as YAMLMap | null | undefined;
  Iif (!oldRoot || !Array.isArray((oldRoot as YAMLMap).items)) {
    doc.contents = doc.schema.createNode(newContent) as typeof doc.contents;
    return;
  }
 
  const schema = doc.schema;
  const oldPairsByKey = new Map<string, Pair>();
  for (const pair of (oldRoot as YAMLMap).items as Pair[]) {
    const keyStr = getKeyString(pair.key);
    if (keyStr !== undefined) oldPairsByKey.set(keyStr, pair);
  }
 
  const newMap = schema.createNode({}) as YAMLMap;
  for (const key of Object.keys(newContent)) {
    const oldPair = oldPairsByKey.get(key);
    const valueNode = createValueWithComments(schema, oldPair?.value, newContent[key]);
    const newPair = schema.createPair(key, valueNode);
    if (oldPair) copyComments(oldPair, newPair);
    newMap.add(newPair);
  }
 
  (doc as { contents: unknown }).contents = newMap;
}
 
export const ymlParser: CustomParser = {
  extension: "yml",
  parse: function <T>(content: string): T {
    return parse(content) as T;
  },
  stringify: function (content: any, filePath?: string) {
    if (!filePath || !fs.existsSync(filePath)) {
      return stringify(content);
    }
 
    const fileContent = fs.readFileSync(filePath, "utf8");
    if (!fileContent.trim()) {
      return stringify(content);
    }
 
    // newObject is the final saved object; existing file is only for ordering/comments
    if (content === null || typeof content !== "object" || Array.isArray(content)) {
      throw new Error("Cannot set root document to a primitive value");
    }
 
    const doc = parseDocument(fileContent) as {
      contents: unknown;
      schema: { createNode: (v: unknown) => unknown; createPair: (k: unknown, v: unknown) => Pair };
    };
    replaceContentsPreservingComments(doc, content);
    return doc.toString();
  },
};