Skip to content

Commit 4bf7ba1

Browse files
authored
fix(compiler-core): detect v-if branch root with comment as dev fragment (#2785)
fix #2780
1 parent 3755e60 commit 4bf7ba1

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ return function render(_ctx, _cache) {
1616
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }, \\"yes\\"))
1717
: (_openBlock(), _createBlock(_Fragment, { key: 1 }, [
1818
_createTextVNode(\\"no\\")
19-
], 64 /* STABLE_FRAGMENT */)),
19+
], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */)),
2020
(_openBlock(true), _createBlock(_Fragment, null, _renderList(list, (value, index) => {
2121
return (_openBlock(), _createBlock(\\"div\\", null, [
2222
_createVNode(\\"span\\", null, _toDisplayString(value + index), 1 /* TEXT */)
@@ -40,7 +40,7 @@ return function render(_ctx, _cache) {
4040
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }, \\"yes\\"))
4141
: (_openBlock(), _createBlock(_Fragment, { key: 1 }, [
4242
_createTextVNode(\\"no\\")
43-
], 64 /* STABLE_FRAGMENT */)),
43+
], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */)),
4444
(_openBlock(true), _createBlock(_Fragment, null, _renderList(_ctx.list, (value, index) => {
4545
return (_openBlock(), _createBlock(\\"div\\", null, [
4646
_createVNode(\\"span\\", null, _toDisplayString(value + index), 1 /* TEXT */)
@@ -63,7 +63,7 @@ export function render(_ctx, _cache) {
6363
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }, \\"yes\\"))
6464
: (_openBlock(), _createBlock(_Fragment, { key: 1 }, [
6565
_createTextVNode(\\"no\\")
66-
], 64 /* STABLE_FRAGMENT */)),
66+
], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */)),
6767
(_openBlock(true), _createBlock(_Fragment, null, _renderList(_ctx.list, (value, index) => {
6868
return (_openBlock(), _createBlock(\\"div\\", null, [
6969
_createVNode(\\"span\\", null, _toDisplayString(value + index), 1 /* TEXT */)

packages/compiler-core/__tests__/transforms/__snapshots__/vIf.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ return function render(_ctx, _cache) {
111111
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }))
112112
: orNot
113113
? (_openBlock(), _createBlock(\\"p\\", { key: 1 }))
114-
: (_openBlock(), _createBlock(_Fragment, { key: 2 }, [\\"fine\\"], 64 /* STABLE_FRAGMENT */))
114+
: (_openBlock(), _createBlock(_Fragment, { key: 2 }, [\\"fine\\"], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */))
115115
}
116116
}"
117117
`;

packages/compiler-core/src/transforms/vIf.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,24 @@ function createChildrenCodegenNode(
246246
injectProp(vnodeCall, keyProperty, context)
247247
return vnodeCall
248248
} else {
249+
let patchFlag = PatchFlags.STABLE_FRAGMENT
250+
let patchFlagText = PatchFlagNames[PatchFlags.STABLE_FRAGMENT]
251+
// check if the fragment actually contains a single valid child with
252+
// the rest being comments
253+
if (
254+
__DEV__ &&
255+
children.filter(c => c.type !== NodeTypes.COMMENT).length === 1
256+
) {
257+
patchFlag |= PatchFlags.DEV_ROOT_FRAGMENT
258+
patchFlagText += `, ${PatchFlagNames[PatchFlags.DEV_ROOT_FRAGMENT]}`
259+
}
260+
249261
return createVNodeCall(
250262
context,
251263
helper(FRAGMENT),
252264
createObjectExpression([keyProperty]),
253265
children,
254-
PatchFlags.STABLE_FRAGMENT +
255-
(__DEV__
256-
? ` /* ${PatchFlagNames[PatchFlags.STABLE_FRAGMENT]} */`
257-
: ``),
266+
patchFlag + (__DEV__ ? ` /* ${patchFlagText} */` : ``),
258267
undefined,
259268
undefined,
260269
true,

0 commit comments

Comments
 (0)