Просмотр исходного кода

add warning for invalid component names (close #1997)

Evan You 10 лет назад
Родитель
Сommit
62229ef1d9
2 измененных файлов с 19 добавлено и 0 удалено
  1. 6 0
      src/instance/api/global.js
  2. 13 0
      test/unit/specs/api/global_spec.js

+ 6 - 0
src/instance/api/global.js

@@ -71,6 +71,12 @@ export default function (Vue) {
       return extendOptions._Ctor
       return extendOptions._Ctor
     }
     }
     var name = extendOptions.name || Super.options.name
     var name = extendOptions.name || Super.options.name
+    if (process.env.NODE_ENV !== 'production') {
+      if (!/^[a-zA-Z][\w-]+$/.test(name)) {
+        warn('Invalid component name: ' + name)
+        name = null
+      }
+    }
     var Sub = createClass(name || 'VueComponent')
     var Sub = createClass(name || 'VueComponent')
     Sub.prototype = Object.create(Super.prototype)
     Sub.prototype = Object.create(Super.prototype)
     Sub.prototype.constructor = Sub
     Sub.prototype.constructor = Sub

+ 13 - 0
test/unit/specs/api/global_spec.js

@@ -4,6 +4,10 @@ var config = require('../../../../src/config')
 
 
 describe('Global API', function () {
 describe('Global API', function () {
 
 
+  beforeEach(function () {
+    spyWarns()
+  })
+
   it('exposed utilities', function () {
   it('exposed utilities', function () {
     expect(Vue.util).toBe(_)
     expect(Vue.util).toBe(_)
     expect(Vue.nextTick).toBe(_.nextTick)
     expect(Vue.nextTick).toBe(_.nextTick)
@@ -39,6 +43,15 @@ describe('Global API', function () {
     expect(t2.$options.b).toBe(2)
     expect(t2.$options.b).toBe(2)
   })
   })
 
 
+  it('extend warn invalid names', function () {
+    Vue.extend({ name: '123' })
+    expect(hasWarned('Invalid component name: 123')).toBe(true)
+    Vue.extend({ name: '_fesf' })
+    expect(hasWarned('Invalid component name: _fesf')).toBe(true)
+    Vue.extend({ name: 'Some App' })
+    expect(hasWarned('Invalid component name: Some App')).toBe(true)
+  })
+
   it('use', function () {
   it('use', function () {
     var def = {}
     var def = {}
     var options = {}
     var options = {}