Skip to content

[lexical][*] Chore: stop using deprecated traversal type parameters in tests - #8667

Merged
etrepum merged 2 commits into
meta:mainfrom
etrepum:claude/cleanup-deprecated-code-t4z1j1
Jun 10, 2026
Merged

etrepum merged 2 commits into
meta:mainfrom
etrepum:claude/cleanup-deprecated-code-t4z1j1

Conversation

@etrepum

@etrepum etrepum commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Migrate the remaining call sites (all in tests) off the deprecated unsafe type parameters that #8661 added to the node traversal methods (getFirstChild(), getParent(), getChildAtIndex(), $getNodeByKey(), etc.). The deprecated overloads themselves are retained for backwards compatibility with consumers.

Add a $assertNodeType test helper that narrows a node via a type guard instead of an unchecked cast, and use it (alongside the existing $is* guards) in place of the deprecated type parameters and a few remaining unsafe as casts in the affected tests.

Follow-up to #8661

Test plan

Only unit test syntax is affected, behavior should be identical and no runtime code has changed.

…eta#8661 follow-up)

Migrate the remaining call sites (all in tests) off the deprecated unsafe
type parameters that meta#8661 added to the node traversal methods
(getFirstChild<T>(), getParent<T>(), getChildAtIndex<T>(), $getNodeByKey<T>(),
etc.). The deprecated overloads themselves are retained for backwards
compatibility with consumers.

Add a $assertNodeType test helper that narrows a node via a type guard
instead of an unchecked cast, and use it (alongside the existing $is*
guards) in place of the deprecated type parameters and a few remaining
unsafe `as` casts in the affected tests.
@vercel

vercel Bot commented Jun 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Jun 10, 2026 1:55pm
lexical-playground Ready Ready Preview, Comment Jun 10, 2026 1:55pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 10, 2026

@potatowagon potatowagon 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.

Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.

Assessment: LGTM ✅

This is a well-scoped refactor that replaces deprecated generic type parameters on traversal methods (e.g. getFirstChildOrThrow<T>(), getFirstChild<T>(), getChildAtIndex<T>()) with a new runtime $assertNodeType() utility that uses type guards for type-safe narrowing.

What I checked:

  1. The $assertNodeType utility (packages/lexical/src/__tests__/utils/index.tsx): Clean implementation — takes a node | null | undefined and a type guard, throws a clear error if the guard fails. Returns the narrowed type. This is strictly better than the previous as T / ! pattern because it provides a runtime assertion rather than silently producing undefined behavior on type mismatch.

  2. Consistency of the migration pattern across all 21 test files: Every change follows the same mechanical pattern — getFirstChildOrThrow<Foo>() → $assertNodeType(getFirstChild(), $isFoo), and getChildAtIndex<Foo>(n)! → $assertNodeType(getChildAtIndex(n), $isFoo). No logic changes, just type narrowing approach.

  3. Import cleanup: Removed unused concrete type imports (e.g. ParagraphNode, TextNode, TableNode) and added the corresponding type guard imports ($isParagraphNode, $isTextNode, $isTableNode). This is correct — the concrete types are no longer needed when using guards.

  4. Edge cases: The utility correctly handles null | undefined by coalescing to null before passing to the guard. One creative use in LexicalReconciler.test.ts uses a custom inline guard (node): node is TestDecoratorNode => node instanceof TestDecoratorNode — this is valid and shows the utility is flexible.

  5. www backwards compatibility: This PR only touches test files and a test utility. No production API changes, no export changes, no signature changes. Zero www impact.

  6. CI: Core unit tests (node 24.x) and core browser tests pass. CLA signed. Vercel previews deployed. Browser/e2e/integration tests still pending but those are test-only changes that should not regress.

Why this is safe: All changes are test-only. The deprecated generic type parameters still work in production code — this just migrates tests away from them so the deprecation can proceed. No runtime behavior change in any library package.

