Skip to content

Commit bb62400

Browse files
committed
wip: test
1 parent 905805f commit bb62400

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/vSkip.spec.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,20 @@ return function render(_ctx, _cache) {
192192
}"
193193
`;
194194

195+
exports[`compiler: v-skip > transform > v-skip with key 1`] = `
196+
"const _Vue = Vue
197+
198+
return function render(_ctx, _cache) {
199+
with (_ctx) {
200+
const { createCommentVNode: _createCommentVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
201+
202+
return (_ctx.nested)
203+
? _createCommentVNode("v-skip", true)
204+
: (_openBlock(), _createElementBlock("div", { key: "foo" }))
205+
}
206+
}"
207+
`;
208+
195209
exports[`compiler: v-skip > transform > with component children 1`] = `
196210
"const _Vue = Vue
197211

packages/compiler-core/__tests__/transforms/vSkip.spec.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
type CompilerOptions,
33
type ElementNode,
44
ElementTypes,
5+
ErrorCodes,
56
type IfBranchNode,
67
type IfNode,
78
NodeTypes,
@@ -190,6 +191,28 @@ describe('compiler: v-skip', () => {
190191
expect(generate(root).code).toMatchSnapshot()
191192
})
192193

194+
test('v-skip with key', () => {
195+
const { root, node } = parseWithSkipTransform(
196+
`<div v-skip="nested" key="foo"/>`,
197+
)
198+
expect(node.type).toBe(NodeTypes.SKIP)
199+
expect((node.test as SimpleExpressionNode).content).toBe(`_ctx.nested`)
200+
expect(node.consequent.type === NodeTypes.JS_CALL_EXPRESSION).toBe(true)
201+
expect(node.alternate.children.length).toBe(1)
202+
expect(node.alternate.children[0].type).toBe(NodeTypes.ELEMENT)
203+
expect((node.alternate.children[0] as ElementNode).tag).toBe(`div`)
204+
expect(
205+
(node.alternate.children[0] as ElementNode).props[0],
206+
).toMatchObject({
207+
name: 'key',
208+
type: NodeTypes.ATTRIBUTE,
209+
value: {
210+
content: 'foo',
211+
},
212+
})
213+
expect(generate(root).code).toMatchSnapshot()
214+
})
215+
193216
test('v-else + v-skip', () => {
194217
const { root, node } = parseWithSkipTransform(
195218
`<div v-if="ok"/><div v-else v-skip="nested"/>`,
@@ -338,5 +361,63 @@ describe('compiler: v-skip', () => {
338361
})
339362
})
340363

341-
describe.todo('errors', () => {})
364+
describe('errors', () => {
365+
test('no expression', () => {
366+
const onError = vi.fn()
367+
const { node } = parseWithSkipTransform(`<div v-skip/>`, { onError })
368+
expect(onError.mock.calls[0]).toMatchObject([
369+
{
370+
code: ErrorCodes.X_V_SKIP_NO_EXPRESSION,
371+
loc: node.loc,
372+
},
373+
])
374+
})
375+
376+
test('on <slot>', () => {
377+
const onError = vi.fn()
378+
parseWithSkipTransform(`<slot v-skip="ok"/>`, { onError })
379+
expect(onError.mock.calls[0]).toMatchObject([
380+
{
381+
code: ErrorCodes.X_V_SKIP_ON_TEMPLATE,
382+
},
383+
])
384+
})
385+
386+
test('on <template>', () => {
387+
const onError = vi.fn()
388+
parseWithSkipTransform(`<template v-skip="ok"/>`, { onError })
389+
expect(onError.mock.calls[0]).toMatchObject([
390+
{
391+
code: ErrorCodes.X_V_SKIP_ON_TEMPLATE,
392+
},
393+
])
394+
})
395+
396+
test('on component without default slot', () => {
397+
const onError = vi.fn()
398+
parseWithSkipTransform(
399+
`<Comp v-skip="ok">
400+
<template #foo>foo</template>
401+
</Comp>`,
402+
{ onError },
403+
)
404+
expect(onError.mock.calls[0]).toMatchObject([
405+
{
406+
code: ErrorCodes.X_V_SKIP_UNEXPECTED_SLOT,
407+
},
408+
])
409+
})
410+
411+
test('with v-for', () => {
412+
const onError = vi.fn()
413+
parseWithSkipTransform(`<div v-for="i in items" v-skip="ok"/>`, {
414+
onError,
415+
})
416+
expect(onError.mock.calls[0]).toMatchObject([
417+
{
418+
code: ErrorCodes.X_V_SKIP_WITH_V_FOR,
419+
},
420+
])
421+
})
422+
})
342423
})

packages/compiler-core/src/transforms/vSkip.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
findDir,
2525
findProp,
2626
isSlotOutlet,
27-
isTemplateNode,
2827
processExpression,
2928
} from '@vue/compiler-core'
3029
import { createCodegenNodeForBranch } from './vIf'
@@ -59,7 +58,10 @@ export function processSkip(
5958
processCodegen?: (skipNode: SkipNode) => () => void,
6059
): (() => void) | undefined {
6160
const loc = dir.exp ? dir.exp.loc : node.loc
62-
if (isTemplateNode(node) || isSlotOutlet(node)) {
61+
if (
62+
(node.type === NodeTypes.ELEMENT && node.tag === 'template') ||
63+
isSlotOutlet(node)
64+
) {
6365
context.onError(createCompilerError(ErrorCodes.X_V_SKIP_ON_TEMPLATE, loc))
6466
return
6567
}

0 commit comments

Comments
 (0)