Skip to content

Commit

Permalink
Fix the generated JSX pragmas
Browse files Browse the repository at this point in the history
Each JSX pragma needs to be on its own line for tools like Babel or
TypeScript to understand them.

There is a broken pragma in the @mdx-js/preact tests, which was left
untouched. Due to a misconfiguration, to fix it, would break type
checking.
  • Loading branch information
remcohaszing committed Feb 8, 2024
1 parent b9b37b6 commit 156187c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docs/_asset/editor.jsx
@@ -1,4 +1,5 @@
/* @jsxRuntime automatic @jsxImportSource react */
/* @jsxRuntime automatic
@jsxImportSource react */

/**
* @typedef {import('@wooorm/starry-night').Grammar} Grammar
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/using-mdx.mdx
Expand Up @@ -41,7 +41,8 @@ That’s *roughly* turned into the following JavaScript.
The below might help to form a mental model:

```tsx path="output-outline.jsx"
/* @jsxRuntime automatic @jsxImportSource react */
/* @jsxRuntime automatic
@jsxImportSource react */

export function Thing() {
return <>World</>
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/lib/plugin/recma-document.js
Expand Up @@ -105,7 +105,7 @@ export function recmaDocument(options) {
if (pragmas.length > 0) {
tree.comments.unshift({
type: 'Block',
value: pragmas.join(' '),
value: pragmas.join('\n'),
data: {_mdxIsPragmaComment: true}
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/mdx/readme.md
Expand Up @@ -653,7 +653,8 @@ Configuration for `createProcessor` (TypeScript type).

```diff
-import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
+/* @jsxRuntime automatic @jsxImportSource react */
+/* @jsxRuntime automatic
+@jsxImportSource react */

export function Thing() {
- return _jsx(_Fragment, {children: 'World'})
Expand Down
24 changes: 16 additions & 8 deletions packages/mdx/test/compile.js
Expand Up @@ -1154,7 +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 @@ -1176,7 +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 @@ -1195,7 +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 @@ -1219,7 +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 @@ -1239,7 +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 @@ -1261,7 +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 @@ -1289,7 +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 @@ -1320,7 +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 156187c

Please sign in to comment.