Skip to content

Commit d855027

Browse files
committed
test: more test cases for $slot usage detection
1 parent 1e5174d commit d855027

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Diff for: src/compiler/parser/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ function childrenHas$Slot (el): boolean {
617617
return el.children ? el.children.some(nodeHas$Slot) : false
618618
}
619619

620-
const $slotRE = /\$slot/
620+
const $slotRE = /(^|[^\w_$])\$slot($|[^\w_$])/
621621
function nodeHas$Slot (node): boolean {
622622
// caching
623623
if (hasOwn(node, 'has$Slot')) {

Diff for: test/unit/features/component/component-scoped-slot.spec.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,33 @@ describe('Component scoped slot', () => {
690690
expect(vm.$el.innerHTML).toBe(`default<div>default</div><div>static</div>`)
691691
})
692692

693+
// testing $slot detection: bracket access, using $slot alone, passing as arguments...
694+
it('should work for alternative $slot usage', () => {
695+
const vm = new Vue({
696+
template: `<foo>{{ $slot['foo'] }}<div slot="foo">{{ $slot }}</div><div>{{ pick($slot) }}</div></foo>`,
697+
methods: { pick: s => s.foo },
698+
components: { foo: { template: `<div><slot foo="default"/><slot name="foo"/></div>` }}
699+
}).$mount()
700+
expect(vm.$el.innerHTML).toBe(`default<div>default</div><div>{}</div>`)
701+
})
702+
703+
// should not consider detection if $slot is inside longer valid identifier
693704
it('should not break when template expression uses $slots', () => {
694705
const vm = new Vue({
695-
template: ``
696-
})
706+
data: { some$slot: 123 },
707+
template: `<foo>{{ some$slot }}<div slot="foo">{{ $slots }}</div></foo>`,
708+
components: {
709+
foo: {
710+
render(h) {
711+
// should be compiled as normal slots
712+
expect(this.$slots.default).toBeTruthy()
713+
expect(this.$slots.foo).toBeTruthy()
714+
return h('div', [this.$slots.default, this.$slots.foo])
715+
}
716+
}
717+
}
718+
}).$mount()
719+
expect(vm.$el.innerHTML).toBe(`123<div>{}</div>`)
697720
})
698721
})
699722
})

0 commit comments

Comments
 (0)