Skip to content

Commit

Permalink
Fix to not add _missingMdxReference when thing is in scope
Browse files Browse the repository at this point in the history
Closes GH-1986.
Related-to: GH-1988.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
vlad-zhukov committed Mar 31, 2022
1 parent 0df684b commit e79fc2b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
18 changes: 16 additions & 2 deletions packages/mdx/lib/plugin/recma-jsx-rewrite.js
Expand Up @@ -136,11 +136,25 @@ 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
)
if (
!isInScope ||
// If the parent scope is `_createMdxContent`, then this
// references a component we can add a check statement for.
(parentScope &&
parentScope.node.type === 'FunctionDeclaration' &&
isNamedFunction(parentScope.node, '_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

1 comment on commit e79fc2b

@vercel
Copy link

@vercel vercel bot commented on e79fc2b Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.