1+ "use strict" ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . default = exports . DefaultTags = exports . DefaultTagPrefixes = void 0 ;
7+
8+ var _Node = require ( "./ast/Node" ) ;
9+
10+ var _errors = require ( "./errors" ) ;
11+
12+ var _schema = _interopRequireDefault ( require ( "./schema" ) ) ;
13+
14+ var _Collection = _interopRequireDefault ( require ( "./schema/Collection" ) ) ;
15+
16+ var _Pair = _interopRequireDefault ( require ( "./schema/Pair" ) ) ;
17+
18+ var _Scalar = _interopRequireDefault ( require ( "./schema/Scalar" ) ) ;
19+
20+ var _string = require ( "./schema/_string" ) ;
21+
22+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
23+
24+ function _typeof ( obj ) { if ( typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ) { _typeof = function _typeof ( obj ) { return typeof obj ; } ; } else { _typeof = function _typeof ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ; } return _typeof ( obj ) ; }
25+
26+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
27+
28+ 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 ) ; } }
29+
30+ function _createClass ( Constructor , protoProps , staticProps ) { if ( protoProps ) _defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) _defineProperties ( Constructor , staticProps ) ; return Constructor ; }
31+
32+ var defaultPrefix = 'tag:yaml.org,2002:' ;
33+ var DefaultTagPrefixes = [ {
34+ handle : '!' ,
35+ prefix : '!'
36+ } , {
37+ handle : '!!' ,
38+ prefix : defaultPrefix
39+ } ] ;
40+ exports . DefaultTagPrefixes = DefaultTagPrefixes ;
41+ var DefaultTags = {
42+ MAP : 'tag:yaml.org,2002:map' ,
43+ SEQ : 'tag:yaml.org,2002:seq' ,
44+ STR : 'tag:yaml.org,2002:str'
45+ } ;
46+ exports . DefaultTags = DefaultTags ;
47+
48+ var isMap = function isMap ( _ref ) {
49+ var type = _ref . type ;
50+ return type === _Node . Type . FLOW_MAP || type === _Node . Type . MAP ;
51+ } ;
52+
53+ var isSeq = function isSeq ( _ref2 ) {
54+ var type = _ref2 . type ;
55+ return type === _Node . Type . FLOW_SEQ || type === _Node . Type . SEQ ;
56+ } ;
57+
58+ var Tags =
59+ /*#__PURE__*/
60+ function ( ) {
61+ _createClass ( Tags , null , [ {
62+ key : "defaultStringifier" ,
63+ value : function defaultStringifier ( value ) {
64+ return JSON . stringify ( value ) ;
65+ }
66+ } ] ) ;
67+
68+ function Tags ( _ref3 ) {
69+ var merge = _ref3 . merge ,
70+ schema = _ref3 . schema ,
71+ tags = _ref3 . tags ;
72+
73+ _classCallCheck ( this , Tags ) ;
74+
75+ this . merge = ! ! merge ;
76+ this . schema = Array . isArray ( schema ) ? schema : _schema . default [ schema ] ;
77+
78+ if ( ! this . schema ) {
79+ var keys = Object . keys ( _schema . default ) . map ( function ( key ) {
80+ return JSON . stringify ( key ) ;
81+ } ) . join ( ', ' ) ;
82+ throw new Error ( "Unknown schema; use " . concat ( keys , ", or { tag, test, resolve }[]" ) ) ;
83+ }
84+
85+ if ( Array . isArray ( tags ) ) {
86+ this . schema = this . schema . concat ( tags ) ;
87+ } else if ( typeof tags === 'function' ) {
88+ this . schema = tags ( this . schema . slice ( ) ) ;
89+ }
90+ } // falls back to string on no match
91+
92+
93+ _createClass ( Tags , [ {
94+ key : "resolveScalar" ,
95+ value : function resolveScalar ( str , tags ) {
96+ if ( ! tags ) tags = this . schema ;
97+
98+ for ( var i = 0 ; i < tags . length ; ++ i ) {
99+ var _tags$i = tags [ i ] ,
100+ test = _tags$i . test ,
101+ resolve = _tags$i . resolve ;
102+
103+ if ( test ) {
104+ var match = str . match ( test ) ;
105+ if ( match ) return new _Scalar . default ( resolve . apply ( null , match ) ) ;
106+ }
107+ }
108+
109+ if ( this . schema . scalarFallback ) str = this . schema . scalarFallback ( str ) ;
110+ return new _Scalar . default ( str ) ;
111+ } // sets node.resolved on success
112+
113+ } , {
114+ key : "resolveNode" ,
115+ value : function resolveNode ( doc , node , tagName ) {
116+ var tags = this . schema . filter ( function ( _ref4 ) {
117+ var tag = _ref4 . tag ;
118+ return tag === tagName ;
119+ } ) ;
120+ var generic = tags . find ( function ( _ref5 ) {
121+ var test = _ref5 . test ;
122+ return ! test ;
123+ } ) ;
124+ if ( node . error ) doc . errors . push ( node . error ) ;
125+
126+ try {
127+ if ( generic ) {
128+ var res = generic . resolve ( doc , node ) ;
129+ if ( ! ( res instanceof _Collection . default ) ) res = new _Scalar . default ( res ) ;
130+ node . resolved = res ;
131+ } else {
132+ var str = ( 0 , _string . resolve ) ( doc , node ) ;
133+
134+ if ( typeof str === 'string' && tags . length > 0 ) {
135+ node . resolved = this . resolveScalar ( str , tags ) ;
136+ }
137+ }
138+ } catch ( error ) {
139+ if ( ! error . source ) error . source = node ;
140+ doc . errors . push ( error ) ;
141+ node . resolved = null ;
142+ }
143+
144+ if ( ! node . resolved ) return null ;
145+ if ( node . hasProps ) node . resolved . anchor = node . anchor ;
146+ if ( tagName ) node . resolved . tag = tagName ;
147+ return node . resolved ;
148+ }
149+ } , {
150+ key : "resolveNodeWithFallback" ,
151+ value : function resolveNodeWithFallback ( doc , node , tagName ) {
152+ var res = this . resolveNode ( doc , node , tagName ) ;
153+ if ( node . hasOwnProperty ( 'resolved' ) ) return res ;
154+ var fallback = isMap ( node ) ? DefaultTags . MAP : isSeq ( node ) ? DefaultTags . SEQ : DefaultTags . STR ;
155+
156+ if ( fallback ) {
157+ doc . warnings . push ( new _errors . YAMLWarning ( node , "The tag " . concat ( tagName , " is unavailable, falling back to " ) . concat ( fallback ) ) ) ;
158+
159+ var _res = this . resolveNode ( doc , node , fallback ) ;
160+
161+ _res . origTag = tagName ;
162+ return _res ;
163+ } else {
164+ doc . errors . push ( new _errors . YAMLReferenceError ( node , "The tag " . concat ( tagName , " is unavailable" ) ) ) ;
165+ }
166+
167+ return null ;
168+ }
169+ } , {
170+ key : "stringify" ,
171+ value : function stringify ( doc , item , options , onComment ) {
172+ if ( ! ( item instanceof _Scalar . default || item instanceof _Collection . default || item instanceof _Pair . default ) ) {
173+ item = doc . resolveValue ( item , true ) ;
174+ }
175+
176+ options . tags = this ;
177+ var match ;
178+
179+ if ( item instanceof _Pair . default ) {
180+ return item . toString ( doc , options , onComment ) ;
181+ } else if ( item . tag ) {
182+ match = this . schema . filter ( function ( _ref6 ) {
183+ var format = _ref6 . format ,
184+ tag = _ref6 . tag ;
185+ return tag === item . tag && ( ! item . format || format === item . format ) ;
186+ } ) ;
187+ if ( match . length === 0 ) throw new Error ( "Tag not available: " . concat ( item . tag ) . concat ( item . format ? ', format ' + item . format : '' ) ) ;
188+ } else if ( item . value === null ) {
189+ match = this . schema . filter ( function ( t ) {
190+ return t . class === null && ! t . format ;
191+ } ) ;
192+ if ( match . length === 0 ) throw new Error ( 'Schema is missing a null stringifier' ) ;
193+ } else {
194+ var obj = item ;
195+
196+ if ( item . hasOwnProperty ( 'value' ) ) {
197+ switch ( _typeof ( item . value ) ) {
198+ case 'boolean' :
199+ obj = new Boolean ( ) ;
200+ break ;
201+
202+ case 'number' :
203+ obj = new Number ( ) ;
204+ break ;
205+
206+ case 'string' :
207+ obj = new String ( ) ;
208+ break ;
209+
210+ default :
211+ obj = item . value ;
212+ }
213+ }
214+
215+ match = this . schema . filter ( function ( t ) {
216+ return t . class && obj instanceof t . class && ! t . format ;
217+ } ) ;
218+ if ( match . length === 0 ) throw new Error ( "Tag not resolved for " . concat ( obj && obj . constructor ? obj . constructor . name : _typeof ( obj ) ) ) ;
219+ }
220+
221+ var stringify = match [ 0 ] . stringify || Tags . defaultStringifier ;
222+ var str = stringify ( item , options , onComment ) ;
223+ var tag = item . origTag || item . tag ;
224+
225+ if ( tag && tag . indexOf ( defaultPrefix ) !== 0 ) {
226+ var p = doc . tagPrefixes . find ( function ( p ) {
227+ return tag . indexOf ( p . prefix ) === 0 ;
228+ } ) ;
229+ var tagProp = p ? p . handle + tag . substr ( p . prefix . length ) : tag [ 0 ] === '!' ? tag : "!<" . concat ( tag , ">" ) ;
230+
231+ if ( item instanceof _Collection . default && ! options . inFlow && item . items . length > 0 ) {
232+ return "" . concat ( tagProp , "\n" ) . concat ( options . indent ) . concat ( str ) ;
233+ } else {
234+ return "" . concat ( tagProp , " " ) . concat ( str ) ;
235+ }
236+ }
237+
238+ return str ;
239+ }
240+ } ] ) ;
241+
242+ return Tags ;
243+ } ( ) ;
244+
245+ exports . default = Tags ;
0 commit comments