Skip to content

Commit 67e85de

Browse files
committed
feat: adjust v-slot per RFC + enable flag
1 parent ec84032 commit 67e85de

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

Diff for: scripts/feature-flags.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
NEW_SLOT_SYNTAX: false
2+
NEW_SLOT_SYNTAX: true
33
}

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

+21-12
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,13 @@ function processSlotContent (el) {
599599
// v-slot on <template>
600600
const slotBinding = getAndRemoveAttrByRegex(el, slotRE)
601601
if (slotBinding) {
602-
if (
603-
process.env.NODE_ENV !== 'production' &&
604-
(el.slotTarget || el.slotScope)
605-
) {
606-
warn(
607-
`Unexpected mixed usage of different slot syntaxes.`,
608-
el
609-
)
602+
if (process.env.NODE_ENV !== 'production') {
603+
if (el.slotTarget || el.slotScope) {
604+
warn(
605+
`Unexpected mixed usage of different slot syntaxes.`,
606+
el
607+
)
608+
}
610609
}
611610
el.slotTarget = getSlotName(slotBinding)
612611
el.slotScope = slotBinding.value
@@ -618,7 +617,7 @@ function processSlotContent (el) {
618617
if (process.env.NODE_ENV !== 'production') {
619618
if (!maybeComponent(el)) {
620619
warn(
621-
`v-slot cannot be used on non-component elements.`,
620+
`v-slot can only be used on components or <template>.`,
622621
slotBinding
623622
)
624623
}
@@ -644,13 +643,23 @@ function processSlotContent (el) {
644643
}
645644
}
646645

647-
function getSlotName ({ name }) {
648-
name = name.replace(slotRE, '')
646+
function getSlotName (binding) {
647+
let name = binding.name.replace(slotRE, '')
648+
if (!name) {
649+
if (binding.name[0] !== '#') {
650+
name = 'default'
651+
} else if (process.env.NODE_ENV !== 'production') {
652+
warn(
653+
`v-slot shorthand syntax requires a slot name.`,
654+
binding
655+
)
656+
}
657+
}
649658
return dynamicKeyRE.test(name)
650659
// dynamic [name]
651660
? name.slice(1, -1)
652661
// static name
653-
: `"${name || `default`}"`
662+
: `"${name}"`
654663
}
655664

656665
// handle <slot/> outlets

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,9 @@ describe('Component scoped slot', () => {
657657
}
658658
}
659659

660-
const toNamed = (syntax, name) => syntax.length === 1
661-
? syntax + name // shorthand
662-
: syntax + ':' + name // full syntax
660+
const toNamed = (syntax, name) => syntax[0] === '#'
661+
? `#${name}` // shorthand
662+
: `${syntax}:${name}` // full syntax
663663

664664
function runSuite(syntax) {
665665
it('default slot', () => {
@@ -689,7 +689,7 @@ describe('Component scoped slot', () => {
689689
it('default + named slots', () => {
690690
const vm = new Vue({
691691
template: `
692-
<foo #="foo">
692+
<foo #default="foo">
693693
{{ foo }}
694694
<template ${toNamed(syntax, 'one')}="one">
695695
{{ one }}
@@ -729,7 +729,7 @@ describe('Component scoped slot', () => {
729729
const vm = new Vue({
730730
template: `<div ${syntax}="foo"/>`
731731
}).$mount()
732-
expect(`v-slot cannot be used on non-component elements`).toHaveBeenWarned()
732+
expect(`v-slot can only be used on components or <template>`).toHaveBeenWarned()
733733
})
734734

735735
it('should warn mixed usage', () => {
@@ -743,12 +743,12 @@ describe('Component scoped slot', () => {
743743

744744
// run tests for both full syntax and shorthand
745745
runSuite('v-slot')
746-
runSuite('#')
746+
runSuite('#default')
747747

748748
it('shorthand named slots', () => {
749749
const vm = new Vue({
750750
template: `
751-
<foo #="foo">
751+
<foo #default="foo">
752752
{{ foo }}
753753
<template #one="one">
754754
{{ one }}

0 commit comments

Comments
 (0)