diff --git a/.ignore b/.ignore index bacdd589..a6920dce 100644 --- a/.ignore +++ b/.ignore @@ -1,3 +1,2 @@ website/static/lib dist - diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 2ccbcfac..6e793295 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -44,6 +44,56 @@ const link = http://example.com; --> +#### 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. + + +```jsx +// Input + + test test +123 + +// Output (Prettier stable) +SyntaxError: Unexpected token (3:9) + 1 | + 2 | test test +> 3 | 123 + | ^ + +// Output (Prettier master) + + test test +123 ^ + + +// Input + + test test + + + test test +123 + +// Output (Prettier stable) +SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...? (4:1) + 2 | test test + 3 | +> 4 | + | ^ + 5 | test test + 6 | 123 + +// Output (Prettier master) + + test test + + + test test +123 +``` + #### TypeScript: Print comment following a JSX element with generic ([#6209] by [@duailibe]) Previous versions would not print this comment, this has been fixed in this version. @@ -274,7 +324,9 @@ Flag used with `--write` to avoid re-checking files that were not changed since [#6236]: https://github.com/prettier/prettier/pull/6236 [#6270]: https://github.com/prettier/prettier/pull/6270 [#6289]: https://github.com/prettier/prettier/pull/6289 +[#6332]: https://github.com/prettier/prettier/pull/6332 [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce [@sosukesuzuki]: https://github.com/sosukesuzuki [@g-harel]: https://github.com/g-harel +[@jounqin]: https://github.com/JounQin diff --git a/src/language-markdown/mdx.js b/src/language-markdown/mdx.js index b231145f..a7259777 100644 --- a/src/language-markdown/mdx.js +++ b/src/language-markdown/mdx.js @@ -24,9 +24,10 @@ * THE SOFTWARE. */ -const IMPORT_REGEX = /^import/; -const EXPORT_REGEX = /^export/; -const BLOCKS_REGEX = "[a-z\\.]+(\\.){0,1}[a-z\\.]"; +const IMPORT_REGEX = /^import\s/; +const EXPORT_REGEX = /^export\s/; +const BLOCKS_REGEX = "[a-z\\.]*(\\.){0,1}[a-z][a-z0-9\\.]*"; +const COMMENT_REGEX = "|"; const EMPTY_NEWLINE = "\n\n"; const isImport = text => IMPORT_REGEX.test(text); @@ -60,5 +61,6 @@ function esSyntax() { module.exports = { esSyntax, - BLOCKS_REGEX + BLOCKS_REGEX, + COMMENT_REGEX }; diff --git a/src/language-markdown/parser-markdown.js b/src/language-markdown/parser-markdown.js index 7ab176df..717394ab 100644 --- a/src/language-markdown/parser-markdown.js +++ b/src/language-markdown/parser-markdown.js @@ -7,6 +7,7 @@ const parseFrontMatter = require("../utils/front-matter"); const { mapAst, INLINE_NODE_WRAPPER_TYPES } = require("./utils"); const mdx = require("./mdx"); const remarkMath = require("remark-math"); +const htmlParser = require("../language-html/parser-html").parsers.html; /** * based on [MDAST](https://github.com/syntax-tree/mdast) with following modifications: @@ -50,16 +51,37 @@ function identity(x) { function htmlToJsx() { return ast => - mapAst(ast, (node, index, [parent]) => { + mapAst(ast, (node, _index, [parent]) => { if ( node.type !== "html" || - /^$/.test(node.value) || + node.value.match(mdx.COMMENT_REGEX) || INLINE_NODE_WRAPPER_TYPES.indexOf(parent.type) !== -1 ) { return node; } - return Object.assign({}, node, { type: "jsx" }); + const nodes = htmlParser.parse(node.value).children; + + // find out if there are adjacent JSX elements which should be allowed in mdx alike in markdown + if (nodes.length <= 1) { + return Object.assign({}, node, { type: "jsx" }); + } + + return nodes.reduce((newNodes, { sourceSpan: position, type }) => { + const value = node.value + .slice(position.start.offset, position.end.offset) + .trim(); + + if (value) { + newNodes.push({ + type: type === "element" ? "jsx" : type, + value, + position + }); + } + + return newNodes; + }, []); }); } diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index b3fbc1eb..5c3e9004 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -35,7 +35,12 @@ const { replaceEndOfLineWith } = require("../common/util"); const TRAILING_HARDLINE_NODES = ["importExport"]; const SINGLE_LINE_NODE_TYPES = ["heading", "tableCell", "link"]; -const SIBLING_NODE_TYPES = ["listItem", "definition", "footnoteDefinition"]; +const SIBLING_NODE_TYPES = [ + "listItem", + "definition", + "footnoteDefinition", + "jsx" +]; function genericPrint(path, options, print) { const node = path.getValue(); diff --git a/src/language-markdown/utils.js b/src/language-markdown/utils.js index a75448a9..702c7ccb 100644 --- a/src/language-markdown/utils.js +++ b/src/language-markdown/utils.js @@ -202,11 +202,21 @@ function mapAst(ast, handler) { return (function preorder(node, index, parentStack) { parentStack = parentStack || []; - const newNode = Object.assign({}, handler(node, index, parentStack)); + let newNode = handler(node, index, parentStack); + if (Array.isArray(newNode)) { + return newNode; + } + + newNode = Object.assign({}, newNode); if (newNode.children) { - newNode.children = newNode.children.map((child, index) => { - return preorder(child, index, [newNode].concat(parentStack)); - }); + newNode.children = newNode.children.reduce((nodes, child, index) => { + let newNodes = preorder(child, index, [newNode].concat(parentStack)); + if (!Array.isArray(newNodes)) { + newNodes = [newNodes]; + } + nodes.push.apply(nodes, newNodes); + return nodes; + }, []); } return newNode; diff --git a/tests/mdx/__snapshots__/jsfmt.spec.js.snap b/tests/mdx/__snapshots__/jsfmt.spec.js.snap index f7d55938..24d1df46 100644 --- a/tests/mdx/__snapshots__/jsfmt.spec.js.snap +++ b/tests/mdx/__snapshots__/jsfmt.spec.js.snap @@ -241,9 +241,18 @@ printWidth: 80 --- + + test test +123 + +--- + test test + + test test +123 --- @@ -256,9 +265,18 @@ printWidth: 80 --- + + test test +123 + +--- + test test + + test test +123 --- @@ -281,9 +299,18 @@ semi: false --- + + test test +123 + +--- + test test + + test test +123 --- @@ -296,9 +323,18 @@ semi: false --- + + test test +123 + +--- + test test + + test test +123 --- diff --git a/tests/mdx/jsx.mdx b/tests/mdx/jsx.mdx index ca80625b..4a937037 100644 --- a/tests/mdx/jsx.mdx +++ b/tests/mdx/jsx.mdx @@ -3,9 +3,18 @@ --- + + test test +123 + +--- + test test + + test test +123 ---