@etrepum
etrepum added this pull request to the merge queue Jun 10, 2026
Merged via the queue into meta:main with commit f18cff5 Jun 10, 2026
48 checks passed
@etrepum
etrepum deleted the claude/cleanup-deprecated-code-t4z1j1 branch June 10, 2026 14:32
etrepum pushed a commit to etrepum/lexical that referenced this pull request Jun 11, 2026
…][lexical-react][lexical-extension][lexical-devtools-core][lexical-playground] Bug Fix: Named-slots audit fixes: collab first-set sync, in-slot copy/cut, export gating, slot cycle guard (meta#8603)

Fixes every issue from the named-slots audit of this PR, in one
cherry-pickable commit.

Critical:
- yjs V1: route a changed `slots` key in YTextEvent/YXmlEvent to
  syncSlotsFromYjs. The first $setSlot on a synced host (and its undo)
  integrates the slots Y.Map in the same transaction as its content, so
  no YMapEvent fires and the slot never reached peers; the follow-up
  peer edit then crashed on an undefined collab node or deleted the
  originator's slot from the shared doc.
- yjs V2: make $equalYTypePNode slots-aware (and exclude the `slots`
  attribute from the plain attribute comparison) so the dirty scan
  recurses instead of repointing the mapping — the originator's first
  $setSlot is now actually written to yjs.
- clipboard/html: walk the RangeSelection's slot frame (new core
  $getSlotFrame) in $generateJSONFromSelectedNodes and
  $generateDOMFromNodes. A selection wholly inside a slot never contains
  its host, so both exporters returned empty payloads and cut inside a
  slot was silent data loss.
- clipboard/html: gate the includeChildrenWhenSelected child promotion
  on NodeSelection. A partial RangeSelection over a host exported the
  full unsliced child text plus wholly unselected siblings on both
  channels.

Major:
- selection: insertNodes with the caret on an empty slot value seeds a
  paragraph and redirects instead of throwing "to have a block
  ancestor" (reachable via Ctrl+A in an empty slot, then paste).
- core: reverse cycle guard $errorOnSlotCycleChild in ElementNode.splice
  and insertBefore/insertAfter/replace. Closing a cycle through the
  children channel (slotValue.append(host)) previously hung the commit
  inside isAttached's up-walk.
- yjs V1: sync root-host slots (the root branch never called
  syncSlotsFromLexical, so $setSlot($getRoot(), ...) silently never
  synced).
- yjs V1/V2: validate-and-skip hostile or invalid remote slot entries
  (reserved names, non-shared-type values, missing __type, invalid slot
  value shapes, duplicate shared types under two names) instead of
  throwing inside the observer update; aliased entries are first-name-
  wins.
- yjs: reserve the `slots` attribute key in property sync in both
  directions for both versions; restored V1 hosts no longer carry a junk
  `slots` Y.Map property, and a user property named `slots` can no
  longer clobber the channel.
- yjs V1: sweep the transaction's deleted structs after remote events
  (mirroring V2) so a remote host deletion no longer leaks its slot
  values' collabNodeMap entries.
- playground: drop CardExtension's ClickAfterLastBlockExtension
  override. The last-wins config merge silently clobbered the app's
  $isCodeNode predicate, and CardNode.isShadowRoot() already satisfies
  $defaultShouldInsertAfter.

Minor:
- clipboard: the excluded-slot-value guard also verifies the exported
  node type, catching the 1-child excludeFromCopy case that silently
  exported the child as the slot value; declare `slots` on
  BaseSerializedNode.
- html: $appendNodeToHTML resolves the session DOM render config so
  disabledForSession overrides apply inside slot subtrees.
- core: $setSlot re-setting the same node under the same name is a
  no-op instead of tripping the already-slotted invariant.
- utils: $dfsWithSlots/$reverseDfsWithSlots stop before the endNode's
  slot subtrees (inclusive stop), document the in-slot endNode
  limitation, and carry @experimental tags.
- react: useLexicalSlot detaches the previously appended container on
  nodeKey/slotName change; useCharacterLimit advances the wrap budget
  past slotted decorator leaves so the overflow boundary lands exactly.
- extension: NodeSelectionDataSelectedExtension mirrors a NodeSelection
  already committed at registration, clears data-selected attributes on
  teardown, and raises an invariant for configured-but-unregistered
  classes.
- yjs V2: keep the (empty) slots Y.Map on remove-last to narrow the
  concurrent-creation window; slot membership reads are snapshot-aware
  (typeMapGetAllSnapshot) so historical renders show historical slots;
  guard the remaining _collabNode dereferences.
- devtools-core: recurse into DecoratorNode children so a decorator
  host's slots are visible when reached through the children channel.
- playground: import rules no longer fabricate seeded "Title" content
  or drop a PullQuote's non-slot children (they land in the quote
  slot); any orphan slot wrapper closes the open rewrap run; chrome
  handlers are gated on editor.isEditable(); nested hosts' chrome is
  clickable (slot-wrapper bail only applies to the host's own slots);
  Tab from the title seeds an empty body paragraph when none exists;
  e2e slash-menu helpers use waitForSelector instead of sleep(300);
  CardSlotFix.spec.mjs renamed to CardSlot.spec.mjs; stale comments
  corrected.

API modernization (new code must use the current protocols):
- All node classes introduced by this commit's tests use the $config()
  protocol instead of legacy static getType/clone/importJSON
  (CardLikeNode, PlainShadowRootNode, ExcludedShadowRootNode,
  TestUpdateDOMTrueHostNode — the latter carrying its instance state via
  afterCloneFrom). The PR's other new classes (CardNode, PullQuoteNode,
  SlotContainerNode and the newer test nodes) already used $config().
