Skip to content

Fix Instance.get() for missing property name pointers - #156

Open
ary4t wants to merge 1 commit into
hyperjump-io:mainfrom
ary4t:fix/instance-get-property-name-pointers
Open

ary4t wants to merge 1 commit into
hyperjump-io:mainfrom
ary4t:fix/instance-get-property-name-pointers

Conversation

@ary4t

@ary4t ary4t commented Nov 18, 2026

Copy link
Copy Markdown

Summary

Fixes an issue where Instance.get() throws an unhandled TypeError instead of returning undefined when attempting to resolve a non-existent property name pointer (e.g. "#*/missing" or "#*").

Additionally, resolves a bug where querying a property name pointer against an array index (e.g. "#*/1") returned the first item of the array instead of undefined.


Cause

In lib/instance.js:

return isPropertyNamePointer ? result.parent.children[0] : result;

When isPropertyNamePointer is true:

  • If result is undefined (missing property), accessing result.parent throws TypeError: Cannot read properties of undefined (reading 'parent').
  • If result is the root node, result.parent is undefined, so accessing result.parent.children throws TypeError: Cannot read properties of undefined (reading 'children').
  • If result is an array element, result.parent is the array node (not a property node), so result.parent.children[0] returns the first array element instead of undefined.

Solution

Verify that result?.parent exists and is of type "property":

return isPropertyNamePointer
  ? (result?.parent?.type === "property" ? result.parent.children[0] : undefined)
  : result;

Verification

  • Added unit tests in lib/instance.spec.ts covering:
    • Valid property name pointer (#*/foo)
    • Missing property name pointer (#*/missing)
    • Root property name pointer (#*)
    • Array item property name pointer (#*/1)
  • npm test passes for lib/instance.spec.ts (44/44 tests)
  • npm run lint passes (0 errors)
  • npm run check-types passes (0 errors)

Instance.get() previously assumed result.parent.children[0] existed
whenever isPropertyNamePointer was true. This caused an unhandled
TypeError when the property was missing or when targeting the root node,
and returned the first array element when targeting an array index.
Safely check result?.parent?.type === 'property' and return undefined
otherwise.
Copilot AI balanced review requested due to automatic review settings November 18, 2026 21:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants