1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . default = void 0 ;
7+
8+ var _Node = require ( "./ast/Node" ) ;
9+
10+ var _errors = require ( "./errors" ) ;
11+
12+ var _Tags = require ( "./Tags" ) ;
13+
14+ var _Collection = _interopRequireWildcard ( require ( "./schema/Collection" ) ) ;
15+
16+ var _Node2 = require ( "./schema/Node" ) ;
17+
18+ function _interopRequireWildcard ( obj ) { if ( obj && obj . __esModule ) { return obj ; } else { var newObj = { } ; if ( obj != null ) { for ( var key in obj ) { if ( Object . prototype . hasOwnProperty . call ( obj , key ) ) { var desc = Object . defineProperty && Object . getOwnPropertyDescriptor ? Object . getOwnPropertyDescriptor ( obj , key ) : { } ; if ( desc . get || desc . set ) { Object . defineProperty ( newObj , key , desc ) ; } else { newObj [ key ] = obj [ key ] ; } } } } newObj . default = obj ; return newObj ; } }
19+
20+ function _sliceIterator ( arr , i ) { var _arr = [ ] ; var _n = true ; var _d = false ; var _e = undefined ; try { for ( var _i = arr [ Symbol . iterator ] ( ) , _s ; ! ( _n = ( _s = _i . next ( ) ) . done ) ; _n = true ) { _arr . push ( _s . value ) ; if ( i && _arr . length === i ) break ; } } catch ( err ) { _d = true ; _e = err ; } finally { try { if ( ! _n && _i [ "return" ] != null ) _i [ "return" ] ( ) ; } finally { if ( _d ) throw _e ; } } return _arr ; }
21+
22+ function _slicedToArray ( arr , i ) { if ( Array . isArray ( arr ) ) { return arr ; } else if ( Symbol . iterator in Object ( arr ) ) { return _sliceIterator ( arr , i ) ; } else { throw new TypeError ( "Invalid attempt to destructure non-iterable instance" ) ; } }
23+
24+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
25+
26+ function _defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } }
27+
28+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
29+
30+ var isCollectionItem = function isCollectionItem ( node ) {
31+ return node && [ _Node . Type . MAP_KEY , _Node . Type . MAP_VALUE , _Node . Type . SEQ_ITEM ] . includes ( node . type ) ;
32+ } ;
33+
34+ var Document =
35+ /*#__PURE__*/
36+ function ( ) {
37+ _createClass ( Document , [ {
38+ key : "parseTagDirective" ,
39+ value : function parseTagDirective ( directive ) {
40+ var _directive$parameters = _slicedToArray ( directive . parameters , 2 ) ,
41+ handle = _directive$parameters [ 0 ] ,
42+ prefix = _directive$parameters [ 1 ] ;
43+
44+ if ( handle && prefix ) {
45+ if ( this . tagPrefixes [ handle ] ) this . errors . push ( new _errors . YAMLSyntaxError ( directive , 'The TAG directive must only be given at most once per handle in the same document.' ) ) ;
46+ this . tagPrefixes [ handle ] = prefix ;
47+ } else {
48+ this . errors . push ( new _errors . YAMLSyntaxError ( directive , 'Insufficient parameters given for TAG directive' ) ) ;
49+ }
50+ }
51+ } , {
52+ key : "parseYamlDirective" ,
53+ value : function parseYamlDirective ( directive ) {
54+ var _directive$parameters2 = _slicedToArray ( directive . parameters , 1 ) ,
55+ version = _directive$parameters2 [ 0 ] ;
56+
57+ if ( this . version ) this . errors . push ( new _errors . YAMLSyntaxError ( directive , 'The YAML directive must only be given at most once per document.' ) ) ;
58+ if ( ! version ) this . errors . push ( new _errors . YAMLSyntaxError ( directive , 'Insufficient parameters given for YAML directive' ) ) ; else if ( version !== '1.2' ) this . errors . push ( new _errors . YAMLWarning ( directive , "Document will be parsed as YAML 1.2 rather than YAML " . concat ( version ) ) ) ;
59+ this . version = version ;
60+ }
61+ } ] ) ;
62+
63+ function Document ( tags ) {
64+ var _this = this ;
65+
66+ var _ref = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : { } ,
67+ _ref$directives = _ref . directives ,
68+ directives = _ref$directives === void 0 ? [ ] : _ref$directives ,
69+ _ref$contents = _ref . contents ,
70+ contents = _ref$contents === void 0 ? [ ] : _ref$contents ,
71+ error = _ref . error ;
72+
73+ var _ref2 = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : { } ,
74+ merge = _ref2 . merge ;
75+
76+ _classCallCheck ( this , Document ) ;
77+
78+ this . anchors = [ ] ;
79+ this . directives = directives ;
80+ this . errors = [ ] ;
81+
82+ if ( error ) {
83+ if ( ! error . source ) error . source = this ;
84+ this . errors . push ( error ) ;
85+ }
86+
87+ this . options = {
88+ merge : merge
89+ } ;
90+ this . rawContents = contents ;
91+ this . tagPrefixes = { } ;
92+ this . tags = tags ;
93+ this . version = null ;
94+ var directiveComments = [ ] ;
95+ directives . forEach ( function ( directive ) {
96+ var comment = directive . comment ,
97+ name = directive . name ;
98+
99+ switch ( name ) {
100+ case 'TAG' :
101+ _this . parseTagDirective ( directive ) ;
102+
103+ break ;
104+
105+ case 'YAML' :
106+ _this . parseYamlDirective ( directive ) ;
107+
108+ break ;
109+
110+ default :
111+ if ( name ) _this . errors . push ( new _errors . YAMLWarning ( directive , "YAML 1.2 only supports TAG and YAML directives, and not " . concat ( name ) ) ) ;
112+ }
113+
114+ if ( comment ) directiveComments . push ( comment ) ;
115+ } ) ;
116+ this . commentBefore = directiveComments . join ( '\n' ) || null ;
117+ var comments = {
118+ before : [ ] ,
119+ after : [ ]
120+ } ;
121+ var contentNodes = [ ] ;
122+ contents . forEach ( function ( node ) {
123+ if ( node . valueRange && ! node . valueRange . isEmpty ) {
124+ contentNodes . push ( _this . resolveNode ( node ) ) ;
125+ } else if ( node . comment ) {
126+ var cc = contentNodes . length === 0 ? comments . before : comments . after ;
127+ cc . push ( node . comment ) ;
128+ }
129+ } ) ;
130+
131+ switch ( contentNodes . length ) {
132+ case 0 :
133+ this . contents = null ;
134+ comments . after = comments . before ;
135+ break ;
136+
137+ case 1 :
138+ this . contents = contentNodes [ 0 ] ;
139+
140+ if ( this . contents ) {
141+ var cb = comments . before . join ( '\n' ) || null ;
142+
143+ if ( cb ) {
144+ var cbNode = this . contents instanceof _Collection . default && this . contents . items [ 0 ] ? this . contents . items [ 0 ] : this . contents ;
145+ cbNode . commentBefore = cbNode . commentBefore ? "" . concat ( cb , "\n" ) . concat ( cbNode . commentBefore ) : cb ;
146+ }
147+ } else {
148+ comments . after = comments . before . concat ( comments . after ) ;
149+ }
150+
151+ break ;
152+
153+ default :
154+ this . errors . push ( new _errors . YAMLSyntaxError ( null , 'Document is not valid YAML (bad indentation?)' ) ) ;
155+ this . contents = contentNodes ;
156+ if ( this . contents [ 0 ] ) this . contents [ 0 ] . commentBefore = comments . before . join ( '\n' ) || null ; else comments . after = comments . before . concat ( comments . after ) ;
157+ }
158+
159+ this . comment = comments . after . join ( '\n' ) || null ;
160+ }
161+
162+ _createClass ( Document , [ {
163+ key : "resolveTagName" ,
164+ value : function resolveTagName ( node ) {
165+ var tag = node . tag ,
166+ type = node . type ;
167+ var nonSpecific = false ;
168+
169+ if ( tag ) {
170+ var handle = tag . handle ,
171+ suffix = tag . suffix ,
172+ verbatim = tag . verbatim ;
173+
174+ if ( verbatim ) {
175+ if ( verbatim !== '!' && verbatim !== '!!' ) return verbatim ;
176+ this . errors . push ( new _errors . YAMLSyntaxError ( node , "Verbatim tags aren't resolved, so " . concat ( verbatim , " is invalid." ) ) ) ;
177+ } else if ( handle === '!' && ! suffix ) {
178+ nonSpecific = true ;
179+ } else {
180+ var prefix = this . tagPrefixes [ handle ] || _Tags . DefaultTagPrefixes [ handle ] ;
181+
182+ if ( prefix ) {
183+ if ( suffix ) return prefix + suffix ;
184+ this . errors . push ( new _errors . YAMLSyntaxError ( node , "The " . concat ( handle , " tag has no suffix." ) ) ) ;
185+ } else {
186+ this . errors . push ( new _errors . YAMLSyntaxError ( node , "The " . concat ( handle , " tag handle is non-default and was not declared." ) ) ) ;
187+ }
188+ }
189+ }
190+
191+ switch ( type ) {
192+ case _Node . Type . BLOCK_FOLDED :
193+ case _Node . Type . BLOCK_LITERAL :
194+ case _Node . Type . QUOTE_DOUBLE :
195+ case _Node . Type . QUOTE_SINGLE :
196+ return _Tags . DefaultTags . STR ;
197+
198+ case _Node . Type . FLOW_MAP :
199+ case _Node . Type . MAP :
200+ return _Tags . DefaultTags . MAP ;
201+
202+ case _Node . Type . FLOW_SEQ :
203+ case _Node . Type . SEQ :
204+ return _Tags . DefaultTags . SEQ ;
205+
206+ case _Node . Type . PLAIN :
207+ return nonSpecific ? _Tags . DefaultTags . STR : null ;
208+
209+ default :
210+ return null ;
211+ }
212+ }
213+ } , {
214+ key : "resolveNode" ,
215+ value : function resolveNode ( node ) {
216+ if ( ! node ) return null ;
217+ var anchors = this . anchors ,
218+ errors = this . errors ,
219+ tags = this . tags ;
220+ var hasAnchor = false ;
221+ var hasTag = false ;
222+ var comments = {
223+ before : [ ] ,
224+ after : [ ]
225+ } ;
226+ var props = isCollectionItem ( node . context . parent ) ? node . context . parent . props . concat ( node . props ) : node . props ;
227+ props . forEach ( function ( _ref3 , i ) {
228+ var start = _ref3 . start ,
229+ end = _ref3 . end ;
230+
231+ switch ( node . context . src [ start ] ) {
232+ case _Node . Char . COMMENT :
233+ if ( ! node . commentHasRequiredWhitespace ( start ) ) errors . push ( new _errors . YAMLSyntaxError ( node , 'Comments must be separated from other tokens by white space characters' ) ) ;
234+ var c = node . context . src . slice ( start + 1 , end ) ;
235+ var header = node . header ,
236+ valueRange = node . valueRange ;
237+
238+ if ( valueRange && ( start > valueRange . start || header && start > header . start ) ) {
239+ comments . after . push ( c ) ;
240+ } else {
241+ comments . before . push ( c ) ;
242+ }
243+
244+ break ;
245+
246+ case _Node . Char . ANCHOR :
247+ if ( hasAnchor ) errors . push ( new _errors . YAMLSyntaxError ( node , 'A node can have at most one anchor' ) ) ;
248+ hasAnchor = true ;
249+ break ;
250+
251+ case _Node . Char . TAG :
252+ if ( hasTag ) errors . push ( new _errors . YAMLSyntaxError ( node , 'A node can have at most one tag' ) ) ;
253+ hasTag = true ;
254+ break ;
255+ }
256+ } ) ;
257+ if ( hasAnchor ) anchors [ node . anchor ] = node ;
258+ var res ;
259+
260+ if ( node . type === _Node . Type . ALIAS ) {
261+ if ( hasAnchor || hasTag ) errors . push ( new _errors . YAMLSyntaxError ( node , 'An alias node must not specify any properties' ) ) ;
262+ var src = anchors [ node . rawValue ] ;
263+
264+ if ( ! src ) {
265+ errors . push ( new _errors . YAMLReferenceError ( node , "Aliased anchor not found: " . concat ( node . rawValue ) ) ) ;
266+ return null ;
267+ }
268+
269+ res = src . resolved ;
270+ } else {
271+ var tagName = this . resolveTagName ( node ) ;
272+
273+ if ( tagName ) {
274+ res = tags . resolve ( this , node , tagName ) ;
275+ } else {
276+ if ( node . type !== _Node . Type . PLAIN ) {
277+ errors . push ( new _errors . YAMLSyntaxError ( node , "Failed to resolve " . concat ( node . type , " node here" ) ) ) ;
278+ return null ;
279+ }
280+
281+ try {
282+ res = tags . resolveScalar ( node . strValue || '' ) ;
283+ } catch ( error ) {
284+ if ( ! error . source ) error . source = node ;
285+ errors . push ( error ) ;
286+ return null ;
287+ }
288+ }
289+ }
290+
291+ if ( res ) {
292+ var cb = comments . before . join ( '\n' ) ;
293+ if ( cb ) res . commentBefore = res . commentBefore ? "" . concat ( res . commentBefore , "\n" ) . concat ( cb ) : cb ;
294+ var ca = comments . after . join ( '\n' ) ;
295+ if ( ca ) res . comment = res . comment ? "" . concat ( res . comment , "\n" ) . concat ( ca ) : ca ;
296+ }
297+
298+ return node . resolved = res ;
299+ }
300+ } , {
301+ key : "toJSON" ,
302+ value : function toJSON ( ) {
303+ return ( 0 , _Collection . toJSON ) ( this . contents ) ;
304+ }
305+ } , {
306+ key : "toString" ,
307+ value : function toString ( ) {
308+ var _this2 = this ;
309+
310+ var contents = this . contents ;
311+
312+ if ( Array . isArray ( contents ) ) {
313+ if ( contents . length <= 1 ) contents = contents [ 0 ] || null ; else throw new Error ( 'Document with multiple content nodes cannot be stringified' ) ;
314+ }
315+
316+ var lines = this . directives . filter ( function ( _ref4 ) {
317+ var comment = _ref4 . comment ;
318+ return comment ;
319+ } ) . map ( function ( _ref5 ) {
320+ var comment = _ref5 . comment ;
321+ return comment . replace ( / ^ / gm, '#' ) ;
322+ } ) ;
323+ var hasDirectives = false ;
324+
325+ if ( this . version ) {
326+ lines . push ( '%YAML 1.2' ) ;
327+ hasDirectives = true ;
328+ }
329+
330+ Object . keys ( this . tagPrefixes ) . forEach ( function ( handle ) {
331+ var prefix = _this2 . tagPrefixes [ handle ] ;
332+ lines . push ( "%TAG " . concat ( handle , " " ) . concat ( prefix ) ) ;
333+ hasDirectives = true ;
334+ } ) ;
335+ if ( hasDirectives ) lines . push ( '---' ) ;
336+
337+ if ( contents ) {
338+ if ( contents . commentBefore ) lines . push ( contents . commentBefore . replace ( / ^ / gm, '#' ) ) ;
339+ var options = {
340+ // top-level block scalars need to be indented if followed by a comment
341+ forceBlockIndent : ! ! this . comment ,
342+ indent : ''
343+ } ;
344+ var comment = contents . comment ;
345+ var body = this . tags . stringify ( contents , options , function ( ) {
346+ comment = null ;
347+ } ) ;
348+ lines . push ( ( 0 , _Node2 . addComment ) ( body , '' , comment ) ) ;
349+ }
350+
351+ if ( this . comment ) lines . push ( this . comment . replace ( / ^ / gm, '#' ) ) ;
352+ return lines . join ( '\n' ) + '\n' ;
353+ }
354+ } ] ) ;
355+
356+ return Document ;
357+ } ( ) ;
358+
359+ exports . default = Document ;
0 commit comments