9
9
ForNode ,
10
10
ComponentNode ,
11
11
VNodeCall ,
12
- SlotsExpression
12
+ SlotsExpression ,
13
+ ObjectExpression ,
14
+ SimpleExpressionNode
13
15
} from '../../src'
14
16
import { transformElement } from '../../src/transforms/transformElement'
15
17
import { transformOn } from '../../src/transforms/vOn'
@@ -27,7 +29,9 @@ import { transformFor } from '../../src/transforms/vFor'
27
29
import { transformIf } from '../../src/transforms/vIf'
28
30
29
31
function parseWithSlots ( template : string , options : CompilerOptions = { } ) {
30
- const ast = parse ( template )
32
+ const ast = parse ( template , {
33
+ whitespace : options . whitespace
34
+ } )
31
35
transform ( ast , {
32
36
nodeTransforms : [
33
37
transformIf ,
@@ -862,4 +866,64 @@ describe('compiler: transform component slots', () => {
862
866
} )
863
867
} )
864
868
} )
869
+
870
+ describe ( `with whitespace: 'preserve'` , ( ) => {
871
+ test ( 'named default slot + implicit whitespace content' , ( ) => {
872
+ const source = `
873
+ <Comp>
874
+ <template #header> Header </template>
875
+ <template #default> Default </template>
876
+ </Comp>
877
+ `
878
+ const { root } = parseWithSlots ( source , {
879
+ whitespace : 'preserve'
880
+ } )
881
+
882
+ expect (
883
+ `Extraneous children found when component already has explicitly named default slot.`
884
+ ) . not . toHaveBeenWarned ( )
885
+ expect ( generate ( root , { prefixIdentifiers : true } ) . code ) . toMatchSnapshot ( )
886
+ } )
887
+
888
+ test ( 'implicit default slot' , ( ) => {
889
+ const source = `
890
+ <Comp>
891
+ <template #header> Header </template>
892
+ <p/>
893
+ </Comp>
894
+ `
895
+ const { root } = parseWithSlots ( source , {
896
+ whitespace : 'preserve'
897
+ } )
898
+
899
+ expect (
900
+ `Extraneous children found when component already has explicitly named default slot.`
901
+ ) . not . toHaveBeenWarned ( )
902
+ expect ( generate ( root , { prefixIdentifiers : true } ) . code ) . toMatchSnapshot ( )
903
+ } )
904
+
905
+ test ( 'should not generate whitespace only default slot' , ( ) => {
906
+ const source = `
907
+ <Comp>
908
+ <template #header> Header </template>
909
+ <template #footer> Footer </template>
910
+ </Comp>
911
+ `
912
+ const { root } = parseWithSlots ( source , {
913
+ whitespace : 'preserve'
914
+ } )
915
+
916
+ // slots is vnodeCall's children as an ObjectExpression
917
+ const slots = ( root as any ) . children [ 0 ] . codegenNode . children
918
+ . properties as ObjectExpression [ 'properties' ]
919
+
920
+ // should be: header, footer, _ (no default)
921
+ expect ( slots . length ) . toBe ( 3 )
922
+ expect (
923
+ slots . some ( p => ( p . key as SimpleExpressionNode ) . content === 'default' )
924
+ ) . toBe ( false )
925
+
926
+ expect ( generate ( root , { prefixIdentifiers : true } ) . code ) . toMatchSnapshot ( )
927
+ } )
928
+ } )
865
929
} )
0 commit comments