]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(runtime-core): support variadic children in `h` for simple JSX compat
authorEvan You <yyx990803@gmail.com>
Sat, 22 Aug 2020 01:54:33 +0000 (21:54 -0400)
committerEvan You <yyx990803@gmail.com>
Sat, 22 Aug 2020 01:54:33 +0000 (21:54 -0400)
ref: #1917

packages/runtime-core/__tests__/h.spec.ts
packages/runtime-core/src/h.ts

index 1e1c6fb8d82e4ef0ff94fedd6cd237b5ef2f3b86..21eab3c56869deacfbd775b2a73086f5c4f86bdc 100644 (file)
@@ -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'
+      }
+    ])
+  })
 })
index 14ddece792c3530e025c0477ca12fb880ef1a4ce..d1fe4f0acebf6b8003c0e40451139c107bfe145a 100644 (file)
@@ -129,7 +129,8 @@ export function h<P>(
 
 // 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)