Skip to content

Commit

Permalink
feat: adjust v-slot per RFC + enable flag
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 23, 2019
1 parent ec84032 commit 67e85de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion scripts/feature-flags.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
NEW_SLOT_SYNTAX: false
NEW_SLOT_SYNTAX: true
}
33 changes: 21 additions & 12 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,13 @@ function processSlotContent (el) {
// v-slot on <template>
const slotBinding = getAndRemoveAttrByRegex(el, slotRE)
if (slotBinding) {
if (
process.env.NODE_ENV !== 'production' &&
(el.slotTarget || el.slotScope)
) {
warn(
`Unexpected mixed usage of different slot syntaxes.`,
el
)
if (process.env.NODE_ENV !== 'production') {
if (el.slotTarget || el.slotScope) {
warn(
`Unexpected mixed usage of different slot syntaxes.`,
el
)
}
}
el.slotTarget = getSlotName(slotBinding)
el.slotScope = slotBinding.value
Expand All @@ -618,7 +617,7 @@ function processSlotContent (el) {
if (process.env.NODE_ENV !== 'production') {
if (!maybeComponent(el)) {
warn(
`v-slot cannot be used on non-component elements.`,
`v-slot can only be used on components or <template>.`,
slotBinding
)
}
Expand All @@ -644,13 +643,23 @@ function processSlotContent (el) {
}
}

function getSlotName ({ name }) {
name = name.replace(slotRE, '')
function getSlotName (binding) {
let name = binding.name.replace(slotRE, '')
if (!name) {
if (binding.name[0] !== '#') {
name = 'default'
} else if (process.env.NODE_ENV !== 'production') {
warn(
`v-slot shorthand syntax requires a slot name.`,
binding
)
}
}
return dynamicKeyRE.test(name)
// dynamic [name]
? name.slice(1, -1)
// static name
: `"${name || `default`}"`
: `"${name}"`
}

// handle <slot/> outlets
Expand Down
14 changes: 7 additions & 7 deletions test/unit/features/component/component-scoped-slot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,9 @@ describe('Component scoped slot', () => {
}
}

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

function runSuite(syntax) {
it('default slot', () => {
Expand Down Expand Up @@ -689,7 +689,7 @@ describe('Component scoped slot', () => {
it('default + named slots', () => {
const vm = new Vue({
template: `
<foo #="foo">
<foo #default="foo">
{{ foo }}
<template ${toNamed(syntax, 'one')}="one">
{{ one }}
Expand Down Expand Up @@ -729,7 +729,7 @@ describe('Component scoped slot', () => {
const vm = new Vue({
template: `<div ${syntax}="foo"/>`
}).$mount()
expect(`v-slot cannot be used on non-component elements`).toHaveBeenWarned()
expect(`v-slot can only be used on components or <template>`).toHaveBeenWarned()
})

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

// run tests for both full syntax and shorthand
runSuite('v-slot')
runSuite('#')
runSuite('#default')

it('shorthand named slots', () => {
const vm = new Vue({
template: `
<foo #="foo">
<foo #default="foo">

This comment has been minimized.

Copy link
@Justineo

Justineo Jan 23, 2019

Member

Shouldn't we enforce the use of <template> wrapper for the default slot when we have named slots? Otherwise the foo here will be easily assumed as accessible for other slots.

This comment has been minimized.

Copy link
@yyx990803

yyx990803 Jan 23, 2019

Author Member

Enforced in 8d84572

This comment has been minimized.

Copy link
@Justineo

Justineo Jan 23, 2019

Member

👍

{{ foo }}
<template #one="one">
{{ one }}
Expand Down

0 comments on commit 67e85de

Please sign in to comment.