Skip to content

Commit

Permalink
Refactor to sort default components
Browse files Browse the repository at this point in the history
Closes GH-2318.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
remcohaszing authored Jul 4, 2023
1 parent dbe9f44 commit 825717f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions packages/mdx/lib/plugin/recma-jsx-rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @property {Array<string>} components
* @property {Array<string>} tags
* @property {Record<string, {node: JSXElement, component: boolean}>} references
* @property {Map<string|number, string>} idToInvalidComponentName
* @property {Map<string, string>} idToInvalidComponentName
* @property {EstreeFunction} node
*/

Expand Down Expand Up @@ -246,7 +246,7 @@ export function recmaJsxRewrite(options) {
/** @type {string} */
let name

for (name of scope.tags) {
for (name of scope.tags.sort()) {
defaults.push({
type: 'Property',
kind: 'init',
Expand All @@ -270,6 +270,8 @@ export function recmaJsxRewrite(options) {
}
}

actual.sort()

/** @type {Array<Statement>} */
const statements = []

Expand Down Expand Up @@ -363,18 +365,20 @@ export function recmaJsxRewrite(options) {
}

if (isNamedFunction(scope.node, '_createMdxContent')) {
for (const [
id,
componentName
] of scope.idToInvalidComponentName) {
for (const [id, componentName] of [
...scope.idToInvalidComponentName
].sort(([a], [b]) => a.localeCompare(b))) {
// For JSX IDs that can’t be represented as JavaScript IDs (as in,
// those with dashes, such as `custom-element`), generate a
// separate variable that is a valid JS ID (such as `_component0`),
// and takes it from components:
// `const _component0 = _components['custom-element']`
declarations.push({
type: 'VariableDeclarator',
id: {type: 'Identifier', name: componentName},
id: {
type: 'Identifier',
name: componentName
},
init: {
type: 'MemberExpression',
object: {type: 'Identifier', name: '_components'},
Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ test('jsx', async () => {
'/*@jsxRuntime automatic @jsxImportSource react*/',
'function _createMdxContent(props) {',
' const _components = Object.assign({',
' p: "p",',
' em: "em"',
' em: "em",',
' p: "p"',
' }, props.components);',
' return <_components.p><_components.em>{"a"}</_components.em></_components.p>;',
'}',
Expand Down

1 comment on commit 825717f

@vercel
Copy link

@vercel vercel bot commented on 825717f Jul 4, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

mdx – ./

mdx-git-main-mdx.vercel.app
mdx-mdx.vercel.app
mdxjs.com
v2.mdxjs.com

Please sign in to comment.