lexical icon indicating copy to clipboard operation
lexical copied to clipboard

[lexical] Feature: error checking for node key re-use with type mismatch in __DEV__

Open etrepum opened this issue 3 months ago • 1 comments

Description

Based on issues that several people have run into, it seems useful to add some additional invariant checking around node keys.

See also: discord message

  • Adds a stub implementation of shared/invariant for use under jest to make development easier
  • Adds a __DEV__ check to make sure that if a new Node is created with an explicit existing key, that the type of the node did not change

Test plan

Before

Bad things will silently happen if you accidentally re-use node keys, such as with a subtly incorrect node replacement function:

const config = {
  // ...
  nodes: [
    EdifactTextNode,
    {
      replace: TextNode,
      with: (node) => {
        return new EdifactTextNode(node.text, node.__key)
      },
    },
  ],
}

After

An error message is shows up now, in __DEV__. There are unit tests that verify that the specific error message comes up when creating a new node in this way (tested in isolation and with a node replacer).

etrepum avatar May 02 '24 22:05 etrepum

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 3, 2024 3:46pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 3, 2024 3:46pm

vercel[bot] avatar May 02 '24 22:05 vercel[bot]

I have a small concern about the size here - would be nice to be able to tree-shake this stuff out of the prod build.

acywatson avatar May 11 '24 17:05 acywatson

I have a small concern about the size here - would be nice to be able to tree-shake this stuff out of the prod build.

Discussed offline - we'll follow up and ensure that we're doing what we can to shake out these DEV things in rollup

acywatson avatar May 11 '24 18:05 acywatson

Just for posterity's sake I did confirm that unused functions that do not get exported are tree-shaken correctly in both the .mjs and .js prod builds at this time. Had this functionality not worked, it would also show up elsewhere (e.g. handleDEVOnlyPendingUpdateGuarantees is only used under __DEV__ in LexicalUpdates.ts)

etrepum avatar May 11 '24 18:05 etrepum