Repository navigation
Don't treat ids like toString as present in the tree - #406
melihozcan1 wants to merge 4 commits into
Conversation
👷 Deploy request for react-arborist pending review.Visit the deploys page to approve it
|
TrevorBurnham
left a comment
There was a problem hiding this comment.
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:
-
Filtering (
modules/react-arborist/src/data/create-list.ts:40):flattenAndFilterTreebuildsmatchesas a plain{}, andcollectchecksmatches[node.id]. If a node's id isconstructorortoString, 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 aSet<string>would fix it. -
Open state (
modules/react-arborist/src/interfaces/tree-api.ts:863):isOpenreadsopen.unfiltered[id] ?? def(andopen.filtered[id] ?? true) from plain{}maps (the default state is instate/open-slice.ts:41). A folder with idtoStringgets backObject.prototype.toString, which is truthy, so it always renders expanded, even withopenByDefault={false}. Creating those maps withObject.create(null), or checkingObject.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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| --- | ||
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks! Folded both into this PR so all the id-keyed maps are fixed together. Filtering collects matches in a |
# Conflicts: # modules/react-arborist/src/interfaces/tree-api.ts
|
Merged |
TrevorBurnham
left a comment
There was a problem hiding this comment.
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:
-
__proto__ids (modules/react-arborist/src/data/create-index.ts:5):idToIndexis a plain{}again, somap["__proto__"] = indexcalls the prototype setter (which ignores a number) instead of adding a key. A visible node with id__proto__can't be found bytree.get(),tree.indexOf()ortree.scrollTo()(which polls until it gives up), andselectAll()skips it becauseObject.keys(idToIndex)doesn't include it.getOwncan't help, since the key never exists. Building the index withObject.create(null)(or aMap) alongside thegetOwnchecks would fix this. -
VISIBILITY_TOGGLE(modules/react-arborist/src/state/open-slice.ts:31, minor):const prev = state[action.id]still reads inherited keys. For idtoString,previsObject.prototype.toString(truthy), so the first toggle storesfalseand the folder stays closed.tree.toggle()doesn't use this action, but the action is exported.utils.getOwn(state, action.id)would fix it. -
openStategetter (modules/react-arborist/src/interfaces/tree-api.ts:98): the public getter returns the raw open map, so consumer code liketree.openState["constructor"]still gets an inherited value. Creating the open maps withObject.create(null)(initial state,VISIBILITY_CLEAR, and the spreads inopenMapReducer) would fix this and #2 at the source, without guarding each read. Otherwise, the docs could point callers totree.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; |
There was a problem hiding this comment.
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.
Summary
Follow-up from the review of #404.
Several id-keyed maps were plain
{}objects read withmap[id]orid in map, so ids that matchObject.prototypekeys (toString,constructor, ...) resolved to inherited functions:scrollTo("toString")resolved immediately and passed a function toscrollToItem, andindexOf("toString")returned a function.matches[node.id]was truthy.openByDefault={false}, becauseisOpengot a function back from the open-state map.The fix reads these maps by own keys only:
get,indexOf,scrollToandisOpengo through a small internalgetOwnhelper (hasOwnProperty.call, since the library targets ES2016), and the filter collects matches in aSet<string>.tree.idToIndexstays a plain object, so consumers see no change to it, andindexOfkeeps itsnumber | nulltype.Tests in
tree-api.test.tscoverindexOf,scrollTo, filtering andopenByDefaultwith these ids; each fails without the change.Checklist
.changes/entry describing the change (see.changes/README.md), or applied theskip-changeloglabel if it has no user-facing effect.yarn lint,yarn fmt:check, andyarn workspace react-arborist testpass (warning-clean — seeAGENTS.md).