Skip to content

Commit c747990

Browse files
committed
Fix injecting providers for jsx in esm, expressions
Closes GH-2449. Related-to: facebook/docusaurus#9905.
1 parent 7dc3766 commit c747990

File tree

2 files changed

+110
-5
lines changed

2 files changed

+110
-5
lines changed

packages/mdx/lib/plugin/remark-mark-and-unravel.js

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import {collapseWhiteSpace} from 'collapse-white-space'
7+
import {walk} from 'estree-walker'
78
import {visit} from 'unist-util-visit'
89

910
/**
@@ -92,6 +93,23 @@ export function remarkMarkAndUnravel() {
9293
const data = node.data || (node.data = {})
9394
data._mdxExplicitJsx = true
9495
}
96+
97+
if (
98+
(node.type === 'mdxFlowExpression' ||
99+
node.type === 'mdxTextExpression' ||
100+
node.type === 'mdxjsEsm') &&
101+
node.data &&
102+
node.data.estree
103+
) {
104+
walk(node.data.estree, {
105+
enter(node) {
106+
if (node.type === 'JSXElement') {
107+
const data = node.data || (node.data = {})
108+
data._mdxExplicitJsx = true
109+
}
110+
}
111+
})
112+
}
95113
})
96114
}
97115
}

packages/mdx/test/compile.js

+92-5
Original file line numberDiff line numberDiff line change
@@ -1246,11 +1246,7 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
12461246
'/*@jsxRuntime automatic*/',
12471247
'/*@jsxImportSource react*/',
12481248
'function _createMdxContent(props) {',
1249-
' const _components = {',
1250-
' "a-b": "a-b",',
1251-
' ...props.components',
1252-
' }, _component0 = _components["a-b"];',
1253-
' return <>{<_component0></_component0>}</>;',
1249+
' return <>{<a-b></a-b>}</>;',
12541250
'}',
12551251
'export default function MDXContent(props = {}) {',
12561252
' const {wrapper: MDXLayout} = props.components || ({});',
@@ -1351,6 +1347,97 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
13511347
}
13521348
)
13531349

1350+
await t.test(
1351+
'should not inject a provider for JSX in ESM',
1352+
async function () {
1353+
assert.equal(
1354+
String(
1355+
await compile(
1356+
'export function A() { return <span /> }\n\nexport class B { render() { return <div /> } }',
1357+
{providerImportSource: '@mdx-js/react'}
1358+
)
1359+
),
1360+
[
1361+
'import {Fragment as _Fragment, jsx as _jsx} from "react/jsx-runtime";',
1362+
'import {useMDXComponents as _provideComponents} from "@mdx-js/react";',
1363+
'export function A() {',
1364+
' return _jsx("span", {});',
1365+
'}',
1366+
'export class B {',
1367+
' render() {',
1368+
' return _jsx("div", {});',
1369+
' }',
1370+
'}',
1371+
'function _createMdxContent(props) {',
1372+
' return _jsx(_Fragment, {});',
1373+
'}',
1374+
'export default function MDXContent(props = {}) {',
1375+
' const {wrapper: MDXLayout} = {',
1376+
' ..._provideComponents(),',
1377+
' ...props.components',
1378+
' };',
1379+
' return MDXLayout ? _jsx(MDXLayout, {',
1380+
' ...props,',
1381+
' children: _jsx(_createMdxContent, {',
1382+
' ...props',
1383+
' })',
1384+
' }) : _createMdxContent(props);',
1385+
'}',
1386+
''
1387+
].join('\n')
1388+
)
1389+
}
1390+
)
1391+
1392+
await t.test(
1393+
'should not inject a provider for JSX in expressions',
1394+
async function () {
1395+
console.log(
1396+
String(
1397+
await compile('{ <span /> }\n\nAnd also { <div /> }.', {
1398+
providerImportSource: '@mdx-js/react'
1399+
})
1400+
)
1401+
)
1402+
assert.equal(
1403+
String(
1404+
await compile('{ <span /> }\n\nAnd also { <div /> }.', {
1405+
providerImportSource: '@mdx-js/react'
1406+
})
1407+
),
1408+
[
1409+
'import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";',
1410+
'import {useMDXComponents as _provideComponents} from "@mdx-js/react";',
1411+
'function _createMdxContent(props) {',
1412+
' const _components = {',
1413+
' p: "p",',
1414+
' ..._provideComponents(),',
1415+
' ...props.components',
1416+
' };',
1417+
' return _jsxs(_Fragment, {',
1418+
' children: [_jsx("span", {}), "\\n", _jsxs(_components.p, {',
1419+
' children: ["And also ", _jsx("div", {}), "."]',
1420+
' })]',
1421+
' });',
1422+
'}',
1423+
'export default function MDXContent(props = {}) {',
1424+
' const {wrapper: MDXLayout} = {',
1425+
' ..._provideComponents(),',
1426+
' ...props.components',
1427+
' };',
1428+
' return MDXLayout ? _jsx(MDXLayout, {',
1429+
' ...props,',
1430+
' children: _jsx(_createMdxContent, {',
1431+
' ...props',
1432+
' })',
1433+
' }) : _createMdxContent(props);',
1434+
'}',
1435+
''
1436+
].join('\n')
1437+
)
1438+
}
1439+
)
1440+
13541441
await t.test(
13551442
'should serialize double quotes in attribute values',
13561443
async function () {

0 commit comments

Comments
 (0)