فهرست منبع

add warning for invalid component names (close #1997)

Evan You 10 سال پیش
والد
کامیت
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
     }
     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')
     Sub.prototype = Object.create(Super.prototype)
     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 () {
 
+  beforeEach(function () {
+    spyWarns()
+  })
+
   it('exposed utilities', function () {
     expect(Vue.util).toBe(_)
     expect(Vue.nextTick).toBe(_.nextTick)
@@ -39,6 +43,15 @@ describe('Global API', function () {
     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 () {
     var def = {}
     var options = {}