فهرست منبع

fix: simplify and use correct ctx in withCtx

Evan You 6 سال پیش
والد
کامیت
4dc8ffc378

+ 5 - 5
packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap

@@ -28,7 +28,7 @@ export const render = _withId(function render(_ctx, _cache) {
   return (_openBlock(), _createBlock(_component_Child, null, {
     default: _withId(() => [
       _createVNode(\\"div\\")
-    ], _ctx),
+    ]),
     _: 1
   }))
 })"
@@ -47,7 +47,7 @@ export const render = _withId(function render(_ctx, _cache) {
           name: \\"foo\\",
           fn: _withId(() => [
             _createVNode(\\"div\\")
-          ], _ctx)
+          ])
         }
       : undefined,
     _renderList(_ctx.list, (i) => {
@@ -55,7 +55,7 @@ export const render = _withId(function render(_ctx, _cache) {
         name: i,
         fn: _withId(() => [
           _createVNode(\\"div\\")
-        ], _ctx)
+        ])
       }
     })
   ]), 1024 /* DYNAMIC_SLOTS */))
@@ -72,10 +72,10 @@ export const render = _withId(function render(_ctx, _cache) {
   return (_openBlock(), _createBlock(_component_Child, null, {
     foo: _withId(({ msg }) => [
       _createTextVNode(_toDisplayString(msg), 1 /* TEXT */)
-    ], _ctx),
+    ]),
     bar: _withId(() => [
       _createVNode(\\"div\\")
-    ], _ctx),
+    ]),
     _: 1
   }))
 })"

+ 1 - 1
packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap

@@ -246,7 +246,7 @@ return function render(_ctx, _cache) {
     return (_openBlock(), _createBlock(_component_Comp, null, {
       default: _withCtx(({ foo }) => [
         _createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */)
-      ], _ctx),
+      ]),
       _: 1
     }))
   }

+ 16 - 16
packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap

@@ -7,8 +7,8 @@ return function render(_ctx, _cache) {
   const _component_Comp = _resolveComponent(\\"Comp\\")
 
   return (_openBlock(), _createBlock(_component_Comp, null, {
-    [_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
-    [_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
+    [_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
+    [_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
     _: 1
   }, 1024 /* DYNAMIC_SLOTS */))
 }"
@@ -23,7 +23,7 @@ return function render(_ctx, _cache) {
   return (_openBlock(), _createBlock(_component_Comp, null, {
     default: _withCtx(() => [
       _createVNode(\\"div\\")
-    ], _ctx),
+    ]),
     _: 1
   }))
 }"
@@ -39,7 +39,7 @@ return function render(_ctx, _cache) {
     _renderList(_ctx.list, (name) => {
       return {
         name: name,
-        fn: _withCtx(() => [_toDisplayString(name)], _ctx)
+        fn: _withCtx(() => [_toDisplayString(name)])
       }
     })
   ]), 1024 /* DYNAMIC_SLOTS */))
@@ -56,7 +56,7 @@ return function render(_ctx, _cache) {
     (_ctx.ok)
       ? {
           name: \\"one\\",
-          fn: _withCtx((props) => [_toDisplayString(props)], _ctx)
+          fn: _withCtx((props) => [_toDisplayString(props)])
         }
       : undefined
   ]), 1024 /* DYNAMIC_SLOTS */))
@@ -76,16 +76,16 @@ return function render(_ctx, _cache) {
       ok
         ? {
             name: \\"one\\",
-            fn: _withCtx(() => [\\"foo\\"], _ctx)
+            fn: _withCtx(() => [\\"foo\\"])
           }
         : orNot
           ? {
               name: \\"two\\",
-              fn: _withCtx((props) => [\\"bar\\"], _ctx)
+              fn: _withCtx((props) => [\\"bar\\"])
             }
           : {
               name: \\"one\\",
-              fn: _withCtx(() => [\\"baz\\"], _ctx)
+              fn: _withCtx(() => [\\"baz\\"])
             }
     ]), 1024 /* DYNAMIC_SLOTS */))
   }
