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

fix: JSX fragment should be allowed in mdx #6342

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion CHANGELOG.unreleased.md
Expand Up @@ -44,6 +44,28 @@ const link = <a href="example.com">http://example.com</a>;

-->

#### MDX: JSX fragment should be allowed in mdx ([#6342] by [@JounQin])

<!-- prettier-ignore -->
```jsx
// Input
<><Hello>
test <World /> test
</Hello></>

// Output (Prettier stable)
SyntaxError: Unexpected token (1:2)
> 1 | </Hello></>
| ^

// Output (Prettier master)
<>
<Hello>
test <World /> test
</Hello>
</>
```

#### MDX: Adjacent JSX elements should be allowed in mdx ([#6332] by [@JounQin])

Previous versions would not format adjacent JSX elements in mdx, this has been fixed in this version.
Expand All @@ -65,7 +87,7 @@ SyntaxError: Unexpected token (3:9)
// Output (Prettier master)
<Hello>
test <World /> test
</Hello>123 ^
</Hello>123


// Input
Expand Down Expand Up @@ -325,6 +347,7 @@ Flag used with `--write` to avoid re-checking files that were not changed since
[#6270]: https://github.com/prettier/prettier/pull/6270
[#6289]: https://github.com/prettier/prettier/pull/6289
[#6332]: https://github.com/prettier/prettier/pull/6332
[#6342]: https://github.com/prettier/prettier/pull/6342
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce
[@sosukesuzuki]: https://github.com/sosukesuzuki
Expand Down
2 changes: 1 addition & 1 deletion src/language-markdown/mdx.js
Expand Up @@ -26,7 +26,7 @@

const IMPORT_REGEX = /^import\s/;
const EXPORT_REGEX = /^export\s/;
const BLOCKS_REGEX = "[a-z\\.]*(\\.){0,1}[a-z][a-z0-9\\.]*";
const BLOCKS_REGEX = "[a-z\\.]*(\\.){0,1}[a-z]{0,1}[a-z0-9\\.]*";
const COMMENT_REGEX = "<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->";
const EMPTY_NEWLINE = "\n\n";

Expand Down
12 changes: 10 additions & 2 deletions src/language-markdown/parser-markdown.js
Expand Up @@ -60,10 +60,18 @@ function htmlToJsx() {
return node;
}

const nodes = htmlParser.parse(node.value).children;
let nodes;

try {
nodes = htmlParser.parse(node.value).children;
} catch (e) {
// do nothing, it could be JSX fragment here
// or, it will be caught by "jsx" parser
// FIXME: adjacent JSX fragments could not be parsed, should we care about that?
}

// find out if there are adjacent JSX elements which should be allowed in mdx alike in markdown
if (nodes.length <= 1) {
if (!nodes || nodes.length <= 1) {
return Object.assign({}, node, { type: "jsx" });
}

Expand Down
28 changes: 28 additions & 0 deletions tests/mdx/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -256,6 +256,12 @@ printWidth: 80

---

<><Hello>
test <World /> test
</Hello></>

---

| Column 1 | Column 2 |
|---|---|
| Text | <Hello>Text</Hello> |
Expand All @@ -280,6 +286,14 @@ printWidth: 80

---

<>
<Hello>
test <World /> test
</Hello>
</>

---

| Column 1 | Column 2 |
| -------- | ------------------- |
| Text | <Hello>Text</Hello> |
Expand Down Expand Up @@ -314,6 +328,12 @@ semi: false

---

<><Hello>
test <World /> test
</Hello></>

---

| Column 1 | Column 2 |
|---|---|
| Text | <Hello>Text</Hello> |
Expand All @@ -338,6 +358,14 @@ semi: false

---

<>
<Hello>
test <World /> test
</Hello>
</>

---

| Column 1 | Column 2 |
| -------- | ------------------- |
| Text | <Hello>Text</Hello> |
Expand Down
6 changes: 6 additions & 0 deletions tests/mdx/jsx.mdx
Expand Up @@ -18,6 +18,12 @@

---

<><Hello>
test <World /> test
</Hello></>

---

| Column 1 | Column 2 |
|---|---|
| Text | <Hello>Text</Hello> |