Skip to content

Commit

Permalink
Split JSX pragma tags over multiple comments
Browse files Browse the repository at this point in the history
This doesn’t affect their meaning, but the output looks slightly nicer.
  • Loading branch information
remcohaszing committed Feb 10, 2024
1 parent 634b6ba commit d9e33f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
4 changes: 2 additions & 2 deletions packages/mdx/lib/plugin/recma-document.js
Expand Up @@ -102,10 +102,10 @@ export function recmaDocument(options) {
/* c8 ignore next -- comments can be missing in the types, we always have it. */
if (!tree.comments) tree.comments = []

if (pragmas.length > 0) {
for (let index = pragmas.length; index--; index >= 0) {
tree.comments.unshift({
type: 'Block',
value: '\n' + pragmas.join('\n') + '\n',
value: pragmas[index],
data: {_mdxIsPragmaComment: true}
})
}
Expand Down
16 changes: 9 additions & 7 deletions packages/mdx/lib/plugin/recma-jsx-build.js
Expand Up @@ -45,13 +45,15 @@ export function recmaJsxBuild(options) {

// Remove the pragma comment that we injected ourselves as it is no longer
// needed.
if (
tree.comments &&
tree.comments[0].type === 'Block' &&
tree.comments[0].data &&
tree.comments[0].data._mdxIsPragmaComment
) {
tree.comments.shift()
if (tree.comments) {
let index = 0
while (index < tree.comments.length) {
if (tree.comments[index].data?._mdxIsPragmaComment) {
tree.comments.splice(index, 1)
} else {
index++
}
}
}

// When compiling to a function body, replace the import that was just
Expand Down
48 changes: 16 additions & 32 deletions packages/mdx/test/compile.js
Expand Up @@ -1154,10 +1154,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
assert.equal(
String(await compile('*a*', {jsx: true})),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'function _createMdxContent(props) {',
' const _components = {',
' em: "em",',
Expand All @@ -1179,10 +1177,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
assert.equal(
String(await compile('<a {...b} c d="1" e={1} />', {jsx: true})),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'function _createMdxContent(props) {',
' return <a {...b} c d="1" e={1} />;',
'}',
Expand All @@ -1201,10 +1197,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
assert.equal(
String(await compile('<><a:b /><c.d/></>', {jsx: true})),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'function _createMdxContent(props) {',
' const {c} = props.components || ({});',
' if (!c) _missingMdxReference("c", false);',
Expand All @@ -1228,10 +1222,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
assert.equal(
String(await compile('<>a {/* 1 */} b</>', {jsx: true})),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'/*1*/',
'function _createMdxContent(props) {',
' return <><>{"a "}{}{" b"}</></>;',
Expand All @@ -1251,10 +1243,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
assert.equal(
String(await compile('{<a-b></a-b>}', {jsx: true})),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'function _createMdxContent(props) {',
' const _components = {',
' "a-b": "a-b",',
Expand All @@ -1276,10 +1266,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
assert.equal(
String(await compile('Hello {props.name}', {jsx: true})),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'function _createMdxContent(props) {',
' const _components = {',
' p: "p",',
Expand Down Expand Up @@ -1307,10 +1295,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
)
),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'const MDXLayout = function Layout({components, ...props}) {',
' return <section {...props} />;',
'};',
Expand Down Expand Up @@ -1341,10 +1327,8 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
})
),
[
'/*',
'@jsxRuntime automatic',
'@jsxImportSource react',
'*/',
'/*@jsxRuntime automatic*/',
'/*@jsxImportSource react*/',
'import {useMDXComponents as _provideComponents} from "@mdx-js/react";',
'function _createMdxContent(props) {',
' const _components = {',
Expand Down

0 comments on commit d9e33f0

Please sign in to comment.