From 82afc9049cb55f9614b243b1cceadcfcab79ed2b Mon Sep 17 00:00:00 2001 From: Vlad Zhukov Date: Fri, 25 Mar 2022 16:46:52 +0400 Subject: [PATCH] Don't add _missingMdxReference when an object is in scope --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 12 ++++--- packages/mdx/test/compile.js | 36 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index db2e16110..2b10825c0 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -136,12 +136,14 @@ export function recmaJsxRewrite(options = {}) { const fullId = ids.join('.') const id = name.name - if (!own.call(fnScope.references, fullId)) { - fnScope.references[fullId] = {node, component: true} - } + if (!inScope(currentScope, id)) { + if (!own.call(fnScope.references, fullId)) { + fnScope.references[fullId] = {node, component: true} + } - if (!fnScope.objects.includes(id) && !inScope(currentScope, id)) { - fnScope.objects.push(id) + if (!fnScope.objects.includes(id)) { + fnScope.objects.push(id) + } } } // ``. diff --git a/packages/mdx/test/compile.js b/packages/mdx/test/compile.js index 9b19012a9..33dc7e162 100644 --- a/packages/mdx/test/compile.js +++ b/packages/mdx/test/compile.js @@ -791,6 +791,42 @@ test('jsx', async () => { 'should serialize fragments, namespaces, members' ) + assert.equal( + String( + compileSync( + [ + 'Hello, world!', + '{(() => {', + ' const x = { y: () => <>x.y };', + ' return ;', + '})()}' + ].join('\n'), + {jsx: true} + ) + ), + [ + '/*@jsxRuntime automatic @jsxImportSource react*/', + 'function MDXContent(props = {}) {', + ' const {wrapper: MDXLayout} = props.components || ({});', + ' return MDXLayout ? <_createMdxContent /> : _createMdxContent();', + ' function _createMdxContent() {', + ' const _components = Object.assign({', + ' p: "p"', + ' }, props.components);', + ' return <><_components.p>{"Hello, world!"}{"\\n"}{(() => {', + ' const x = {', + ' y: () => <>x.y', + ' };', + ' return ;', + ' })()};', + ' }', + '}', + 'export default MDXContent;', + '' + ].join('\n'), + 'should serialize members, expressions' + ) + assert.equal( String(compileSync('<>a {/* 1 */} b', {jsx: true})), [