mdx-js / mdx

Markdown for the component era
https://mdxjs.com
MIT License
17.43k stars 1.14k forks source link

Cannot parse JS line comment #2079

Closed Ayc0 closed 2 years ago

Ayc0 commented 2 years ago

Initial checklist

Affected packages and versions

MDX 2

Link to runnable example

No response

Steps to reproduce

If you open https://mdxjs.com/playground/ and try this code:

<div
  {/* comment */}
>
  I am JSX
</div>

The compiler will crash with this error:

example.mdx
  2:4  error  Unexpected empty expression  unexpected-empty-expression  micromark-extension-mdx-expression

✖ 1 error

And if you try:

<div
  // comment
>
  I am JSX
</div>

it'll crash with

example.mdx
  2:4  error  Unexpected character `/` (U+002F) after self-closing slash, expected `>` to end the tag (note: JS comments in JSX tags are not supported in MDX)  unexpected-character  micromark-extension-mdx-jsx

✖ 1 error
Block Line
image image

Expected behavior

In MDX 1, the block comment syntax wasn't supported, but line one was (it followed JSX's spec):

Line Block
image image

Would it be possible add back this syntax in MDX 2?

Actual behavior

It crashes for all form of comments

Runtime

Not relevant

Package manager

Not relevant

OS

Not relevant

Build and bundle tools

Not relevant

wooorm commented 2 years ago

Correct, those are not supported. It's a small deviation from jsx that not a lot of people know even exists, and not a lot of people use, that significantly makes parsing jsx harder.

wooorm commented 2 years ago

Closing as this is not planned, and rather complex. If you (or anyone stumbling on this) is interested in working on it, I can help advise, the work will have to be done in https://github.com/micromark/micromark-extension-mdx-jsx. You’d need to test a bit how different parsers (TS, Babel) handle comments in JSX, but essentially I believe it’s similar to whitespace, which is allowed almost everywhere. Some examples in the Babel playground.

Even more complex is that we retain comments in the tree, but we can’t do that for comments in JSX tags. Can that be solved? This is also a good reason not to support them: if they could be parsed, people would try to use // eslint-ignore ... comments in there, but we can likely not make that work.