- $getSlot drops its unchecked type parameter (the pattern deprecated by
  meta#8661) — as a brand-new @experimental API it returns LexicalNode | null
  and callers narrow with type guards; flow declaration updated and the
  explicit generic call sites converted.
- Every deprecated traversal type parameter in the PR's added code is
  scrubbed (62 call sites across LexicalSlot / LexicalSlotSelection /
  LexicalSlotDfs / SlotClipboardExport tests), using the same
  $assertNodeType(node, $isX) narrowing pattern meta#8667 applied to the
  rest of the test suite; $isTestShadowRootNode /
  $isTestUpdateDOMTrueHostNode guards added to the shared test utils.

Tests: 23 new regression tests across lexical, lexical-clipboard,
lexical-yjs (including real two-doc relay observer tests for first-set,
undo, hostile data, root slots, and the deletion-leak sweep),
lexical-utils, lexical-react, lexical-extension, lexical-devtools-core,
and the playground. Full unit suite: 169 files, 3965 passed. tsc, flow,
prettier, and eslint clean.

Intentionally not addressed here (need product/design decisions rather
than fixes): the SELECT_ALL shadow-root scoping behavior change (kept,
already e2e-pinned; recommend a separate PR + breaking-change note),
per-client slot Map ordering under concurrent distinct-name adds
(inherent to the Y.Map channel design), and a mixed-version rollout
note for pre-slots V1 clients.

https://claude.ai/code/session_0125UE7Cv1mUaM82UH8N1nJX
etrepum pushed a commit to etrepum/lexical that referenced this pull request Jun 12, 2026
…][lexical-react][lexical-extension][lexical-devtools-core][lexical-playground] Bug Fix: Named-slots audit fixes: collab first-set sync, in-slot copy/cut, export gating, slot cycle guard (meta#8603)

Fixes every issue from the named-slots audit of this PR, in one
cherry-pickable commit.

Critical:
- yjs V1: route a changed `slots` key in YTextEvent/YXmlEvent to
  syncSlotsFromYjs. The first $setSlot on a synced host (and its undo)
  integrates the slots Y.Map in the same transaction as its content, so
  no YMapEvent fires and the slot never reached peers; the follow-up
  peer edit then crashed on an undefined collab node or deleted the
  originator's slot from the shared doc.
- yjs V2: make $equalYTypePNode slots-aware (and exclude the `slots`
  attribute from the plain attribute comparison) so the dirty scan
  recurses instead of repointing the mapping — the originator's first
  $setSlot is now actually written to yjs.
- clipboard/html: walk the RangeSelection's slot frame (new core
  $getSlotFrame) in $generateJSONFromSelectedNodes and
  $generateDOMFromNodes. A selection wholly inside a slot never contains
  its host, so both exporters returned empty payloads and cut inside a
  slot was silent data loss.
- clipboard/html: gate the includeChildrenWhenSelected child promotion
  on NodeSelection. A partial RangeSelection over a host exported the
  full unsliced child text plus wholly unselected siblings on both
  channels.

Major:
- selection: insertNodes with the caret on an empty slot value seeds a
  paragraph and redirects instead of throwing "to have a block
  ancestor" (reachable via Ctrl+A in an empty slot, then paste).
- core: reverse cycle guard $errorOnSlotCycleChild in ElementNode.splice
  and insertBefore/insertAfter/replace. Closing a cycle through the
  children channel (slotValue.append(host)) previously hung the commit
  inside isAttached's up-walk.
- yjs V1: sync root-host slots (the root branch never called
  syncSlotsFromLexical, so $setSlot($getRoot(), ...) silently never
  synced).
- yjs V1/V2: validate-and-skip hostile or invalid remote slot entries
  (reserved names, non-shared-type values, missing __type, invalid slot
  value shapes, duplicate shared types under two names) instead of
  throwing inside the observer update; aliased entries are first-name-
  wins.
- yjs: reserve the `slots` attribute key in property sync in both
  directions for both versions; restored V1 hosts no longer carry a junk
  `slots` Y.Map property, and a user property named `slots` can no
  longer clobber the channel.
