From 825717fdfa765bafafb29579b36c2e9a4738b40d Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Tue, 4 Jul 2023 14:00:18 +0200 Subject: [PATCH] Refactor to sort default components Closes GH-2318. Reviewed-by: Titus Wormer --- packages/mdx/lib/plugin/recma-jsx-rewrite.js | 18 +++++++++++------- packages/mdx/test/compile.js | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/mdx/lib/plugin/recma-jsx-rewrite.js b/packages/mdx/lib/plugin/recma-jsx-rewrite.js index 50dd37a19..4bc1dca98 100644 --- a/packages/mdx/lib/plugin/recma-jsx-rewrite.js +++ b/packages/mdx/lib/plugin/recma-jsx-rewrite.js @@ -36,7 +36,7 @@ * @property {Array} components * @property {Array} tags * @property {Record} references - * @property {Map} idToInvalidComponentName + * @property {Map} idToInvalidComponentName * @property {EstreeFunction} node */ @@ -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', @@ -270,6 +270,8 @@ export function recmaJsxRewrite(options) { } } + actual.sort() + /** @type {Array} */ const statements = [] @@ -363,10 +365,9 @@ 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`), @@ -374,7 +375,10 @@ export function recmaJsxRewrite(options) { // `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'}, diff --git a/packages/mdx/test/compile.js b/packages/mdx/test/compile.js index 5920b414f..05d86479e 100644 --- a/packages/mdx/test/compile.js +++ b/packages/mdx/test/compile.js @@ -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"};', '}',