diff --git a/.changeset/sweet-rabbits-lick.md b/.changeset/sweet-rabbits-lick.md new file mode 100644 index 00000000..0bae3291 --- /dev/null +++ b/.changeset/sweet-rabbits-lick.md @@ -0,0 +1,5 @@ +--- +'@mdx-js/language-service': patch +--- + +Fix bug in virtual code generation for JSX closing elements diff --git a/packages/language-service/lib/virtual-code.js b/packages/language-service/lib/virtual-code.js index 6e8fee11..f7fcc2db 100644 --- a/packages/language-service/lib/virtual-code.js +++ b/packages/language-service/lib/virtual-code.js @@ -1,6 +1,6 @@ /** * @import {CodeMapping, VirtualCode} from '@volar/language-service' - * @import {ExportDefaultDeclaration, Program} from 'estree' + * @import {ExportDefaultDeclaration, JSXClosingElement, JSXOpeningElement, Program} from 'estree-jsx' * @import {Nodes, Root} from 'mdast' * @import {MdxjsEsm} from 'mdast-util-mdxjs-esm' * @import {IScriptSnapshot} from 'typescript' @@ -449,18 +449,32 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) { function processJsxExpression(program, lastIndex) { let newIndex = lastIndex let functionNesting = 0 + + /** + * @param {JSXClosingElement | JSXOpeningElement} node + * @returns {undefined} + */ + function processJsxTag(node) { + const {name} = node + + if (name.type !== 'JSXIdentifier') { + return + } + + if (!isInjectableComponent(name.name, variables)) { + return + } + + jsx = + addOffset(jsxMapping, mdx, jsx, newIndex, name.start) + '_components.' + newIndex = name.start + } + walk(program, { enter(node) { switch (node.type) { - case 'JSXIdentifier': { - if (!isInjectableComponent(node.name, variables)) { - return - } - - jsx = - addOffset(jsxMapping, mdx, jsx, newIndex, node.start) + - '_components.' - newIndex = node.start + case 'JSXElement': { + processJsxTag(node.openingElement) break } @@ -500,6 +514,16 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) { break } + case 'JSXElement': { + const {closingElement} = node + + if (closingElement) { + processJsxTag(closingElement) + } + + break + } + default: } } diff --git a/packages/language-service/test/language-plugin.js b/packages/language-service/test/language-plugin.js index 474499ab..59e78cf8 100644 --- a/packages/language-service/test/language-plugin.js +++ b/packages/language-service/test/language-plugin.js @@ -1752,8 +1752,12 @@ test('create virtual code w/ prefixed JSX expressions for mdxFlowExpression', () '{