Skip to content

Commit 7dac6c4

Browse files
committed
Refactor to pass single child, not pass empty children
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.
1 parent ef9ca3f commit 7dac6c4

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

lib/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
* @typedef {JSX.Element | string | null | undefined} Child
8080
* Child.
8181
*
82-
* @typedef {{children: Array<Child> | undefined, node?: Element | undefined, [prop: string]: Value | Element | undefined | Array<Child>}} Props
82+
* @typedef {{children?: Array<Child> | Child, node?: Element | undefined, [prop: string]: Value | Element | undefined | Child | Array<Child>}} Props
8383
* Properties and children.
8484
*
8585
* @callback Create
@@ -295,7 +295,7 @@ export function toJsxRuntime(tree, options) {
295295
return state.create(
296296
tree,
297297
state.Fragment,
298-
{children: result ? [result] : undefined},
298+
{children: result || undefined},
299299
undefined
300300
)
301301
}
@@ -348,7 +348,13 @@ function one(state, node, key) {
348348
}
349349
}
350350

351-
props.children = children
351+
if (children.length > 0) {
352+
const value = children.length > 1 ? children : children[0]
353+
354+
if (value) {
355+
props.children = value
356+
}
357+
}
352358

353359
// Restore parent schema.
354360
state.schema = parentSchema
@@ -375,7 +381,8 @@ function productionCreate(_, jsx, jsxs) {
375381
return create
376382
/** @type {Create} */
377383
function create(_, type, props, key) {
378-
const isStaticChildren = props.children ? props.children.length > 1 : false
384+
// Only an array when there are 2 or more children.
385+
const isStaticChildren = Array.isArray(props.children)
379386
const fn = isStaticChildren ? jsxs : jsx
380387
return key ? fn(type, props, key) : fn(type, props)
381388
}
@@ -393,7 +400,8 @@ function developmentCreate(filePath, jsxDEV) {
393400
return create
394401
/** @type {Create} */
395402
function create(node, type, props, key) {
396-
const isStaticChildren = props.children ? props.children.length > 1 : false
403+
// Only an array when there are 2 or more children.
404+
const isStaticChildren = Array.isArray(props.children)
397405
const point = pointStart(node)
398406
return jsxDEV(
399407
type,
@@ -417,7 +425,7 @@ function developmentCreate(filePath, jsxDEV) {
417425
* Info passed around.
418426
* @param {Parent} node
419427
* Current element.
420-
* @returns {Array<Child> | undefined}
428+
* @returns {Array<Child>}
421429
* Children.
422430
*/
423431
function createChildren(state, node) {
@@ -444,7 +452,7 @@ function createChildren(state, node) {
444452
if (result !== undefined) children.push(result)
445453
}
446454

447-
return children.length > 0 ? children : undefined
455+
return children
448456
}
449457

450458
/**
@@ -459,7 +467,7 @@ function createChildren(state, node) {
459467
*/
460468
function createProperties(state, node) {
461469
/** @type {Props} */
462-
const props = {children: []}
470+
const props = {}
463471
/** @type {string} */
464472
let prop
465473

test/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ test('properties', () => {
235235
...production,
236236
jsx(type, props) {
237237
foundProps = props
238-
return production.jsx('div', {children: []}, undefined)
238+
return production.jsx(type, {})
239239
},
240240
stylePropertyNameCase: 'css'
241241
}
@@ -248,7 +248,6 @@ test('properties', () => {
248248
assert.deepEqual(
249249
foundProps,
250250
{
251-
children: undefined,
252251
style: {
253252
'-webkit-transform': 'rotate(0.01turn)',
254253
'--fg': '#0366d6',

0 commit comments

Comments
 (0)