Skip to content

fix(editor): prevent crash when getDatumByIndexes returns undefined o… - #195

Merged
Aarebecca merged 3 commits into
antvis:mainfrom
taosin:fix/editor-data-null-guard-and-root-items
Feb 2, 2026
Merged

Aarebecca merged 3 commits into
antvis:mainfrom
taosin:fix/editor-data-null-guard-and-root-items

Conversation

@taosin

@taosin taosin commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

问题

  • getDatumByIndexes 在索引越界或路径不存在时会返回 undefined,但 updateItemDatum、updateBuiltInElement、getChildrenDataByIndexes 等未做判空,会导致运行时报错(如 Cannot read property 'children' of undefined)。
  • getChildrenDataByIndexes(data, []) 使用 data.data 获取根级子列表,而 ParsedData 仅有 items,实际运行时 data.data 常为 undefined,后续 list.splice 会崩溃。

修改

  • editor/utils/data.ts:根级子列表改为使用 data.items;当 getDatumByIndexes 结果为 null/undefined 时返回空数组,避免访问 datum.children 崩溃。
  • editor/managers/state.ts:在 updateItemDatum 和 updateBuiltInElement 中对 getDatumByIndexes 结果做判空,为 null/undefined 时直接返回,避免后续属性访问崩溃。

影响

  • 仅增加防御性判断和正确的根级数据来源,不改变正常路径行为。
  • 单元测试中根级子列表断言由 toBe(data.data) 改为 toBe(data.items),与类型和实现一致。
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @taosin, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决编辑器模块中因数据访问不当导致的潜在运行时崩溃问题。通过引入防御性编程实践,对 getDatumByIndexes 方法可能返回的 null 或 undefined 结果进行显式检查,并修正了获取根级子列表的正确数据源,从而显著提高了系统的健壮性和稳定性,而不会改变现有正常业务逻辑的行为。

Highlights

  • 修复潜在崩溃: 在 editor/managers/state.ts 中,为 updateItemDatum 和 updateBuiltInElement 方法中 getDatumByIndexes 的结果添加了空值检查,以防止在数据不存在时访问属性导致的运行时崩溃。
  • 修正根级数据源: 在 editor/utils/data.ts 中,将获取根级子列表的数据源从 data.data 修正为 data.items,以符合 ParsedData 类型的实际结构。
  • 增强数据访问安全性: 在 editor/utils/data.ts 的 getChildrenDataByIndexes 方法中,增加了对 getDatumByIndexes 结果的空值检查,确保在获取不到数据时返回空数组,避免后续操作崩溃。
  • 更新单元测试: 相应地更新了 __tests__/unit/editor/utils/data.test.ts 中的单元测试断言,以反映根级子列表数据源的正确性。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

您好,感谢您的贡献。这次的 PR 很好地解决了 getDatumByIndexes 返回 undefined 时可能引发的运行时崩溃问题。

您在 updateItemDatum、updateBuiltInElement 和 getChildrenDataByIndexes 中增加的空值检查,以及在 getChildrenDataByIndexes 中对根级数据源的修正(从 data.data 到 data.items),都非常到位,显著增强了代码的健壮性和稳定性。

我只发现一个与此相关的潜在改进点,请查看我的具体评论。总体来说,这是一次高质量的修复!

Comment thread src/editor/managers/state.ts Resolved
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@codecov-commenter

codecov-commenter commented Feb 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 45.23%. Comparing base (bb8761a) to head (267f8bf).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #195   +/-   ##
=======================================
  Coverage   45.22%   45.23%           
=======================================
  Files         334      334           
  Lines       26635    26638    +3     
  Branches     2115     2118    +3     
=======================================
+ Hits        12046    12049    +3     
  Misses      14579    14579           
  Partials       10       10           
Flag Coverage Δ
infographic 45.23% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/editor/managers/state.ts 98.64% <100.00%> (+0.01%) ⬆️
src/editor/utils/data.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@Aarebecca
Aarebecca merged commit a842fb3 into antvis:main Feb 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants