Skip to content

Commit

Permalink
Refactor to pass single child, not pass empty children
Browse files Browse the repository at this point in the history
Babel compiles `<b/>` to `jsx('b', {})`, that is, no `children` prop,
but properties nonetheless.
And it compiled `<b>c</b>` to `jsx('b', {children: 'c'})`, which is,
the single child as the `children` value.

This commit matches that behavior, instead of always passing an (empty)
array.
  • Loading branch information
wooorm committed Feb 2, 2023
1 parent ef9ca3f commit 7dac6c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
24 changes: 16 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
* @typedef {JSX.Element | string | null | undefined} Child
* Child.
*
* @typedef {{children: Array<Child> | undefined, node?: Element | undefined, [prop: string]: Value | Element | undefined | Array<Child>}} Props
* @typedef {{children?: Array<Child> | Child, node?: Element | undefined, [prop: string]: Value | Element | undefined | Child | Array<Child>}} Props
* Properties and children.
*
* @callback Create
Expand Down Expand Up @@ -295,7 +295,7 @@ export function toJsxRuntime(tree, options) {
return state.create(
tree,
state.Fragment,
{children: result ? [result] : undefined},
{children: result || undefined},
undefined
)
}
Expand Down Expand Up @@ -348,7 +348,13 @@ function one(state, node, key) {
}
}

props.children = children
if (children.length > 0) {
const value = children.length > 1 ? children : children[0]

if (value) {
props.children = value
}
}

// Restore parent schema.
state.schema = parentSchema
Expand All @@ -375,7 +381,8 @@ function productionCreate(_, jsx, jsxs) {
return create
/** @type {Create} */
function create(_, type, props, key) {
const isStaticChildren = props.children ? props.children.length > 1 : false
// Only an array when there are 2 or more children.
const isStaticChildren = Array.isArray(props.children)
const fn = isStaticChildren ? jsxs : jsx
return key ? fn(type, props, key) : fn(type, props)
}
Expand All @@ -393,7 +400,8 @@ function developmentCreate(filePath, jsxDEV) {
return create
/** @type {Create} */
function create(node, type, props, key) {
const isStaticChildren = props.children ? props.children.length > 1 : false
// Only an array when there are 2 or more children.
const isStaticChildren = Array.isArray(props.children)
const point = pointStart(node)
return jsxDEV(
type,
Expand All @@ -417,7 +425,7 @@ function developmentCreate(filePath, jsxDEV) {
* Info passed around.
* @param {Parent} node
* Current element.
* @returns {Array<Child> | undefined}
* @returns {Array<Child>}
* Children.
*/
function createChildren(state, node) {
Expand All @@ -444,7 +452,7 @@ function createChildren(state, node) {
if (result !== undefined) children.push(result)
}

return children.length > 0 ? children : undefined
return children
}

/**
Expand All @@ -459,7 +467,7 @@ function createChildren(state, node) {
*/
function createProperties(state, node) {
/** @type {Props} */
const props = {children: []}
const props = {}
/** @type {string} */
let prop

Expand Down
3 changes: 1 addition & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test('properties', () => {
...production,
jsx(type, props) {
foundProps = props
return production.jsx('div', {children: []}, undefined)
return production.jsx(type, {})
},
stylePropertyNameCase: 'css'
}
Expand All @@ -248,7 +248,6 @@ test('properties', () => {
assert.deepEqual(
foundProps,
{
children: undefined,
style: {
'-webkit-transform': 'rotate(0.01turn)',
'--fg': '#0366d6',
Expand Down

0 comments on commit 7dac6c4

Please sign in to comment.