- yjs V1: sweep the transaction's deleted structs after remote events
  (mirroring V2) so a remote host deletion no longer leaks its slot
  values' collabNodeMap entries.
- playground: drop CardExtension's ClickAfterLastBlockExtension
  override. The last-wins config merge silently clobbered the app's
  $isCodeNode predicate, and CardNode.isShadowRoot() already satisfies
  $defaultShouldInsertAfter.

Minor:
- clipboard: the excluded-slot-value guard also verifies the exported
  node type, catching the 1-child excludeFromCopy case that silently
  exported the child as the slot value; declare `slots` on
  BaseSerializedNode.
- html: $appendNodeToHTML resolves the session DOM render config so
  disabledForSession overrides apply inside slot subtrees.
- core: $setSlot re-setting the same node under the same name is a
  no-op instead of tripping the already-slotted invariant.
- utils: $dfsWithSlots/$reverseDfsWithSlots stop before the endNode's
  slot subtrees (inclusive stop), document the in-slot endNode
  limitation, and carry @experimental tags.
- react: useLexicalSlot detaches the previously appended container on
  nodeKey/slotName change; useCharacterLimit advances the wrap budget
  past slotted decorator leaves so the overflow boundary lands exactly.
- extension: NodeSelectionDataSelectedExtension mirrors a NodeSelection
  already committed at registration, clears data-selected attributes on
  teardown, and raises an invariant for configured-but-unregistered
  classes.
- yjs V2: keep the (empty) slots Y.Map on remove-last to narrow the
  concurrent-creation window; slot membership reads are snapshot-aware
  (typeMapGetAllSnapshot) so historical renders show historical slots;
  guard the remaining _collabNode dereferences.
- devtools-core: recurse into DecoratorNode children so a decorator
  host's slots are visible when reached through the children channel.
- playground: import rules no longer fabricate seeded "Title" content
  or drop a PullQuote's non-slot children (they land in the quote
  slot); any orphan slot wrapper closes the open rewrap run; chrome
  handlers are gated on editor.isEditable(); nested hosts' chrome is
  clickable (slot-wrapper bail only applies to the host's own slots);
  Tab from the title seeds an empty body paragraph when none exists;
  e2e slash-menu helpers use waitForSelector instead of sleep(300);
  CardSlotFix.spec.mjs renamed to CardSlot.spec.mjs; stale comments
  corrected.

API modernization (new code must use the current protocols):
- All node classes introduced by this commit's tests use the $config()
  protocol instead of legacy static getType/clone/importJSON
  (CardLikeNode, PlainShadowRootNode, ExcludedShadowRootNode,
  TestUpdateDOMTrueHostNode — the latter carrying its instance state via
  afterCloneFrom). The PR's other new classes (CardNode, PullQuoteNode,
  SlotContainerNode and the newer test nodes) already used $config().
- $getSlot drops its unchecked type parameter (the pattern deprecated by
  meta#8661) — as a brand-new @experimental API it returns LexicalNode | null
  and callers narrow with type guards; flow declaration updated and the
  explicit generic call sites converted.
- Every deprecated traversal type parameter in the PR's added code is
  scrubbed (62 call sites across LexicalSlot / LexicalSlotSelection /
  LexicalSlotDfs / SlotClipboardExport tests), using the same
  $assertNodeType(node, $isX) narrowing pattern meta#8667 applied to the
  rest of the test suite; $isTestShadowRootNode /
  $isTestUpdateDOMTrueHostNode guards added to the shared test utils.

Tests: 23 new regression tests across lexical, lexical-clipboard,
lexical-yjs (including real two-doc relay observer tests for first-set,
undo, hostile data, root slots, and the deletion-leak sweep),
lexical-utils, lexical-react, lexical-extension, lexical-devtools-core,
and the playground. Full unit suite: 169 files, 3965 passed. tsc, flow,
prettier, and eslint clean.

Intentionally not addressed here (need product/design decisions rather
than fixes): the SELECT_ALL shadow-root scoping behavior change (kept,
already e2e-pinned; recommend a separate PR + breaking-change note),
per-client slot Map ordering under concurrent distinct-name adds
(inherent to the Y.Map channel design), and a mixed-version rollout
note for pre-slots V1 clients.

https://claude.ai/code/session_0125UE7Cv1mUaM82UH8N1nJX
@etrepum etrepum mentioned this pull request Jun 25, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — e8a0b363 Deployed Jun 10, 2026 by vercel[bot]
Preview – lexical-playground — e8a0b363 Deployed Jun 10, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants