Skip to content

Commit 919c447

Browse files
authored
fix(slots): make cache indexes marker non-enumerable (#13469)
close #13468
1 parent cb14b86 commit 919c447

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/runtime-core/__tests__/componentSlots.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
nodeOps,
77
ref,
88
render,
9+
useSlots,
910
} from '@vue/runtime-test'
1011
import { createBlock, normalizeVNode } from '../src/vnode'
1112
import { createSlots } from '../src/helpers/createSlots'
@@ -42,6 +43,29 @@ describe('component: slots', () => {
4243
expect(slots).toMatchObject({})
4344
})
4445

46+
test('initSlots: ensure compiler marker non-enumerable', () => {
47+
const Comp = {
48+
render() {
49+
const slots = useSlots()
50+
// Only user-defined slots should be enumerable
51+
expect(Object.keys(slots)).toEqual(['foo'])
52+
53+
// Internal compiler markers must still exist but be non-enumerable
54+
expect(slots).toHaveProperty('_')
55+
expect(Object.getOwnPropertyDescriptor(slots, '_')!.enumerable).toBe(
56+
false,
57+
)
58+
expect(slots).toHaveProperty('__')
59+
expect(Object.getOwnPropertyDescriptor(slots, '__')!.enumerable).toBe(
60+
false,
61+
)
62+
return h('div')
63+
},
64+
}
65+
const slots = { foo: () => {}, _: 1, __: [1] }
66+
render(createBlock(Comp, null, slots), nodeOps.createElement('div'))
67+
})
68+
4569
test('initSlots: should normalize object slots (when value is null, string, array)', () => {
4670
const { slots } = renderWithSlots({
4771
_inner: '_inner',

packages/runtime-core/src/componentSlots.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ export const initSlots = (
193193
): void => {
194194
const slots = (instance.slots = createInternalObject())
195195
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
196+
const cacheIndexes = (children as RawSlots).__
197+
// make cache indexes marker non-enumerable
198+
if (cacheIndexes) def(slots, '__', cacheIndexes, true)
199+
196200
const type = (children as RawSlots)._
197201
if (type) {
198202
assignSlots(slots, children as Slots, optimized)

0 commit comments

Comments
 (0)