Skip to content

Commit f67cabe

Browse files
authored
fix: getLocationForJsonPath should handle incomplete property pair (#73)
1 parent a77a1f0 commit f67cabe

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/__tests__/getLocationForJsonPath.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,37 @@ describe('getLocationForJsonPath', () => {
179179
);
180180
});
181181
});
182+
183+
describe('incomplete property pair', () => {
184+
const result = parseWithPointers(`{
185+
"foo": {
186+
"bar": {
187+
"baz",
188+
}
189+
}
190+
}`);
191+
192+
test.each`
193+
start | end | path | closest
194+
${[2, 10]} | ${[4, 4]} | ${['foo', 'bar', 'baz', 'baz-inga']} | ${true}
195+
${[]} | ${[]} | ${['foo', 'bar', 'baz', 'baz-inga']} | ${false}
196+
`('should return proper location for given JSONPath $path', ({ start, end, path, closest }) => {
197+
expect(getLocationForJsonPath(result, path, closest)).toEqual(
198+
start.length > 0 && end.length > 0
199+
? {
200+
range: {
201+
start: {
202+
character: start[1],
203+
line: start[0],
204+
},
205+
end: {
206+
character: end[1],
207+
line: end[0],
208+
},
209+
},
210+
}
211+
: void 0,
212+
);
213+
});
214+
});
182215
});

src/getLocationForJsonPath.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function findNodeAtPath(node: IJsonASTNode, path: JsonPath, closest: boolean): I
2525
}
2626

2727
for (const propertyNode of node.children) {
28-
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === String(segment)) {
28+
if (
29+
Array.isArray(propertyNode.children) &&
30+
propertyNode.children[0].value === String(segment) &&
31+
propertyNode.children.length === 2
32+
) {
2933
node = propertyNode.children[1];
3034
continue pathLoop;
3135
}

0 commit comments

Comments
 (0)