@@ -7,7 +7,20 @@ import {
7
7
ExpressionNode
8
8
} from '../src/ast'
9
9
import { ErrorCodes , createCompilerError } from '../src/errors'
10
- import { TO_STRING , CREATE_VNODE , COMMENT } from '../src/runtimeConstants'
10
+ import {
11
+ TO_STRING ,
12
+ CREATE_VNODE ,
13
+ COMMENT ,
14
+ OPEN_BLOCK ,
15
+ CREATE_BLOCK ,
16
+ FRAGMENT ,
17
+ RENDER_SLOT
18
+ } from '../src/runtimeConstants'
19
+ import { transformIf } from '../src/transforms/vIf'
20
+ import { transformFor } from '../src/transforms/vFor'
21
+ import { transformElement } from '../src/transforms/transformElement'
22
+ import { transformSlotOutlet } from '../src/transforms/transfromSlotOutlet'
23
+ import { optimizeText } from '../src/transforms/optimizeText'
11
24
12
25
describe ( 'compiler: transform' , ( ) => {
13
26
test ( 'context state' , ( ) => {
@@ -221,4 +234,110 @@ describe('compiler: transform', () => {
221
234
expect ( ast . imports ) . toContain ( CREATE_VNODE )
222
235
expect ( ast . imports ) . toContain ( COMMENT )
223
236
} )
237
+
238
+ describe ( 'root codegenNode' , ( ) => {
239
+ function transformWithCodegen ( template : string ) {
240
+ const ast = parse ( template )
241
+ transform ( ast , {
242
+ nodeTransforms : [
243
+ transformIf ,
244
+ transformFor ,
245
+ optimizeText ,
246
+ transformSlotOutlet ,
247
+ transformElement
248
+ ]
249
+ } )
250
+ return ast
251
+ }
252
+
253
+ function createBlockMatcher ( args : any [ ] ) {
254
+ return {
255
+ type : NodeTypes . JS_SEQUENCE_EXPRESSION ,
256
+ expressions : [
257
+ {
258
+ type : NodeTypes . JS_CALL_EXPRESSION ,
259
+ callee : `_${ OPEN_BLOCK } `
260
+ } ,
261
+ {
262
+ type : NodeTypes . JS_CALL_EXPRESSION ,
263
+ callee : `_${ CREATE_BLOCK } ` ,
264
+ arguments : args
265
+ }
266
+ ]
267
+ }
268
+ }
269
+
270
+ test ( 'no chidlren' , ( ) => {
271
+ const ast = transformWithCodegen ( `` )
272
+ expect ( ast . codegenNode ) . toBeUndefined ( )
273
+ } )
274
+
275
+ test ( 'single <slot/>' , ( ) => {
276
+ const ast = transformWithCodegen ( `<slot/>` )
277
+ expect ( ast . codegenNode ) . toMatchObject (
278
+ createBlockMatcher ( [
279
+ `_${ FRAGMENT } ` ,
280
+ `null` ,
281
+ {
282
+ type : NodeTypes . JS_CALL_EXPRESSION ,
283
+ callee : `_${ RENDER_SLOT } `
284
+ }
285
+ ] )
286
+ )
287
+ } )
288
+
289
+ test ( 'single element' , ( ) => {
290
+ const ast = transformWithCodegen ( `<div/>` )
291
+ expect ( ast . codegenNode ) . toMatchObject ( createBlockMatcher ( [ `"div"` ] ) )
292
+ } )
293
+
294
+ test ( 'root v-if' , ( ) => {
295
+ const ast = transformWithCodegen ( `<div v-if="ok" />` )
296
+ expect ( ast . codegenNode ) . toMatchObject ( {
297
+ type : NodeTypes . IF
298
+ } )
299
+ } )
300
+
301
+ test ( 'root v-for' , ( ) => {
302
+ const ast = transformWithCodegen ( `<div v-for="i in list" />` )
303
+ expect ( ast . codegenNode ) . toMatchObject ( {
304
+ type : NodeTypes . FOR
305
+ } )
306
+ } )
307
+
308
+ test ( 'single text' , ( ) => {
309
+ const ast = transformWithCodegen ( `hello` )
310
+ expect ( ast . codegenNode ) . toMatchObject ( {
311
+ type : NodeTypes . TEXT
312
+ } )
313
+ } )
314
+
315
+ test ( 'single interpolation' , ( ) => {
316
+ const ast = transformWithCodegen ( `{{ foo }}` )
317
+ expect ( ast . codegenNode ) . toMatchObject ( {
318
+ type : NodeTypes . INTERPOLATION
319
+ } )
320
+ } )
321
+
322
+ test ( 'single CompoundExpression' , ( ) => {
323
+ const ast = transformWithCodegen ( `{{ foo }} bar baz` )
324
+ expect ( ast . codegenNode ) . toMatchObject ( {
325
+ type : NodeTypes . COMPOUND_EXPRESSION
326
+ } )
327
+ } )
328
+
329
+ test ( 'multiple children' , ( ) => {
330
+ const ast = transformWithCodegen ( `<div/><div/>` )
331
+ expect ( ast . codegenNode ) . toMatchObject (
332
+ createBlockMatcher ( [
333
+ `_${ FRAGMENT } ` ,
334
+ `null` ,
335
+ [
336
+ { type : NodeTypes . ELEMENT , tag : `div` } ,
337
+ { type : NodeTypes . ELEMENT , tag : `div` }
338
+ ]
339
+ ] )
340
+ )
341
+ } )
342
+ } )
224
343
} )
0 commit comments