Skip to content

Commit 62229ef

Browse files
committed
add warning for invalid component names (close #1997)
1 parent 55a56a6 commit 62229ef

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/instance/api/global.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export default function (Vue) {
7171
return extendOptions._Ctor
7272
}
7373
var name = extendOptions.name || Super.options.name
74+
if (process.env.NODE_ENV !== 'production') {
75+
if (!/^[a-zA-Z][\w-]+$/.test(name)) {
76+
warn('Invalid component name: ' + name)
77+
name = null
78+
}
79+
}
7480
var Sub = createClass(name || 'VueComponent')
7581
Sub.prototype = Object.create(Super.prototype)
7682
Sub.prototype.constructor = Sub

test/unit/specs/api/global_spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ var config = require('../../../../src/config')
44

55
describe('Global API', function () {
66

7+
beforeEach(function () {
8+
spyWarns()
9+
})
10+
711
it('exposed utilities', function () {
812
expect(Vue.util).toBe(_)
913
expect(Vue.nextTick).toBe(_.nextTick)
@@ -39,6 +43,15 @@ describe('Global API', function () {
3943
expect(t2.$options.b).toBe(2)
4044
})
4145

46+
it('extend warn invalid names', function () {
47+
Vue.extend({ name: '123' })
48+
expect(hasWarned('Invalid component name: 123')).toBe(true)
49+
Vue.extend({ name: '_fesf' })
50+
expect(hasWarned('Invalid component name: _fesf')).toBe(true)
51+
Vue.extend({ name: 'Some App' })
52+
expect(hasWarned('Invalid component name: Some App')).toBe(true)
53+
})
54+
4255
it('use', function () {
4356
var def = {}
4457
var options = {}

0 commit comments

Comments
 (0)