Skip to content

fix: #556 guard against undefined window in getScroll#567

Merged
afc163 merged 4 commits into
react-component:masterfrom
jeasonnow:fix/#556
Jun 10, 2026
Merged

fix: #556 guard against undefined window in getScroll#567
afc163 merged 4 commits into
react-component:masterfrom
jeasonnow:fix/#556

Conversation

@jeasonnow

@jeasonnow jeasonnow commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

#566

Summary by CodeRabbit

  • Bug Fixes

    • 提升了滚动偏移计算在边界情况下的容错性,减少在缺失或无效窗口引用时的异常,增强稳定性与可靠性。
  • Tests

    • 新增测试场景,覆盖在缺失或为 null/undefined 的窗口引用下的行为,确保偏移计算返回安全默认值。

@vercel

vercel Bot commented Jun 10, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1c31915a-775a-4088-b4ce-a92876e1984f

📥 Commits

Reviewing files that changed from the base of the PR and between 9fbe571 and a886974.

📒 Files selected for processing (2)
  • src/util.ts
  • tests/util.spec.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/util.ts

Walkthrough

src/util.ts 中,getScroll() 参数扩展为 Window | null | undefined,函数入口对假值返回 0。同时在 tests/util.spec.tsxoffset 添加两条断言,覆盖缺失或为 null/undefined 的 window 场景。

Changes

getScroll 空值防护与 offset 测试

Layer / File(s) Summary
getScroll 参数空值保护与 offset 测试
src/util.ts, tests/util.spec.tsx
getScroll 的参数类型放宽为可空/可未定义并在入口对假值返回 0;为 offset 增加两条测试,验证当 defaultViewparentWindow 缺失或为 null/undefined 时返回 {left:0, top:0}

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • zombieJ

Poem

兔兔在窗边守望,
一行护盾挡风霜,
空值无惧滚动安,
小测验来证光芒,
代码轻跳心欢畅。 🐰

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清楚地总结了主要变更:在 getScroll 函数中添加对未定义 window 的保护逻辑,这正是 PR 的核心改动。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/util.ts

ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.

tests/util.spec.tsx

ESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@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

This pull request adds a runtime guard to the getScroll function in src/util.ts to handle cases where the window object is falsy. The review feedback correctly suggests updating the TypeScript signature of getScroll to accept Window | null | undefined to prevent compilation errors under strictNullChecks.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread src/util.ts Outdated

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/util.ts`:
- Around line 11-12: The getScroll function's runtime guard shows w can be
null/undefined but its signature is non-null Window; change the parameter type
of getScroll to accept Window | null | undefined (e.g., getScroll(w: Window |
null | undefined, top?: boolean): number) so the type reflects the runtime
check, and update any call sites (such as where you do const w = doc.defaultView
|| doc.parentWindow; pos.left += getScroll(w)) if necessary to satisfy
TypeScript or overloads.
- Line 12: 为覆盖 getScroll 中的空值保护分支,新增单测到 tests/util.spec.tsx:构造一个元素上下文使
ownerDocument.defaultView 和 ownerDocument.parentWindow 为 null/undefined(例如通过创建一个
mock document 或手动设置 element.ownerDocument.defaultView = undefined /
ownerDocument.parentWindow = null),然后调用 offset(element) 并断言返回值为 { left: 0, top:
0 };请在测试中同时验证 defaultView 缺失和 parentWindow 缺失两种情况以确保 getScroll 中的 if (!w) return
0 分支被覆盖(定位到 util.ts 中的 getScroll 和 offset 函数)。
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d18aa098-b7dd-4c8e-872f-f880217da316

📥 Commits

Reviewing files that changed from the base of the PR and between 3ebf65c and 9fbe571.

📒 Files selected for processing (1)
  • src/util.ts

Comment thread src/util.ts Outdated
Comment thread src/util.ts Outdated
santree.g and others added 2 commits June 10, 2026 23:01
Cover offset() when defaultView and parentWindow are missing or null.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread src/util.ts Outdated
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.94%. Comparing base (3ebf65c) to head (a886974).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #567      +/-   ##
==========================================
+ Coverage   98.93%   98.94%   +0.01%     
==========================================
  Files           8        8              
  Lines         188      190       +2     
  Branches       68       69       +1     
==========================================
+ Hits          186      188       +2     
  Misses          2        2              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

@afc163 afc163 merged commit ca56f32 into react-component:master Jun 10, 2026
9 of 10 checks passed
@jeasonnow jeasonnow deleted the fix/#556 branch June 11, 2026 01:36
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