1
+ "use strict" ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . default = void 0 ;
7
+
8
+ var _Alias = _interopRequireDefault ( require ( "./schema/Alias" ) ) ;
9
+
10
+ var _Map = _interopRequireDefault ( require ( "./schema/Map" ) ) ;
11
+
12
+ var _Merge = _interopRequireDefault ( require ( "./schema/Merge" ) ) ;
13
+
14
+ var _Scalar = _interopRequireDefault ( require ( "./schema/Scalar" ) ) ;
15
+
16
+ var _Seq = _interopRequireDefault ( require ( "./schema/Seq" ) ) ;
17
+
18
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
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
+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
27
+
28
+ var Anchors =
29
+ /*#__PURE__*/
30
+ function ( ) {
31
+ function Anchors ( ) {
32
+ _classCallCheck ( this , Anchors ) ;
33
+
34
+ _defineProperty ( this , "map" , { } ) ;
35
+ }
36
+
37
+ _createClass ( Anchors , [ {
38
+ key : "createAlias" ,
39
+ value : function createAlias ( node , name ) {
40
+ this . setAnchor ( node , name ) ;
41
+ return new _Alias . default ( node ) ;
42
+ }
43
+ } , {
44
+ key : "createMergePair" ,
45
+ value : function createMergePair ( ) {
46
+ var _this = this ;
47
+
48
+ var merge = new _Merge . default ( ) ;
49
+
50
+ for ( var _len = arguments . length , sources = new Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
51
+ sources [ _key ] = arguments [ _key ] ;
52
+ }
53
+
54
+ merge . value . items = sources . map ( function ( s ) {
55
+ if ( s instanceof _Alias . default ) {
56
+ if ( s . source instanceof _Map . default ) return s ;
57
+ } else if ( s instanceof _Map . default ) {
58
+ return _this . createAlias ( s ) ;
59
+ }
60
+
61
+ throw new Error ( 'Merge sources must be Map nodes or their Aliases' ) ;
62
+ } ) ;
63
+ return merge ;
64
+ }
65
+ } , {
66
+ key : "getName" ,
67
+ value : function getName ( node ) {
68
+ var map = this . map ;
69
+ return Object . keys ( map ) . find ( function ( a ) {
70
+ return map [ a ] === node ;
71
+ } ) ;
72
+ }
73
+ } , {
74
+ key : "getNode" ,
75
+ value : function getNode ( name ) {
76
+ return this . map [ name ] ;
77
+ }
78
+ } , {
79
+ key : "newName" ,
80
+ value : function newName ( prefix ) {
81
+ var names = Object . keys ( this . map ) ;
82
+
83
+ for ( var i = 1 ; true ; ++ i ) {
84
+ var name = "" . concat ( prefix ) . concat ( i ) ;
85
+ if ( ! names . includes ( name ) ) return name ;
86
+ }
87
+ } // During parsing, map & aliases contain CST nodes
88
+
89
+ } , {
90
+ key : "resolveNodes" ,
91
+ value : function resolveNodes ( ) {
92
+ var map = this . map ,
93
+ _cstAliases = this . _cstAliases ;
94
+ Object . keys ( map ) . forEach ( function ( a ) {
95
+ map [ a ] = map [ a ] . resolved ;
96
+ } ) ;
97
+
98
+ _cstAliases . forEach ( function ( a ) {
99
+ a . source = a . source . resolved ;
100
+ } ) ;
101
+
102
+ delete this . _cstAliases ;
103
+ }
104
+ } , {
105
+ key : "setAnchor" ,
106
+ value : function setAnchor ( node , name ) {
107
+ if ( node != null && ! Anchors . validAnchorNode ( node ) ) {
108
+ throw new Error ( 'Anchors may only be set for Scalar, Seq and Map nodes' ) ;
109
+ }
110
+
111
+ if ( name && / [ \x00 - \x19 \s , [ \] { } ] / . test ( name ) ) {
112
+ throw new Error ( 'Anchor names must not contain whitespace or control characters' ) ;
113
+ }
114
+
115
+ var map = this . map ;
116
+ var prev = node && Object . keys ( map ) . find ( function ( a ) {
117
+ return map [ a ] === node ;
118
+ } ) ;
119
+
120
+ if ( prev ) {
121
+ if ( ! name ) {
122
+ return prev ;
123
+ } else if ( prev !== name ) {
124
+ delete map [ prev ] ;
125
+ map [ name ] = node ;
126
+ }
127
+ } else {
128
+ if ( ! name ) {
129
+ if ( ! node ) return null ;
130
+ name = this . newName ( 'a' ) ;
131
+ }
132
+
133
+ map [ name ] = node ;
134
+ }
135
+
136
+ return name ;
137
+ }
138
+ } ] , [ {
139
+ key : "validAnchorNode" ,
140
+ value : function validAnchorNode ( node ) {
141
+ return node instanceof _Scalar . default || node instanceof _Seq . default || node instanceof _Map . default ;
142
+ }
143
+ } ] ) ;
144
+
145
+ return Anchors ;
146
+ } ( ) ;
147
+
148
+ exports . default = Anchors ;
0 commit comments