Skip to content

Commit 1f1577e

Browse files
removed the "debug" dependency
1 parent e4eb212 commit 1f1577e

10 files changed

Lines changed: 208 additions & 239 deletions

File tree

lib/bundle.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
var $Ref = require('./ref'),
44
Pointer = require('./pointer'),
5-
debug = require('./util/debug'),
65
url = require('./util/url');
76

87
module.exports = bundle;
@@ -16,7 +15,7 @@ module.exports = bundle;
1615
* @param {$RefParserOptions} options
1716
*/
1817
function bundle (parser, options) {
19-
debug('Bundling $ref pointers in %s', parser.$refs._root$Ref.path);
18+
// console.log('Bundling $ref pointers in %s', parser.$refs._root$Ref.path);
2019

2120
// Build an inventory of all $ref pointers in the JSON Schema
2221
var inventory = [];
@@ -203,7 +202,7 @@ function remap (inventory) {
203202

204203
var file, hash, pathFromRoot;
205204
inventory.forEach(function (entry) {
206-
debug('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
205+
// console.log('Re-mapping $ref pointer "%s" at %s', entry.$ref.$ref, entry.pathFromRoot);
207206

208207
if (!entry.external) {
209208
// This $ref already resolves to the main JSON Schema file
@@ -233,7 +232,7 @@ function remap (inventory) {
233232
}
234233
}
235234

236-
debug(' new value: %s', (entry.$ref && entry.$ref.$ref) ? entry.$ref.$ref : '[object Object]');
235+
// console.log(' new value: %s', (entry.$ref && entry.$ref.$ref) ? entry.$ref.$ref : '[object Object]');
237236
});
238237
}
239238

lib/dereference.js

Lines changed: 149 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,149 @@
1-
'use strict';
2-
3-
var $Ref = require('./ref'),
4-
Pointer = require('./pointer'),
5-
ono = require('ono'),
6-
debug = require('./util/debug'),
7-
url = require('./util/url');
8-
9-
module.exports = dereference;
10-
11-
/**
12-
* Crawls the JSON schema, finds all JSON references, and dereferences them.
13-
* This method mutates the JSON schema object, replacing JSON references with their resolved value.
14-
*
15-
* @param {$RefParser} parser
16-
* @param {$RefParserOptions} options
17-
*/
18-
function dereference (parser, options) {
19-
debug('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
20-
var dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, '#', [], parser.$refs, options);
21-
parser.$refs.circular = dereferenced.circular;
22-
parser.schema = dereferenced.value;
23-
}
24-
25-
/**
26-
* Recursively crawls the given value, and dereferences any JSON references.
27-
*
28-
* @param {*} obj - The value to crawl. If it's not an object or array, it will be ignored.
29-
* @param {string} path - The full path of `obj`, possibly with a JSON Pointer in the hash
30-
* @param {string} pathFromRoot - The path of `obj` from the schema root
31-
* @param {object[]} parents - An array of the parent objects that have already been dereferenced
32-
* @param {$Refs} $refs
33-
* @param {$RefParserOptions} options
34-
* @returns {{value: object, circular: boolean}}
35-
*/
36-
function crawl (obj, path, pathFromRoot, parents, $refs, options) {
37-
var dereferenced;
38-
var result = {
39-
value: obj,
40-
circular: false
41-
};
42-
43-
if (obj && typeof obj === 'object') {
44-
parents.push(obj);
45-
46-
if ($Ref.isAllowed$Ref(obj, options)) {
47-
dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, $refs, options);
48-
result.circular = dereferenced.circular;
49-
result.value = dereferenced.value;
50-
}
51-
else {
52-
Object.keys(obj).forEach(function (key) {
53-
var keyPath = Pointer.join(path, key);
54-
var keyPathFromRoot = Pointer.join(pathFromRoot, key);
55-
var value = obj[key];
56-
var circular = false;
57-
58-
if ($Ref.isAllowed$Ref(value, options)) {
59-
dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, $refs, options);
60-
circular = dereferenced.circular;
61-
obj[key] = dereferenced.value;
62-
}
63-
else {
64-
if (parents.indexOf(value) === -1) {
65-
dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, $refs, options);
66-
circular = dereferenced.circular;
67-
obj[key] = dereferenced.value;
68-
}
69-
else {
70-
circular = foundCircularReference(keyPath, $refs, options);
71-
}
72-
}
73-
74-
// Set the "isCircular" flag if this or any other property is circular
75-
result.circular = result.circular || circular;
76-
});
77-
}
78-
79-
parents.pop();
80-
}
81-
82-
return result;
83-
}
84-
85-
/**
86-
* Dereferences the given JSON Reference, and then crawls the resulting value.
87-
*
88-
* @param {{$ref: string}} $ref - The JSON Reference to resolve
89-
* @param {string} path - The full path of `$ref`, possibly with a JSON Pointer in the hash
90-
* @param {string} pathFromRoot - The path of `$ref` from the schema root
91-
* @param {object[]} parents - An array of the parent objects that have already been dereferenced
92-
* @param {$Refs} $refs
93-
* @param {$RefParserOptions} options
94-
* @returns {{value: object, circular: boolean}}
95-
*/
96-
function dereference$Ref ($ref, path, pathFromRoot, parents, $refs, options) {
97-
debug('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
98-
99-
var $refPath = url.resolve(path, $ref.$ref);
100-
var pointer = $refs._resolve($refPath, options);
101-
102-
// Check for circular references
103-
var directCircular = pointer.circular;
104-
var circular = directCircular || parents.indexOf(pointer.value) !== -1;
105-
circular && foundCircularReference(path, $refs, options);
106-
107-
// Dereference the JSON reference
108-
var dereferencedValue = $Ref.dereference($ref, pointer.value);
109-
110-
// Crawl the dereferenced value (unless it's circular)
111-
if (!circular) {
112-
// Determine if the dereferenced value is circular
113-
var dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, $refs, options);
114-
circular = dereferenced.circular;
115-
dereferencedValue = dereferenced.value;
116-
}
117-
118-
if (circular && !directCircular && options.dereference.circular === 'ignore') {
119-
// The user has chosen to "ignore" circular references, so don't change the value
120-
dereferencedValue = $ref;
121-
}
122-
123-
if (directCircular) {
124-
// The pointer is a DIRECT circular reference (i.e. it references itself).
125-
// So replace the $ref path with the absolute path from the JSON Schema root
126-
dereferencedValue.$ref = pathFromRoot;
127-
}
128-
129-
return {
130-
circular: circular,
131-
value: dereferencedValue
132-
};
133-
}
134-
135-
/**
136-
* Called when a circular reference is found.
137-
* It sets the {@link $Refs#circular} flag, and throws an error if options.dereference.circular is false.
138-
*
139-
* @param {string} keyPath - The JSON Reference path of the circular reference
140-
* @param {$Refs} $refs
141-
* @param {$RefParserOptions} options
142-
* @returns {boolean} - always returns true, to indicate that a circular reference was found
143-
*/
144-
function foundCircularReference (keyPath, $refs, options) {
145-
$refs.circular = true;
146-
if (!options.dereference.circular) {
147-
throw ono.reference('Circular $ref pointer found at %s', keyPath);
148-
}
149-
return true;
150-
}
1+
'use strict';
2+
3+
var $Ref = require('./ref'),
4+
Pointer = require('./pointer'),
5+
ono = require('ono'),
6+
url = require('./util/url');
7+
8+
module.exports = dereference;
9+
10+
/**
11+
* Crawls the JSON schema, finds all JSON references, and dereferences them.
12+
* This method mutates the JSON schema object, replacing JSON references with their resolved value.
13+
*
14+
* @param {$RefParser} parser
15+
* @param {$RefParserOptions} options
16+
*/
17+
function dereference (parser, options) {
18+
// console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
19+
var dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, '#', [], parser.$refs, options);
20+
parser.$refs.circular = dereferenced.circular;
21+
parser.schema = dereferenced.value;
22+
}
23+
24+
/**
25+
* Recursively crawls the given value, and dereferences any JSON references.
26+
*
27+
* @param {*} obj - The value to crawl. If it's not an object or array, it will be ignored.
28+
* @param {string} path - The full path of `obj`, possibly with a JSON Pointer in the hash
29+
* @param {string} pathFromRoot - The path of `obj` from the schema root
30+
* @param {object[]} parents - An array of the parent objects that have already been dereferenced
31+
* @param {$Refs} $refs
32+
* @param {$RefParserOptions} options
33+
* @returns {{value: object, circular: boolean}}
34+
*/
35+
function crawl (obj, path, pathFromRoot, parents, $refs, options) {
36+
var dereferenced;
37+
var result = {
38+
value: obj,
39+
circular: false
40+
};
41+
42+
if (obj && typeof obj === 'object') {
43+
parents.push(obj);
44+
45+
if ($Ref.isAllowed$Ref(obj, options)) {
46+
dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, $refs, options);
47+
result.circular = dereferenced.circular;
48+
result.value = dereferenced.value;
49+
}
50+
else {
51+
Object.keys(obj).forEach(function (key) {
52+
var keyPath = Pointer.join(path, key);
53+
var keyPathFromRoot = Pointer.join(pathFromRoot, key);
54+
var value = obj[key];
55+
var circular = false;
56+
57+
if ($Ref.isAllowed$Ref(value, options)) {
58+
dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, $refs, options);
59+
circular = dereferenced.circular;
60+
obj[key] = dereferenced.value;
61+
}
62+
else {
63+
if (parents.indexOf(value) === -1) {
64+
dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, $refs, options);
65+
circular = dereferenced.circular;
66+
obj[key] = dereferenced.value;
67+
}
68+
else {
69+
circular = foundCircularReference(keyPath, $refs, options);
70+
}
71+
}
72+
73+
// Set the "isCircular" flag if this or any other property is circular
74+
result.circular = result.circular || circular;
75+
});
76+
}
77+
78+
parents.pop();
79+
}
80+
81+
return result;
82+
}
83+
84+
/**
85+
* Dereferences the given JSON Reference, and then crawls the resulting value.
86+
*
87+
* @param {{$ref: string}} $ref - The JSON Reference to resolve
88+
* @param {string} path - The full path of `$ref`, possibly with a JSON Pointer in the hash
89+
* @param {string} pathFromRoot - The path of `$ref` from the schema root
90+
* @param {object[]} parents - An array of the parent objects that have already been dereferenced
91+
* @param {$Refs} $refs
92+
* @param {$RefParserOptions} options
93+
* @returns {{value: object, circular: boolean}}
94+
*/
95+
function dereference$Ref ($ref, path, pathFromRoot, parents, $refs, options) {
96+
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
97+
98+
var $refPath = url.resolve(path, $ref.$ref);
99+
var pointer = $refs._resolve($refPath, options);
100+
101+
// Check for circular references
102+
var directCircular = pointer.circular;
103+
var circular = directCircular || parents.indexOf(pointer.value) !== -1;
104+
circular && foundCircularReference(path, $refs, options);
105+
106+
// Dereference the JSON reference
107+
var dereferencedValue = $Ref.dereference($ref, pointer.value);
108+
109+
// Crawl the dereferenced value (unless it's circular)
110+
if (!circular) {
111+
// Determine if the dereferenced value is circular
112+
var dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, $refs, options);
113+
circular = dereferenced.circular;
114+
dereferencedValue = dereferenced.value;
115+
}
116+
117+
if (circular && !directCircular && options.dereference.circular === 'ignore') {
118+
// The user has chosen to "ignore" circular references, so don't change the value
119+
dereferencedValue = $ref;
120+
}
121+
122+
if (directCircular) {
123+
// The pointer is a DIRECT circular reference (i.e. it references itself).
124+
// So replace the $ref path with the absolute path from the JSON Schema root
125+
dereferencedValue.$ref = pathFromRoot;
126+
}
127+
128+
return {
129+
circular: circular,
130+
value: dereferencedValue
131+
};
132+
}
133+
134+
/**
135+
* Called when a circular reference is found.
136+
* It sets the {@link $Refs#circular} flag, and throws an error if options.dereference.circular is false.
137+
*
138+
* @param {string} keyPath - The JSON Reference path of the circular reference
139+
* @param {$Refs} $refs
140+
* @param {$RefParserOptions} options
141+
* @returns {boolean} - always returns true, to indicate that a circular reference was found
142+
*/
143+
function foundCircularReference (keyPath, $refs, options) {
144+
$refs.circular = true;
145+
if (!options.dereference.circular) {
146+
throw ono.reference('Circular $ref pointer found at %s', keyPath);
147+
}
148+
return true;
149+
}

