Преглед на файлове

better warning for fragment instance attributes

Evan You преди 10 години
родител
ревизия
778734aa88
променени са 3 файла, в които са добавени 23 реда и са изтрити 15 реда
  1. 11 10
      src/compiler/compile.js
  2. 4 2
      test/unit/lib/util.js
  3. 8 3
      test/unit/specs/compiler/compile_spec.js

+ 11 - 10
src/compiler/compile.js

@@ -208,16 +208,17 @@ exports.compileRoot = function (el, options, contextOptions) {
     }
   } else if (process.env.NODE_ENV !== 'production' && containerAttrs) {
     // warn container directives for fragment instances
-    containerAttrs.forEach(function (attr) {
-      if (attr.name.indexOf('v-') === 0 || attr.name === 'transition') {
-        _.warn(
-          attr.name + ' is ignored on component ' +
-          '<' + options.el.tagName.toLowerCase() + '> because ' +
-          'the component is a fragment instance: ' +
-          'http://vuejs.org/guide/components.html#Fragment_Instance'
-        )
-      }
-    })
+    var names = containerAttrs.map(function (attr) {
+      return '"' + attr.name + '"'
+    }).join(', ')
+    var plural = containerAttrs.length > 1
+    _.warn(
+      'Attribute' + (plural ? 's ' : ' ') + names +
+      (plural ? ' are' : ' is') + ' ignored on component ' +
+      '<' + options.el.tagName.toLowerCase() + '> because ' +
+      'the component is a fragment instance: ' +
+      'http://vuejs.org/guide/components.html#Fragment_Instance'
+    )
   }
 
   return function rootLinkFn (vm, el, scope) {

+ 4 - 2
test/unit/lib/util.js

@@ -2,7 +2,7 @@ var scope = typeof window === 'undefined'
   ? global
   : window
 
-scope.hasWarned = function (_, msg) {
+scope.hasWarned = function (_, msg, silent) {
   var count = _.warn.calls.count()
   while (count--) {
     var args = _.warn.calls.argsFor(count)
@@ -11,7 +11,9 @@ scope.hasWarned = function (_, msg) {
     }
   }
 
-  console.warn('[test] "' + msg + '" was never warned.')
+  if (!silent) {
+    console.warn('[test] "' + msg + '" was never warned.')
+  }
 
   function containsMsg (arg) {
     return arg.indexOf(msg) > -1

+ 8 - 3
test/unit/specs/compiler/compile_spec.js

@@ -517,15 +517,20 @@ if (_.inBrowser) {
     it('warn directives on fragment instances', function () {
       new Vue({
         el: el,
-        template: '<test v-show="ok"></test>',
+        template: '<test id="hi" class="ok" :prop="123"></test>',
         components: {
           test: {
             replace: true,
-            template: '123'
+            props: ['prop'],
+            template: '{{prop}}'
           }
         }
       })
-      expect(hasWarned(_, 'v-show is ignored on component <test>')).toBe(true)
+      expect(_.warn.calls.count()).toBe(1)
+      expect(
+        hasWarned(_, 'Attributes "id", "class" are ignored on component <test>', true) ||
+        hasWarned(_, 'Attributes "class", "id" are ignored on component <test>')
+      ).toBe(true)
     })
 
     it('should compile component container directives using correct context', function () {