Skip to main content
Version: v0.0.4

Function: attachComments()

attachComments(records, warnings): AstmRecord[]

Attach every C (comment) record to its parent by position - the immediately-preceding H/P/O/R record; consecutive comments share that parent. Fail-safe: a comment with no valid preceding parent is an orphan - it is attached to the message root (attachedToRoot: true) with an ASTM_RECORD_ORPHAN_COMMENT warning, never dropped. Returns a new array; the non-comment records pass through unchanged.

Parameters

records

readonly AstmRecord[]

The built records, in wire order.

warnings

AstmRecordWarning[]

The accumulator an orphan comment warns onto.

Returns

AstmRecord[]

The records with each comment's parentIndex / attachedToRoot resolved.

Example

import { attachComments } from "@cosyte/astm";
// A comment with no preceding H/P/O/R is an orphan attached to the root.
const warnings: import("@cosyte/astm").AstmRecordWarning[] = [];
const out = attachComments(
[{ type: "C", recordIndex: 0, fields: [], attachedToRoot: false }],
warnings,
);
(out[0] as { attachedToRoot: boolean }).attachedToRoot; // true