lib/parse.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
var ono = require('ono'),
4-
debug = require('./util/debug'),
54
url = require('./util/url'),
65
plugins = require('./util/plugins');
76

@@ -62,7 +61,7 @@ function parse (path, $refs, options) {
6261
*/
6362
function readFile (file, options) {
6463
return new Promise(function (resolve, reject) {
65-
debug('Reading %s', file.url);
64+
// console.log('Reading %s', file.url);
6665

6766
// Find the resolvers that can read this file
6867
var resolvers = plugins.all(options.resolve);
@@ -100,7 +99,7 @@ function readFile (file, options) {
10099
*/
101100
function parseFile (file, options) {
102101
return new Promise(function (resolve, reject) {
103-
debug('Parsing %s', file.url);
102+
// console.log('Parsing %s', file.url);
104103

105104
// Find the parsers that can read this file type.
106105
// If none of the parsers are an exact match for this file, then we'll try ALL of them.

lib/resolve-external.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var $Ref = require('./ref'),
44
Pointer = require('./pointer'),
55
parse = require('./parse'),
6-
debug = require('./util/debug'),
76
url = require('./util/url');
87

98
module.exports = resolveExternal;
@@ -28,7 +27,7 @@ function resolveExternal (parser, options) {
2827
}
2928

3029
try {
31-
debug('Resolving $ref pointers in %s', parser.$refs._root$Ref.path);
30+
// console.log('Resolving $ref pointers in %s', parser.$refs._root$Ref.path);
3231
var promises = crawl(parser.schema, parser.$refs._root$Ref.path + '#', parser.$refs, options);
3332
return Promise.all(promises);
3433
}
@@ -89,7 +88,7 @@ function crawl (obj, path, $refs, options) {
8988
* including nested references that are contained in externally-referenced files.
9089
*/
9190
function resolve$Ref ($ref, path, $refs, options) {
92-
debug('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
91+
// console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path);
9392

9493
var resolvedPath = url.resolve(path, $ref.$ref);
9594
var withoutHash = url.stripHash(resolvedPath);
@@ -105,7 +104,7 @@ function resolve$Ref ($ref, path, $refs, options) {
105104
return parse(resolvedPath, $refs, options)
106105
.then(function (result) {
107106
// Crawl the parsed value
108-
debug('Resolving $ref pointers in %s', withoutHash);
107+
// console.log('Resolving $ref pointers in %s', withoutHash);
109108
var promises = crawl(result, withoutHash + '#', $refs, options);
110109
return Promise.all(promises);
111110
});

0 commit comments

Comments
 (0)