Skip to content

Commit c7f21bb

Browse files
committed
feat(parser): throw error when using interpolation instead of JS expression for slot names (vuejs#9038)
1 parent 0008e0c commit c7f21bb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,18 @@ function processOnce (el) {
454454
function processSlot (el) {
455455
if (el.tag === 'slot') {
456456
el.slotName = getBindingAttr(el, 'name')
457+
// Fixes: #9038
458+
if(process.env.NODE_ENV !== 'production'){
459+
const slotName = el.attrsMap['name']
460+
if(!dirRE.test(slotName) && parseText(slotName, delimiters)){
461+
warn(
462+
`name="${slotName}": ` +
463+
'Interpolation inside attributes has been removed. ' +
464+
'Use v-bind or the colon shorthand instead. For example, ' +
465+
`instead of <slot name="{{ val }}">, use <slot :name="val">.`
466+
)
467+
}
468+
}
457469
if (process.env.NODE_ENV !== 'production' && el.key) {
458470
warn(
459471
`\`key\` does not work on <slot> because slots are abstract outlets ` +

Diff for: test/unit/modules/compiler/parser.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,29 @@ describe('parser', () => {
737737
const ast = parse(`<p>{{\r\nmsg\r\n}}</p>`, baseOptions)
738738
expect(ast.children[0].expression).toBe('_s(msg)')
739739
})
740+
741+
// #9038
742+
it('should warn if interpolation is used in slot name attribute', () => {
743+
parse(`
744+
<div class="wrapper">
745+
<slot name="line-{{ index }}"></slot>
746+
</div>`, baseOptions)
747+
748+
expect('name="line-{{ index }}": Interpolation inside attributes has been removed. ' +
749+
'Use v-bind or the colon shorthand instead. ' +
750+
'For example, instead of <slot name="{{ val }}">, use <slot :name="val">.')
751+
.toHaveBeenWarned()
752+
})
753+
754+
it('should not warn if interpolation is not used in slot name attribute', () => {
755+
parse(`
756+
<div class="wrapper">
757+
<slot :name="'line-' + index"></slot>
758+
</div>`, baseOptions)
759+
760+
expect('name="line-{{ index }}": Interpolation inside attributes has been removed. ' +
761+
'Use v-bind or the colon shorthand instead. ' +
762+
'For example, instead of <slot name="{{ val }}">, use <slot :name="val">.')
763+
.not.toHaveBeenWarned()
764+
})
740765
})

0 commit comments

Comments
 (0)