fix(entity): traverse front-to-back in getComponentsInChildren#3030
fix(entity): traverse front-to-back in getComponentsInChildren#3030cptbtptpbcptdtptp wants to merge 3 commits into
Conversation
…mponentsInChildren The previous back-to-front traversal caused getComponentsInChildren to return results in reverse hierarchy order. All major engines (Unity, Cocos, Three.js, Babylon.js, Godot) use front-to-back depth-first pre-order for this API. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThis PR documents and implements a traversal order change in ChangesComponent Traversal Order
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev/2.0 #3030 +/- ##
===========================================
+ Coverage 77.31% 79.16% +1.85%
===========================================
Files 914 914
Lines 101723 101669 -54
Branches 10437 11208 +771
===========================================
+ Hits 78647 80488 +1841
+ Misses 22892 20994 -1898
- Partials 184 187 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Verify depth-first front-to-back order: parent components first, then recurse into children left-to-right. Also verify multiple components on the same entity are returned in add order. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
GuoLei1990
left a comment
There was a problem hiding this comment.
总结
把 _getComponentsInChildren 从 back-to-front(length-1 → 0)改为 front-to-back(0 → N)深度优先前序遍历,使返回顺序符合层级直觉(self 在前、子节点按 sibling 顺序、深度优先)。方向正确,是一个干净的小修复,且 loop 写法(hoist _components/_children 到局部、缓存 n)与同文件 _setActiveInHierarchy、getComponents 一致,无新分配、无热路径退化。
验证
- 真实 bug 修复(反向证伪通过):手工回放旧逻辑——
- 顺序测试旧实现产出
[compRoot, compChild1, compChild0, compGrandchild],新实现[compRoot, compChild0, compGrandchild, compChild1]; - add-order 测试旧实现产出
[comp2, comp1],新实现[comp1, comp2]。
两条测试在修复前必 fail、修复后 pass,是合格回归。
- 顺序测试旧实现产出
- 消费者零回归:3 个内部消费者(
Animator._controlledRenderers仅做isCulled任一为真的判断、两处ParticleGeneratorplay/stop 遍历)均按无序集合消费,改顺序不影响任何现有行为。 - 递归 vs 迭代:保持递归(深度优先),与意图一致,无需改写。
- 竞品对标:Unity
GetComponentsInChildren同样是深度优先前序(self → 递归 children),本次对齐正确。
问题
无 P0/P1。
- [P2] packages/core/src/Entity.ts:297-302 — 公开方法
getComponentsIncludeChildren的 JSDoc 仍未承诺返回顺序,但本 PR 的测试已把"深度优先前序(父在子前、子按 sibling 顺序)"锁成契约。文档应与被测试锁定的契约对齐,建议补一句说明返回顺序。顺带:该 JSDoc 第 301 行@returns后是个 tab 而非空格(pre-existing),改这块时一并修掉。非阻塞。
简化建议
代码已足够简洁,无进一步精简空间。
Align the public JSDoc with the order contract now locked by tests: depth-first pre-order (self first, then each child's subtree in sibling order). Also fix the tab after @returns. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
GuoLei1990
left a comment
There was a problem hiding this comment.
总结
增量审查针对最新 commit `1f1d6e7d`(自上轮以来唯一改动,`Entity.ts` +2 -1)。这一 commit 正是上轮我提的 [P2]:把"深度优先前序"的返回顺序写进公开 JSDoc,并把 `@returns` 后的 tab 修成空格。问题已闭环,本 PR 现在是一个干净完整的小修复,可以合入。
验证
- JSDoc 与契约对齐(已修复) — 新增 `@remarks The components are returned in depth-first pre-order: the entity itself first, then each child's subtree in sibling order.`。核对实现:`_getComponentsInChildren` 先 front-to-back push 本 entity 的匹配组件("entity itself first"),再 front-to-back 递归 `_children`("each child's subtree in sibling order"),与测试锁定的 `[compRoot, compChild0, compGrandchild, compChild1]` 完全一致,文档无 aspirational 偏差。
- tab → space(已修复) — 第 301 行 `@returns` 后的 tab 已改为常规空格。
- JSDoc 风格合规 — 单行 `@remarks` 形式与项目惯例一致(`Background.ts`、`Camera.ts:68/250`、`Engine.ts:205/411` 均为单行内联 `@remarks`),无需展开多行。
问题
无。上轮唯一的 [P2] 已修复,本次 commit 不引入任何新代码或行为变化(CodeRabbit 与 `git compare` 均确认 diff 仅此一处文档改动)。
之前已关闭的几点(消费者零回归、与 `getComponents`/`_setActiveInHierarchy` 的 loop 写法一致、反向证伪测试合格、Unity 深度优先前序对标)维持结论,不再重复。
LGTM 👍
Summary
_getComponentsInChildrenpreviously traversed components and children back-to-front (length-1→0), causing results to be returned in reverse hierarchy order.0→N) depth-first pre-order traversal, which is the industry-standard convention for hierarchy queries.Test plan
pnpm run test)getComponentsIncludeChildrenreturns components in the expected hierarchy order (parent-first, depth-first)🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Documentation
Tests