All files yml.ts

87.17% Statements 102/117
73.46% Branches 72/98
100% Functions 10/10
94.89% Lines 93/98

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 169 170 171 172 173 174 175 176 177 178 179 180 181 1821x   1x           8x 8x 8x           5x 5x 5x 5x 5x     5x 5x 5x       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 6x 6x     2x 2x 5x 5x 5x 5x 5x     2x     10x     3x     7x 3x     4x 4x 1x       3x 1x     2x 2x 2x      
import * as fs from "node:fs";
 
import { parse, parseDocument, stringify, Scalar as ScalarCtor } from "yaml";
import type { Pair, YAMLMap, YAMLSeq } from "yaml";
 
import type { CustomParser } from "./index";
 
function getKeyString(keyNode: unknown): string | undefined {
  Iif (keyNode == null) return undefined;
  Eif (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 {
  const sourcePair = source as Pair & { comment?: string | null; commentBefore?: string | null };
  const targetPair = target as Pair & { comment?: string | null; commentBefore?: string | null };
  Iif (sourcePair.comment != null) targetPair.comment = sourcePair.comment;
  Iif (sourcePair.commentBefore != null) targetPair.commentBefore = sourcePair.commentBefore;
  const srcKey = source.key as
    | { comment?: string | null; commentBefore?: string | null }
    | undefined;
  const tgtKey = target.key;
  Eif (srcKey && tgtKey && typeof tgtKey === "object") {
    const t = tgtKey as {
      comment?: string | null;
      commentBefore?: string | null;
    };
    Iif (srcKey.comment != null) t.comment = srcKey.comment;
    if (srcKey.commentBefore != null) t.commentBefore = srcKey.commentBefore;
  }
  const srcVal = source.value as
    | { comment?: string | null; commentBefore?: string | null }
    | undefined;
  const tgtVal = target.value;
  Eif (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 };
  Eif (typeof n.value !== "undefined") return JSON.stringify(n.value);
  if (typeof n.toJSON === "function") return JSON.stringify(n.toJSON());
  return JSON.stringify(item);
}
 
function primitiveValueKey(v: unknown): string {
  return JSON.stringify(v);
}
 
interface NodeCreator {
  createNode: (value: unknown) => unknown;
  createPair: (key: unknown, value: unknown) => Pair;
}
 
function createValueWithComments(
  creator: NodeCreator,
  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>();
    Eif (oldSeq && oldSeq.items) {
      for (const item of oldSeq.items) {
        oldItemsByValue.set(seqItemValueKey(item), item);
      }
    }
    const newSeq = creator.createNode([]) as YAMLSeq;
    for (const el of newValue) {
      const oldItem = oldItemsByValue.get(primitiveValueKey(el));
      const itemNode = createValueWithComments(creator, 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>();
  Eif (oldMap) {
    for (const pair of (oldMap.items || []) as Pair[]) {
      const keyStr = getKeyString(pair.key);
      Eif (keyStr !== undefined) oldPairsByKey.set(keyStr, pair);
    }
  }
  const newMap = creator.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(creator, childOldNode, obj[k]);
    const newPair = creator.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;
  Eif (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;
  } & NodeCreator,
  newContent: Record<string, unknown>,
): void {
  const oldRoot = doc.contents as YAMLMap | null | undefined;
  Iif (!oldRoot || !Array.isArray((oldRoot as YAMLMap).items)) {
    doc.contents = doc.createNode(newContent) as typeof doc.contents;
    return;
  }
 
  const oldPairsByKey = new Map<string, Pair>();
  for (const pair of (oldRoot as YAMLMap).items as Pair[]) {
    const keyStr = getKeyString(pair.key);
    Eif (keyStr !== undefined) oldPairsByKey.set(keyStr, pair);
  }
 
  const newMap = doc.createNode({}) as YAMLMap;
  for (const key of Object.keys(newContent)) {
    const oldPair = oldPairsByKey.get(key);
    const valueNode = createValueWithComments(doc, oldPair?.value, newContent[key]);
    const newPair = doc.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 unknown as { contents: unknown } & NodeCreator;
    replaceContentsPreservingComments(doc, content);
    return doc.toString();
  },
};