File tree 3 files changed +35
-13
lines changed
3 files changed +35
-13
lines changed Original file line number Diff line number Diff line change @@ -99,15 +99,26 @@ describe('compiler: v-once transform', () => {
99
99
expect ( generate ( root ) . code ) . toMatchSnapshot ( )
100
100
} )
101
101
102
- test ( 'with v-if' , ( ) => {
103
- const root = transformWithOnce ( `<div v-if="true " v-once />` )
102
+ test ( 'with v-if/else ' , ( ) => {
103
+ const root = transformWithOnce ( `<div v-if="BOOLEAN " v-once /><p v-else />` )
104
104
expect ( root . cached ) . toBe ( 1 )
105
105
expect ( root . helpers ) . toContain ( SET_BLOCK_TRACKING )
106
106
expect ( root . children [ 0 ] ) . toMatchObject ( {
107
107
type : NodeTypes . IF ,
108
- // should cache the entire v-if expression, not just a single branch
108
+ // should cache the entire v-if/else-if/else expression, not just a single branch
109
109
codegenNode : {
110
- type : NodeTypes . JS_CACHE_EXPRESSION
110
+ type : NodeTypes . JS_CACHE_EXPRESSION ,
111
+ value : {
112
+ type : NodeTypes . JS_CONDITIONAL_EXPRESSION ,
113
+ consequent : {
114
+ type : NodeTypes . VNODE_CALL ,
115
+ tag : `"div"`
116
+ } ,
117
+ alternate : {
118
+ type : NodeTypes . VNODE_CALL ,
119
+ tag : `"p"`
120
+ }
121
+ }
111
122
}
112
123
} )
113
124
} )
Original file line number Diff line number Diff line change @@ -236,7 +236,7 @@ export interface CompoundExpressionNode extends Node {
236
236
export interface IfNode extends Node {
237
237
type : NodeTypes . IF
238
238
branches : IfBranchNode [ ]
239
- codegenNode ?: IfConditionalExpression
239
+ codegenNode ?: IfConditionalExpression | CacheExpression // <div v-if v-once>
240
240
}
241
241
242
242
export interface IfBranchNode extends Node {
Original file line number Diff line number Diff line change @@ -20,7 +20,8 @@ import {
20
20
IfNode ,
21
21
createVNodeCall ,
22
22
AttributeNode ,
23
- locStub
23
+ locStub ,
24
+ CacheExpression
24
25
} from '../ast'
25
26
import { createCompilerError , ErrorCodes } from '../errors'
26
27
import { processExpression } from './transformExpression'
@@ -62,13 +63,7 @@ export const transformIf = createStructuralDirectiveTransform(
62
63
) as IfConditionalExpression
63
64
} else {
64
65
// attach this branch's codegen node to the v-if root.
65
- let parentCondition = ifNode . codegenNode !
66
- while (
67
- parentCondition . alternate . type ===
68
- NodeTypes . JS_CONDITIONAL_EXPRESSION
69
- ) {
70
- parentCondition = parentCondition . alternate
71
- }
66
+ const parentCondition = getParentCondition ( ifNode . codegenNode ! )
72
67
parentCondition . alternate = createCodegenNodeForBranch (
73
68
branch ,
74
69
key + ifNode . branches . length - 1 ,
@@ -293,3 +288,19 @@ function isSameKey(
293
288
}
294
289
return true
295
290
}
291
+
292
+ function getParentCondition (
293
+ node : IfConditionalExpression | CacheExpression
294
+ ) : IfConditionalExpression {
295
+ while ( true ) {
296
+ if ( node . type === NodeTypes . JS_CONDITIONAL_EXPRESSION ) {
297
+ if ( node . alternate . type === NodeTypes . JS_CONDITIONAL_EXPRESSION ) {
298
+ node = node . alternate
299
+ } else {
300
+ return node
301
+ }
302
+ } else if ( node . type === NodeTypes . JS_CACHE_EXPRESSION ) {
303
+ node = node . value as IfConditionalExpression
304
+ }
305
+ }
306
+ }
You can’t perform that action at this time.
0 commit comments