From 40930b209f16990932e6c0764f8de5e83fe5275d Mon Sep 17 00:00:00 2001 From: Vlad Zhukov Date: Wed, 30 Mar 2022 14:47:23 +0400 Subject: [PATCH 1/5] Don't add _missingMdxReference when an object is in scope --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 17 +++++++++++++++-- packages/mdx/test/compile.js | 16 ++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index 0858a0be5..a449a9f51 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -136,11 +136,24 @@ export function recmaJsxRewrite(options = {}) { const fullId = ids.join('.') const id = name.name + const isInScope = inScope(currentScope, id) + if (!own.call(fnScope.references, fullId)) { - fnScope.references[fullId] = {node, component: true} + const parentScope = /** @type {Scope|null} */ ( + currentScope.parent + ) + const parentNode = parentScope && parentScope.node + if ( + !isInScope || + (parentNode && + parentNode.type === 'FunctionDeclaration' && + isNamedFunction(parentNode, '_createMdxContent')) + ) { + fnScope.references[fullId] = {node, component: true} + } } - if (!fnScope.objects.includes(id) && !inScope(currentScope, id)) { + if (!fnScope.objects.includes(id) && !isInScope) { fnScope.objects.push(id) } } diff --git a/packages/mdx/test/compile.js b/packages/mdx/test/compile.js index b98cf6c16..70db8b800 100644 --- a/packages/mdx/test/compile.js +++ b/packages/mdx/test/compile.js @@ -504,7 +504,6 @@ test('compile', async () => { ) } - // TODO: this is incorrect behavior, will be fixed in GH-1986 try { renderToStaticMarkup( React.createElement( @@ -521,11 +520,10 @@ test('compile', async () => { ) } - // TODO: this is incorrect behavior, will be fixed in GH-1986 try { renderToStaticMarkup( React.createElement( - await run(compileSync(' } />')) + await run(compileSync(' } />')) ) ) assert.unreachable() @@ -533,11 +531,21 @@ test('compile', async () => { const exception = /** @type {Error} */ (error) assert.match( exception.message, - /x is not defined/, + /Expected object `x` to be defined/, 'should throw if a used member is not defined locally (JSX in a function)' ) } + assert.equal( + renderToStaticMarkup( + React.createElement( + await run(compileSync(' } />')) + ) + ), + '', + 'should render if a used member is defined locally (JSX in a function)' + ) + try { renderToStaticMarkup( React.createElement(await run(compileSync('', {development: true}))) From e9b1de485ad9bfe5feed8b395a4154ec8f34ad56 Mon Sep 17 00:00:00 2001 From: Vlad Zhukov Date: Wed, 30 Mar 2022 14:54:46 +0400 Subject: [PATCH 2/5] Add a comment --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index a449a9f51..d08279f6f 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -145,6 +145,7 @@ export function recmaJsxRewrite(options = {}) { const parentNode = parentScope && parentScope.node if ( !isInScope || + // If the parent is a _createMdxContent function, it's a top-level component in MDX. (parentNode && parentNode.type === 'FunctionDeclaration' && isNamedFunction(parentNode, '_createMdxContent')) From f08661e77c37930344fbd02ff0b032fa4f6b9390 Mon Sep 17 00:00:00 2001 From: Vlad Zhukov Date: Thu, 31 Mar 2022 14:00:23 +0300 Subject: [PATCH 3/5] Update packages/mdx/lib/plugin/recma-jsx-rewrite.js Co-authored-by: Titus --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index d08279f6f..47181f6d7 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -145,7 +145,8 @@ export function recmaJsxRewrite(options = {}) { const parentNode = parentScope && parentScope.node if ( !isInScope || - // If the parent is a _createMdxContent function, it's a top-level component in MDX. + // If the parent scope is `_createMdxContent`, then this + // references a component we can add a check statement for. (parentNode && parentNode.type === 'FunctionDeclaration' && isNamedFunction(parentNode, '_createMdxContent')) From ba26de16c4b727be96a415e230d8e8ecdf8dc633 Mon Sep 17 00:00:00 2001 From: Vlad Zhukov Date: Thu, 31 Mar 2022 14:08:27 +0300 Subject: [PATCH 4/5] Update packages/mdx/lib/plugin/recma-jsx-rewrite.js Co-authored-by: Titus --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index 47181f6d7..018aae962 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -142,7 +142,6 @@ export function recmaJsxRewrite(options = {}) { const parentScope = /** @type {Scope|null} */ ( currentScope.parent ) - const parentNode = parentScope && parentScope.node if ( !isInScope || // If the parent scope is `_createMdxContent`, then this From 59de96ef37d5dac3dc0e50256aed671847c2112a Mon Sep 17 00:00:00 2001 From: Vlad Zhukov Date: Thu, 31 Mar 2022 14:08:34 +0300 Subject: [PATCH 5/5] Update packages/mdx/lib/plugin/recma-jsx-rewrite.js Co-authored-by: Titus --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index 018aae962..e1d8dbf50 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -146,9 +146,9 @@ export function recmaJsxRewrite(options = {}) { !isInScope || // If the parent scope is `_createMdxContent`, then this // references a component we can add a check statement for. - (parentNode && - parentNode.type === 'FunctionDeclaration' && - isNamedFunction(parentNode, '_createMdxContent')) + (parentScope && + parentScope.node.type === 'FunctionDeclaration' && + isNamedFunction(parentScope.node, '_createMdxContent')) ) { fnScope.references[fullId] = {node, component: true} }