]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: add test edison/fix/vShowOnComponent 12951/head
authordaiwei <daiwei521@126.com>
Wed, 26 Feb 2025 08:46:27 +0000 (16:46 +0800)
committerdaiwei <daiwei521@126.com>
Wed, 26 Feb 2025 08:57:11 +0000 (16:57 +0800)
packages/compiler-vapor/__tests__/transforms/__snapshots__/vShow.spec.ts.snap
packages/compiler-vapor/__tests__/transforms/vShow.spec.ts
packages/compiler-vapor/src/transform.ts
packages/compiler-vapor/src/transforms/transformElement.ts

index f595da5ef8f964f9f83a2647e330a02ead268b85..b0de354cf13b95be5090db10707544fa908af33d 100644 (file)
@@ -1,5 +1,16 @@
 // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
 
+exports[`compiler: v-show transform > on component 1`] = `
+"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, applyVShow as _applyVShow } from 'vue';
+
+export function render(_ctx) {
+  const _component_Comp = _resolveComponent("Comp")
+  const n0 = _createComponentWithFallback(_component_Comp, null, null, true)
+  _applyVShow(n0, () => (_ctx.foo))
+  return n0
+}"
+`;
+
 exports[`compiler: v-show transform > simple expression 1`] = `
 "import { applyVShow as _applyVShow, template as _template } from 'vue';
 const t0 = _template("<div></div>", true)
index 800f64cc0b4468806186f687b3a08b9c84475136..a5e4c9649f9ea6b49aab01b667a40f075a90638f 100644 (file)
@@ -26,4 +26,9 @@ describe('compiler: v-show transform', () => {
       }),
     )
   })
+
+  test('on component', () => {
+    const { code } = compileWithVShow(`<Comp v-show="foo"/>`)
+    expect(code).toMatchSnapshot()
+  })
 })
index 534f28e5d22af75007c0ed1536e13a57a6b53b82..2543d7cd583935a3a90a26036bea228cfac545f3 100644 (file)
@@ -147,7 +147,7 @@ export class TransformContext<T extends AllNode = AllNode> {
         isStaticExpression(e, this.root.options.bindingMetadata),
       )
     ) {
-      return this.registerOperation(...operations)
+      return operations.forEach(op => this.registerOperation(op))
     }
 
     this.block.expressions.push(...expressions)
@@ -172,12 +172,12 @@ export class TransformContext<T extends AllNode = AllNode> {
     }
   }
 
-  registerOperation(...node: OperationNode[]): void {
-    this.block.operation.push(...node)
-  }
-
-  registerOperationAt(node: OperationNode, index: number): void {
-    this.block.operation.splice(index, 0, node)
+  registerOperation(node: OperationNode, index?: number): void {
+    if (index !== undefined) {
+      this.block.operation.splice(index, 0, node)
+    } else {
+      this.block.operation.push(node)
+    }
   }
 
   create<T extends TemplateChildNode>(
index c47e589953cd5ba7bc54a9874639d86b50e32a74..b030576917a8f35eb4e485d5d232f2d95afce8cb 100644 (file)
@@ -33,7 +33,6 @@ import {
   type IRProps,
   type IRPropsDynamicAttribute,
   type IRPropsStatic,
-  type OperationNode,
   type VaporDirectiveNode,
 } from '../ir'
 import { EMPTY_EXPRESSION } from './utils'
@@ -125,26 +124,28 @@ function transformComponentElement(
   }
 
   context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
-  const op: OperationNode = {
-    type: IRNodeTypes.CREATE_COMPONENT_NODE,
-    id: context.reference(),
-    tag,
-    props: propsResult[0] ? propsResult[1] : [propsResult[1]],
-    asset,
-    root: singleRoot,
-    slots: [...context.slots],
-    once: context.inVOnce,
-    dynamic: dynamicComponent,
-  }
-  const hasVShow = findDir(node, 'show')
-  if (hasVShow) {
-    const showOperationIndex = context.block.operation.findIndex(
+
+  // ensure v-show is handled after the component is created
+  let showOperationIndex
+  if (findDir(node, 'show')) {
+    showOperationIndex = context.block.operation.findIndex(
       op => op.type === IRNodeTypes.DIRECTIVE && op.name === 'show',
     )
-    context.registerOperationAt(op, showOperationIndex)
-  } else {
-    context.registerOperation(op)
   }
+  context.registerOperation(
+    {
+      type: IRNodeTypes.CREATE_COMPONENT_NODE,
+      id: context.reference(),
+      tag,
+      props: propsResult[0] ? propsResult[1] : [propsResult[1]],
+      asset,
+      root: singleRoot,
+      slots: [...context.slots],
+      once: context.inVOnce,
+      dynamic: dynamicComponent,
+    },
+    showOperationIndex,
+  )
   context.slots = []
 }