@@ -105,7 +105,7 @@ return function render(_ctx, _cache) {
       ok
         ? {
             name: \\"one\\",
-            fn: _withCtx(() => [\\"hello\\"], _ctx)
+            fn: _withCtx(() => [\\"hello\\"])
           }
         : undefined
     ]), 1024 /* DYNAMIC_SLOTS */))
@@ -120,8 +120,8 @@ return function render(_ctx, _cache) {
   const _component_Comp = _resolveComponent(\\"Comp\\")
 
   return (_openBlock(), _createBlock(_component_Comp, null, {
-    one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
-    two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
+    one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
+    two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
     _: 1
   }))
 }"
@@ -137,11 +137,11 @@ return function render(_ctx, _cache) {
     const _component_Comp = _resolveComponent(\\"Comp\\")
 
     return (_openBlock(), _createBlock(_component_Comp, null, {
-      one: _withCtx(() => [\\"foo\\"], _ctx),
+      one: _withCtx(() => [\\"foo\\"]),
       default: _withCtx(() => [
         \\"bar\\",
         _createVNode(\\"span\\")
-      ], _ctx),
+      ]),
       _: 1
     }))
   }
@@ -158,14 +158,14 @@ return function render(_ctx, _cache) {
   return (_openBlock(), _createBlock(_component_Comp, null, {
     default: _withCtx(({ foo }) => [
       _createVNode(_component_Inner, null, {
-        default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)], _ctx),
+        default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)]),
         _: 1
       }, 1024 /* DYNAMIC_SLOTS */),
       \\" \\",
       _toDisplayString(foo),
       _toDisplayString(_ctx.bar),
       _toDisplayString(_ctx.baz)
-    ], _ctx),
+    ]),
     _: 1
   }))
 }"
@@ -178,7 +178,7 @@ return function render(_ctx, _cache) {
   const _component_Comp = _resolveComponent(\\"Comp\\")
 
   return (_openBlock(), _createBlock(_component_Comp, null, {
-    default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
+    default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
     _: 1
   }))
 }"

+ 4 - 3
packages/compiler-core/src/codegen.ts

@@ -49,7 +49,8 @@ import {
   WITH_DIRECTIVES,
   CREATE_BLOCK,
   OPEN_BLOCK,
-  CREATE_STATIC
+  CREATE_STATIC,
+  WITH_CTX
 } from './runtimeHelpers'
 import { ImportItem } from './transform'
 
@@ -769,7 +770,7 @@ function genFunctionExpression(
   if (genScopeId) {
     push(`_withId(`)
   } else if (isSlot) {
-    push(`_withCtx(`)
+    push(`_${helperNameMap[WITH_CTX]}(`)
   }
   push(`(`, node)
   if (isArray(params)) {
@@ -799,7 +800,7 @@ function genFunctionExpression(
     push(`}`)
   }
   if (genScopeId || isSlot) {
-    push(`, _ctx)`)
+    push(`)`)
   }
 }
 

+ 8 - 8
packages/compiler-ssr/__tests__/ssrComponent.spec.ts

@@ -60,7 +60,7 @@ describe('ssr: components', () => {
                   _createVNode(\\"div\\")
                 ]
               }
-            }, _ctx),
+            }),
             _: 1
           }, _parent))
         }"
@@ -85,7 +85,7 @@ describe('ssr: components', () => {
                   _createTextVNode(_toDisplayString(msg + _ctx.outer), 1 /* TEXT */)
                 ]
               }
-            }, _ctx),
+            }),
             _: 1
           }, _parent))
         }"
@@ -114,7 +114,7 @@ describe('ssr: components', () => {
                   _createTextVNode(\\"foo\\")
                 ]
               }
-            }, _ctx),
+            }),
             named: _withCtx((_, _push, _parent, _scopeId) => {
               if (_push) {
                 _push(\`bar\`)
@@ -123,7 +123,7 @@ describe('ssr: components', () => {
                   _createTextVNode(\\"bar\\")
                 ]
               }
-            }, _ctx),
+            }),
             _: 1
           }, _parent))
         }"
@@ -154,7 +154,7 @@ describe('ssr: components', () => {
                         _createTextVNode(\\"foo\\")
                       ]
                     }
-                  }, _ctx)
+                  })
                 }
               : undefined
           ]), _parent))
@@ -186,7 +186,7 @@ describe('ssr: components', () => {
                       _createTextVNode(_toDisplayString(msg + _ctx.key + _ctx.bar), 1 /* TEXT */)
                     ]
                   }
-                }, _ctx)
+                })
               }
             })
           ]), _parent))
@@ -238,7 +238,7 @@ describe('ssr: components', () => {
                     : _createCommentVNode(\\"v-if\\", true)
                 ]
               }
-            }, _ctx),
+            }),
             bar: _withCtx(({ ok }, _push, _parent, _scopeId) => {
               if (_push) {
                 if (ok) {
@@ -261,7 +261,7 @@ describe('ssr: components', () => {
                     : _createCommentVNode(\\"v-if\\", true)
                 ]
               }
-            }, _ctx),
+            }),
             _: 1
           }, _parent))
         }"

+ 5 - 5
packages/compiler-ssr/__tests__/ssrScopeId.spec.ts

@@ -38,7 +38,7 @@ describe('ssr: scopeId', () => {
                 _createTextVNode(\\"foo\\")
               ]
             }
-          }, _ctx),
+          }),
           _: 1
         }, _parent))
       }"
@@ -66,7 +66,7 @@ describe('ssr: scopeId', () => {
                 _createVNode(\\"span\\", null, \\"hello\\")
               ]
             }
-          }, _ctx),
+          }),
           _: 1
         }, _parent))
       }"
@@ -99,7 +99,7 @@ describe('ssr: scopeId', () => {
                       _createVNode(\\"span\\")
                     ]
                   }
-                }, _ctx),
+                }),
                 _: 1
               }, _parent))
             } else {
@@ -108,12 +108,12 @@ describe('ssr: scopeId', () => {
                 _createVNode(_component_bar, null, {
                   default: _withCtx(() => [
                     _createVNode(\\"span\\")
-                  ], _ctx),
+                  ]),
                   _: 1
                 })
               ]
             }
-          }, _ctx),
+          }),
           _: 1
         }, _parent))
       }"

+ 3 - 5
packages/runtime-core/src/helpers/scopeId.ts

@@ -2,7 +2,6 @@
 // These are only used in esm-bundler builds, but since exports cannot be
 // conditional, we can only drop inner implementations in non-bundler builds.
 
-import { ComponentInternalInstance } from '../component'
 import { withCtx } from './withRenderContext'
 
 export let currentScopeId: string | null = null
@@ -23,14 +22,13 @@ export function popScopeId() {
 
 export function withScopeId(id: string): <T extends Function>(fn: T) => T {
   if (__BUNDLER__) {
-    return ((fn: Function, ctx?: ComponentInternalInstance) => {
-      return withCtx(function(this: any) {
+    return ((fn: Function) =>
+      withCtx(function(this: any) {
         pushScopeId(id)
         const res = fn.apply(this, arguments)
         popScopeId()
         return res
-      }, ctx)
-    }) as any
+      })) as any
   } else {
     return undefined as any
   }

+ 2 - 2
packages/runtime-core/src/helpers/withRenderContext.ts

@@ -1,13 +1,13 @@
 import { Slot } from '../componentSlots'
-import { ComponentInternalInstance } from '../component'
 import {
   setCurrentRenderingInstance,
   currentRenderingInstance
 } from '../componentRenderUtils'
+import { ComponentInternalInstance } from '../component'
 
 export function withCtx(
   fn: Slot,
-  ctx: ComponentInternalInstance | null | undefined
+  ctx: ComponentInternalInstance | null = currentRenderingInstance
 ) {
   if (!ctx) return fn
   return function renderFnWithContext() {