Skip to content

[lexical][lexical-code] Refactor: Use NodeCaret to implement RangeSelection.getNodes() - #7135

Merged
etrepum merged 99 commits into
meta:mainfrom
etrepum:lexical-caret-getNodes
Feb 24, 2025
Merged

etrepum merged 99 commits into
meta:mainfrom
etrepum:lexical-caret-getNodes

Conversation

@etrepum

@etrepum etrepum commented Feb 4, 2025 •

Copy link
Copy Markdown
Collaborator

Description

Refactor RangeSelection.getNodes() and lexical-code getEndOfCodeInLine / getStartOfCodeInLine to use the new NodeCaret API internally.

Adds new APIs:

  • $comparePointCaretNext (finally we can differentiate between a point being outside and inside an element at its border)
  • $getCommonAncestor (determine if two nodes are the same, an ancestor/descendant of each other, or on separate branches. In the branch case it returns the common ancestor and the two siblings corresponding to the given points for efficient isBefore/isAfter)
  • $getCommonAncestorResultBranchOrder (the computation to determine which sibling comes first isn't computed eagerly, you do that here)

Deprecations in lexical-code due to $ rule violations:

  • getEndOfCodeInLine -> $getEndOfCodeInLine
  • getStartOfCodeInLine -> $getEndOfCodeInLine

(commit history is long as it was stacked on #7046 and #7132 and not rebased)

Closes #6310 - it's not really possible to change the existing APIs to return coherent results, but now we have new APIs that can be used instead

Test Plan

All existing unit and e2e tests pass. No expected behavior changes. Development of the getNodes() refactor was backed by an invariant that threw any time the previous and new implementations returned a different result, but that code has been removed after the whole suite was verified.

@etrepum etrepum changed the title [DO NOT MERGE] WIP NodeCaret RangeSelection.getNodes() [lexical][lexical-code] Refactor: Use NodeCaret to implement RangeSelection.getNodes() Feb 19, 2025
@etrepum
etrepum marked this pull request as ready for review February 19, 2025 03:36

@ivailop7 ivailop7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Approving to unblock, because I see it's been 3 weeks, ideally @fantactuka and @zurfyx would be nice to give this a read too.

@etrepum

etrepum commented Feb 23, 2025

Copy link
Copy Markdown
Collaborator Author

To be fair it wasn't marked for review until 5 days ago

@etrepum
etrepum added this pull request to the merge queue Feb 24, 2025
Merged via the queue into meta:main with commit 322870e Feb 24, 2025
mayrang added a commit to mayrang/lexical that referenced this pull request Sep 26, 2026
Removes the instance method entirely rather than layering a second
deprecation notice on it, per maintainer review.

$getCommonAncestor has been the replacement since meta#7135. The method has
no in-repo callers; its unit test block is dropped because
$getCommonAncestor carries its own coverage in LexicalCaret.test.ts.

Migration is not a direct substitution: the removed method lifted each
operand to an ElementNode before delegating, and $getCommonAncestor
returns the node itself when both arguments are the same node.

  const aElement = $isElementNode(a) ? a : a.getParent();
  const bElement = $isElementNode(b) ? b : b.getParent();
  const ancestor =
    aElement && bElement
      ? ($getCommonAncestor(aElement, bElement)?.commonAncestor ?? null)
      : null;

Co-Authored-By: Claude Opus 5 <[email protected]>
mayrang added a commit to mayrang/lexical that referenced this pull request Sep 26, 2026
## Description

`LexicalNode.prototype.getCommonAncestor` is removed entirely, both the
generic and the non-generic form, along with its Flow declaration and its
unit test block.

Current behavior: the method exists and returns `ElementNode | null`, with
a type parameter that is an unchecked cast. The preceding revision of this
PR deprecated that type parameter. Per review, layering a second
deprecation notice on an already-superseded method is worse than removing
it, so the method is dropped instead.

`$getCommonAncestor` (meta#7135) is the replacement and has been available
since February 2025. No in-repo callers remain. The other type-parameter
deprecations from meta#8661 stay as they are — this is the only one of that set
with a full `$` replacement.

AGENTS.md ordinarily forbids removing public APIs; this removal is an
intentional exception at maintainer direction.

Migration is not a direct substitution. The removed method lifted each
operand to an `ElementNode` before delegating, and `$getCommonAncestor`
returns the node itself when both arguments are the same node:

    const aElement = $isElementNode(a) ? a : a.getParent();
    const bElement = $isElementNode(b) ? b : b.getParent();
    const ancestor =
      aElement && bElement
        ? ($getCommonAncestor(aElement, bElement)?.commonAncestor ?? null)
        : null;

Callers that passed a type argument should narrow with a type guard.

## Test plan

### Before

    $ git grep -n 'getCommonAncestor' c23a367 -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    packages/lexical/flow/Lexical.js.flow:584:  getCommonAncestor<T extends ElementNode>(node: LexicalNode): T | null;
    packages/lexical/src/LexicalNode.ts:1506:  getCommonAncestor<T extends ElementNode = ElementNode>(

### After

    $ git grep -n 'getCommonAncestor' HEAD -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    (no matches)

    $ npx tsc --noEmit
    (no output, exit 0)

    $ pnpm run test-unit
     Test Files  2 failed | 351 passed (353)
          Tests  3 failed | 8613 passed | 1 skipped (8617)
       Duration  27.05s

     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a flat install
     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a pnpm install
     FAIL  |unit| packages/lexical-compiler/src/__tests__/unit/subpathImports.test.ts > subpathImports > refreshes mappings and invalidates cached consumers in a Vite dev server

The same three failures reproduce with this change stashed, so they are
pre-existing and unrelated:

    $ git stash && npx vitest --no-watch run typedocExternalLinks.test subpathImports.test
     Test Files  2 failed (2)
          Tests  3 failed | 69 passed (72)

`pnpm run build` and `pnpm run build-types` both exit 0, and the generated
`.d.ts` no longer declares the method. `pnpm run lint-flow` is unchanged
from baseline (62 warnings, 0 errors, none referencing this method).

Browser and e2e matrix jobs were not exercised locally; they run in CI.

Co-Authored-By: Claude Opus 5 <[email protected]>
mayrang added a commit to mayrang/lexical that referenced this pull request Sep 26, 2026
## Description

`LexicalNode.prototype.getCommonAncestor` is removed entirely, both the
generic and the non-generic form, along with its Flow declaration and its
unit test block.

Current behavior: the method exists and returns `ElementNode | null`, with
a type parameter that is an unchecked cast. The preceding revision of this
PR deprecated that type parameter. Per review, layering a second
deprecation notice on an already-superseded method is worse than removing
it, so the method is dropped instead.

`$getCommonAncestor` (meta#7135) is the replacement and has been available
since February 2025. No in-repo callers remain. The other type-parameter
deprecations from meta#8661 stay as they are — this is the only one of that set
with a full `$` replacement.

AGENTS.md ordinarily forbids removing public APIs; this removal is an
intentional exception at maintainer direction.

Migration is not a direct substitution. The removed method lifted each
operand to an `ElementNode` before delegating, and when both arguments are
the same node `$getCommonAncestor` returns a result whose `commonAncestor`
property is that node rather than its parent:

    const aElement = $isElementNode(a) ? a : a.getParent();
    const bElement = $isElementNode(b) ? b : b.getParent();
    const ancestor =
      aElement && bElement
        ? ($getCommonAncestor(aElement, bElement)?.commonAncestor ?? null)
        : null;

Callers that passed a type argument should narrow with a type guard.

## Test plan

### Before

    $ git grep -nE '^[[:space:]]*getCommonAncestor[<(]' c23a367 -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    c23a367:packages/lexical/flow/Lexical.js.flow:584:  getCommonAncestor<T extends ElementNode>(node: LexicalNode): T | null;
    c23a367:packages/lexical/src/LexicalNode.ts:1506:  getCommonAncestor<T extends ElementNode = ElementNode>(

### After

    $ git grep -nE '^[[:space:]]*getCommonAncestor[<(]' HEAD -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    $ echo $?
    1

    $ npx tsc --noEmit
    $ echo $?
    0

    $ pnpm run test-unit
     Test Files  2 failed | 351 passed (353)
          Tests  3 failed | 8613 passed | 1 skipped (8617)
       Duration  27.05s

     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a flat install
     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a pnpm install
     FAIL  |unit| packages/lexical-compiler/src/__tests__/unit/subpathImports.test.ts > subpathImports > refreshes mappings and invalidates cached consumers in a Vite dev server

The same three failures reproduce with this change stashed, so they are
pre-existing and unrelated:

    $ git stash && npx vitest --no-watch run typedocExternalLinks.test subpathImports.test
     Test Files  2 failed (2)
          Tests  3 failed | 69 passed (72)

`pnpm run build` and `pnpm run build-types` both exit 0, and the generated
`.d.ts` no longer declares the method. `pnpm run lint-flow` is unchanged
from baseline (62 warnings, 0 errors, none referencing this method).

Browser and e2e matrix jobs were not exercised locally; they run in CI.

Co-Authored-By: Claude Opus 5 <[email protected]>
mayrang added a commit to mayrang/lexical that referenced this pull request Sep 26, 2026
## Description

`LexicalNode.prototype.getCommonAncestor` is removed entirely, both the
generic and the non-generic form, along with its Flow declaration and its
unit test block.

Current behavior: the method exists and returns `ElementNode | null`, with
a type parameter that is an unchecked cast. The preceding revision of this
PR deprecated that type parameter; per review
(meta#9238 (comment))
the method is removed instead, since layering a second deprecation notice
on an already-superseded method is worse than dropping it.

`$getCommonAncestor` (meta#7135, February 2025) is the replacement. No in-repo
callers remain. meta#8661 deprecated the unsafe type parameters on the node
traversal methods but did not cover this one; those deprecations stay as
they are, because `getCommonAncestor` is the only method of that shape
with a full `$` replacement.

AGENTS.md ordinarily forbids removing public APIs; this removal is an
intentional exception at maintainer direction, linked above.

Migration is not a direct substitution. For a non-element node the removed
method lifted the operand to its parent before delegating, whereas
`$getCommonAncestor` compares the nodes as given and, when both arguments
are the same node, returns a result whose `commonAncestor` is that node.
For an `ElementNode` argument the two already agree.

    const aElement = $isElementNode(a) ? a : a.getParent();
    const bElement = $isElementNode(b) ? b : b.getParent();
    const ancestor =
      aElement && bElement
        ? ($getCommonAncestor(aElement, bElement)?.commonAncestor ?? null)
        : null;

Callers that passed a type argument should narrow with a type guard.

## Test plan

### Before

    $ git grep -nE '^[[:space:]]*getCommonAncestor[<(]' c23a367 -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    c23a367:packages/lexical/flow/Lexical.js.flow:584:  getCommonAncestor<T extends ElementNode>(node: LexicalNode): T | null;
    c23a367:packages/lexical/src/LexicalNode.ts:1506:  getCommonAncestor<T extends ElementNode = ElementNode>(

### After

    $ git grep -nE '^[[:space:]]*getCommonAncestor[<(]' HEAD -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    $ echo $?
    1

    $ npx tsc --noEmit
    $ echo $?
    0

    $ pnpm run test-unit
     Test Files  2 failed | 351 passed (353)
          Tests  3 failed | 8613 passed | 1 skipped (8617)
       Duration  27.05s

     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a flat install
     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a pnpm install
     FAIL  |unit| packages/lexical-compiler/src/__tests__/unit/subpathImports.test.ts > subpathImports > refreshes mappings and invalidates cached consumers in a Vite dev server

Those three are pre-existing. Restoring the three touched files to the
merge-base and rerunning the same two files reproduces them:

    $ git checkout c23a367 -- packages/lexical/src/LexicalNode.ts \
        packages/lexical/flow/Lexical.js.flow \
        packages/lexical/src/__tests__/unit/LexicalNode.test.ts
    $ npx vitest --no-watch run typedocExternalLinks.test subpathImports.test
     Test Files  2 failed (2)
          Tests  3 failed | 69 passed (72)

`pnpm run build` and `pnpm run build-types` both exit 0, and the generated
`.d.ts` no longer declares the method. `pnpm run lint-flow` reports 62
warnings and 0 errors, none referencing this method, matching the
merge-base.

Browser and e2e matrix jobs were not exercised locally; they run in CI.

Co-Authored-By: Claude Opus 5 <[email protected]>
mayrang added a commit to mayrang/lexical that referenced this pull request Sep 26, 2026
## Description

`LexicalNode.prototype.getCommonAncestor` is removed entirely, both the
generic and the non-generic form, along with its Flow declaration and its
unit test block.

Current behavior: the method exists and returns `ElementNode | null`, with
a type parameter that is an unchecked cast. The preceding revision of this
PR deprecated that type parameter; per review
(meta#9238 (comment))
the method is removed instead, since layering a second deprecation notice
on an already-superseded method is worse than dropping it.

`$getCommonAncestor` (meta#7135, February 2025) is the replacement. No in-repo
callers remain. meta#8661 deprecated the unsafe type parameters on the node
traversal methods but did not cover this one; those deprecations stay as
they are, because `getCommonAncestor` is the only method of that shape
with a full `$` replacement.

AGENTS.md ordinarily forbids removing public APIs; this removal is an
intentional exception at maintainer direction, linked above.

Migration is not a direct substitution. For a non-element node the removed
method lifted the operand to its parent before delegating, whereas
`$getCommonAncestor` compares the nodes as given and, when both arguments
are the same node, returns a result whose `commonAncestor` is that node.
For an `ElementNode` argument the two already agree.

    const aElement = $isElementNode(a) ? a : a.getParent();
    const bElement = $isElementNode(b) ? b : b.getParent();
    const ancestor =
      aElement && bElement
        ? ($getCommonAncestor(aElement, bElement)?.commonAncestor ?? null)
        : null;

Callers that passed a type argument should narrow with a type guard.

## Test plan

### Before

    $ git grep -nE '^[[:space:]]*getCommonAncestor[<(]' c23a367 -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    c23a367:packages/lexical/flow/Lexical.js.flow:584:  getCommonAncestor<T extends ElementNode>(node: LexicalNode): T | null;
    c23a367:packages/lexical/src/LexicalNode.ts:1506:  getCommonAncestor<T extends ElementNode = ElementNode>(

### After

    $ git grep -nE '^[[:space:]]*getCommonAncestor[<(]' HEAD -- \
        packages/lexical/src/LexicalNode.ts packages/lexical/flow/Lexical.js.flow
    $ echo $?
    1

    $ npx tsc --noEmit
    $ echo $?
    0

    $ pnpm run test-unit
     Test Files  2 failed | 351 passed (353)
          Tests  3 failed | 8613 passed | 1 skipped (8617)
       Duration  27.05s

     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a flat install
     FAIL  |scripts-unit| scripts/__tests__/unit/typedocExternalLinks.test.ts > links external types and sources with a pnpm install
     FAIL  |unit| packages/lexical-compiler/src/__tests__/unit/subpathImports.test.ts > subpathImports > refreshes mappings and invalidates cached consumers in a Vite dev server

Those three are pre-existing. Restoring the three touched files to the
merge-base and rerunning the same two files reproduces them:

    $ git checkout c23a367 -- packages/lexical/src/LexicalNode.ts \
        packages/lexical/flow/Lexical.js.flow \
        packages/lexical/src/__tests__/unit/LexicalNode.test.ts
    $ npx vitest --no-watch run typedocExternalLinks.test subpathImports.test
     Test Files  2 failed (2)
          Tests  3 failed | 69 passed (72)
    $ git restore --source=HEAD --staged --worktree -- \
        packages/lexical/src/LexicalNode.ts \
        packages/lexical/flow/Lexical.js.flow \
        packages/lexical/src/__tests__/unit/LexicalNode.test.ts
    $ git status --porcelain
    $ echo $?
    0

The build checks below were run after that restore, on a clean tree.

`pnpm run build` and `pnpm run build-types` both exit 0, and the generated
`.d.ts` no longer declares the method. `pnpm run lint-flow` reports 62
warnings and 0 errors, none referencing this method, matching the
merge-base.

Browser and e2e matrix jobs were not exercised locally; they run in CI.

Co-Authored-By: Claude Opus 5 <[email protected]>

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 203ed362 Deployed Feb 19, 2025 by vercel[bot]
Preview – lexical — 203ed362 Deployed Feb 19, 2025 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants