Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit aef8077

Browse files
committed
refactor(compiler): move patchFlag text generation to codegen phase
1 parent f05b3ae commit aef8077

File tree

18 files changed

+192
-209
lines changed

18 files changed

+192
-209
lines changed

packages/compiler-core/__tests__/__snapshots__/codegen.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ return function render(_ctx, _cache) {
5454
[foo + bar]: bar
5555
}, [
5656
_createElementVNode("p", { "some-key": "foo" })
57-
], 16)
57+
], 16 /* FULL_PROPS */)
5858
}
5959
}"
6060
`;
@@ -98,7 +98,7 @@ exports[`compiler: codegen > forNode 1`] = `
9898
"
9999
return function render(_ctx, _cache) {
100100
with (_ctx) {
101-
return (_openBlock(true), _createElementBlock(_Fragment, null, _renderList(), 1))
101+
return (_openBlock(true), _createElementBlock(_Fragment, null, _renderList(), 1 /* TEXT */))
102102
}
103103
}"
104104
`;

packages/compiler-core/__tests__/codegen.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe('compiler: codegen', () => {
267267
disableTracking: true,
268268
props: undefined,
269269
children: createCallExpression(RENDER_LIST),
270-
patchFlag: '1',
270+
patchFlag: PatchFlags.TEXT,
271271
dynamicProps: undefined,
272272
directives: undefined,
273273
loc: locStub,
@@ -303,7 +303,7 @@ describe('compiler: codegen', () => {
303303
disableTracking: false,
304304
props: undefined,
305305
children: createCallExpression(RENDER_LIST),
306-
patchFlag: genFlagText(PatchFlags.STABLE_FRAGMENT),
306+
patchFlag: PatchFlags.STABLE_FRAGMENT,
307307
dynamicProps: undefined,
308308
directives: undefined,
309309
loc: locStub,
@@ -364,7 +364,7 @@ describe('compiler: codegen', () => {
364364
),
365365
],
366366
// flag
367-
PatchFlags.FULL_PROPS + '',
367+
PatchFlags.FULL_PROPS,
368368
),
369369
}),
370370
)
@@ -375,7 +375,7 @@ describe('compiler: codegen', () => {
375375
[foo + bar]: bar
376376
}, [
377377
_${helperNameMap[CREATE_ELEMENT_VNODE]}("p", { "some-key": "foo" })
378-
], ${PatchFlags.FULL_PROPS})`)
378+
], ${genFlagText(PatchFlags.FULL_PROPS)})`)
379379
expect(code).toMatchSnapshot()
380380
})
381381

@@ -666,11 +666,14 @@ describe('compiler: codegen', () => {
666666
})
667667

668668
test('with patchFlag and no children/props', () => {
669-
expect(genCode(createVNodeCall(null, `"div"`, undefined, undefined, '1')))
670-
.toMatchInlineSnapshot(`
671-
"return _createElementVNode("div", null, null, 1)
672-
"
673-
`)
669+
expect(
670+
genCode(
671+
createVNodeCall(null, `"div"`, undefined, undefined, PatchFlags.TEXT),
672+
),
673+
).toMatchInlineSnapshot(`
674+
"return _createElementVNode("div", null, null, 1 /* TEXT */)
675+
"
676+
`)
674677
})
675678

676679
test('as block', () => {

packages/compiler-core/__tests__/transform.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { transformFor } from '../src/transforms/vFor'
1919
import { transformElement } from '../src/transforms/transformElement'
2020
import { transformSlotOutlet } from '../src/transforms/transformSlotOutlet'
2121
import { transformText } from '../src/transforms/transformText'
22-
import { genFlagText } from './testUtils'
2322
import { PatchFlags } from '@vue/shared'
2423

2524
describe('compiler: transform', () => {
@@ -358,7 +357,7 @@ describe('compiler: transform', () => {
358357
{ type: NodeTypes.ELEMENT, tag: `div` },
359358
{ type: NodeTypes.ELEMENT, tag: `div` },
360359
] as any,
361-
genFlagText(PatchFlags.STABLE_FRAGMENT),
360+
PatchFlags.STABLE_FRAGMENT,
362361
),
363362
)
364363
})
@@ -374,10 +373,7 @@ describe('compiler: transform', () => {
374373
{ type: NodeTypes.ELEMENT, tag: `div` },
375374
{ type: NodeTypes.COMMENT },
376375
] as any,
377-
genFlagText([
378-
PatchFlags.STABLE_FRAGMENT,
379-
PatchFlags.DEV_ROOT_FRAGMENT,
380-
]),
376+
PatchFlags.STABLE_FRAGMENT | PatchFlags.DEV_ROOT_FRAGMENT,
381377
),
382378
)
383379
})

packages/compiler-core/__tests__/transforms/hoistStatic.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { transformIf } from '../../src/transforms/vIf'
2121
import { transformFor } from '../../src/transforms/vFor'
2222
import { transformBind } from '../../src/transforms/vBind'
2323
import { transformOn } from '../../src/transforms/vOn'
24-
import { createObjectMatcher, genFlagText } from '../testUtils'
24+
import { createObjectMatcher } from '../testUtils'
2525
import { transformText } from '../../src/transforms/transformText'
2626
import { PatchFlags } from '@vue/shared'
2727

@@ -180,7 +180,7 @@ describe('compiler: hoistStatic transform', () => {
180180
id: `[foo]`,
181181
}),
182182
children: undefined,
183-
patchFlag: genFlagText(PatchFlags.PROPS),
183+
patchFlag: PatchFlags.PROPS,
184184
dynamicProps: {
185185
type: NodeTypes.SIMPLE_EXPRESSION,
186186
content: `_hoisted_1`,
@@ -242,7 +242,7 @@ describe('compiler: hoistStatic transform', () => {
242242
ref: `[foo]`,
243243
}),
244244
children: undefined,
245-
patchFlag: genFlagText(PatchFlags.NEED_PATCH),
245+
patchFlag: PatchFlags.NEED_PATCH,
246246
},
247247
},
248248
])
@@ -263,7 +263,7 @@ describe('compiler: hoistStatic transform', () => {
263263
content: `_hoisted_1`,
264264
},
265265
children: undefined,
266-
patchFlag: genFlagText(PatchFlags.NEED_PATCH),
266+
patchFlag: PatchFlags.NEED_PATCH,
267267
directives: {
268268
type: NodeTypes.JS_ARRAY_EXPRESSION,
269269
},
@@ -286,7 +286,7 @@ describe('compiler: hoistStatic transform', () => {
286286
tag: `"div"`,
287287
props: { content: `_hoisted_1` },
288288
children: { type: NodeTypes.INTERPOLATION },
289-
patchFlag: genFlagText(PatchFlags.TEXT),
289+
patchFlag: PatchFlags.TEXT,
290290
},
291291
},
292292
])
@@ -365,7 +365,7 @@ describe('compiler: hoistStatic transform', () => {
365365
type: NodeTypes.JS_CALL_EXPRESSION,
366366
callee: RENDER_LIST,
367367
},
368-
patchFlag: genFlagText(PatchFlags.UNKEYED_FRAGMENT),
368+
patchFlag: PatchFlags.UNKEYED_FRAGMENT,
369369
})
370370
const innerBlockCodegen = forBlockCodegen!.children.arguments[1]
371371
expect(innerBlockCodegen.returns).toMatchObject({
@@ -496,7 +496,7 @@ describe('compiler: hoistStatic transform', () => {
496496
constType: ConstantTypes.NOT_CONSTANT,
497497
},
498498
},
499-
patchFlag: `1 /* TEXT */`,
499+
patchFlag: PatchFlags.TEXT,
500500
},
501501
},
502502
],

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { transformStyle } from '../../../compiler-dom/src/transforms/transformSt
3737
import { transformOn } from '../../src/transforms/vOn'
3838
import { transformBind } from '../../src/transforms/vBind'
3939
import { PatchFlags } from '@vue/shared'
40-
import { createObjectMatcher, genFlagText } from '../testUtils'
40+
import { createObjectMatcher } from '../testUtils'
4141
import { transformText } from '../../src/transforms/transformText'
4242
import { parseWithForTransform } from './vFor.spec'
4343

@@ -521,7 +521,7 @@ describe('compiler: element transform', () => {
521521
// keep-alive should not compile content to slots
522522
children: [{ type: NodeTypes.ELEMENT, tag: 'span' }],
523523
// should get a dynamic slots flag to force updates
524-
patchFlag: genFlagText(PatchFlags.DYNAMIC_SLOTS),
524+
patchFlag: PatchFlags.DYNAMIC_SLOTS,
525525
})
526526
}
527527

@@ -588,7 +588,7 @@ describe('compiler: element transform', () => {
588588
})
589589
// should factor in props returned by custom directive transforms
590590
// in patchFlag analysis
591-
expect(node.patchFlag).toMatch(PatchFlags.PROPS + '')
591+
expect(node.patchFlag).toBe(PatchFlags.PROPS)
592592
expect(node.dynamicProps).toMatch(`"bar"`)
593593
})
594594

@@ -612,7 +612,7 @@ describe('compiler: element transform', () => {
612612
tag: `"div"`,
613613
props: undefined,
614614
children: undefined,
615-
patchFlag: genFlagText(PatchFlags.NEED_PATCH), // should generate appropriate flag
615+
patchFlag: PatchFlags.NEED_PATCH, // should generate appropriate flag
616616
directives: {
617617
type: NodeTypes.JS_ARRAY_EXPRESSION,
618618
elements: [
@@ -945,26 +945,26 @@ describe('compiler: element transform', () => {
945945
expect(node.patchFlag).toBeUndefined()
946946

947947
const { node: node2 } = parseWithBind(`<div>{{ foo }}</div>`)
948-
expect(node2.patchFlag).toBe(genFlagText(PatchFlags.TEXT))
948+
expect(node2.patchFlag).toBe(PatchFlags.TEXT)
949949

950950
// multiple nodes, merged with optimize text
951951
const { node: node3 } = parseWithBind(`<div>foo {{ bar }} baz</div>`)
952-
expect(node3.patchFlag).toBe(genFlagText(PatchFlags.TEXT))
952+
expect(node3.patchFlag).toBe(PatchFlags.TEXT)
953953
})
954954

955955
test('CLASS', () => {
956956
const { node } = parseWithBind(`<div :class="foo" />`)
957-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.CLASS))
957+
expect(node.patchFlag).toBe(PatchFlags.CLASS)
958958
})
959959

960960
test('STYLE', () => {
961961
const { node } = parseWithBind(`<div :style="foo" />`)
962-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.STYLE))
962+
expect(node.patchFlag).toBe(PatchFlags.STYLE)
963963
})
964964

965965
test('PROPS', () => {
966966
const { node } = parseWithBind(`<div id="foo" :foo="bar" :baz="qux" />`)
967-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.PROPS))
967+
expect(node.patchFlag).toBe(PatchFlags.PROPS)
968968
expect(node.dynamicProps).toBe(`["foo", "baz"]`)
969969
})
970970

@@ -973,7 +973,7 @@ describe('compiler: element transform', () => {
973973
`<div id="foo" :class="cls" :style="styl" :foo="bar" :baz="qux"/>`,
974974
)
975975
expect(node.patchFlag).toBe(
976-
genFlagText([PatchFlags.CLASS, PatchFlags.STYLE, PatchFlags.PROPS]),
976+
PatchFlags.CLASS | PatchFlags.STYLE | PatchFlags.PROPS,
977977
)
978978
expect(node.dynamicProps).toBe(`["foo", "baz"]`)
979979
})
@@ -983,40 +983,40 @@ describe('compiler: element transform', () => {
983983
const { node } = parseWithBind(
984984
`<Foo :id="foo" :class="cls" :style="styl" />`,
985985
)
986-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.PROPS))
986+
expect(node.patchFlag).toBe(PatchFlags.PROPS)
987987
expect(node.dynamicProps).toBe(`["id", "class", "style"]`)
988988
})
989989

990990
test('FULL_PROPS (v-bind)', () => {
991991
const { node } = parseWithBind(`<div v-bind="foo" />`)
992-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.FULL_PROPS))
992+
expect(node.patchFlag).toBe(PatchFlags.FULL_PROPS)
993993
})
994994

995995
test('FULL_PROPS (dynamic key)', () => {
996996
const { node } = parseWithBind(`<div :[foo]="bar" />`)
997-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.FULL_PROPS))
997+
expect(node.patchFlag).toBe(PatchFlags.FULL_PROPS)
998998
})
999999

10001000
test('FULL_PROPS (w/ others)', () => {
10011001
const { node } = parseWithBind(
10021002
`<div id="foo" v-bind="bar" :class="cls" />`,
10031003
)
1004-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.FULL_PROPS))
1004+
expect(node.patchFlag).toBe(PatchFlags.FULL_PROPS)
10051005
})
10061006

10071007
test('NEED_PATCH (static ref)', () => {
10081008
const { node } = parseWithBind(`<div ref="foo" />`)
1009-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH))
1009+
expect(node.patchFlag).toBe(PatchFlags.NEED_PATCH)
10101010
})
10111011

10121012
test('NEED_PATCH (dynamic ref)', () => {
10131013
const { node } = parseWithBind(`<div :ref="foo" />`)
1014-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH))
1014+
expect(node.patchFlag).toBe(PatchFlags.NEED_PATCH)
10151015
})
10161016

10171017
test('NEED_PATCH (custom directives)', () => {
10181018
const { node } = parseWithBind(`<div v-foo />`)
1019-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH))
1019+
expect(node.patchFlag).toBe(PatchFlags.NEED_PATCH)
10201020
})
10211021

10221022
test('NEED_PATCH (vnode hooks)', () => {
@@ -1025,7 +1025,7 @@ describe('compiler: element transform', () => {
10251025
cacheHandlers: true,
10261026
}).ast
10271027
const node = (root as any).children[0].codegenNode
1028-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_PATCH))
1028+
expect(node.patchFlag).toBe(PatchFlags.NEED_PATCH)
10291029
})
10301030

10311031
test('script setup inline mode template ref (binding exists)', () => {
@@ -1120,7 +1120,7 @@ describe('compiler: element transform', () => {
11201120
},
11211121
})
11221122
// should only have props flag
1123-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.PROPS))
1123+
expect(node.patchFlag).toBe(PatchFlags.PROPS)
11241124

11251125
const { node: node2 } = parseWithElementTransform(
11261126
`<div @keyup="foo" />`,
@@ -1130,21 +1130,15 @@ describe('compiler: element transform', () => {
11301130
},
11311131
},
11321132
)
1133-
expect(node2.patchFlag).toBe(
1134-
genFlagText([PatchFlags.PROPS, PatchFlags.NEED_HYDRATION]),
1135-
)
1133+
expect(node2.patchFlag).toBe(PatchFlags.PROPS | PatchFlags.NEED_HYDRATION)
11361134
})
11371135

11381136
test('NEED_HYDRATION for v-bind.prop', () => {
11391137
const { node } = parseWithBind(`<div v-bind:id.prop="id" />`)
1140-
expect(node.patchFlag).toBe(
1141-
genFlagText([PatchFlags.PROPS, PatchFlags.NEED_HYDRATION]),
1142-
)
1138+
expect(node.patchFlag).toBe(PatchFlags.PROPS | PatchFlags.NEED_HYDRATION)
11431139

11441140
const { node: node2 } = parseWithBind(`<div .id="id" />`)
1145-
expect(node2.patchFlag).toBe(
1146-
genFlagText([PatchFlags.PROPS, PatchFlags.NEED_HYDRATION]),
1147-
)
1141+
expect(node2.patchFlag).toBe(PatchFlags.PROPS | PatchFlags.NEED_HYDRATION)
11481142
})
11491143

11501144
// #5870
@@ -1157,9 +1151,7 @@ describe('compiler: element transform', () => {
11571151
},
11581152
},
11591153
)
1160-
expect(node.patchFlag).toBe(
1161-
genFlagText([PatchFlags.PROPS, PatchFlags.NEED_HYDRATION]),
1162-
)
1154+
expect(node.patchFlag).toBe(PatchFlags.PROPS | PatchFlags.NEED_HYDRATION)
11631155
})
11641156

11651157
test('should not have PROPS patchflag for constant v-on handlers', () => {
@@ -1173,7 +1165,7 @@ describe('compiler: element transform', () => {
11731165
},
11741166
})
11751167
// should only have hydration flag
1176-
expect(node.patchFlag).toBe(genFlagText(PatchFlags.NEED_HYDRATION))
1168+
expect(node.patchFlag).toBe(PatchFlags.NEED_HYDRATION)
11771169
})
11781170
})
11791171

0 commit comments

Comments
 (0)