Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): throw error when using interpolation instead of expresssion (#9038) #9050

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ function processOnce (el) {
function processSlot (el) {
if (el.tag === 'slot') {
el.slotName = getBindingAttr(el, 'name')
// Fixes: #9038
if(process.env.NODE_ENV !== 'production'){
const slotName = el.attrsMap['name']
if(!dirRE.test(slotName) && parseText(slotName, delimiters)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why dirRE.test here? Seems unnecessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this to keep the same behavior as in place where previous warning exists (line 596). As you can see that previous warning is shown only when dirRE.test is false.

processSlot is executed outside of processAttrs thus I've added this additional check, to prevent double warnings

warn(
`name="${slotName}": ` +
'Interpolation inside attributes has been removed. ' +
'Use v-bind or the colon shorthand instead. For example, ' +
`instead of <slot name="{{ val }}">, use <slot :name="val">.`
)
}
}
if (process.env.NODE_ENV !== 'production' && el.key) {
warn(
`\`key\` does not work on <slot> because slots are abstract outlets ` +
Expand Down
25 changes: 25 additions & 0 deletions test/unit/modules/compiler/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,4 +737,29 @@ describe('parser', () => {
const ast = parse(`<p>{{\r\nmsg\r\n}}</p>`, baseOptions)
expect(ast.children[0].expression).toBe('_s(msg)')
})

// #9038
it('should warn if interpolation is used in slot name attribute', () => {
parse(`
<div class="wrapper">
<slot name="line-{{ index }}"></slot>
</div>`, baseOptions)

expect('name="line-{{ index }}": Interpolation inside attributes has been removed. ' +
'Use v-bind or the colon shorthand instead. ' +
'For example, instead of <slot name="{{ val }}">, use <slot :name="val">.')
.toHaveBeenWarned()
})

it('should not warn if interpolation is not used in slot name attribute', () => {
parse(`
<div class="wrapper">
<slot :name="'line-' + index"></slot>
</div>`, baseOptions)

expect('name="line-{{ index }}": Interpolation inside attributes has been removed. ' +
'Use v-bind or the colon shorthand instead. ' +
'For example, instead of <slot name="{{ val }}">, use <slot :name="val">.')
.not.toHaveBeenWarned()
})
})