Repository navigation
[Breaking Changes][lexical] BC: allow importJSON() overrides to return null (regressed by #8640 $config port) - #8868
potatowagon wants to merge 2 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| 'out of JSON import (by overriding importJSON to return null) must not ' + | ||
| 'be serialized into the EditorState it is being parsed from.', | ||
| nodeClass.name, | ||
| ); |
There was a problem hiding this comment.
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.
Either null should have semantics or this PR can be replaced with a behavior change to use invariant instead.
|
Closing this — after digging into the actual usage, the nullable Data:
@etrepum's point on the deserializer change was the same conclusion from the other side: there's no meaningful difference between returning Resolution: the single internal consumer was migrated to the standard Thanks for the review. The genuinely-needed follow-up from this line of work was the |
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 overrideimportJSON()to returnnull:#8640 (the
$config()port) broke this pattern. It made the base contract non-nullable — the base/auto-synthesizedimportJSONis now typed(): LexicalNode, and the generated Flow stub declaresstatic importJSON(serializedNode: $FlowFixMe): LexicalNode. A subclass that returnsnullno 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, extendsElementNode) is exactly this kind of node — an insert-line marker that renders as<hr>and is intentionally never serialized:Once #8640's non-nullable contract reaches the vendored Flow stub, this override fails
flow:@flow strict-localhas no$FlowFixMeescape 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 returnnullagain.LexicalNode.importJSON(...)return type widened back toLexicalNode | null.StaticNodeConfigValue.$importJSONhelper widened to=> T | nullto mirror it.flow/Lexical.js.flow) base declaration widened tostatic importJSON(serializedNode: $FlowFixMe): LexicalNode | null— this is the declarationwwwFlow consumers actually resolve against.$parseSerializedNodeImpl) nowinvariants that a registered node'simportJSONdid not returnnull, with a clear message. A node that opts out of JSON import is, by definition, never serialized into theEditorStatebeing 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
LexicalNode.test.ts(importJSON() override may return null to opt out of JSON import (BC #8640)): a node overridingimportJSONto returnnulltypechecks and returnsnullat runtime without throwing.tsc -p ./tsconfig.json --noEmit— clean.check-flow-types—No errors!.vitest --project unitforLexicalNode.test+LexicalNodeState.test— 220/220 pass.References
$config()port (the regressing change)clone()BC fix (landed)getType()collision fix (landed)Scope
importJSONnullability BC only. The separategetType()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).