From 30108cc828b5393d55dfbfc8087ab713f7145946 Mon Sep 17 00:00:00 2001 From: Dmitry Patsura Date: Thu, 24 Sep 2026 21:50:43 +0200 Subject: [PATCH] Parse the first block mapping key once (#800) Insert the MAPPING event via splice before the already parsed key events instead of rolling back and re-parsing the key. Co-authored-by: Claude Opus 5.5 (1M context) --- src/parser/parser.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/parser/parser.ts b/src/parser/parser.ts index edf1d09d..861ee60b 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -1100,20 +1100,18 @@ function readBlockMapping (state: ParserState, nodeIndent: number, flowIndent: n } if (!mappingOpened) { - restoreState(state, beforeKey) - addMappingEvent(state, beforeKey.position, props.anchorStart, props.anchorEnd, props.tagStart, props.tagEnd, COLLECTION_STYLE.BLOCK) + // The key is already parsed; insert the MAPPING event before its + // events instead of rolling back and re-reading the key. + state.events.splice(beforeKey.eventsLength, 0, { + type: EVENT_ID.MAPPING, + start: beforeKey.position, + anchorStart: props.anchorStart, + anchorEnd: props.anchorEnd, + tagStart: props.tagStart, + tagEnd: props.tagEnd, + style: COLLECTION_STYLE.BLOCK + }) mappingOpened = true - // The key, the `:` and the space after it were already validated - // above, before the rollback. Re-reading the same input cannot - // fail, so just consume it again without error checks. - parseNode(state, flowIndent, CONTEXT_FLOW_OUT, false, true) - - ch = state.input.charCodeAt(state.position) - while (isWhiteSpace(ch)) { - ch = state.input.charCodeAt(++state.position) - } - - state.position++ } detected = true