From 41f1939970295bfda7988ccf20d170016c861934 Mon Sep 17 00:00:00 2001 From: Jim Fisher Date: Thu, 6 Jan 2022 00:44:48 +0000 Subject: [PATCH] Warn when normalization removes node (#4769) * Warn when normalization removes node Slate requires the invariant that children are all blocks or all inlines. It enforces this in default normalization by removing children. When such a node is removed, it's almost certainly due to a programming error: the developer needs to fix their application to ensure it maintains this invariant. But currently Slate does not tell the developer this. I made such a programming error, and spent a long time debugging nodes that mysteriously went missing. I would have fixed it within 30 seconds if Slate had warned me when it detected this error. (Note I have used console.warn despite the eslint rule. As far as I can see, Slate has no other facility for runtime warnings.) * Add changeset --- .changeset/wild-penguins-shout.md | 5 +++++ packages/slate/src/create-editor.ts | 8 ++++++++ 2 files changed, 13 insertions(+) create mode 100644 .changeset/wild-penguins-shout.md diff --git a/.changeset/wild-penguins-shout.md b/.changeset/wild-penguins-shout.md new file mode 100644 index 00000000000..ff78829fd78 --- /dev/null +++ b/.changeset/wild-penguins-shout.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +Warn when normalization removes node diff --git a/packages/slate/src/create-editor.ts b/packages/slate/src/create-editor.ts index b1a903b5eb6..59ff2324d7d 100644 --- a/packages/slate/src/create-editor.ts +++ b/packages/slate/src/create-editor.ts @@ -227,6 +227,14 @@ export const createEditor = (): Editor => { // other inline nodes, or parent blocks that only contain inlines and // text. if (isInlineOrText !== shouldHaveInlines) { + // eslint-disable-next-line no-console + console.warn( + `Removing ${ + isInlineOrText ? 'inline' : 'block' + } node at path ${path.concat(n)} because parent expects ${ + shouldHaveInlines ? 'inline' : 'block' + } children` + ) Transforms.removeNodes(editor, { at: path.concat(n), voids: true }) n-- } else if (Element.isElement(child)) {