@@ -11,7 +11,7 @@ import { transformDocument } from "./document.ts";
1111
1212export type DocumentData = {
1313 tokensBeforeBody : ( YAML_CST . CommentSourceToken | YAML . CST . Directive ) [ ] ;
14- cstNode : YAML . CST . Document ;
14+ cstNode : YAML . CST . Document | null ;
1515 node : YAML . Document . Parsed ;
1616 tokensAfterBody : YAML_CST . CommentSourceToken [ ] ;
1717 documentEnd : YAML . CST . DocumentEnd | null ;
@@ -81,45 +81,37 @@ export function transformDocuments(
8181 }
8282 }
8383
84- // Append buffered comments to the last document
85- if ( currentDocumentData && ! currentDocumentData . documentEnd ) {
86- currentDocumentData . tokensAfterBody . push ( ...bufferComments ) ;
87- bufferComments . length = 0 ;
84+ // istanbul ignore if -- @preserve
85+ if ( tokensBeforeBody . length > 0 ) {
86+ const [ firstToken ] = tokensBeforeBody ;
87+ throw new Error (
88+ `Unexpected '${ firstToken . type } ' token at ${ getPointText ( context . transformOffset ( firstToken . offset ) ) } ` ,
89+ ) ;
8890 }
8991
90- const nodes = documents . map ( document => transformDocument ( document , context ) ) ;
92+ if ( bufferComments . length > 0 ) {
93+ // If there is no document seen
94+ if ( ! currentDocumentData ) {
95+ currentDocumentData = {
96+ tokensBeforeBody : [ ...bufferComments ] ,
97+ cstNode : null ,
98+ node : parsedDocuments [ documents . length ] ,
99+ tokensAfterBody : [ ] ,
100+ documentEnd : null ,
101+ } ;
102+
103+ documents . push ( currentDocumentData ) ;
104+ bufferComments . length = 0 ;
105+ }
91106
92- if ( bufferComments . length === 0 ) {
93- return nodes ;
107+ // Append buffered comments to the last document
108+ if ( bufferComments . length > 0 ) {
109+ currentDocumentData . tokensAfterBody . push ( ...bufferComments ) ;
110+ bufferComments . length = 0 ;
111+ }
94112 }
95113
96- // Append remaining comments as a new document
97- const firstComment = bufferComments [ 0 ] ;
98- const commentDoc : Document = createDocument (
99- createPosition (
100- context . transformOffset ( firstComment . offset ) ,
101- context . transformOffset ( context . text . length ) ,
102- ) ,
103- false ,
104- false ,
105- createDocumentHead (
106- createPosition (
107- context . transformOffset ( firstComment . offset ) ,
108- context . transformOffset ( firstComment . offset ) ,
109- ) ,
110- [ ] ,
111- [ ] ,
112- null ,
113- ) ,
114- createDocumentBody (
115- createPosition (
116- context . transformOffset ( firstComment . offset ) ,
117- context . transformOffset ( context . text . length ) ,
118- ) ,
119- null ,
120- bufferComments . map ( token => context . transformComment ( token ) ) ,
121- ) ,
122- null ,
123- ) ;
124- return [ ...nodes , commentDoc ] ;
114+ const nodes = documents . map ( document => transformDocument ( document , context ) ) ;
115+
116+ return nodes ;
125117}
0 commit comments