Jelajahi Sumber

fix: fix v-for alias deconstruct regression

fix #7096
Evan You 8 tahun lalu
induk
melakukan
ebcef58645
2 mengubah file dengan 19 tambahan dan 2 penghapusan
  1. 1 1
      src/compiler/error-detector.js
  2. 18 1
      test/unit/features/directives/for.spec.js

+ 1 - 1
src/compiler/error-detector.js

@@ -80,7 +80,7 @@ function checkIdentifier (
 ) {
   if (typeof ident === 'string') {
     try {
-      new Function(`var ${ident}`)
+      new Function(`var ${ident}=_`)
     } catch (e) {
       errors.push(`invalid ${type} "${ident}" in expression: ${text.trim()}`)
     }

+ 18 - 1
test/unit/features/directives/for.spec.js

@@ -446,7 +446,7 @@ describe('Directive v-for', () => {
     }).then(done)
   })
 
-  it('strings', done => {
+  it('should work with strings', done => {
     const vm = new Vue({
       data: {
         text: 'foo'
@@ -463,4 +463,21 @@ describe('Directive v-for', () => {
       expect(vm.$el.textContent).toMatch('f.o.o.b.a.r.')
     }).then(done)
   })
+
+  const supportsDeconstruct = (() => {
+    try {
+      new Function('var { foo } = bar')
+      return true
+    } catch (e) {}
+  })()
+
+  if (supportsDeconstruct) {
+    it('should support deconstruct syntax in alias position', () => {
+      const vm = new Vue({
+        data: { list: [{ foo: 'hi' }] },
+        template: '<div><div v-for="({ foo }, i) in list">{{ foo }}{{ i }}</div></div>'
+      }).$mount()
+      expect(vm.$el.textContent).toBe('hi0')
+    })
+  }
 })