Skip to content

[Breaking Changes][lexical] BC: allow importJSON() overrides to return null (regressed by #8640 $config port) - #8868

Closed
potatowagon wants to merge 2 commits into
mainfrom
potatowagon/fix-8640-importjson-nullable-bc
Closed

potatowagon wants to merge 2 commits into
mainfrom
potatowagon/fix-8640-importjson-nullable-bc

Conversation

@potatowagon

@potatowagon potatowagon commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

The problem this solves

A Lexical node can be non-JSON-serializable — an ephemeral or decorative node that exists in the editor tree but has no meaningful JSON representation. The established way to express that, since well before the $config() protocol, is to override importJSON() to return null:

// "This node type has no JSON representation."
static importJSON(_serializedNode: SerializedElementNode): ?MyNode {
  return null;
}

#8640 (the $config() port) broke this pattern. It made the base contract non-nullable — the base/auto-synthesized importJSON is now typed (): LexicalNode, and the generated Flow stub declares static importJSON(serializedNode: $FlowFixMe): LexicalNode. A subclass that returns null no longer typechecks against the stricter base, in both TS and Flow. There is no BC shim and no documented alternative.

Concrete breakage (real consumer)

In Meta's www, CaaSLexicalInsertLineNode (@flow strict-local, extends ElementNode) is exactly this kind of node — an insert-line marker that renders as <hr> and is intentionally never serialized:

static importJSON(_serializedNode: SerializedElementNode): ?CaaSLexicalInsertLineNode {
  return null;
}

Once #8640's non-nullable contract reaches the vendored Flow stub, this override fails flow:

Cannot extend ElementNode with CaaSLexicalInsertLineNode because in property
importJSON the return value: null or undefined is incompatible with LexicalNode.

@flow strict-local has no $FlowFixMe escape hatch, so the node cannot be patched locally without weakening its type mode. This is a backward-compatibility regression in Lexical's public API, and the pre-#8640 pattern needs to keep working (or have a sanctioned replacement).

The fix

Restore the pre-#8640 contract: importJSON() overrides may return null again.

  • LexicalNode.importJSON(...) return type widened back to LexicalNode | null.
  • StaticNodeConfigValue.$importJSON helper widened to => T | null to mirror it.
  • Hand-written Flow stub (flow/Lexical.js.flow) base declaration widened to static importJSON(serializedNode: $FlowFixMe): LexicalNode | null — this is the declaration www Flow consumers actually resolve against.
  • Internal deserializer ($parseSerializedNodeImpl) now invariants that a registered node's importJSON did not return null, with a clear message. A node that opts out of JSON import is, by definition, never serialized into the EditorState being parsed — so that path is unreachable for such nodes at runtime, and the internal caller stays non-null and fully type-safe.

This is a pure widening of the return type: existing overrides that return a concrete node remain assignable; only code that immediately dereferences a direct Node.importJSON(json) call must now account for | null (the pre-#8640 contract already required this).

Tradeoff / note for maintainers

Widening the base return does make the common path slightly stricter for callers that call SomeNode.importJSON(json) directly and dereference the result immediately. If you'd prefer not to loosen the common path, the alternative is a sanctioned "non-importable node" API/pattern with a documented migration — happy to reshape this PR that way. Either way, the public API currently offers neither a BC shim nor a documented path for opting a node out of JSON import.

Test plan

  • Added a regression test in LexicalNode.test.ts (importJSON() override may return null to opt out of JSON import (BC #8640)): a node overriding importJSON to return null typechecks and returns null at runtime without throwing.
  • tsc -p ./tsconfig.json --noEmit — clean.
  • check-flow-types — No errors!.
  • vitest --project unit for LexicalNode.test + LexicalNodeState.test — 220/220 pass.

References

Scope

importJSON nullability BC only. The separate getType() recursion regressed by #8867 under compiled output is fixed independently in its own follow-up PR (#8869) (touches disjoint code — the $config() synthesis runtime, not the importJSON contract).

@vercel

vercel Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 21, 2026 9:26am
lexical-playground Ready Ready Preview Jul 21, 2026 9:26am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 21, 2026
etrepum
etrepum previously approved these changes Jul 21, 2026
'out of JSON import (by overriding importJSON to return null) must not ' +
'be serialized into the EditorState it is being parsed from.',
nodeClass.name,
);

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.

Given this implementation, there's no difference between returning null or using an invariant(false, '…') as the implementation. It's probably not worth the type churn to do that. It's also generally not sufficient to include just the class name since it's probably mangled, typically the type is also very useful debugging information.

@etrepum
etrepum dismissed their stale review July 21, 2026 15:50

Either null should have semantics or this PR can be replaced with a behavior change to use invariant instead.

@potatowagon

Copy link
Copy Markdown
Contributor Author

Closing this — after digging into the actual usage, the nullable importJSON() contract isn't worth restoring.

Data:

  • Across Meta's entire www codebase, exactly one node overrode importJSON() to return null (an internal CaaSLexicalInsertLineNode). No other real consumer does this.
  • The Lexical repo itself has zero nodes using the pattern, and the base implementation invariant(false, 'Node %s does not implement .importJSON()') deliberately throws — a registered node is expected to be importable.
  • The one internal node even had an exportJSON() emitting a valid SerializedElementNode, so it wasn't genuinely non-serializable — the return null was effectively a no-op quirk that happened to typecheck before the $config() port ([Breaking Changes][lexical] Refactor: Port node classes to the $config() protocol #8640), not an intended API contract.

@etrepum's point on the deserializer change was the same conclusion from the other side: there's no meaningful difference between returning null and invariant(false, …), so it's not worth the type churn.

Resolution: the single internal consumer was migrated to the standard construct + updateFromJSON pattern (matching every other node), which is strictly more correct than returning null. Adding a sanctioned "non-importable node" API for one anomalous case isn't justified.

Thanks for the review. The genuinely-needed follow-up from this line of work was the getType() recursion fix in #8869 (merged), which addressed a real stack-overflow hitting $config({extends}) nodes under compiled output.

This branch was successfully deployed

2 active deployments
Preview – lexical — d60c96dc Deployed Jul 21, 2026 by vercel[bot]
Preview – lexical-playground — d60c96dc Deployed Jul 21, 2026 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants