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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185import { nonNullishOrThrow, } from '@monochromatic-dev/module-or-throw/ts';import type { JsoncComment, } from './comment.ts';import { appendComments, } from './parse-trivia.ts';import type { JsoncRecordEntry, JsoncValue,} from './value.ts';
/** Returns the elements with any comment found before the closing `]` attached to the last element, or the elements unchanged when there is no such comment. Builds a new array rather than mutating the input. @param elements - Parsed elements. @param dangling - Comments found before the close (possibly empty). @returns Elements with the dangling comment folded into the last one. @example ```ts attachDanglingToLastElement({ elements: [n], dangling: [c] }); // => [n with c appended] ``` */function attachDanglingToLastElement({ elements, dangling,}: { readonly elements: readonly JsoncValue[]; readonly dangling: readonly JsoncComment[];},): readonly JsoncValue[] { if ((dangling.length === 0) || (elements.length === 0)) return elements; /** Last element, which receives the dangling comment. */ const last = nonNullishOrThrow(elements.at(-1,),); return [ ...elements.slice( 0, -1, ), appendComments({ node: last, comments: dangling, },), ];}
/** Finishes an array node, attaching any comment found before the closing `]`: to the last element when present, otherwise to the empty array node itself. @param elements - Parsed elements. @param dangling - Comments found before the close (possibly empty). @returns Array node. @example ```ts closeArray({ elements: [], dangling: [{ type: 'block', text: ' x ' }] }); // => { kind: 'array', elements: [], comment: { type: 'block', text: ' x ' } } ``` */export function closeArray({ elements, dangling,}: { readonly elements: readonly JsoncValue[]; readonly dangling: readonly JsoncComment[];},): JsoncValue { /** Comments owned by the array node itself (only when it has no elements). */ const ownComments = (elements.length === 0) ? dangling : []; /** Array node before any own-comment attachment. */ const arrayNode: JsoncValue = { kind: 'array', elements: attachDanglingToLastElement({ elements, dangling, },), }; return appendComments({ node: arrayNode, comments: ownComments, },);}
/** Returns the entries with any comment found before the closing `}` attached to the last entry's value, or the entries unchanged when there is no such comment. Builds a new array rather than mutating the input. @param entries - Parsed entries. @param dangling - Comments found before the close (possibly empty). @returns Entries with the dangling comment folded into the last value. @example ```ts attachDanglingToLastEntry({ entries: [e], dangling: [c] }); // => [e with c appended to its value] ``` */function attachDanglingToLastEntry({ entries, dangling,}: { readonly entries: readonly JsoncRecordEntry[]; readonly dangling: readonly JsoncComment[];},): readonly JsoncRecordEntry[] { if ((dangling.length === 0) || (entries.length === 0)) return entries; /** Last entry, whose value receives the dangling comment. */ const last = nonNullishOrThrow(entries.at(-1,),); return [ ...entries.slice( 0, -1, ), { key: last.key, value: appendComments({ node: last.value, comments: dangling, },), }, ];}
/** Finishes a record node, attaching any comment found before the closing `}`: to the last entry's value when present, otherwise to the empty record node. @param entries - Parsed entries. @param dangling - Comments found before the close (possibly empty). @returns Record node. @example ```ts closeRecord({ entries: [], dangling: [] }); // => { kind: 'record', entries: [] } ``` */export function closeRecord({ entries, dangling,}: { readonly entries: readonly JsoncRecordEntry[]; readonly dangling: readonly JsoncComment[];},): JsoncValue { /** Comments owned by the record node itself (only when it has no entries). */ const ownComments = (entries.length === 0) ? dangling : []; /** Record node before any own-comment attachment. */ const recordNode: JsoncValue = { kind: 'record', entries: attachDanglingToLastEntry({ entries, dangling, },), }; return appendComments({ node: recordNode, comments: ownComments, },);}