@@ -3,7 +3,9 @@ import Pointer from "./pointer.js";
33import { ono } from "@jsdevtools/ono" ;
44import * as url from "./util/url.js" ;
55import 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" ;
79
810export default dereference ;
911
@@ -14,11 +16,14 @@ export default dereference;
1416 * @param parser
1517 * @param options
1618 */
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+ ) {
1823 // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
19- const dereferenced = crawl (
24+ const dereferenced = crawl < S , O > (
2025 parser . schema ,
21- parser . $refs . _root$Ref . path ,
26+ parser . $refs . _root$Ref . path ! ,
2227 "#" ,
2328 new Set ( ) ,
2429 new Set ( ) ,
@@ -43,25 +48,26 @@ function dereference(parser: any, options: any) {
4348 * @param options
4449 * @returns
4550 */
46- function crawl (
51+ function crawl < S extends JSONSchema = JSONSchema , O extends ParserOptions = ParserOptions > (
4752 obj : any ,
4853 path : string ,
4954 pathFromRoot : string ,
5055 parents : Set < any > ,
5156 processedObjects : Set < any > ,
5257 dereferencedCache : any ,
53- $refs : $Refs ,
54- options : $RefParserOptions ,
58+ $refs : $Refs < S > ,
59+ options : O ,
5560) {
5661 let dereferenced ;
5762 const result = {
5863 value : obj ,
5964 circular : false ,
6065 } ;
6166
62- const isExcludedPath = options . dereference . excludedPathMatcher || ( ( ) => false ) ;
67+ const derefOptions = ( options . dereference || { } ) as DereferenceOptions ;
68+ const isExcludedPath = derefOptions . excludedPathMatcher || ( ( ) => false ) ;
6369
64- if ( options . dereference . circular === "ignore" || ! processedObjects . has ( obj ) ) {
70+ if ( derefOptions ? .circular === "ignore" || ! processedObjects . has ( obj ) ) {
6571 if ( obj && typeof obj === "object" && ! ArrayBuffer . isView ( obj ) && ! isExcludedPath ( pathFromRoot ) ) {
6672 parents . add ( obj ) ;
6773 processedObjects . add ( obj ) ;
@@ -106,9 +112,7 @@ function crawl(
106112 // Avoid pointless mutations; breaks frozen objects to no profit
107113 if ( obj [ key ] !== dereferenced . value ) {
108114 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 ) ;
112116 }
113117 } else {
114118 if ( ! parents . has ( value ) ) {
@@ -157,18 +161,18 @@ function crawl(
157161 * @param options
158162 * @returns
159163 */
160- function dereference$Ref (
164+ function dereference$Ref < S extends JSONSchema = JSONSchema , O extends ParserOptions = ParserOptions > (
161165 $ref : any ,
162166 path : string ,
163167 pathFromRoot : string ,
164168 parents : Set < any > ,
165169 processedObjects : any ,
166170 dereferencedCache : any ,
167- $refs : $Refs ,
168- options : $RefParserOptions ,
171+ $refs : $Refs < S > ,
172+ options : O ,
169173) {
170174 const isExternalRef = $Ref . isExternal$Ref ( $ref ) ;
171- const shouldResolveOnCwd = isExternalRef && options ?. dereference . externalReferenceResolution === "root" ;
175+ const shouldResolveOnCwd = isExternalRef && options ?. dereference ? .externalReferenceResolution === "root" ;
172176 const $refPath = url . resolve ( shouldResolveOnCwd ? url . cwd ( ) : path , $ref . $ref ) ;
173177
174178 const cache = dereferencedCache . get ( $refPath ) ;
@@ -225,7 +229,7 @@ function dereference$Ref(
225229 dereferencedValue = dereferenced . value ;
226230 }
227231
228- if ( circular && ! directCircular && options . dereference . circular === "ignore" ) {
232+ if ( circular && ! directCircular && options . dereference ? .circular === "ignore" ) {
229233 // The user has chosen to "ignore" circular references, so don't change the value
230234 dereferencedValue = $ref ;
231235 }
0 commit comments