Skip to content

Commit fee184d

Browse files
authored
Merge pull request #44 from stoplightio/get-location-incomplete-property-pair
findNodeAtLocation does not handle incomplete property pair
2 parents 4df0535 + 7e6ad07 commit fee184d

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/impl/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export function findNodeAtLocation(root: Node | undefined, path: JSONPath): Node
288288
}
289289
let found = false;
290290
for (const propertyNode of node.children) {
291-
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment) {
291+
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) {
292292
node = propertyNode.children[1];
293293
found = true;
294294
break;

src/test/json.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ suite('JSON', () => {
575575
});
576576

577577
test('tree: find location', () => {
578-
let root = parseTree('{ "key1": { "key11": [ "val111", "val112" ] }, "key2": [ { "key21": false, "key22": 221 }, null, [{}] ] }');
578+
let root = parseTree('{ "key1": { "key11": [ "val111", "val112" ] }, "key2": [ { "key21": false, "key22": 221 }, null, [{}] ], "key3": { "key31":, "key32": 32 } }');
579579
assertNodeAtLocation(root, ['key1'], { key11: ['val111', 'val112'] });
580580
assertNodeAtLocation(root, ['key1', 'key11'], ['val111', 'val112']);
581581
assertNodeAtLocation(root, ['key1', 'key11', 0], 'val111');
@@ -586,6 +586,8 @@ suite('JSON', () => {
586586
assertNodeAtLocation(root, ['key2', 1], null);
587587
assertNodeAtLocation(root, ['key2', 2], [{}]);
588588
assertNodeAtLocation(root, ['key2', 2, 0], {});
589+
assertNodeAtLocation(root, ['key3', 'key31', 'key311'], undefined);
590+
assertNodeAtLocation(root, ['key3', 'key32'], 32);
589591
});
590592

591593
test('location: matches', () => {

0 commit comments

Comments
 (0)