Skip to content

Commit 1511d6c

Browse files
committed
wip: add TransformTransition
1 parent 05d9f53 commit 1511d6c

File tree

6 files changed

+110
-35
lines changed

6 files changed

+110
-35
lines changed

packages/compiler-dom/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@ export {
7676
} from './errors'
7777
export { resolveModifiers } from './transforms/vOn'
7878
export { isValidHTMLNesting } from './htmlNesting'
79+
export { postTransformTransition } from './transforms/Transition'
7980
export * from '@vue/compiler-core'

packages/compiler-dom/src/transforms/Transition.ts

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
type CompilerError,
23
type ComponentNode,
34
ElementTypes,
45
type IfBranchNode,
@@ -15,40 +16,43 @@ export const transformTransition: NodeTransform = (node, context) => {
1516
) {
1617
const component = context.isBuiltInComponent(node.tag)
1718
if (component === TRANSITION) {
18-
return () => {
19-
if (!node.children.length) {
20-
return
21-
}
19+
return postTransformTransition(node, context.onError)
20+
}
21+
}
22+
}
2223

23-
// warn multiple transition children
24-
if (hasMultipleChildren(node)) {
25-
context.onError(
26-
createDOMCompilerError(
27-
DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN,
28-
{
29-
start: node.children[0].loc.start,
30-
end: node.children[node.children.length - 1].loc.end,
31-
source: '',
32-
},
33-
),
34-
)
35-
}
24+
export function postTransformTransition(
25+
node: ComponentNode,
26+
onError: (error: CompilerError) => void,
27+
): () => void {
28+
return () => {
29+
if (!node.children.length) {
30+
return
31+
}
32+
33+
if (hasMultipleChildren(node)) {
34+
onError(
35+
createDOMCompilerError(DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN, {
36+
start: node.children[0].loc.start,
37+
end: node.children[node.children.length - 1].loc.end,
38+
source: '',
39+
}),
40+
)
41+
}
3642

37-
// check if it's s single child w/ v-show
38-
// if yes, inject "persisted: true" to the transition props
39-
const child = node.children[0]
40-
if (child.type === NodeTypes.ELEMENT) {
41-
for (const p of child.props) {
42-
if (p.type === NodeTypes.DIRECTIVE && p.name === 'show') {
43-
node.props.push({
44-
type: NodeTypes.ATTRIBUTE,
45-
name: 'persisted',
46-
nameLoc: node.loc,
47-
value: undefined,
48-
loc: node.loc,
49-
})
50-
}
51-
}
43+
// check if it's s single child w/ v-show
44+
// if yes, inject "persisted: true" to the transition props
45+
const child = node.children[0]
46+
if (child.type === NodeTypes.ELEMENT) {
47+
for (const p of child.props) {
48+
if (p.type === NodeTypes.DIRECTIVE && p.name === 'show') {
49+
node.props.push({
50+
type: NodeTypes.ATTRIBUTE,
51+
name: 'persisted',
52+
nameLoc: node.loc,
53+
value: undefined,
54+
loc: node.loc,
55+
})
5256
}
5357
}
5458
}

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

+34-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import {
55
transformText,
66
transformVBind,
77
transformVIf,
8+
transformVShow,
89
transformVSlot,
910
} from '@vue/compiler-vapor'
10-
import { expect } from 'vitest'
11+
import { transformTransition } from '../../src/transforms/transformTransition'
12+
import { DOMErrorCodes } from '@vue/compiler-dom'
1113

1214
const compileWithElementTransform = makeCompile({
1315
nodeTransforms: [
@@ -16,9 +18,11 @@ const compileWithElementTransform = makeCompile({
1618
transformElement,
1719
transformVSlot,
1820
transformChildren,
21+
transformTransition,
1922
],
2023
directiveTransforms: {
2124
bind: transformVBind,
25+
show: transformVShow,
2226
},
2327
})
2428

@@ -52,4 +56,33 @@ describe('compiler: transition', () => {
5256
// should preserve key
5357
expect(code).contains('n0.$key = _ctx.key')
5458
})
59+
60+
test('warns if multiple children', () => {
61+
const onError = vi.fn()
62+
compileWithElementTransform(
63+
`<Transition>
64+
<h1>foo</h1>
65+
<h2>bar</h2>
66+
</Transition>`,
67+
{
68+
onError,
69+
},
70+
)
71+
expect(onError).toHaveBeenCalled()
72+
expect(onError.mock.calls).toMatchObject([
73+
[{ code: DOMErrorCodes.X_TRANSITION_INVALID_CHILDREN }],
74+
])
75+
})
76+
77+
test('inject persisted when child has v-show', () => {
78+
expect(
79+
compileWithElementTransform(`
80+
<Transition>
81+
<div v-show="ok" />
82+
</Transition>
83+
`).code,
84+
).toMatchSnapshot()
85+
})
86+
87+
// TODO more tests
5588
})

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`compiler: transition > basic 1`] = `
4-
"import { VaporTransition as _VaporTransition, createComponent as _createComponent, template as _template } from 'vue';
4+
"import { VaporTransition as _VaporTransition, applyVShow as _applyVShow, createComponent as _createComponent, template as _template } from 'vue';
55
const t0 = _template("<h1>foo</h1>")
66
77
export function render(_ctx) {
8-
const n1 = _createComponent(_VaporTransition, { appear: () => ("") }, {
8+
const n1 = _createComponent(_VaporTransition, {
9+
appear: () => (""),
10+
persisted: () => ("")
11+
}, {
912
"default": () => {
1013
const n0 = t0()
14+
_applyVShow(n0, () => (_ctx.show))
15+
return n0
16+
}
17+
}, true)
18+
return n1
19+
}"
20+
`;
21+
22+
exports[`compiler: transition > inject persisted when child has v-show 1`] = `
23+
"import { VaporTransition as _VaporTransition, applyVShow as _applyVShow, createComponent as _createComponent, template as _template } from 'vue';
24+
const t0 = _template("<div></div>")
25+
26+
export function render(_ctx) {
27+
const n1 = _createComponent(_VaporTransition, { persisted: () => ("") }, {
28+
"default": () => {
29+
const n0 = t0()
30+
_applyVShow(n0, () => (_ctx.ok))
1131
return n0
1232
}
1333
}, true)

packages/compiler-vapor/src/compile.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { transformVFor } from './transforms/vFor'
2626
import { transformComment } from './transforms/transformComment'
2727
import { transformSlotOutlet } from './transforms/transformSlotOutlet'
2828
import { transformVSlot } from './transforms/vSlot'
29+
import { transformTransition } from './transforms/transformTransition'
2930
import type { HackOptions } from './ir'
3031

3132
export { wrapTemplate } from './transforms/utils'
@@ -54,6 +55,7 @@ export function compile(
5455
extend({}, resolvedOptions, {
5556
nodeTransforms: [
5657
...nodeTransforms,
58+
...(__DEV__ ? [transformTransition] : []),
5759
...(options.nodeTransforms || []), // user transforms
5860
],
5961
directiveTransforms: extend(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { NodeTransform } from '@vue/compiler-vapor'
2+
import { ElementTypes, NodeTypes } from '@vue/compiler-core'
3+
import { isTransitionTag } from '../utils'
4+
import { postTransformTransition } from '@vue/compiler-dom'
5+
6+
export const transformTransition: NodeTransform = (node, context) => {
7+
if (
8+
node.type === NodeTypes.ELEMENT &&
9+
node.tagType === ElementTypes.COMPONENT
10+
) {
11+
if (isTransitionTag(node.tag)) {
12+
return postTransformTransition(node, context.options.onError)
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)