Skip to content

Commit 2b730e3

Browse files
committed
test: add test
1 parent 73bb298 commit 2b730e3

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vShow.spec.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`compiler: v-show transform > on component 1`] = `
4+
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, applyVShow as _applyVShow } from 'vue';
5+
6+
export function render(_ctx) {
7+
const _component_Comp = _resolveComponent("Comp")
8+
const n0 = _createComponentWithFallback(_component_Comp, null, null, true)
9+
_applyVShow(n0, () => (_ctx.foo))
10+
return n0
11+
}"
12+
`;
13+
314
exports[`compiler: v-show transform > simple expression 1`] = `
415
"import { applyVShow as _applyVShow, template as _template } from 'vue';
516
const t0 = _template("<div></div>", true)

packages/compiler-vapor/__tests__/transforms/vShow.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ describe('compiler: v-show transform', () => {
2626
}),
2727
)
2828
})
29+
30+
test('on component', () => {
31+
const { code } = compileWithVShow(`<Comp v-show="foo"/>`)
32+
expect(code).toMatchSnapshot()
33+
})
2934
})

packages/compiler-vapor/src/transform.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export class TransformContext<T extends AllNode = AllNode> {
147147
isStaticExpression(e, this.root.options.bindingMetadata),
148148
)
149149
) {
150-
return this.registerOperation(...operations)
150+
return operations.forEach(op => this.registerOperation(op))
151151
}
152152

153153
this.block.expressions.push(...expressions)
@@ -172,12 +172,12 @@ export class TransformContext<T extends AllNode = AllNode> {
172172
}
173173
}
174174

175-
registerOperation(...node: OperationNode[]): void {
176-
this.block.operation.push(...node)
177-
}
178-
179-
registerOperationAt(node: OperationNode, index: number): void {
180-
this.block.operation.splice(index, 0, node)
175+
registerOperation(node: OperationNode, index?: number): void {
176+
if (index !== undefined) {
177+
this.block.operation.splice(index, 0, node)
178+
} else {
179+
this.block.operation.push(node)
180+
}
181181
}
182182

183183
create<T extends TemplateChildNode>(

packages/compiler-vapor/src/transforms/transformElement.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
type IRProps,
3434
type IRPropsDynamicAttribute,
3535
type IRPropsStatic,
36-
type OperationNode,
3736
type VaporDirectiveNode,
3837
} from '../ir'
3938
import { EMPTY_EXPRESSION } from './utils'
@@ -125,26 +124,28 @@ function transformComponentElement(
125124
}
126125

127126
context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
128-
const op: OperationNode = {
129-
type: IRNodeTypes.CREATE_COMPONENT_NODE,
130-
id: context.reference(),
131-
tag,
132-
props: propsResult[0] ? propsResult[1] : [propsResult[1]],
133-
asset,
134-
root: singleRoot,
135-
slots: [...context.slots],
136-
once: context.inVOnce,
137-
dynamic: dynamicComponent,
138-
}
139-
const hasVShow = findDir(node, 'show')
140-
if (hasVShow) {
141-
const showOperationIndex = context.block.operation.findIndex(
127+
128+
// ensure v-show is handled after the component is created
129+
let showOperationIndex
130+
if (findDir(node, 'show')) {
131+
showOperationIndex = context.block.operation.findIndex(
142132
op => op.type === IRNodeTypes.DIRECTIVE && op.name === 'show',
143133
)
144-
context.registerOperationAt(op, showOperationIndex)
145-
} else {
146-
context.registerOperation(op)
147134
}
135+
context.registerOperation(
136+
{
137+
type: IRNodeTypes.CREATE_COMPONENT_NODE,
138+
id: context.reference(),
139+
tag,
140+
props: propsResult[0] ? propsResult[1] : [propsResult[1]],
141+
asset,
142+
root: singleRoot,
143+
slots: [...context.slots],
144+
once: context.inVOnce,
145+
dynamic: dynamicComponent,
146+
},
147+
showOperationIndex,
148+
)
148149
context.slots = []
149150
}
150151

0 commit comments

Comments
 (0)