Skip to content

Commit

Permalink
Fix components used directly and as objects
Browse files Browse the repository at this point in the history
Closes GH-36.
  • Loading branch information
wooorm committed Apr 11, 2021
1 parent e3d387d commit 9011b9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/plugin/recma-jsx-rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,18 @@ export function recmaJsxRewrite(options = {}) {
}

for (name of scope.objects) {
actual.push(
u('Property', {
kind: 'init',
shorthand: true,
key: u('Identifier', {name}),
value: u('Identifier', {name})
})
)
// In some cases, a component is used directly (`<X>`) but it’s also
// used as an object (`<X.Y>`).
if (!actual.some((d) => d.value.name === name)) {
actual.push(
u('Property', {
kind: 'init',
shorthand: true,
key: u('Identifier', {name}),
value: u('Identifier', {name})
})
)
}
}

if (defaults.length > 0 || actual.length > 0) {
Expand Down
21 changes: 21 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ test('xdm', async function (t) {
'should support passing in `components` (for members) to `MDXContent`'
)

t.equal(
renderToStaticMarkup(
React.createElement(await run(compileSync('<X /> and <X.Y />')), {
components: {
X: Object.assign(
function (props) {
return React.createElement('span', props, '!')
},
{
Y(props) {
return React.createElement('span', props, '?')
}
}
)
}
})
),
'<p><span>!</span> and <span>?</span></p>',
'should support passing in `components` directly and as an object w/ members'
)

t.equal(
renderToStaticMarkup(
React.createElement(await run(compileSync('*a*')), {
Expand Down

0 comments on commit 9011b9b

Please sign in to comment.