Skip to content

Commit b3f108f

Browse files
committed
Add baseUri and anchors to schema resource
1 parent a86f43f commit b3f108f

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

language-server/src/json-schema-document.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as JsonPointer from "@hyperjump/json-pointer";
33
import { resolveIri, toAbsoluteIri } from "@hyperjump/uri";
44
import { getNodeValue, parseTree } from "jsonc-parser";
55
import * as Instance from "./json-instance.js";
6+
import { uriFragment } from "./util.js";
67

78

89
export class JsonSchemaDocument {
@@ -69,14 +70,14 @@ export class JsonSchemaDocument {
6970
return document;
7071
}
7172

72-
#buildSchemaResources(node, uri = "", dialectUri = "", pointer = "", parent = undefined) {
73+
#buildSchemaResources(node, uri = "", dialectUri = "", pointer = "", parent = undefined, anchors = {}) {
7374
const jsonNode = Instance.cons(uri, pointer, getNodeValue(node), node.type, [], parent, node.offset, node.length);
7475

7576
switch (node.type) {
7677
case "array":
7778
jsonNode.children = node.children.map((child, index) => {
7879
const itemPointer = JsonPointer.append(index, pointer);
79-
return this.#buildSchemaResources(child, uri, dialectUri, itemPointer, jsonNode);
80+
return this.#buildSchemaResources(child, uri, dialectUri, itemPointer, jsonNode, anchors);
8081
});
8182
break;
8283

@@ -118,9 +119,25 @@ export class JsonSchemaDocument {
118119
}
119120
}
120121

122+
const anchorToken = keywordNameFor("https://json-schema.org/keyword/anchor", dialectUri);
123+
const $anchorNode = anchorToken && nodeStep(node, anchorToken);
124+
if ($anchorNode) {
125+
const anchor = getNodeValue($anchorNode);
126+
anchors[anchor] = pointer;
127+
}
128+
129+
const legacyAnchorToken = keywordNameFor("https://json-schema.org/keyword/draft-04/id", dialectUri);
130+
const legacyAnchorNode = legacyAnchorToken && nodeStep(node, legacyAnchorToken);
131+
if (legacyAnchorNode) {
132+
const anchor = getNodeValue(legacyAnchorNode);
133+
if (anchor[0] === "#") {
134+
anchors[uriFragment(anchor)] = pointer;
135+
}
136+
}
137+
121138
for (const child of node.children) {
122139
const propertyPointer = JsonPointer.append(getNodeValue(child.children[0]), pointer);
123-
const propertyNode = this.#buildSchemaResources(child, uri, dialectUri, propertyPointer, jsonNode);
140+
const propertyNode = this.#buildSchemaResources(child, uri, dialectUri, propertyPointer, jsonNode, anchors);
124141

125142
if (propertyNode) {
126143
jsonNode.children.push(propertyNode);
@@ -134,13 +151,18 @@ export class JsonSchemaDocument {
134151
}
135152

136153
jsonNode.children = node.children.map((child) => {
137-
return this.#buildSchemaResources(child, uri, dialectUri, pointer, jsonNode);
154+
return this.#buildSchemaResources(child, uri, dialectUri, pointer, jsonNode, anchors);
138155
});
139156
break;
140157
}
141158

142159
if (jsonNode.pointer === "") {
143-
this.schemaResources.push({ dialectUri, schemaResource: jsonNode });
160+
this.schemaResources.push({
161+
schemaResource: jsonNode,
162+
dialectUri: dialectUri,
163+
baseUri: uri,
164+
anchors: anchors
165+
});
144166
}
145167

146168
return jsonNode;

0 commit comments

Comments
 (0)