1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . default = void 0 ;
7+
8+ var _rawYaml = require ( "raw-yaml" ) ;
9+
10+ var _errors = require ( "./errors" ) ;
11+
12+ var _Tags = require ( "./Tags" ) ;
13+
14+ var _Collection = require ( "./schema/Collection" ) ;
15+
16+ 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 ; }
17+
18+ 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" ) ; } }
19+
20+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
21+
22+ 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 ) ; } }
23+
24+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
25+
26+ var Document =
27+ /*#__PURE__*/
28+ function ( ) {
29+ _createClass ( Document , [ {
30+ key : "parseTagDirective" ,
31+ value : function parseTagDirective ( directive ) {
32+ var _directive$parameters = _slicedToArray ( directive . parameters , 2 ) ,
33+ handle = _directive$parameters [ 0 ] ,
34+ prefix = _directive$parameters [ 1 ] ;
35+
36+ if ( handle && prefix ) {
37+ 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.' ) ) ;
38+ this . tagPrefixes [ handle ] = prefix ;
39+ } else {
40+ this . errors . push ( new _errors . YAMLSyntaxError ( directive , 'Insufficient parameters given for TAG directive' ) ) ;
41+ }
42+ }
43+ } , {
44+ key : "parseYamlDirective" ,
45+ value : function parseYamlDirective ( directive ) {
46+ var _directive$parameters2 = _slicedToArray ( directive . parameters , 1 ) ,
47+ version = _directive$parameters2 [ 0 ] ;
48+
49+ if ( this . version ) this . errors . push ( new _errors . YAMLSyntaxError ( directive , 'The YAML directive must only be given at most once per document.' ) ) ;
50+ 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 ) ) ) ;
51+ this . version = version ;
52+ }
53+ } ] ) ;
54+
55+ function Document ( tags ) {
56+ var _this = this ;
57+
58+ var _ref = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : { } ,
59+ _ref$directives = _ref . directives ,
60+ directives = _ref$directives === void 0 ? [ ] : _ref$directives ,
61+ _ref$contents = _ref . contents ,
62+ contents = _ref$contents === void 0 ? [ ] : _ref$contents ;
63+
64+ var _ref2 = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : { } ,
65+ merge = _ref2 . merge ;
66+
67+ _classCallCheck ( this , Document ) ;
68+
69+ this . anchors = [ ] ;
70+ this . directives = directives ;
71+ this . errors = [ ] ;
72+ this . options = {
73+ merge : merge !== false
74+ } ;
75+ this . rawContents = contents ;
76+ this . tagPrefixes = { } ;
77+ this . tags = tags ;
78+ this . version = null ;
79+ directives . forEach ( function ( directive ) {
80+ var name = directive . name ;
81+
82+ switch ( name ) {
83+ case 'TAG' :
84+ _this . parseTagDirective ( directive ) ;
85+
86+ break ;
87+
88+ case 'YAML' :
89+ _this . parseYamlDirective ( directive ) ;
90+
91+ break ;
92+
93+ default :
94+ if ( name ) _this . errors . push ( new _errors . YAMLWarning ( directive , "YAML 1.2 only supports TAG and YAML directives, and not " . concat ( name ) ) ) ;
95+ }
96+ } ) ;
97+ var contentNodes = contents . filter ( function ( node ) {
98+ return node . valueRange && ! node . valueRange . isEmpty ;
99+ } ) . map ( function ( node ) {
100+ return _this . resolveNode ( node ) ;
101+ } ) ;
102+
103+ switch ( contentNodes . length ) {
104+ case 0 :
105+ this . contents = null ;
106+ break ;
107+
108+ case 1 :
109+ this . contents = contentNodes [ 0 ] ;
110+ break ;
111+
112+ default :
113+ this . errors . push ( new _errors . YAMLSyntaxError ( null , 'Document is not valid YAML (bad indentation?)' ) ) ;
114+ this . contents = contentNodes ;
115+ }
116+ }
117+
118+ _createClass ( Document , [ {
119+ key : "resolveTagName" ,
120+ value : function resolveTagName ( node ) {
121+ var tag = node . tag ,
122+ type = node . type ;
123+ var nonSpecific = false ;
124+
125+ if ( tag ) {
126+ var handle = tag . handle ,
127+ suffix = tag . suffix ,
128+ verbatim = tag . verbatim ;
129+
130+ if ( verbatim ) {
131+ if ( verbatim !== '!' && verbatim !== '!!' ) return verbatim ;
132+ this . errors . push ( new _errors . YAMLSyntaxError ( node , "Verbatim tags aren't resolved, so " . concat ( verbatim , " is invalid." ) ) ) ;
133+ } else if ( handle === '!' && ! suffix ) {
134+ nonSpecific = true ;
135+ } else {
136+ var prefix = this . tagPrefixes [ handle ] || _Tags . DefaultTagPrefixes [ handle ] ;
137+
138+ if ( prefix ) {
139+ if ( suffix ) return prefix + suffix ;
140+ this . errors . push ( new _errors . YAMLSyntaxError ( node , "The " . concat ( handle , " tag has no suffix." ) ) ) ;
141+ } else {
142+ this . errors . push ( new _errors . YAMLSyntaxError ( node , "The " . concat ( handle , " tag handle is non-default and was not declared." ) ) ) ;
143+ }
144+ }
145+ }
146+
147+ switch ( type ) {
148+ case _rawYaml . Type . BLOCK_FOLDED :
149+ case _rawYaml . Type . BLOCK_LITERAL :
150+ case _rawYaml . Type . QUOTE_DOUBLE :
151+ case _rawYaml . Type . QUOTE_SINGLE :
152+ return _Tags . DefaultTags . STR ;
153+
154+ case _rawYaml . Type . FLOW_MAP :
155+ case _rawYaml . Type . MAP :
156+ return _Tags . DefaultTags . MAP ;
157+
158+ case _rawYaml . Type . FLOW_SEQ :
159+ case _rawYaml . Type . SEQ :
160+ return _Tags . DefaultTags . SEQ ;
161+
162+ case _rawYaml . Type . PLAIN :
163+ return nonSpecific ? _Tags . DefaultTags . STR : null ;
164+
165+ default :
166+ return null ;
167+ }
168+ }
169+ } , {
170+ key : "resolveNode" ,
171+ value : function resolveNode ( node ) {
172+ if ( ! node ) return null ;
173+ var anchors = this . anchors ,
174+ errors = this . errors ,
175+ tags = this . tags ;
176+ var anchor = node . anchor ;
177+ if ( anchor ) anchors [ anchor ] = node ;
178+
179+ if ( node . type === _rawYaml . Type . ALIAS ) {
180+ var src = anchors [ node . rawValue ] ;
181+ if ( src ) return node . resolved = src . resolved ;
182+ errors . push ( new _errors . YAMLReferenceError ( node , "Aliased anchor not found: " . concat ( node . rawValue ) ) ) ;
183+ return null ;
184+ }
185+
186+ var tagName = this . resolveTagName ( node ) ;
187+ if ( tagName ) return node . resolved = tags . resolve ( this , node , tagName ) ;
188+ if ( node . type === _rawYaml . Type . PLAIN ) try {
189+ return node . resolved = tags . resolveScalar ( node . strValue || '' ) ;
190+ } catch ( error ) {
191+ if ( error instanceof SyntaxError ) error = new _errors . YAMLSyntaxError ( node , error . message ) ; else error . source = node ;
192+ errors . push ( error ) ;
193+ return null ;
194+ }
195+ errors . push ( new _errors . YAMLSyntaxError ( node , "Failed to resolve " . concat ( node . type , " node here" ) ) ) ;
196+ return null ;
197+ }
198+ } , {
199+ key : "toJSON" ,
200+ value : function toJSON ( ) {
201+ return ( 0 , _Collection . toJSON ) ( this . contents ) ;
202+ }
203+ } , {
204+ key : "toString" ,
205+ value : function toString ( ) {
206+ var _this2 = this ;
207+
208+ if ( Array . isArray ( this . contents ) ) {
209+ return this . contents . map ( function ( c ) {
210+ return _this2 . tags . stringify ( c , {
211+ indent : ''
212+ } ) ;
213+ } ) . join ( '\n---\n' ) + '\n' ;
214+ }
215+
216+ return this . tags . stringify ( this . contents , {
217+ indent : ''
218+ } ) + '\n' ;
219+ }
220+ } ] ) ;
221+
222+ return Document ;
223+ } ( ) ;
224+
225+ exports . default = Document ;
0 commit comments