Repository navigation
[lexical][lexical-code] Refactor: Use NodeCaret to implement RangeSelection.getNodes() - #7135
Merged
Merged
Conversation
etrepum
marked this pull request as ready for review
February 19, 2025 03:36
etrepum
requested review from
Fetz,
Sahejkm,
acywatson,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
February 19, 2025 03:36
ivailop7
approved these changes
Feb 23, 2025
ivailop7
left a comment
Collaborator
There was a problem hiding this comment.
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.
Collaborator
Author
|
To be fair it wasn't marked for review until 5 days ago |
zurfyx
approved these changes
Feb 23, 2025
This was referenced Feb 27, 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Refactor RangeSelection.getNodes() and lexical-code getEndOfCodeInLine / getStartOfCodeInLine to use the new NodeCaret API internally.
Adds new APIs:
Deprecations in lexical-code due to $ rule violations:
(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.