From: Evan You Date: Sat, 22 Aug 2020 01:54:33 +0000 (-0400) Subject: feat(runtime-core): support variadic children in `h` for simple JSX compat X-Git-Tag: v3.0.0-rc.8~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54d06ec495a1743415de9426962024ffb764e4fe;p=thirdparty%2Fvuejs%2Fcore.git feat(runtime-core): support variadic children in `h` for simple JSX compat ref: #1917 --- diff --git a/packages/runtime-core/__tests__/h.spec.ts b/packages/runtime-core/__tests__/h.spec.ts index 1e1c6fb8d8..21eab3c568 100644 --- a/packages/runtime-core/__tests__/h.spec.ts +++ b/packages/runtime-core/__tests__/h.spec.ts @@ -64,4 +64,20 @@ describe('renderer: h', () => { }) ) }) + + // for simple JSX compat + // note this signature is not supported in types; it's purely for usage with + // compiled code. + test('support variadic children', () => { + // @ts-ignore + const vnode = h('div', null, h('span'), h('span')) + expect(vnode.children).toMatchObject([ + { + type: 'span' + }, + { + type: 'span' + } + ]) + }) }) diff --git a/packages/runtime-core/src/h.ts b/packages/runtime-core/src/h.ts index 14ddece792..d1fe4f0ace 100644 --- a/packages/runtime-core/src/h.ts +++ b/packages/runtime-core/src/h.ts @@ -129,7 +129,8 @@ export function h

( // Actual implementation export function h(type: any, propsOrChildren?: any, children?: any): VNode { - if (arguments.length === 2) { + const l = arguments.length + if (l === 2) { if (isObject(propsOrChildren) && !isArray(propsOrChildren)) { // single vnode without props if (isVNode(propsOrChildren)) { @@ -142,7 +143,9 @@ export function h(type: any, propsOrChildren?: any, children?: any): VNode { return createVNode(type, null, propsOrChildren) } } else { - if (isVNode(children)) { + if (l > 3) { + children = Array.prototype.slice.call(arguments, 2) + } else if (l === 3 && isVNode(children)) { children = [children] } return createVNode(type, propsOrChildren, children)