Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsxFrag pragma, use fragment for shortcode warning #1394

Merged
merged 2 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/mdx/estree-to-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ module.exports = estreeToJs
const customGenerator = Object.assign({}, astring.baseGenerator, {
JSXAttribute: JSXAttribute,
JSXClosingElement: JSXClosingElement,
JSXClosingFragment: JSXClosingFragment,
JSXElement: JSXElement,
JSXEmptyExpression: JSXEmptyExpression,
JSXExpressionContainer: JSXExpressionContainer,
JSXFragment: JSXFragment,
JSXIdentifier: JSXIdentifier,
JSXMemberExpression: JSXMemberExpression,
JSXNamespacedName: JSXNamespacedName,
JSXOpeningElement: JSXOpeningElement,
JSXOpeningFragment: JSXOpeningFragment,
JSXSpreadAttribute: JSXSpreadAttribute,
JSXText: JSXText
})
Expand All @@ -36,6 +39,11 @@ function JSXClosingElement(node, state) {
this[node.name.type](node.name, state)
}

// `</>`
function JSXClosingFragment(node, state) {
state.write('</>')
}

// `<div></div>`
function JSXElement(node, state) {
state.write('<')
Expand All @@ -56,6 +64,24 @@ function JSXElement(node, state) {
}
}

// `<></>`
function JSXFragment(node, state) {
this[node.openingFragment.type](node.openingElement, state)

let index = -1

while (++index < node.children.length) {
this[node.children[index].type](node.children[index], state)
}

/* istanbul ignore if - incorrect tree. */
if (!node.closingFragment) {
throw new Error('Cannot handle fragment w/o closing tag')
}

this[node.closingFragment.type](node.closingElement, state)
}

// `{}`
function JSXEmptyExpression() {}

Expand All @@ -77,6 +103,11 @@ function JSXOpeningElement(node, state) {
}
}

// `<>`
function JSXOpeningFragment(node, state) {
state.write('<>')
}

