Skip to content

Commit 0132c2b

Browse files
authored
test(vIf): add tests and refactor comment node check to use isCommentOrWhitespace utility. (#14140)
1 parent a32650a commit 0132c2b

File tree

3 files changed

+194
-12
lines changed

3 files changed

+194
-12
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,17 @@ export function render(_ctx) {
308308
return n0
309309
}"
310310
`;
311+
312+
exports[`compiler: v-if > v-on with v-if 1`] = `
313+
"import { setDynamicEvents as _setDynamicEvents, renderEffect as _renderEffect, createIf as _createIf, template as _template } from 'vue';
314+
const t0 = _template("<button>w/ v-if</button>", true)
315+
316+
export function render(_ctx) {
317+
const n0 = _createIf(() => (true), () => {
318+
const n2 = t0()
319+
_renderEffect(() => _setDynamicEvents(n2, { click: _ctx.clickEvent }))
320+
return n2
321+
}, null, true)
322+
return n0
323+
}"
324+
`;

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

Lines changed: 178 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
transformVOnce,
1212
transformVText,
1313
} from '../../src'
14-
import { NodeTypes } from '@vue/compiler-dom'
14+
import { ErrorCodes, NodeTypes, type RootNode } from '@vue/compiler-dom'
1515

1616
const compileWithVIf = makeCompile({
1717
nodeTransforms: [
@@ -380,7 +380,181 @@ describe('compiler: v-if', () => {
380380
])
381381
})
382382

383-
describe.todo('errors')
384-
describe.todo('codegen')
385-
test.todo('v-on with v-if')
383+
test('v-on with v-if', () => {
384+
const { code, ir } = compileWithVIf(
385+
`<button v-on="{ click: clickEvent }" v-if="true">w/ v-if</button>`,
386+
)
387+
expect(code).toMatchSnapshot()
388+
expect([...ir.template.keys()]).toEqual(['<button>w/ v-if</button>'])
389+
390+
expect(ir.block.returns).toEqual([0])
391+
expect(ir.block.dynamic.children[0].operation).toMatchObject({
392+
type: IRNodeTypes.IF,
393+
condition: {
394+
type: NodeTypes.SIMPLE_EXPRESSION,
395+
content: 'true',
396+
isStatic: false,
397+
},
398+
positive: {
399+
type: IRNodeTypes.BLOCK,
400+
dynamic: {
401+
children: [{ template: 0 }],
402+
},
403+
},
404+
})
405+
})
406+
407+
describe('errors', () => {
408+
test('error on v-else missing adjacent v-if', () => {
409+
const onError = vi.fn()
410+
411+
{
412+
const { ir } = compileWithVIf(`<div v-else/>`, { onError })
413+
expect(onError.mock.calls[0]).toMatchObject([
414+
{
415+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
416+
loc: ir.node.loc,
417+
},
418+
])
419+
}
420+
421+
{
422+
const { ir } = compileWithVIf(`<div/><div v-else/>`, {
423+
onError,
424+
})
425+
expect(onError.mock.calls[1]).toMatchObject([
426+
{
427+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
428+
loc: (ir.block.node as RootNode).children[1].loc,
429+
},
430+
])
431+
}
432+
433+
{
434+
const { ir } = compileWithVIf(`<div/>foo<div v-else/>`, { onError })
435+
expect(onError.mock.calls[2]).toMatchObject([
436+
{
437+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
438+
loc: (ir.block.node as RootNode).children[2].loc,
439+
},
440+
])
441+
}
442+
443+
{
444+
const { ir } = compileWithVIf(`<div v-if="bar"/>foo<div v-else/>`, {
445+
onError,
446+
})
447+
expect(onError.mock.calls[3]).toMatchObject([
448+
{
449+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
450+
loc: (ir.block.node as RootNode).children[2].loc,
451+
},
452+
])
453+
}
454+
455+
// Non-breaking space
456+
{
457+
const { ir } = compileWithVIf(`<div v-if="bar"/>\u00a0<div v-else/>`, {
458+
onError,
459+
})
460+
expect(onError.mock.calls[4]).toMatchObject([
461+
{
462+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
463+
loc: (ir.block.node as RootNode).children[2].loc,
464+
},
465+
])
466+
}
467+
})
468+
469+
test('error on v-else-if missing adjacent v-if or v-else-if', () => {
470+
const onError = vi.fn()
471+
{
472+
const { ir } = compileWithVIf(`<div v-else-if="foo"/>`, {
473+
onError,
474+
})
475+
expect(onError.mock.calls[0]).toMatchObject([
476+
{
477+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
478+
loc: ir.node.loc,
479+
},
480+
])
481+
}
482+
{
483+
const { ir } = compileWithVIf(`<div/><div v-else-if="foo"/>`, {
484+
onError,
485+
})
486+
expect(onError.mock.calls[1]).toMatchObject([
487+
{
488+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
489+
loc: (ir.block.node as RootNode).children[1].loc,
490+
},
491+
])
492+
}
493+
{
494+
const { ir } = compileWithVIf(`<div/>foo<div v-else-if="foo"/>`, {
495+
onError,
496+
})
497+
expect(onError.mock.calls[2]).toMatchObject([
498+
{
499+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
500+
loc: (ir.block.node as RootNode).children[2].loc,
501+
},
502+
])
503+
}
504+
{
505+
const { ir } = compileWithVIf(
506+
`<div v-if="bar"/>foo<div v-else-if="foo"/>`,
507+
{ onError },
508+
)
509+
expect(onError.mock.calls[3]).toMatchObject([
510+
{
511+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
512+
loc: (ir.block.node as RootNode).children[2].loc,
513+
},
514+
])
515+
}
516+
{
517+
// Non-breaking space
518+
const { ir } = compileWithVIf(
519+
`<div v-if="bar"/>\u00a0<div v-else-if="foo"/>`,
520+
{ onError },
521+
)
522+
expect(onError.mock.calls[4]).toMatchObject([
523+
{
524+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
525+
loc: (ir.block.node as RootNode).children[2].loc,
526+
},
527+
])
528+
}
529+
530+
{
531+
const { ir } = compileWithVIf(
532+
`<div v-if="notOk"/><div v-else/><div v-else-if="ok"/>`,
533+
{ onError },
534+
)
535+
expect(onError.mock.calls[5]).toMatchObject([
536+
{
537+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
538+
loc: (ir.block.node as RootNode).children[2].loc,
539+
},
540+
])
541+
}
542+
})
543+
544+
test('error on adjacent v-else', () => {
545+
const onError = vi.fn()
546+
547+
const { ir } = compileWithVIf(
548+
`<div v-if="false"/><div v-else/><div v-else/>`,
549+
{ onError },
550+
)
551+
552+
expect(onError.mock.calls[0]).toMatchObject([
553+
{
554+
code: ErrorCodes.X_V_ELSE_NO_ADJACENT_IF,
555+
loc: (ir.block.node as RootNode).children[2].loc,
556+
},
557+
])
558+
})
559+
})
386560
})

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type ElementNode,
44
NodeTypes,
55
type TemplateChildNode,
6+
isCommentOrWhitespace,
67
} from '@vue/compiler-dom'
78
import type { NodeTransform, TransformContext } from '../transform'
89
import { DynamicFlag } from '../ir'
@@ -31,7 +32,7 @@ export function getSiblingIf(
3132
let i = siblings.indexOf(context.node)
3233
while (reverse ? --i >= 0 : ++i < siblings.length) {
3334
sibling = siblings[i]
34-
if (!isCommentLike(sibling)) {
35+
if (!isCommentOrWhitespace(sibling)) {
3536
break
3637
}
3738
}
@@ -48,10 +49,3 @@ export function getSiblingIf(
4849
return sibling
4950
}
5051
}
51-
52-
function isCommentLike(node: TemplateChildNode) {
53-
return (
54-
node.type === NodeTypes.COMMENT ||
55-
(node.type === NodeTypes.TEXT && !node.content.trim().length)
56-
)
57-
}

0 commit comments

Comments
 (0)