Skip to content

Commit f871eb3

Browse files
committed
fix(language-service): use JSX node name map
1 parent f88955b commit f871eb3

1 file changed

Lines changed: 46 additions & 32 deletions

File tree

packages/language-service/lib/virtual-code.js

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,7 @@ import {isInjectableComponent, isInjectableEstree} from './jsx-utils.js'
2323
* The string to use for the JSX import source tag.
2424
*/
2525
const jsPrefix = (jsxImportSource) => `/* @jsxRuntime automatic
26-
@jsxImportSource ${jsxImportSource} */
27-
28-
/**
29-
* @internal
30-
* @template T
31-
* @typedef {void extends MDX.IntrinsicElements ? any : T extends keyof MDX.IntrinsicElements ? MDX.IntrinsicElements[T] : any} _MDXProps
32-
*/
33-
34-
/**
35-
* @internal
36-
* @typedef {void extends MDX.Element ? JSX.Element : MDX.Element} _MDXElement
37-
*/
38-
39-
/**
40-
* @internal
41-
* @type {{
42-
* [Name in keyof _MDXNodeNames]: (props: _MDXProps<Name>) => _MDXElement
43-
* }}
44-
*/
45-
const _MDXNodes = /** @type {any} */(0);
46-
`
26+
@jsxImportSource ${jsxImportSource} */`
4727

4828
/**
4929
* @param {string} propsName
@@ -244,6 +224,48 @@ function padOffsets(mapping, padding) {
244224
}
245225
}
246226

227+
/** @type {Partial<Record<Nodes["type"], string>>} */
228+
const mdastElementNodeNameMap = {
229+
blockquote: 'blockquote',
230+
break: 'br',
231+
delete: 'del',
232+
emphasis: 'em',
233+
html: 'html',
234+
image: 'img',
235+
imageReference: 'img',
236+
inlineCode: 'code',
237+
link: 'a',
238+
linkReference: 'a',
239+
listItem: 'li',
240+
paragraph: 'p',
241+
strong: 'strong',
242+
table: 'table',
243+
tableCell: 'td',
244+
tableRow: 'tr',
245+
thematicBreak: 'hr'
246+
}
247+
/**
248+
* Get the JSX node names for a given AST node.
249+
* @param {Nodes} node
250+
* @returns {string[]}
251+
*/
252+
function getJsxNodeNameForMdast(node) {
253+
if (node.type === 'code') {
254+
return ['pre', 'code']
255+
}
256+
if (node.type === 'heading') {
257+
return ['h' + node.depth]
258+
}
259+
if (node.type === 'list') {
260+
return node.ordered ? ['ol'] : ['ul']
261+
}
262+
if (mdastElementNodeNameMap[node.type]) {
263+
return [/** @type string */ (mdastElementNodeNameMap[node.type])]
264+
}
265+
// JSX Fragment
266+
return ['']
267+
}
268+
247269
/**
248270
* @param {string} mdx
249271
* @param {Root} ast
@@ -266,8 +288,6 @@ function getEmbeddedCodes(
266288
let jsxVariables = ''
267289
let markdown = ''
268290
let nextMarkdownSourceStart = 0
269-
/** @type {Set<string>} */
270-
const foundMdxNodeNames = new Set()
271291

272292
const plugins = virtualCodePlugins.map((plugin) => plugin())
273293
/** @type {CodeMapping[]} */
@@ -760,8 +780,7 @@ function getEmbeddedCodes(
760780
}
761781

762782
default: {
763-
foundMdxNodeNames.add(node.type)
764-
jsx += jsxIndent + `<_MDXNodes.${node.type}>`
783+
jsx += jsxIndent + getJsxNodeNameForMdast(node).map((name) => `<${name}>`).join('')
765784
break
766785
}
767786
}
@@ -811,7 +830,7 @@ function getEmbeddedCodes(
811830
}
812831

813832
default: {
814-
jsx += jsxIndent + `</_MDXNodes.${node.type}>`
833+
jsx += jsxIndent + getJsxNodeNameForMdast(node).map((name) => `</${name}>`).join('')
815834
break
816835
}
817836
}
@@ -842,12 +861,6 @@ function getEmbeddedCodes(
842861
esmMapping.lengths.unshift(0)
843862
}
844863

845-
esm += `/**
846-
* @typedef {object} _MDXNodeNames
847-
${[...foundMdxNodeNames].map((name) => ` * @property {unknown} ${name}`).join('\n')}
848-
*/
849-
`
850-
851864
updateMarkdownFromOffsets(mdx.length, mdx.length)
852865
esm += componentStart(hasAwait, programScope)
853866

@@ -868,6 +881,7 @@ ${[...foundMdxNodeNames].map((name) => ` * @property {unknown} ${name}`).join('\
868881
if (jsxVariablesMapping.sourceOffsets.length > 0) {
869882
jsMappings.push(jsxVariablesMapping)
870883
}
884+
console.log(esm)
871885

872886
virtualCodes.unshift(
873887
{

0 commit comments

Comments
 (0)