1
- import { createSimpleExpression } from '@vue/compiler-dom '
1
+ import { ErrorCodes , NodeTypes } from '@vue/compiler-core '
2
2
import {
3
- type CreateComponentIRNode ,
4
3
IRNodeTypes ,
5
4
transformChildren ,
6
5
transformElement ,
@@ -36,10 +35,22 @@ describe('compiler: transform slot', () => {
36
35
expect ( code ) . toMatchSnapshot ( )
37
36
38
37
expect ( ir . template ) . toEqual ( [ '<div></div>' ] )
39
- expect ( ir . block . operation [ 0 ] . type ) . toBe ( IRNodeTypes . CREATE_COMPONENT_NODE )
40
- const slots = ( ir . block . operation [ 0 ] as CreateComponentIRNode ) . slots !
41
- expect ( slots . length ) . toBe ( 1 )
42
- expect ( slots [ 0 ] . name . content ) . toBe ( 'default' )
38
+ expect ( ir . block . operation ) . toMatchObject ( [
39
+ {
40
+ type : IRNodeTypes . CREATE_COMPONENT_NODE ,
41
+ id : 1 ,
42
+ tag : 'Comp' ,
43
+ props : [ [ ] ] ,
44
+ slots : {
45
+ default : {
46
+ type : IRNodeTypes . BLOCK ,
47
+ dynamic : {
48
+ children : [ { template : 0 } ] ,
49
+ } ,
50
+ } ,
51
+ } ,
52
+ } ,
53
+ ] )
43
54
expect ( ir . block . returns ) . toEqual ( [ 1 ] )
44
55
expect ( ir . block . dynamic ) . toMatchObject ( {
45
56
children : [ { id : 1 } ] ,
@@ -55,11 +66,28 @@ describe('compiler: transform slot', () => {
55
66
expect ( code ) . toMatchSnapshot ( )
56
67
57
68
expect ( ir . template ) . toEqual ( [ 'foo' , 'bar' , '<span></span>' ] )
58
- expect ( ir . block . operation [ 0 ] . type ) . toBe ( IRNodeTypes . CREATE_COMPONENT_NODE )
59
- const slots = ( ir . block . operation [ 0 ] as CreateComponentIRNode ) . slots !
60
- expect ( slots . length ) . toBe ( 2 )
61
- expect ( slots [ 0 ] . name . content ) . toBe ( 'one' )
62
- expect ( slots [ 1 ] . name . content ) . toBe ( 'default' )
69
+ expect ( ir . block . operation ) . toMatchObject ( [
70
+ {
71
+ type : IRNodeTypes . CREATE_COMPONENT_NODE ,
72
+ id : 4 ,
73
+ tag : 'Comp' ,
74
+ props : [ [ ] ] ,
75
+ slots : {
76
+ one : {
77
+ type : IRNodeTypes . BLOCK ,
78
+ dynamic : {
79
+ children : [ { template : 0 } ] ,
80
+ } ,
81
+ } ,
82
+ default : {
83
+ type : IRNodeTypes . BLOCK ,
84
+ dynamic : {
85
+ children : [ { } , { template : 1 } , { template : 2 } ] ,
86
+ } ,
87
+ } ,
88
+ } ,
89
+ } ,
90
+ ] )
63
91
} )
64
92
65
93
test ( 'nested slots' , ( ) => {
@@ -72,13 +100,75 @@ describe('compiler: transform slot', () => {
72
100
} )
73
101
74
102
test ( 'dynamic slots name' , ( ) => {
75
- const { ir, code } = compileWithSlots ( `<Comp>
76
- <template #[dynamicName]>foo</template>
77
- </Comp>` )
78
- expect ( ir . block . operation [ 0 ] . type ) . toBe ( IRNodeTypes . CREATE_COMPONENT_NODE )
79
- const slots = ( ir . block . operation [ 0 ] as CreateComponentIRNode ) . slots !
80
- expect ( slots . length ) . toBe ( 1 )
81
- expect ( slots [ 0 ] . name . isStatic ) . toBe ( false )
103
+ const { ir, code } = compileWithSlots (
104
+ `<Comp>
105
+ <template #[name]>foo</template>
106
+ </Comp>` ,
107
+ )
82
108
expect ( code ) . toMatchSnapshot ( )
109
+ expect ( ir . block . operation [ 0 ] . type ) . toBe ( IRNodeTypes . CREATE_COMPONENT_NODE )
110
+ expect ( ir . block . operation ) . toMatchObject ( [
111
+ {
112
+ type : IRNodeTypes . CREATE_COMPONENT_NODE ,
113
+ tag : 'Comp' ,
114
+ slots : undefined ,
115
+ dynamicSlots : [
116
+ {
117
+ name : {
118
+ type : NodeTypes . SIMPLE_EXPRESSION ,
119
+ content : 'name' ,
120
+ isStatic : false ,
121
+ } ,
122
+ fn : { type : IRNodeTypes . BLOCK } ,
123
+ } ,
124
+ ] ,
125
+ } ,
126
+ ] )
127
+ } )
128
+
129
+ describe ( 'errors' , ( ) => {
130
+ test ( 'error on extraneous children w/ named default slot' , ( ) => {
131
+ const onError = vi . fn ( )
132
+ const source = `<Comp><template #default>foo</template>bar</Comp>`
133
+ compileWithSlots ( source , { onError } )
134
+ const index = source . indexOf ( 'bar' )
135
+ expect ( onError . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
136
+ code : ErrorCodes . X_V_SLOT_EXTRANEOUS_DEFAULT_SLOT_CHILDREN ,
137
+ loc : {
138
+ start : {
139
+ offset : index ,
140
+ line : 1 ,
141
+ column : index + 1 ,
142
+ } ,
143
+ end : {
144
+ offset : index + 3 ,
145
+ line : 1 ,
146
+ column : index + 4 ,
147
+ } ,
148
+ } ,
149
+ } )
150
+ } )
151
+
152
+ test ( 'error on duplicated slot names' , ( ) => {
153
+ const onError = vi . fn ( )
154
+ const source = `<Comp><template #foo></template><template #foo></template></Comp>`
155
+ compileWithSlots ( source , { onError } )
156
+ const index = source . lastIndexOf ( '#foo' )
157
+ expect ( onError . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
158
+ code : ErrorCodes . X_V_SLOT_DUPLICATE_SLOT_NAMES ,
159
+ loc : {
160
+ start : {
161
+ offset : index ,
162
+ line : 1 ,
163
+ column : index + 1 ,
164
+ } ,
165
+ end : {
166
+ offset : index + 4 ,
167
+ line : 1 ,
168
+ column : index + 5 ,
169
+ } ,
170
+ } ,
171
+ } )
172
+ } )
83
173
} )
84
174
} )
0 commit comments