@@ -5,10 +5,10 @@ import { createRoot } from "./factories/root.js";
55import { removeCstBlankLine } from "./preprocess.js" ;
66import Context from "./transforms/context.js" ;
77import { transformError } from "./transforms/error.js" ;
8+ import type { Document } from "./types.js" ;
89import { type ParseOptions , type Root } from "./types.js" ;
910import { removeFakeNodes } from "./utils/remove-fake-nodes.js" ;
1011import { updatePositions } from "./utils/update-positions.js" ;
11- import type { Document } from "./types.js" ;
1212
1313const MAP_KEY_DUPLICATE_ERROR_MESSAGE_PREFIX = 'Map keys must be unique; "' ;
1414const MAP_KEY_DUPLICATE_ERROR_MESSAGE_SUFFIX = '" is repeated' ;
@@ -32,17 +32,11 @@ function shouldIgnoreError(
3232
3333export function parse ( text : string , options ?: ParseOptions ) : Root {
3434 const allowDuplicateKeysInMap = options ?. allowDuplicateKeysInMap ;
35- const context = new Context ( text ) ;
36-
35+ const cst = YAML . parseCST ( text ) ;
36+ const context = new Context ( cst , text ) ;
3737 const documents : Document [ ] = [ ] ;
3838
39- const root = createRoot (
40- context . transformRange ( { origStart : 0 , origEnd : text . length } ) ,
41- documents ,
42- context . comments ,
43- ) ;
44-
45- for ( const cstDocument of context . cst ) {
39+ for ( const cstDocument of cst ) {
4640 const yamlDocument = new YAML . Document ( {
4741 merge : false ,
4842 keepCstNodes : true ,
@@ -61,6 +55,12 @@ export function parse(text: string, options?: ParseOptions): Root {
6155 documents . push ( document ) ;
6256 }
6357
58+ const root = createRoot (
59+ context . transformRange ( { origStart : 0 , origEnd : text . length } ) ,
60+ documents ,
61+ context . comments ,
62+ ) ;
63+
6464 attachComments ( root ) ;
6565 updatePositions ( root ) ;
6666 removeFakeNodes ( root ) ;
0 commit comments