Skip to content

Location type discriminators#6000

Merged
dylans merged 4 commits intoianstormtaylor:mainfrom
nabbydude:location-is
Jan 10, 2026
Merged

Location type discriminators#6000
dylans merged 4 commits intoianstormtaylor:mainfrom
nabbydude:location-is

Conversation

@nabbydude
Copy link
Contributor

Description
Adds Location.isPath, Location.isPoint, Location.isRange, and also Location.isSpan as type-aware discriminators.
Essentially #5982 but for Locations. (Except I had the decency to include unit tests this time.)

Also fixes Path.isPath to correctly reject arrays containing a mix of numbers and other values and adds a unit test for it.

Issue
Performance improvement. Differentiating between Locations doesn't need exhaustive type verification.

Context
Spans are not technically Locations (they are location-like but only accepted by Editor#nodes, which is only passed a Span by Editor#previous and Editor#next--I'm uncertain if its used by any users or even meant to be) but I figured they should be included in this. Location.isSpan accepts arguments of type Location | Span whereas the others accept solely Location so it needs to be checked for first if its a possibility.

In theory if a user implements a custom extension to either Point or Range that implements only some of the properties of the other then some false positives could be thrown, but that would be an insane thing to do I think.

Unfortunately because of one (1) call in each of slate-dom and slate-react this needs a peerDependency bump 😅 If I should add it to this PR just lmk

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

@changeset-bot
Copy link

changeset-bot bot commented Jan 10, 2026

🦋 Changeset detected

Latest commit: 660c4b9

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

This PR includes changesets to release 3 packages
Name Type
slate Minor
slate-react Minor
slate-dom Minor

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

@dylans dylans merged commit 8d9bf30 into ianstormtaylor:main Jan 10, 2026
11 checks passed
@github-actions github-actions bot mentioned this pull request Jan 10, 2026
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