Skip to content

Commit

Permalink
Prettify generated JSX pragmas
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Feb 8, 2024
1 parent 156187c commit 634b6ba
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
6 changes: 4 additions & 2 deletions docs/_asset/editor.jsx
@@ -1,5 +1,7 @@
/* @jsxRuntime automatic
@jsxImportSource react */
/**
* @jsxRuntime automatic
* @jsxImportSource react
*/

/**
* @typedef {import('@wooorm/starry-night').Grammar} Grammar
Expand Down
6 changes: 4 additions & 2 deletions docs/docs/using-mdx.mdx
Expand Up @@ -41,8 +41,10 @@ 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('\n'),
value: '\n' + pragmas.join('\n') + '\n',
data: {_mdxIsPragmaComment: true}
})
}
Expand Down
6 changes: 4 additions & 2 deletions packages/mdx/readme.md
Expand Up @@ -653,8 +653,10 @@ 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
48 changes: 32 additions & 16 deletions packages/mdx/test/compile.js
Expand Up @@ -1154,8 +1154,10 @@ 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 @@ -1177,8 +1179,10 @@ 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 @@ -1197,8 +1201,10 @@ 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 @@ -1222,8 +1228,10 @@ 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 @@ -1243,8 +1251,10 @@ 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 @@ -1266,8 +1276,10 @@ 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 @@ -1295,8 +1307,10 @@ 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 @@ -1327,8 +1341,10 @@ 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 634b6ba

Please sign in to comment.