Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';import type { JsoncComment, } from './comment.ts';import type { JsoncEditState, } from './edit-state.ts';import { findNode, type JsoncPath, NODE_ABSENT,} from './edit-navigate.ts';import { JsoncPathNotFoundError, JsoncTypeError,} from './errors.ts';import type { JsoncValue, } from './value.ts';
//region Sentinel
/** Sentinel returned by the comment getters when the addressed node or key has no comment. A `Symbol` rather than `undefined` so absence is an explicit value, not a nullish union. */export const COMMENT_ABSENT: unique symbol = Symbol( 'jsonc-edit/the queried JSONC value or key carries no attached comment',);
//endregion Sentinel
//region Generic transform
/** Rebuilds `node` with `transform` applied to the node the path addresses, descending the tree structurally. The target must exist. @param node - Current node being descended. @param path - Path to the node to transform. @param pathIndex - Index of the segment to resolve at this level. @param transform - Pure mapping applied to the addressed node. @returns Rebuilt node. @throws JsoncPathNotFoundError when a segment is missing. @throws JsoncTypeError when a segment cannot index the node's kind. @example ```ts transformAtPath({ node, path: ['a'], pathIndex: 0, transform: keepNode }); ``` */function transformAtPath({ node, path, pathIndex, transform,}: { readonly node: JsoncValue; readonly path: JsoncPath; readonly pathIndex: number; readonly transform: (target: JsoncValue) => JsoncValue;},): JsoncValue { if (pathIndex >= path.length) return transform(node,); /** Segment resolved at this level. */ const segment = nonNullishOrThrow(path.at(pathIndex,),); if ((node.kind === 'record') && ((typeof segment) === 'string')) { /** Index of the last entry matching the key, or -1 when absent. */ const matchIndex = node.entries .findLastIndex(function matchesKey(entry,): boolean { return entry.key .value === segment; },); if (matchIndex === (-1)) throw new JsoncPathNotFoundError({ path, },); /** Matched entry retained while its value is rebuilt. */ const entry = nonNullishOrThrow(node.entries[matchIndex],); return { ...node, entries: node.entries .with( matchIndex, { key: entry.key, value: transformAtPath({ node: entry.value, path, pathIndex: pathIndex + 1, transform, },), }, ), }; } if ((node.kind === 'array') && ((typeof segment) === 'number')) { if (!Number.isInteger(segment,)) throw new JsoncPathNotFoundError({ path, },); if ((segment < 0) || (segment >= node.elements .length)) throw new JsoncPathNotFoundError({ path, },); /** Matched element rebuilt while siblings retain identity. */ const element = nonNullishOrThrow(node.elements[segment],); return { ...node, elements: node.elements .with( segment, transformAtPath({ node: element, path, pathIndex: pathIndex + 1, transform, },), ), }; } throw new JsoncTypeError({ message: `jsonc comment edit: cannot index ${node.kind} with ${JSON.stringify(segment,)}`, },);}
//endregion Generic transform
//region Value comment
/** Reads the comment attached to the value at a path. @param state - Edit state. @param path - Path to the value. @returns The value's comment, or {@link COMMENT_ABSENT}. @throws JsoncPathNotFoundError when the path does not resolve. @example ```ts jsoncGetComment({ state, path: ['a'] }); ``` */export function jsoncGetComment({ state, path,}: { readonly state: JsoncEditState; readonly path: JsoncPath;},): JsoncComment | typeof COMMENT_ABSENT { /** Node addressed by the path. */ const node = findNode({ root: state.root, path, },); if (node === NODE_ABSENT) throw new JsoncPathNotFoundError({ path, },); return node.comment ?? COMMENT_ABSENT;}
/** Sets the comment attached to the value at a path, returning a fresh state. @param state - Edit state. @param path - Path to the value. @param comment - Comment to attach. @returns Fresh state with the comment applied. @throws JsoncPathNotFoundError when the path does not resolve. @example ```ts jsoncSetComment({ state, path: ['a'], comment: { type: 'inline', text: ' n' } }); ``` */export function jsoncSetComment({ state, path, comment,}: { readonly state: JsoncEditState; readonly path: JsoncPath; readonly comment: JsoncComment;},): JsoncEditState { return { root: transformAtPath({ node: state.root, path, pathIndex: 0, transform: function applyComment(target: JsoncValue,): JsoncValue { return { ...target, comment, }; }, },), };}
//endregion Value comment
//region Key comment
/** Reads the comment attached to the record key at a path. The final segment must be a string key. @param state - Edit state. @param path - Path whose final segment is the key. @returns The key's comment, or {@link COMMENT_ABSENT}. @throws JsoncTypeError when the path is empty or its final segment is not a key. @throws JsoncPathNotFoundError when the key does not resolve. @example ```ts jsoncGetKeyComment({ state, path: ['a'] }); ``` */export function jsoncGetKeyComment({ state, path,}: { readonly state: JsoncEditState; readonly path: JsoncPath;},): JsoncComment | typeof COMMENT_ABSENT { /** Final segment, which must address a record key. */ const key = lastKeySegment({ path, },); /** Parent record holding the keyed entry. */ const parent = findNode({ root: state.root, path: path.slice( 0, -1, ), },); if ((parent === NODE_ABSENT) || (parent.kind !== 'record')) throw new JsoncPathNotFoundError({ path, },); /** Last entry matching the key. */ const entry = parent.entries .findLast(function matchesKey(candidate,): boolean { return candidate.key .value === key; },); if (entry === undefined) throw new JsoncPathNotFoundError({ path, },); return entry.key .comment ?? COMMENT_ABSENT;}
/** Sets the comment attached to the record key at a path, returning a fresh state. @param state - Edit state. @param path - Path whose final segment is the key. @param comment - Comment to attach to the key. @returns Fresh state with the key comment applied. @throws JsoncTypeError when the path is empty or its final segment is not a key. @throws JsoncPathNotFoundError when the key does not resolve. @example ```ts jsoncSetKeyComment({ state, path: ['a'], comment: { type: 'inline', text: ' k' } }); ``` */export function jsoncSetKeyComment({ state, path, comment,}: { readonly state: JsoncEditState; readonly path: JsoncPath; readonly comment: JsoncComment;},): JsoncEditState { /** Final segment, which must address a record key. */ const key = lastKeySegment({ path, },); return { root: transformAtPath({ node: state.root, path: path.slice( 0, -1, ), pathIndex: 0, transform: function applyKeyComment(parent: JsoncValue,): JsoncValue { if (parent.kind !== 'record') throw new JsoncPathNotFoundError({ path, },); /** Index of the last entry matching the key. */ const matchIndex = parent.entries .findLastIndex(function matchesKey(entry,): boolean { return entry.key .value === key; },); if (matchIndex === (-1)) throw new JsoncPathNotFoundError({ path, },); /** Matched entry rebuilt while siblings retain identity. */ const entry = nonNullishOrThrow(parent.entries[matchIndex],); return { ...parent, entries: parent.entries .with( matchIndex, { key: { ...entry.key, comment, }, value: entry.value, }, ), }; }, },), };}
/** Extracts and validates the final path segment as a record key. @param path - Path whose final segment addresses a key. @returns The key string. @throws JsoncTypeError when the path is empty or its final segment is not a string. @example ```ts lastKeySegment({ path: ['a', 'b'] }); // => 'b' ``` */function lastKeySegment({ path,}: { readonly path: JsoncPath;},): string { if (path.length === 0) throw new JsoncTypeError({ message: 'jsonc key comment: empty path has no key', },); /** Final path segment. */ const key = nonNullishOrThrow(path.at(-1,),); if ((typeof key) !== 'string') throw new JsoncTypeError({ message: 'jsonc key comment: final segment is not a key', },); return key;}
//endregion Key comment