Skip to content

Don't treat ids like toString as present in the tree - #406

Open
melihozcan1 wants to merge 4 commits into
jameskerr:mainfrom
melihozcan1:fix/idtoindex-own-keys
Open

melihozcan1 wants to merge 4 commits into
jameskerr:mainfrom
melihozcan1:fix/idtoindex-own-keys

Conversation

@melihozcan1

@melihozcan1 melihozcan1 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up from the review of #404.

Several id-keyed maps were plain {} objects read with map[id] or id in map, so ids that match Object.prototype keys (toString, constructor, ...) resolved to inherited functions:

  • scrollTo("toString") resolved immediately and passed a function to scrollToItem, and indexOf("toString") returned a function.
  • A search showed nodes with those ids even when they didn't match, because the filter's matches[node.id] was truthy.
  • Folders with those ids always rendered expanded, even with openByDefault={false}, because isOpen got a function back from the open-state map.

The fix reads these maps by own keys only: get, indexOf, scrollTo and isOpen go through a small internal getOwn helper (hasOwnProperty.call, since the library targets ES2016), and the filter collects matches in a Set<string>. tree.idToIndex stays a plain object, so consumers see no change to it, and indexOf keeps its number | null type.

Tests in tree-api.test.ts cover indexOf, scrollTo, filtering and openByDefault with these ids; each fails without the change.

Checklist

  • Added a .changes/ entry describing the change (see .changes/README.md), or applied the skip-changelog label if it has no user-facing effect.
  • yarn lint, yarn fmt:check, and yarn workspace react-arborist test pass (warning-clean — see AGENTS.md).

Copilot AI balanced review requested due to automatic review settings November 16, 2026 08:38
@netlify

netlify Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

👷 Deploy request for react-arborist pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 276f0e2

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@TrevorBurnham TrevorBurnham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this! The Object.create(null) change does fix the inherited-key lookups in indexOf, get and scrollTo. Two other id-keyed maps have the same bug. They're outside this diff, so I'm noting them here instead of inline:

  1. Filtering (modules/react-arborist/src/data/create-list.ts:40): flattenAndFilterTree builds matches as a plain {}, and collect checks matches[node.id]. If a node's id is constructor or toString, that lookup returns the inherited function, which is truthy, so the node shows up in filtered results even when it doesn't match the search term. Object.create(null) or a Set<string> would fix it.

  2. Open state (modules/react-arborist/src/interfaces/tree-api.ts:863): isOpen reads open.unfiltered[id] ?? def (and open.filtered[id] ?? true) from plain {} maps (the default state is in state/open-slice.ts:41). A folder with id toString gets back Object.prototype.toString, which is truthy, so it always renders expanded, even with openByDefault={false}. Creating those maps with Object.create(null), or checking Object.hasOwn(map, id) first, would fix it.

These could go into this PR, so all the id-keyed maps are fixed together, or into a follow-up. Either works.

map[node.id] = index;
return map;
}, {});
}, Object.create(null));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor: tree.idToIndex is a public field, so it now has no prototype for consumers either. Code like tree.idToIndex.hasOwnProperty(id) or String(tree.idToIndex) will start throwing a TypeError after a patch release. It's probably rare, but either mention in the changeset that idToIndex is now a null-prototype object, or keep a plain object here and check Object.hasOwn(this.idToIndex, id) in indexOf, get and scrollTo instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Went with the second option: idToIndex is a plain object again, and get, indexOf and scrollTo look ids up through a small own-key helper (hasOwnProperty.call, since the lib targets ES2016). Consumers see no change to idToIndex.

Comment thread .changes/idtoindex-inherited-keys.md Outdated
---
type: fix
---
`tree.scrollTo()`, `tree.indexOf()` and `tree.idToIndex` no longer treat ids such as `toString` or `constructor` as present in the tree when no node has that id.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor: tree.get() (tree-api.ts:272) is also fixed by this change, so it could be listed too. There's also a behavior change worth mentioning: before, a real node with id __proto__ was never indexed, because map["__proto__"] = index set the object's prototype instead of adding a key. Now get, indexOf and scrollTo find it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated. Since idToIndex stays a plain object, __proto__ behaves as before, so there's nothing new to mention. I left get() out: it already returned null for these ids (visibleNodes[fn] is undefined), so its result doesn't change.

Copilot AI balanced review requested due to automatic review settings November 28, 2026 19:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@melihozcan1

Copy link
Copy Markdown
Contributor Author

Thanks! Folded both into this PR so all the id-keyed maps are fixed together. Filtering collects matches in a Set<string>, and isOpen reads the open maps through the same own-key check, which also covers maps passed in as initialOpenState. Added a test for each; both fail without the change.

# Conflicts:
#	modules/react-arborist/src/interfaces/tree-api.ts
Copilot AI balanced review requested due to automatic review settings November 29, 2026 19:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@melihozcan1

Copy link
Copy Markdown
Contributor Author

Merged main in (276f0e2) to resolve the conflict with #407. The only overlap was scrollTo: #407 moved its lookup into scrollWhenReady, so the own-key check now lives there, next to the superseded() check. Against main, the diff is just this PR's changes. Unit tests (152) and the e2e suite pass, and CI is green.

@TrevorBurnham TrevorBurnham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the update! The Set in flattenAndFilterTree and the getOwn checks fix the toString/constructor cases, and the new tests cover all of them. A few things are still open. They're in code outside this diff, so they're listed here:

  1. __proto__ ids (modules/react-arborist/src/data/create-index.ts:5): idToIndex is a plain {} again, so map["__proto__"] = index calls the prototype setter (which ignores a number) instead of adding a key. A visible node with id __proto__ can't be found by tree.get(), tree.indexOf() or tree.scrollTo() (which polls until it gives up), and selectAll() skips it because Object.keys(idToIndex) doesn't include it. getOwn can't help, since the key never exists. Building the index with Object.create(null) (or a Map) alongside the getOwn checks would fix this.

  2. VISIBILITY_TOGGLE (modules/react-arborist/src/state/open-slice.ts:31, minor): const prev = state[action.id] still reads inherited keys. For id toString, prev is Object.prototype.toString (truthy), so the first toggle stores false and the folder stays closed. tree.toggle() doesn't use this action, but the action is exported. utils.getOwn(state, action.id) would fix it.

  3. openState getter (modules/react-arborist/src/interfaces/tree-api.ts:98): the public getter returns the raw open map, so consumer code like tree.openState["constructor"] still gets an inherited value. Creating the open maps with Object.create(null) (initial state, VISIBILITY_CLEAR, and the spreads in openMapReducer) would fix this and #2 at the source, without guarding each read. Otherwise, the docs could point callers to tree.isOpen().

None of these need to block the merge. #1 is probably worth doing here, since it's the same class of bug.

if (!key) return null;
return this.idToIndex[key];
// Keeps the public `number | null` type; a missing id still returns undefined, as before.
return utils.getOwn(this.idToIndex, key) as number;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor: I see this is deliberate, and keeping the existing public type in a patch release makes sense. Still, as number hides the undefined case from TypeScript callers. For example, const i = tree.indexOf(id); if (i !== null) list[i] passes the check for an unknown id. It could be worth widening the return type to number | null | undefined in a later minor release, or at least noting it in the docs.

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.

3 participants