Skip to content

Commit b5f1f05

Browse files
committed
fix(warn): do not warn when destructuring in v-for
Fixes vuejs#7096
1 parent f9f7423 commit b5f1f05

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/compiler/error-detector.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ function checkIdentifier (
8080
) {
8181
if (typeof ident === 'string') {
8282
try {
83-
new Function(`var ${ident}`)
83+
// #7096 use an array because it makes it possible to destructure
84+
// arrays and objects:
85+
// var { foo } = [] works
86+
// var [ foo ] = [] works
87+
new Function(`var ${ident} = []`)
8488
} catch (e) {
8589
errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
8690
}

test/unit/features/options/template.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ describe('Options template', () => {
7878
expect('Raw expression: v-for="(1, 2) in a----"').toHaveBeenWarned()
7979
})
8080

81+
// #7096
82+
it('should not warn with object destructuring in v-for', () => {
83+
new Vue({
84+
data: { items: [] },
85+
template: '<div><div v-for="{ foo } in items"></div></div>'
86+
}).$mount()
87+
expect('Error compiling template').not.toHaveBeenWarned()
88+
expect('invalid v-for alias ').not.toHaveBeenWarned()
89+
})
90+
91+
it('should not warn with array destructuring in v-for', () => {
92+
new Vue({
93+
data: { items: [] },
94+
template: '<div><div v-for="[ foo ] in items"></div></div>'
95+
}).$mount()
96+
expect('Error compiling template').not.toHaveBeenWarned()
97+
expect('invalid v-for alias ').not.toHaveBeenWarned()
98+
})
99+
81100
it('warn error in generated function (v-on)', () => {
82101
new Vue({
83102
template: `<div @click="delete('Delete')"></div>`,

0 commit comments

Comments
 (0)