Skip to content

Commit

Permalink
Don't add _missingMdxReference when an object is in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-zhukov committed Mar 30, 2022
1 parent 0df684b commit 40930b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
17 changes: 15 additions & 2 deletions packages/mdx/lib/plugin/recma-jsx-rewrite.js
Expand Up @@ -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)
}
}
Expand Down
16 changes: 12 additions & 4 deletions packages/mdx/test/compile.js
Expand Up @@ -504,7 +504,6 @@ test('compile', async () => {
)
}

// TODO: this is incorrect behavior, will be fixed in GH-1986
try {
renderToStaticMarkup(
React.createElement(
Expand All @@ -521,23 +520,32 @@ test('compile', async () => {
)
}

// TODO: this is incorrect behavior, will be fixed in GH-1986
try {
renderToStaticMarkup(
React.createElement(
await run(compileSync('<a render={(x) => <x.y />} />'))
await run(compileSync('<a render={() => <x.y />} />'))
)
)
assert.unreachable()
} catch (/** @type {unknown} */ error) {
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('<a render={(x) => <x.y />} />'))
)
),
'<a></a>',
'should render if a used member is defined locally (JSX in a function)'
)

try {
renderToStaticMarkup(
React.createElement(await run(compileSync('<X />', {development: true})))
Expand Down

0 comments on commit 40930b2

Please sign in to comment.