Repository navigation
fix(core): include JSX imports from nested editors - #989
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Import generation has unresolved correctness issues that can produce invalid MDX.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Addresses #733 by extending MDXEditor’s export pipeline to generate root imports for JSX inside directives and tables.
Changes:
- Collect component references from exported MDAST subtrees.
- Suppress newly generated imports during table-cell export.
- Add regression tests and an interactive reproduction example.
| File | Description |
|---|---|
tests/browser/nested-jsx-imports.spec.ts |
Tests insertion, import deduplication, and save/reload. |
src/test/nested-jsx-imports.test.tsx |
Tests subtree traversal and import selection. |
src/test/nested-jsx-imports-integration.test.tsx |
Tests nested-editor and table-cell exports. |
src/plugins/table/TableEditor.tsx |
Disables fresh imports during cell export. |
src/exportMarkdownFromLexical.ts |
Collects descriptor-backed JSX references from MDAST. |
src/examples/bug-733.tsx |
Adds an interactive nested-JSX reproduction. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
petyosi
left a comment
There was a problem hiding this comment.
Two export regressions need fixes before merging. The fragment failure was reproduced with the exporter and a minimal visitor on main and this revision. Full test suites were not run because dependencies were not installed.
petyosi
left a comment
There was a problem hiding this comment.
Thanks for picking this up. The admonition case from #733 now gets its import, and the TableEditor change fixes a real bug where a cell with a source-backed component was wiped. Some documents that round-trip on main now break, though.
1. Nested member-expression and non-HTML lowercase tags produce invalid imports (src/exportMarkdownFromLexical.ts, registerNestedComponents)
The new walk registers node.name as-is. With a wildcard descriptor { name: '*', source: '@fallback' }:
| Input | main | this PR |
|---|---|---|
:::tip\n<UI.Badge />\n::: |
unchanged, parses | import { UI.Badge } from '@fallback', MDX parse error |
:::tip\n<my-element />\n::: |
parses | import { my-element } …, parse error |
<UI.Badge /> in a table cell |
parses | parse error |
<svg /> / <foo /> in a directive |
no import | import { svg } / import { foo } |
Once saved, the editor can't load the document again. Root-level <UI.Badge /> already has this problem on main, but this change spreads it to directives and tables. Please use one identifier predicate for both registration paths (this walk and LexicalJsxVisitor.traverseNestedJsxNodes), e.g. only register names matching /^[A-Z_$][\w$]*$/, or the root object of a member expression. That also replaces the img special case. Currently <Outer><img /></Outer> at root still emits import { Outer, img } through the visitor path.
2. Root imports used only in nested content get the wildcard source
import { Foo } from './custom'\n\n:::tip\n<Foo />\n::: with a wildcard @fallback now exports import { Foo } from '@fallback'. With import { Foo as Bar } from './x' and nested <Bar />, it imports a name that doesn't exist. Main drops the import; this PR replaces it with a wrong one, silently. The root ESM is discarded at import time, so nested nodes have no importStatement to carry the original source. Please either carry the root import declarations through to nested JSX nodes, or skip registration when the source can't be resolved, rather than falling back to the wildcard.
3. Export cost
Each mdxjsEsm node without ESTree data is parsed again (micromark + acorn) on every root export, and that runs on every update. With 100 directives carrying ESM, exportLexicalTreeToMdast goes from ~0.7 ms to ~6.4 ms. Caching the fallback parse by node.value fixes this.
Smaller things
- Most of the 74 unit cases are a 14 × 2 × 2 matrix of export-declaration shapes. Together with
collectPatternBindings, that's more ESM coverage than the editor needs. A handful of representative shapes plus the integration tests would be easier to maintain. - Please also check that a table cell holding a JSX node with an existing
importStatement(e.g. pasted from root) doesn't put that import atchildren[0]and wipe the cell, sinceaddImportStatements: falseonly blocks new imports.
Maintainer review identified overlapping declaration coverage in nested JSX import tests. Combine representative binding and re-export cases while retaining exact and wildcard descriptor checks. Preserve parser metadata and reconstructed ESM coverage without changing production or integration tests.
|
Follow-up on the October 13 review, verified against current head ca64d4c:
Validation: 250 unit/integration tests passed (70 nested-JSX cases), with the existing 1 skipped and 1 todo; typecheck, lint, and build passed. Current upstream CI passes Build JS, Build Docs, and Test. Browser compatibility is still red: 140/141 passed, including all 12 nested-JSX cases. The sole Firefox linked-image failure occurs in setup while waiting for the initial image, before any link interaction. The artifact trace shows slow picsum.photos resolution/redirect and unfinished fastly.picsum.photos image requests, consistent with external image-loading latency. Production code and browser fixtures are unchanged from the previous all-green head; no timeout or assertion was weakened. I attempted a failed-job rerun, but GitHub returned 403 “Resource not accessible by integration”, so no rerun started. Could someone with Actions permission rerun that failed job? The PR description now reflects the current head and this outstanding CI result. |
The linked-image setup can time out while Picsum redirects or image responses remain pending. Fulfill the two existing image URLs with a fixed SVG before navigating to the story. Keep the original URLs, link assertions, and timeout while removing external image availability from the test.
|
Fixed the linked-image setup in 44cbc3d85bc80bca3dd98eacee8837a5462c03e9. The two existing Picsum URLs now receive a fixed 200×300 SVG before navigation, following the table-image test fixture pattern. All original URLs, link/Markdown assertions, and timeouts remain unchanged; no production code changed. The unchanged test passed once locally. Holding the image requests pending then reproduced the same 5-second setup failure seen in the upstream trace. After the fix, both original scenarios and the temporary stalled-network diagnostic passed in Chromium, Firefox, and WebKit. The diagnostic is excluded from the commit. Exact-head CI run 39464626931 has completed:
The unchanged The PR is conflict-free, but CI remains red and the review state is still |
petyosi
left a comment
There was a problem hiding this comment.
Thanks for the follow-up. The identifier guard and carrying root imports through nested editors both work now: UI.Badge, my-element, svg, aliases and namespace imports in directives and table cells all round-trip. What's left:
1. Import blocks are now re-emitted verbatim. importText stores the whole mdxjsEsm node, and consecutive ESM lines form one node. So import { A } from './a'\nimport { B } from './b'\n\n<A /> now keeps the unused B, and import { A } from './a'\nexport const meta = 1\n\n<A /> keeps the export. Main drops both. Whether a declaration survives now depends on whether it happens to share a block with a used import. Please carry structured data per specifier (source, imported name, local name, default/namespace) instead of the raw text, and rebuild only the specifiers in use. That also removes the need for the fallback MDX parse, esmParseCache and collectPatternBindings.
2. Local exports in nested content are only protected until the first edit. Take a directive that contains export const Existing = () => null and <Existing />. The nested editor drops the ESM node on import, as on main. After an edit and blur, collectPreservedBindings no longer sees Existing, and a wildcard descriptor then generates import { Existing } from '@fallback'. With (1) in place this logic can probably go. If it stays, please add a focus/edit/blur integration test for it.
3. Bundle and API surface. exportMarkdownFromLexical now imports micromark-extension-mdxjs and fromMarkdown at runtime, so users without jsxPlugin pull in acorn. The declare module 'mdast' augmentation ships to every consumer. Both go away with (1). Please keep the new ImportStatement fields internal.
Smaller things
/^[A-Z_$][\w$]*$/now also applies at the root, so a root<Äpfel />with a matching descriptor no longer gets its import.\p{Lu}/\p{ID_Continue}with theuflag would cover it.- Please drop the Firefox clipboard fixture (now on main) and the Picsum stubbing in
linked-image.spec.ts. They are unrelated to #733. - The unit matrix is still 70 cases. A handful of cases plus the integration tests would be enough.
Integrate current main without rewriting branch history. Reconstruct only used import specifiers, keep metadata internal, remove runtime ESM reparsing, and preserve Unicode component identifiers.
|
Reworked this in
Local full tests passed (266, plus 1 existing skip/1 todo), along with lint, typecheck, build and declaration checks. The no- |
|
Merged, thanks for reworking it. Rebuilding only the used specifiers fixed a number of cases main got wrong, too. I added a follow-up commit (bdaf8ea) for three remaining cases:
|
|
🎉 This PR is included in version 4.4.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |

Fixes #733.
Implementation
mdastaugmentation.ImportStatementkeeps its original public shape; this does not introduce general ESM preservation.Current main is integrated without rewriting branch history. The already-landed Firefox fixture has no feature diff, and the unrelated Picsum stubs have been removed.
Verification
Head:
9a64b4b09b1e23c2afcf1bfd0b3fa855a9c5417c. Published tree491dd2e57ac176e108f567a32897b1df8a675b5bmatches the tested local tree exactly.ImportStatementfields and no new metadata/global augmentation. A bundled MDXEditor-only consumer (withoutjsxPlugin) retains no acorn/MDX-parser modules. This is dependency-graph validation, not a runtime consumer test.Exact-head CI run 40539590451: Test, Build Docs and Build JS have passed, including packed React consumers and cross-version Markdown. Browser compatibility is still running; no browser pass is claimed yet.
Maintainer re-review remains pending; successful tests do not constitute acceptance or merge.