Skip to content

Commit e79fc2b

Browse files
authoredMar 31, 2022
Fix to not add _missingMdxReference when thing is in scope
Closes GH-1986. Related-to: GH-1988. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 0df684b commit e79fc2b

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed
 

‎packages/mdx/lib/plugin/recma-jsx-rewrite.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,25 @@ export function recmaJsxRewrite(options = {}) {
136136
const fullId = ids.join('.')
137137
const id = name.name
138138

139+
const isInScope = inScope(currentScope, id)
140+
139141
if (!own.call(fnScope.references, fullId)) {
140-
fnScope.references[fullId] = {node, component: true}
142+
const parentScope = /** @type {Scope|null} */ (
143+
currentScope.parent
144+
)
145+
if (
146+
!isInScope ||
147+
// If the parent scope is `_createMdxContent`, then this
148+
// references a component we can add a check statement for.
149+
(parentScope &&
150+
parentScope.node.type === 'FunctionDeclaration' &&
151+
isNamedFunction(parentScope.node, '_createMdxContent'))
152+
) {
153+
fnScope.references[fullId] = {node, component: true}
154+
}
141155
}
142156

143-
if (!fnScope.objects.includes(id) && !inScope(currentScope, id)) {
157+
if (!fnScope.objects.includes(id) && !isInScope) {
144158
fnScope.objects.push(id)
145159
}
146160
}

‎packages/mdx/test/compile.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,6 @@ test('compile', async () => {
504504
)
505505
}
506506

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

524-
// TODO: this is incorrect behavior, will be fixed in GH-1986
525523
try {
526524
renderToStaticMarkup(
527525
React.createElement(
528-
await run(compileSync('<a render={(x) => <x.y />} />'))
526+
await run(compileSync('<a render={() => <x.y />} />'))
529527
)
530528
)
531529
assert.unreachable()
532530
} catch (/** @type {unknown} */ error) {
533531
const exception = /** @type {Error} */ (error)
534532
assert.match(
535533
exception.message,
536-
/x is not defined/,
534+
/Expected object `x` to be defined/,
537535
'should throw if a used member is not defined locally (JSX in a function)'
538536
)
539537
}
540538

539+
assert.equal(
540+
renderToStaticMarkup(
541+
React.createElement(
542+
await run(compileSync('<a render={(x) => <x.y />} />'))
543+
)
544+
),
545+
'<a></a>',
546+
'should render if a used member is defined locally (JSX in a function)'
547+
)
548+
541549
try {
542550
renderToStaticMarkup(
543551
React.createElement(await run(compileSync('<X />', {development: true})))

1 commit comments

Comments
 (1)

vercel[bot] commented on Mar 31, 2022

@vercel[bot]

Successfully deployed to the following URLs:

Please sign in to comment.