prettier: MDX: empty lines inside JSX blocks

Prettier 1.16.1 Playground link

# Options (if any):
--single-quote

Input:

## Basic usage

<Playground>
  {() => {
    const message = 'Hello world'

    return (
      <Alert>{message}</Alert>
    )

}}

</Playground>

Output:

SyntaxError: Unexpected token (3:34)
  1 | <Playground>
  2 |   {() => {
> 3 |     const message = 'Hello world'
    |                                  ^

Expected behavior:

## Basic usage

<Playground>
  {() => {
    const message = 'Hello world';

    return (
      <Alert>{message}</Alert>
    );
  }}
</Playground>                             

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 8
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I also ran into this issue 😞

This block:

// index.html.mdx
import Layout from "./layout.html.jsx"

export const layout = (props) => (
  <Layout title="Title">
    {props.children}
  </Layout>
)

# Heading

Gets corrected to:

// index.html.mdx
import Layout from "./layout.html.jsx"

export const layout = (props) => (
<Layout title="Title">
{props.children}
</Layout>
)

# Heading

Our team uses docz too and we also have this issue. Removing empty lines from the mdx examples above seems to help.

Looks like React formatting is successful only when the entire node is parsed as a single markdown paragraph before the JSX parser comes into play. If you add spaces to the lines after const and }} here, formatting will work once:

<Playground>
  {() => {
    const message    = "Hello world"
    
    return <Alert   >{message}</Alert   >
  }}
  
</Playground>

Playground

However, the following formatting of the result will be unsuccessful, because after the spaces are removed from the empty lines, we’ve ended up with two markdown paragraphs again. This means that both React nodes are now partial and thus unparsable.


UPD: Another example (jsx only, no functional components and js statements):