Skip to content

Commit 57534d6

Browse files
committed
feat(callback): pass additional parameters to canRead
1 parent 6efef32 commit 57534d6

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

lib/parse.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as url from "./util/url.js";
2-
import * as plugins from "./util/plugins.js";
2+
import { filter, all, sort, run } from "./util/plugins.js";
33
import {
44
ResolverError,
55
ParserError,
@@ -77,13 +77,13 @@ async function readFile<S extends object = JSONSchema, O extends ParserOptions<S
7777
// console.log('Reading %s', file.url);
7878

7979
// Find the resolvers that can read this file
80-
let resolvers = plugins.all(options.resolve);
81-
resolvers = plugins.filter(resolvers, "canRead", file);
80+
let resolvers = all(options.resolve);
81+
resolvers = filter(resolvers, "canRead", file, undefined, $refs);
8282

8383
// Run the resolvers, in order, until one of them succeeds
84-
plugins.sort(resolvers);
84+
sort(resolvers);
8585
try {
86-
const data = await plugins.run(resolvers, "read", file, $refs);
86+
const data = await run(resolvers, "read", file, $refs);
8787
return data;
8888
} catch (err: any) {
8989
if (!err && options.continueOnError) {
@@ -123,14 +123,14 @@ async function parseFile<S extends object = JSONSchema, O extends ParserOptions<
123123
// Find the parsers that can read this file type.
124124
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
125125
// This handles situations where the file IS a supported type, just with an unknown extension.
126-
const allParsers = plugins.all(options.parse);
127-
const filteredParsers = plugins.filter(allParsers, "canParse", file);
126+
const allParsers = all(options.parse);
127+
const filteredParsers = filter(allParsers, "canParse", file);
128128
const parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
129129

130130
// Run the parsers, in order, until one of them succeeds
131-
plugins.sort(parsers);
131+
sort(parsers);
132132
try {
133-
const parser = await plugins.run<S, O>(parsers, "parse", file, $refs);
133+
const parser = await run<S, O>(parsers, "parse", file, $refs);
134134
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
135135
throw new SyntaxError(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
136136
} else {

lib/util/plugins.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ export function all<S extends object = JSONSchema, O extends ParserOptions<S> =
2626
/**
2727
* Filters the given plugins, returning only the ones return `true` for the given method.
2828
*/
29-
export function filter(plugins: Plugin[], method: any, file: any) {
29+
export function filter<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
30+
plugins: Plugin[],
31+
method: keyof Plugin | keyof ResolverOptions<S>,
32+
file: FileInfo,
33+
callback?: (err?: Error, result?: any) => void,
34+
$refs?: $Refs<S, O>,
35+
) {
3036
return plugins.filter((plugin: Plugin) => {
31-
return !!getResult(plugin, method, file);
37+
return !!getResult(plugin, method, file, callback, $refs);
3238
});
3339
}
3440

@@ -40,8 +46,8 @@ export function sort(plugins: Plugin[]) {
4046
plugin.order = plugin.order || Number.MAX_SAFE_INTEGER;
4147
}
4248

43-
return plugins.sort((a: any, b: any) => {
44-
return a.order - b.order;
49+
return plugins.sort((a: Plugin, b: Plugin) => {
50+
return a.order! - b.order!;
4551
});
4652
}
4753

0 commit comments

Comments
 (0)