// `div`
function JSXIdentifier(node, state) {
state.write(node.name)
Expand Down
3 changes: 2 additions & 1 deletion packages/mdx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const mdxAstToMdxHast = require('./mdx-ast-to-mdx-hast')
const mdxHastToJsx = require('./mdx-hast-to-jsx')

const pragma = `/* @jsxRuntime classic */
/* @jsx mdx */`
/* @jsx mdx */
/* @jsxFrag mdx.Fragment */`

function createMdxAstCompiler(options = {}) {
return unified()
Expand Down
56 changes: 38 additions & 18 deletions packages/mdx/mdx-hast-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ function serializeEstree(estree, options) {
}

estree.body = [
...createMakeShortcodeHelper(magicShortcodes),
...createMakeShortcodeHelper(
magicShortcodes,
options.mdxFragment === false
),
...estree.body,
...exports
]
Expand Down Expand Up @@ -282,7 +285,7 @@ function createMdxLayout(declaration, mdxLayoutDefault) {
]
}

function createMakeShortcodeHelper(names) {
function createMakeShortcodeHelper(names, useElement) {
const func = {
type: 'VariableDeclaration',
declarations: [
Expand Down Expand Up @@ -329,22 +332,39 @@ function createMakeShortcodeHelper(names) {
},
{
type: 'ReturnStatement',
argument: {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
attributes: [
{
type: 'JSXSpreadAttribute',
argument: {type: 'Identifier', name: 'props'}
}
],
name: {type: 'JSXIdentifier', name: 'div'},
selfClosing: true
},
closingElement: null,
children: []
}
argument: useElement
? {
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
attributes: [
{
type: 'JSXSpreadAttribute',
argument: {type: 'Identifier', name: 'props'}
}
],
name: {type: 'JSXIdentifier', name: 'div'},
selfClosing: true
},
closingElement: null,
children: []
}
: {
type: 'JSXFragment',
openingFragment: {type: 'JSXOpeningFragment'},
closingFragment: {type: 'JSXClosingFragment'},
children: [
{
type: 'JSXExpressionContainer',
expression: {
type: 'MemberExpression',
object: {type: 'Identifier', name: 'props'},
property: {type: 'Identifier', name: 'children'},
computed: false
}
}
]
}
}
]
}
Expand Down
12 changes: 3 additions & 9 deletions packages/mdx/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,23 +564,17 @@ describe('@mdx-js/mdx', () => {
})

it('should not crash but issue a warning when an undefined component is used', async () => {
const Content = await run('x <Y /> z')
const Content = await run('w <X>y</X> z')
const warn = console.warn
console.warn = jest.fn()

// To do: a fragment would probably be better?
// Maybe the components children?
expect(renderToStaticMarkup(<Content />)).toEqual(
renderToStaticMarkup(
<p>
x <div /> z
</p>
)
renderToStaticMarkup(<p>w y z</p>)
)

expect(console.warn).toHaveBeenCalledWith(
'Component `%s` was not imported, exported, or provided by MDXProvider as global scope',
'Y'
'X'
)

console.warn = warn
Expand Down
6 changes: 4 additions & 2 deletions packages/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
],
"scripts": {
"build": "microbundle -f modern,es,cjs src/index.js",
"test": "jest test --coverage",
"test-types": "dtslint types"
"test-api": "jest test",
"test-coverage": "jest test --coverage",
"test-types": "dtslint types",
"test": "yarn test-coverage && yarn test-types"
},
"peerDependencies": {
"preact": "^10.4.6"
Expand Down
6 changes: 5 additions & 1 deletion packages/preact/src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const MDXCreateElement = forwardRef((props, ref) => {

MDXCreateElement.displayName = 'MDXCreateElement'

export default function (type, props) {
function mdx(type, props) {
const args = arguments
const mdxType = props && props.mdxType

Expand Down Expand Up @@ -73,3 +73,7 @@ export default function (type, props) {

return h.apply(null, args)
}

mdx.Fragment = Fragment

export default mdx
2 changes: 1 addition & 1 deletion packages/preact/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('@mdx-js/preact', () => {
const warn = console.warn
console.warn = jest.fn()

expect(render(<Content />)).toEqual('<div></div>')
expect(render(<Content />)).toEqual('')

expect(console.warn).toHaveBeenCalledWith(
'Component `%s` was not imported, exported, or provided by MDXProvider as global scope',
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const MDXCreateElement = React.forwardRef((props, ref) => {

MDXCreateElement.displayName = 'MDXCreateElement'

export default function (type, props) {
function mdx(type, props) {
const args = arguments
const mdxType = props && props.mdxType

Expand Down Expand Up @@ -72,3 +72,7 @@ export default function (type, props) {

return React.createElement.apply(null, args)
}

mdx.Fragment = React.Fragment

export default mdx
4 changes: 2 additions & 2 deletions packages/react/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ describe('@mdx-js/react', () => {
})

test('should warn on missing components', async () => {
const Content = await run('<Component />')
const Content = await run('<Component>x</Component>')
const warn = console.warn
console.warn = jest.fn()

expect(renderToString(<Content />)).toEqual('<div></div>')
expect(renderToString(<Content />)).toEqual('<p>x</p>')

expect(console.warn).toHaveBeenCalledWith(
'Component `%s` was not imported, exported, or provided by MDXProvider as global scope',
Expand Down
6 changes: 5 additions & 1 deletion packages/vue-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ async function mdxLoader(content) {
let result

try {
result = await mdx(content, {...options, skipExport: true})
result = await mdx(content, {
...options,
skipExport: true,
mdxFragment: false
})
} catch (err) {
return callback(err)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {MDXProvider, mdx} from '../src'

const run = async value => {
// Turn the serialized MDX code into serialized JSX…
const doc = await mdxTransform(value, {skipExport: true})
const doc = await mdxTransform(value, {skipExport: true, mdxFragment: false})

// …and that into serialized JS.
const {code} = await babelTransform(doc, {
Expand Down