@@ -3,7 +3,9 @@ import Pointer from "./pointer.js";
3
3
import { ono } from "@jsdevtools/ono" ;
4
4
import * as url from "./util/url.js" ;
5
5
import type $Refs from "./refs.js" ;
6
- import type $RefParserOptions from "./options.js" ;
6
+ import type { DereferenceOptions , ParserOptions } from "./options.js" ;
7
+ import type { JSONSchema } from "./types" ;
8
+ import type $RefParser from "./index" ;
7
9
8
10
export default dereference ;
9
11
@@ -14,11 +16,14 @@ export default dereference;
14
16
* @param parser
15
17
* @param options
16
18
*/
17
- function dereference ( parser : any , options : any ) {
19
+ function dereference < S extends JSONSchema = JSONSchema , O extends ParserOptions = ParserOptions > (
20
+ parser : $RefParser < S , O > ,
21
+ options : O ,
22
+ ) {
18
23
// console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
19
- const dereferenced = crawl (
24
+ const dereferenced = crawl < S , O > (
20
25
parser . schema ,
21
- parser . $refs . _root$Ref . path ,
26
+ parser . $refs . _root$Ref . path ! ,
22
27
"#" ,
23
28
new Set ( ) ,
24
29
new Set ( ) ,
@@ -43,25 +48,26 @@ function dereference(parser: any, options: any) {
43
48
* @param options
44
49
* @returns
45
50
*/
46
- function crawl (
51
+ function crawl < S extends JSONSchema = JSONSchema , O extends ParserOptions = ParserOptions > (
47
52
obj : any ,
48
53
path : string ,
49
54
pathFromRoot : string ,
50
55
parents : Set < any > ,
51
56
processedObjects : Set < any > ,
52
57
dereferencedCache : any ,
53
- $refs : $Refs ,
54
- options : $RefParserOptions ,
58
+ $refs : $Refs < S > ,
59
+ options : O ,
55
60
) {
56
61
let dereferenced ;
57
62
const result = {
58
63
value : obj ,
59
64
circular : false ,
60
65
} ;
61
66
62
- const isExcludedPath = options . dereference . excludedPathMatcher || ( ( ) => false ) ;
67
+ const derefOptions = ( options . dereference || { } ) as DereferenceOptions ;
68
+ const isExcludedPath = derefOptions . excludedPathMatcher || ( ( ) => false ) ;
63
69
64
- if ( options . dereference . circular === "ignore" || ! processedObjects . has ( obj ) ) {
70
+ if ( derefOptions ? .circular === "ignore" || ! processedObjects . has ( obj ) ) {
65
71
if ( obj && typeof obj === "object" && ! ArrayBuffer . isView ( obj ) && ! isExcludedPath ( pathFromRoot ) ) {
66
72
parents . add ( obj ) ;
67
73
processedObjects . add ( obj ) ;
@@ -106,9 +112,7 @@ function crawl(
106
112
// Avoid pointless mutations; breaks frozen objects to no profit
107
113
if ( obj [ key ] !== dereferenced . value ) {
108
114
obj [ key ] = dereferenced . value ;
109
- if ( options . dereference . onDereference ) {
110
- options . dereference . onDereference ( value . $ref , obj [ key ] , obj , key ) ;
111
- }
115
+ derefOptions ?. onDereference ?.( value . $ref , obj [ key ] , obj , key ) ;
112
116
}
113
117
} else {
114
118
if ( ! parents . has ( value ) ) {
@@ -157,18 +161,18 @@ function crawl(
157
161
* @param options
158
162
* @returns
159
163
*/
160
- function dereference$Ref (
164
+ function dereference$Ref < S extends JSONSchema = JSONSchema , O extends ParserOptions = ParserOptions > (
161
165
$ref : any ,
162
166
path : string ,
163
167
pathFromRoot : string ,
164
168
parents : Set < any > ,
165
169
processedObjects : any ,
166
170
dereferencedCache : any ,
167
- $refs : $Refs ,
168
- options : $RefParserOptions ,
171
+ $refs : $Refs < S > ,
172
+ options : O ,
169
173
) {
170
174
const isExternalRef = $Ref . isExternal$Ref ( $ref ) ;
171
- const shouldResolveOnCwd = isExternalRef && options ?. dereference . externalReferenceResolution === "root" ;
175
+ const shouldResolveOnCwd = isExternalRef && options ?. dereference ? .externalReferenceResolution === "root" ;
172
176
const $refPath = url . resolve ( shouldResolveOnCwd ? url . cwd ( ) : path , $ref . $ref ) ;
173
177
174
178
const cache = dereferencedCache . get ( $refPath ) ;
@@ -225,7 +229,7 @@ function dereference$Ref(
225
229
dereferencedValue = dereferenced . value ;
226
230
}
227
231
228
- if ( circular && ! directCircular && options . dereference . circular === "ignore" ) {
232
+ if ( circular && ! directCircular && options . dereference ? .circular === "ignore" ) {
229
233
// The user has chosen to "ignore" circular references, so don't change the value
230
234
dereferencedValue = $ref ;
231
235
}
0 commit comments