Skip to content

Conversation

@Tigge
Copy link
Contributor

@Tigge Tigge commented Nov 13, 2019

For MDX wrap JSX parsing in a fragment before sending it of to babel for parsing and formatting. In MDX adjacent JSX tags are permitted in the root.

An option sent to the babel parser omits printing the root fragment and instead just prints its children.

Fixes #6943

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0
Copy link
Member

thorn0 commented Nov 14, 2019

Thanks. This issue with text doesn't look critical and can be addressed later in a separate PR if needed. It doesn't break code. That's the usual way Prettier formats JSX and that text is JSX.

@thorn0
Copy link
Member

thorn0 commented Nov 14, 2019

@Tigge Please add a changelog entry.

For MDX wrap JSX parsing in a fragment before sending it of to babel for
parsing and formatting. In MDX adjacent JSX tags are permitted in the
root.

An option sent to the babel parser omits printing the root fragment and
instead just prints its children.

Solves prettier#6943
@Tigge Tigge marked this pull request as ready for review November 14, 2019 11:11
@Tigge
Copy link
Contributor Author

Tigge commented Nov 14, 2019

@Tigge Please add a changelog entry.

I've added a changelog entry

Copy link
Member

@JounQin JounQin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't approve this due to breaking current behavior of inline texts.

Copy link
Member

@thorn0 thorn0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not okay definitely:

Prettier pr-6949
Playground link

--parser mdx

Input:

<Thingamabob>
  Some text
</Thingamabob> Some text

Output:

<Thingamabob>Some text</Thingamabob>{" "}
Some
text

@thorn0
Copy link
Member

thorn0 commented Nov 14, 2019

@JounQin I added a commit to this PR where I tried to address your comments. Please play with it and tell me what you think now.

<Hello>
test <World /> test
</Hello>123
</Hello>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're different.

Check following in playground:

<button>Here is a button</button>1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. So far it's the only incompatibility between MDX's block-level JSX and React. Before trying to fix this, I'd first make sure it's not a bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, another evidence proving mdx is not simply jsx, right?

Copy link
Member

@thorn0 thorn0 Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec (which is not detailed enough unfortunately) has a clear distinction between block-level and inline JSX. And so far it looked like the block-level one is supposed to be 100% React-compatible so that it was possible to copy-paste React code from JS to MDX. Could the creators of MDX deliberately break this use case by introducing this incompatibility? I'm not sure. We need to ask them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @johno @wooorm Any advices here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@thorn0 thorn0 Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JounQin What would you say to merging this PR as is and opening an issue about that whitespace?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool to me then, but we should post an issue with reference first before merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait
Copy link
Member

feel free to merge 👍

@thorn0 thorn0 merged commit e76ce27 into prettier:master Nov 15, 2019
@Tigge Tigge deleted the mdx-html-fix branch November 15, 2019 14:25
@Tigge
Copy link
Contributor Author

Tigge commented Nov 15, 2019

A few hours at work and a lot of things can happen. Thank you all for all the input, help and updates to this merge request!

lipis added a commit that referenced this pull request Jan 8, 2020
* 'next' of github.com:prettier/prettier:
  Optimize some usage of `Array#filter` (#6996)
  Update `jest` to v24 (#6954)
  Replace `trim{Left,Right}` with `trim{Start,End}` (#6994)
  Set `trailingComma` default value to `es5` (#6963)
  Fix `new` usage for builtin objects (#6968)
  Replace `indexOf` with `includes` (#6967)
  fix: tests for empty type parameters in TS (#6960)
  Fix MDX html parsing errors (#6949)
  fix: issue #6813 (Zero-based lists are broken) (#6852)
  Style: use async functions (#6935)
  Disable trailingComma for Angular internal parser (#6912)
  Update `snapshot-diff` to v0.6.1 (#6955)
  Update build scripts to target Node.js 10 (#6908)
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Feb 14, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Feb 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MDX with "html" tag in JSX fails

4 participants