Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to not add _missingMdxReference when thing is in scope #1986

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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} */ (
wooorm marked this conversation as resolved.
Show resolved Hide resolved
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/,
wooorm marked this conversation as resolved.
Show resolved Hide resolved
'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