@@ -67,89 +67,99 @@ const bundle = (document: unknown, bundleRoot: JsonPath, errorsRoot: JsonPath, k
67
67
const $refTarget = pointerToPath ( path ) ;
68
68
const objectToBundle = get ( document , $refTarget ) ;
69
69
70
- traverse ( cur ? cur : objectToBundle , ( { parent } ) => {
71
- if ( hasRef ( parent ) && isLocalRef ( parent . $ref ) ) {
72
- const $ref = parent . $ref ;
73
- if ( errorsObj [ $ref ] ) return ;
70
+ traverse ( cur ? cur : objectToBundle , {
71
+ onEnter : ( { value : parent } ) => {
72
+ if ( hasRef ( parent ) && isLocalRef ( parent . $ref ) ) {
73
+ const $ref = parent . $ref ;
74
+ if ( errorsObj [ $ref ] ) return ;
75
+
76
+ if ( $ref === path ) {
77
+ bundledRefInventory [ $ref ] = '#' ;
78
+ }
74
79
75
- if ( bundledRefInventory [ $ref ] ) {
76
- parent . $ref = bundledRefInventory [ $ref ] ;
80
+ if ( bundledRefInventory [ $ref ] ) {
81
+ parent . $ref = bundledRefInventory [ $ref ] ;
77
82
78
- // no need to continue, this $ref has already been bundled
79
- return ;
80
- }
83
+ // no need to continue, this $ref has already been bundled
84
+ return ;
85
+ }
81
86
82
- let _path ;
83
- let inventoryPath ;
84
- let inventoryKey ;
85
- let inventoryRef ;
87
+ let _path ;
88
+ let inventoryPath ;
89
+ let inventoryKey ;
90
+ let inventoryRef ;
86
91
87
- try {
88
- _path = pointerToPath ( $ref ) ;
92
+ try {
93
+ _path = pointerToPath ( $ref ) ;
89
94
90
- let _inventoryKey ;
91
- if ( keyProvider ) {
92
- _inventoryKey = keyProvider ( { document, path : _path } ) ;
93
- }
95
+ let _inventoryKey ;
96
+ if ( keyProvider ) {
97
+ _inventoryKey = keyProvider ( { document, path : _path } ) ;
98
+ }
94
99
95
- if ( ! _inventoryKey ) {
96
- _inventoryKey = defaultKeyProvider ( { document, path : _path } ) ;
97
- }
100
+ if ( ! _inventoryKey ) {
101
+ _inventoryKey = defaultKeyProvider ( { document, path : _path } ) ;
102
+ }
98
103
99
- inventoryKey = _inventoryKey ;
104
+ inventoryKey = _inventoryKey ;
100
105
101
- let i = 1 ;
102
- while ( takenKeys . has ( inventoryKey ) ) {
103
- i ++ ;
104
- inventoryKey = `${ _inventoryKey } _${ i } ` ;
106
+ let i = 1 ;
107
+ while ( takenKeys . has ( inventoryKey ) ) {
108
+ i ++ ;
109
+ inventoryKey = `${ _inventoryKey } _${ i } ` ;
105
110
106
- if ( i > 20 ) {
107
- throw new Error ( `Keys ${ _inventoryKey } _2 through ${ _inventoryKey } _${ 20 } already taken.` ) ;
111
+ if ( i > 20 ) {
112
+ throw new Error ( `Keys ${ _inventoryKey } _2 through ${ _inventoryKey } _${ 20 } already taken.` ) ;
113
+ }
108
114
}
109
- }
110
115
111
- takenKeys . add ( inventoryKey ) ;
116
+ takenKeys . add ( inventoryKey ) ;
112
117
113
- inventoryPath = [ ...bundleRoot , inventoryKey ] ;
118
+ inventoryPath = [ ...bundleRoot , inventoryKey ] ;
114
119
115
- inventoryRef = pathToPointer ( inventoryPath ) ;
116
- } catch ( error ) {
117
- errorsObj [ $ref ] = error instanceof Error ? error . message : String ( error ) ;
118
- }
120
+ inventoryRef = pathToPointer ( inventoryPath ) ;
121
+ } catch ( error ) {
122
+ errorsObj [ $ref ] = error instanceof Error ? error . message : String ( error ) ;
123
+ }
119
124
120
- // Ignore invalid $refs and carry on
121
- if ( ! _path || ! inventoryPath || ! inventoryRef ) return ;
125
+ // Ignore invalid $refs and carry on
126
+ if ( ! _path || ! inventoryPath || ! inventoryRef ) return ;
122
127
123
- let bundled$Ref : unknown ;
124
- if ( typeof document === 'object' && document !== null ) {
125
- try {
126
- bundled$Ref = resolveInlineRef ( Object ( document ) , $ref ) ;
127
- } catch {
128
+ let bundled$Ref : unknown ;
129
+ if ( typeof document === 'object' && document !== null ) {
130
+ // check the simple way first, to preserve these relationships when possible
128
131
bundled$Ref = get ( document , _path ) ;
132
+
133
+ if ( ! bundled$Ref ) {
134
+ try {
135
+ // if we could not find it with a simple lookup, check for deep refs etc via resolveInlineRef
136
+ bundled$Ref = resolveInlineRef ( Object ( document ) , $ref ) ;
137
+ } catch { }
138
+ }
129
139
}
130
- }
131
140
132
- if ( bundled$Ref !== void 0 ) {
133
- bundledRefInventory [ $ref ] = inventoryRef ;
134
- parent . $ref = inventoryRef ;
141
+ if ( bundled$Ref !== void 0 ) {
142
+ bundledRefInventory [ $ref ] = inventoryRef ;
143
+ parent . $ref = inventoryRef ;
135
144
136
- if ( ! has ( bundledObj , inventoryPath ) ) {
137
- if ( Array . isArray ( bundled$Ref ) ) {
138
- set ( bundledObj , inventoryPath , new Array ( bundled$Ref . length ) . fill ( null ) ) ;
139
- } else if ( typeof bundled$Ref === 'object' ) {
140
- setWith ( bundledObj , inventoryPath , { } , Object ) ;
141
- }
145
+ if ( ! has ( bundledObj , inventoryPath ) ) {
146
+ if ( Array . isArray ( bundled$Ref ) ) {
147
+ set ( bundledObj , inventoryPath , new Array ( bundled$Ref . length ) . fill ( null ) ) ;
148
+ } else if ( typeof bundled$Ref === 'object' ) {
149
+ setWith ( bundledObj , inventoryPath , { } , Object ) ;
150
+ }
142
151
143
- set ( bundledObj , inventoryPath , bundled$Ref ) ;
152
+ set ( bundledObj , inventoryPath , bundled$Ref ) ;
144
153
145
- if ( ! stack [ $ref ] ) {
146
- stack [ $ref ] = true ;
147
- _bundle ( path , stack , bundled$Ref , bundledRefInventory , bundledObj , errorsObj ) ;
148
- stack [ $ref ] = false ;
154
+ if ( ! stack [ $ref ] ) {
155
+ stack [ $ref ] = true ;
156
+ _bundle ( path , stack , bundled$Ref , bundledRefInventory , bundledObj , errorsObj ) ;
157
+ stack [ $ref ] = false ;
158
+ }
149
159
}
150
160
}
151
161
}
152
- }
162
+ } ,
153
163
} ) ;
154
164
155
165
const finalObjectToBundle = get ( bundledObj , bundleRoot ) ;
0 commit comments