Skip to content

test: check __current_slot_index in snapshot#2425

Merged
HuJean merged 1 commit intomainfrom
p/test-slot-index
Apr 7, 2026
Merged

test: check __current_slot_index in snapshot#2425
HuJean merged 1 commit intomainfrom
p/test-slot-index

Conversation

@HuJean
Copy link
Copy Markdown
Collaborator

@HuJean HuJean commented Apr 7, 2026

Summary by CodeRabbit

  • Tests
    • Added test coverage for insertBefore operations with multi-slot dynamic content insertion and removal scenarios, validating slot index tracking behavior.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).
  • Changeset added, and when a BREAKING CHANGE occurs, it needs to be clearly marked (or not required).

@HuJean HuJean requested review from Yradex and hzy as code owners April 7, 2026 02:23
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 7, 2026

🦋 Changeset detected

Latest commit: ad8126d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 7, 2026

📝 Walkthrough

Walkthrough

This PR introduces slot index tracking for multi-slot dynamic parts in the React snapshot runtime. It adds a __current_slot_index field to SnapshotInstance, updates the insertBefore method to use auto-incrementing slot indexing, and includes a new test case validating the insertion and removal behavior within multi-slot wrapper scenarios.

Changes

Cohort / File(s) Summary
Changeset
.changeset/few-shirts-laugh.md
Added new changeset entry with empty YAML frontmatter block.
Runtime Source
packages/react/runtime/src/snapshot/snapshot.ts
Added __current_slot_index: number field to SnapshotInstance (initialized to 0) and modified insertBefore to use auto-incrementing slot indexing (this.__current_slot_index++) when handling multi-slot dynamic parts.
Runtime Tests
packages/react/runtime/__test__/basic.test.jsx
Added comprehensive insertBefore test case that validates slot index increment behavior during wrapper insertion, child replacement, and removal operations with inline DOM structure snapshots.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

framework:React

Suggested reviewers

  • Yradex
  • hzy
  • upupming

Poem

🐰✨ A slot index hops and increments with grace,
Dynamic parts now find their rightful place,
Insertions dance, removals stay serene,
The snapshot knows just what it's seen! 🎯

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'test: check __current_slot_index in snapshot' directly summarizes the main change—a new test validating __current_slot_index behavior in SnapshotInstance, which aligns with the test file additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch p/test-slot-index

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/react/runtime/src/snapshot/snapshot.ts (1)

376-377: ⚠️ Potential issue | 🔴 Critical

Guard slot-index overflow before dereferencing slot[index].

At Line 376/377, index is incremented and dereferenced with ! without bounds checks. If insertions exceed available slots (or state drifts), this becomes a runtime crash.

Suggested fix
-      const index = this.__current_slot_index++;
-      const [s, elementIndex] = __snapshot_def.slot[index]!;
+      const index = this.__current_slot_index;
+      const slotEntry = __snapshot_def.slot[index];
+      if (!slotEntry) {
+        throw new Error(
+          `Slot index overflow: index=${index}, slotCount=${__snapshot_def.slot.length}, type=${this.type}`,
+        );
+      }
+      this.__current_slot_index = index + 1;
+      const [s, elementIndex] = slotEntry;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react/runtime/src/snapshot/snapshot.ts` around lines 376 - 377,
Guard the slot access by checking that the computed index
(this.__current_slot_index++) is strictly less than the available slots array
length before dereferencing __snapshot_def.slot[index]; if it is out of range,
either expand/allocate a new slot entry or throw a clear error (including index
and __snapshot_def.slot.length) instead of using the non-null assertion (!) on
slot[index]; replace the direct use of slot[index]! with a safe read that
handles undefined and update the logic around __current_slot_index and
__snapshot_def.slot to prevent silent runtime crashes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/react/runtime/src/snapshot/snapshot.ts`:
- Around line 83-87: The cloned snapshot can miss initialization of the new
field __current_slot_index causing undefined++ in insertBefore; update the clone
path used by takeElements so that it either copies the source's
__current_slot_index or initializes it to 0 when creating/cloning an instance.
Specifically, in the takeElements/cloning logic ensure you assign
this.__current_slot_index = source.__current_slot_index ?? 0 (or set to 0 when
no source) so insertBefore and subsequent slot lookups never operate on
undefined.

---

