Skip to content

Commit ed96998

Browse files
committed
Add reviver option to jsonast-parse and jref-parse
1 parent 587ab00 commit ed96998

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

src/jref/jref-parse.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,38 @@ import { fromJref } from "./jref-util.js";
66
* @import { Plugin } from "unified"
77
* @import { VFile } from "vfile"
88
* @import { Options } from "vfile-message"
9-
* @import { JrefDocumentNode } from "./jref-ast.js"
9+
* @import { JrefDocumentNode, JrefNode } from "./jref-ast.js"
10+
* @import { Reviver } from "./jref-util.js"
1011
*/
1112

1213

13-
/** @type Plugin<[], string, JrefDocumentNode> */
14-
export function jrefParse() {
14+
/**
15+
* @typedef {{
16+
* reviver?: Reviver<JrefNode | undefined>;
17+
* }} JrefParseOptions
18+
*/
19+
20+
/** @type Plugin<[JrefParseOptions?], string, JrefDocumentNode> */
21+
export function jrefParse(options) {
1522
/** @type (document: string, file: VFile) => JrefDocumentNode */
1623
this.parser = function (document, file) {
1724
try {
1825
const uri = pathToFileURL(file.path).toString();
19-
return {
26+
27+
/** @type JrefDocumentNode */
28+
const jrefDocumentNode = {
2029
type: "jref-document",
21-
children: [fromJref(document, uri)],
30+
children: [],
2231
uri: uri,
2332
fragmentKind: "json-pointer"
2433
};
34+
35+
const jrefNode = fromJref(document, uri, options?.reviver);
36+
if (jrefNode) {
37+
jrefDocumentNode.children.push(jrefNode);
38+
}
39+
40+
return jrefDocumentNode;
2541
} catch (error) {
2642
if (error instanceof VFileMessage) {
2743
return file.fail(error.message, /** @type Options */ (error));

src/json/rejson-parse.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@ import { fromJson } from "./jsonast-util.js";
55
* @import { Plugin } from "unified"
66
* @import { VFile } from "vfile"
77
* @import { Options } from "vfile-message"
8-
* @import { JsonDocumentNode } from "./jsonast.js"
8+
* @import { JsonDocumentNode, JsonNode } from "./jsonast.js"
9+
* @import { Reviver } from "./jsonast-util.js"
910
*/
1011

1112

12-
/** @type Plugin<[], string, JsonDocumentNode> */
13-
export function rejsonParse() {
13+
/**
14+
* @typedef {{
15+
* reviver?: Reviver<JsonNode | undefined>;
16+
* }} JsonParseOptions
17+
*/
18+
19+
/** @type Plugin<[JsonParseOptions?], string, JsonDocumentNode> */
20+
export function rejsonParse(options) {
1421
/** @type (document: string, file: VFile) => JsonDocumentNode */
1522
this.parser = function (document, file) {
1623
try {
17-
return {
24+
/** @type JsonDocumentNode */
25+
const jsonDocumentNode = {
1826
type: "json-document",
19-
children: [fromJson(document)]
27+
children: []
2028
};
29+
30+
const jsonNode = fromJson(document, options?.reviver);
31+
if (jsonNode) {
32+
jsonDocumentNode.children.push(jsonNode);
33+
}
34+
35+
return jsonDocumentNode;
2136
} catch (error) {
2237
if (error instanceof VFileMessage) {
2338
return file.fail(error.message, /** @type Options */ (error));

0 commit comments

Comments
 (0)