Outside diff comments:
In `@packages/react/runtime/src/snapshot/snapshot.ts`:
- Around line 376-377: Guard the slot access by checking that the computed index
(this.__current_slot_index++) is strictly less than the available slots array
length before dereferencing __snapshot_def.slot[index]; if it is out of range,
either expand/allocate a new slot entry or throw a clear error (including index
and __snapshot_def.slot.length) instead of using the non-null assertion (!) on
slot[index]; replace the direct use of slot[index]! with a safe read that
handles undefined and update the logic around __current_slot_index and
__snapshot_def.slot to prevent silent runtime crashes.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 410252af-9f3e-4ffb-980c-826f0da8a96e

📥 Commits

Reviewing files that changed from the base of the PR and between 9df4844 and ad8126d.

📒 Files selected for processing (3)
  • .changeset/few-shirts-laugh.md
  • packages/react/runtime/__test__/basic.test.jsx
  • packages/react/runtime/src/snapshot/snapshot.ts

Comment thread packages/react/runtime/src/snapshot/snapshot.ts
@relativeci
Copy link
Copy Markdown

relativeci Bot commented Apr 7, 2026

React External

#200 Bundle Size — 591.44KiB (0%).

ad8126d(current) vs 9df4844 main#197(baseline)

Bundle metrics  no changes
                 Current
#200
     Baseline
#197
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
No change  Cache Invalidation 0% 0%
No change  Chunks 0 0
No change  Assets 3 3
No change  Modules 17 17
No change  Duplicate Modules 5 5
No change  Duplicate Code 8.59% 8.59%
No change  Packages 0 0
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#200
     Baseline
#197
No change  Other 591.44KiB 591.44KiB

Bundle analysis reportBranch p/test-slot-indexProject dashboard


Generated by RelativeCIDocumentationReport issue

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 7, 2026

Merging this PR will not alter performance

✅ 72 untouched benchmarks
⏩ 21 skipped benchmarks1


Comparing p/test-slot-index (ad8126d) with main (9df4844)

Open in CodSpeed

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@relativeci
Copy link
Copy Markdown

relativeci Bot commented Apr 7, 2026

Web Explorer

#8656 Bundle Size — 728.84KiB (0%).

ad8126d(current) vs 9df4844 main#8653(baseline)

Bundle metrics  no changes
                 Current
#8656
     Baseline
#8653
No change  Initial JS 43.31KiB 43.31KiB
No change  Initial CSS 2.16KiB 2.16KiB
No change  Cache Invalidation 0% 0%
No change  Chunks 8 8
No change  Assets 10 10
No change  Modules 148 148
No change  Duplicate Modules 11 11
No change  Duplicate Code 34.69% 34.69%
No change  Packages 3 3
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#8656
     Baseline
#8653
No change  Other 384.62KiB 384.62KiB
No change  JS 342.07KiB 342.07KiB
No change  CSS 2.16KiB 2.16KiB

Bundle analysis reportBranch p/test-slot-indexProject dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci Bot commented Apr 7, 2026

React Example

#7081 Bundle Size — 237.81KiB (0%).

ad8126d(current) vs 9df4844 main#7078(baseline)

Bundle metrics  no changes
                 Current
#7081
     Baseline
#7078
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
No change  Cache Invalidation 0% 0%
No change  Chunks 0 0
No change  Assets 4 4
No change  Modules 180 180
No change  Duplicate Modules 71 71
No change  Duplicate Code 46.39% 46.39%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#7081
     Baseline
#7078
No change  IMG 145.76KiB 145.76KiB
No change  Other 92.05KiB 92.05KiB

Bundle analysis reportBranch p/test-slot-indexProject dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci Bot commented Apr 7, 2026

React MTF Example

#214 Bundle Size — 207.38KiB (0%).

ad8126d(current) vs 9df4844 main#211(baseline)

Bundle metrics  no changes
                 Current
#214
     Baseline
#211
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
No change  Cache Invalidation 0% 0%
No change  Chunks 0 0
No change  Assets 3 3
No change  Modules 174 174
No change  Duplicate Modules 68 68
No change  Duplicate Code 46.08% 46.08%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#214
     Baseline
#211
No change  IMG 111.23KiB 111.23KiB
No change  Other 96.15KiB 96.15KiB

Bundle analysis reportBranch p/test-slot-indexProject dashboard


Generated by RelativeCIDocumentationReport issue

@HuJean HuJean enabled auto-merge (squash) April 7, 2026 02:55
@HuJean HuJean merged commit 55602e2 into main Apr 7, 2026
77 of 80 checks passed
@HuJean HuJean deleted the p/test-slot-index branch April 7, 2026 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants