Skip to content

Commit 4f432be

Browse files
yyx990803aJean
authored andcommitted
fix: more consistent component naming warnings across the API
close vuejs#7212
1 parent 386e909 commit 4f432be

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

src/core/global-api/assets.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* @flow */
22

3-
import config from '../config'
43
import { ASSET_TYPES } from 'shared/constants'
5-
import { warn, isPlainObject } from '../util/index'
4+
import { isPlainObject, validateComponentName } from '../util/index'
65

76
export function initAssetRegisters (Vue: GlobalAPI) {
87
/**
@@ -17,13 +16,8 @@ export function initAssetRegisters (Vue: GlobalAPI) {
1716
return this.options[type + 's'][id]
1817
} else {
1918
/* istanbul ignore if */
20-
if (process.env.NODE_ENV !== 'production') {
21-
if (type === 'component' && config.isReservedTag(id)) {
22-
warn(
23-
'Do not use built-in or reserved HTML elements as component ' +
24-
'id: ' + id
25-
)
26-
}
19+
if (process.env.NODE_ENV !== 'production' && type === 'component') {
20+
validateComponentName(id)
2721
}
2822
if (type === 'component' && isPlainObject(definition)) {
2923
definition.name = definition.name || id

src/core/global-api/extend.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* @flow */
22

33
import { ASSET_TYPES } from 'shared/constants'
4-
import { warn, extend, mergeOptions } from '../util/index'
54
import { defineComputed, proxy } from '../instance/state'
5+
import { extend, mergeOptions, validateComponentName } from '../util/index'
66

77
export function initExtend (Vue: GlobalAPI) {
88
/**
@@ -26,14 +26,8 @@ export function initExtend (Vue: GlobalAPI) {
2626
}
2727

2828
const name = extendOptions.name || Super.options.name
29-
if (process.env.NODE_ENV !== 'production') {
30-
if (!/^[a-zA-Z][\w-]*$/.test(name)) {
31-
warn(
32-
'Invalid component name: "' + name + '". Component names ' +
33-
'can only contain alphanumeric characters and the hyphen, ' +
34-
'and must start with a letter.'
35-
)
36-
}
29+
if (process.env.NODE_ENV !== 'production' && name) {
30+
validateComponentName(name)
3731
}
3832

3933
const Sub = function VueComponent (options) {

src/core/util/options.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,24 @@ const defaultStrat = function (parentVal: any, childVal: any): any {
248248
*/
249249
function checkComponents (options: Object) {
250250
for (const key in options.components) {
251-
const lower = key.toLowerCase()
252-
if (isBuiltInTag(lower) || config.isReservedTag(lower)) {
253-
warn(
254-
'Do not use built-in or reserved HTML elements as component ' +
255-
'id: ' + key
256-
)
257-
}
251+
validateComponentName(key)
252+
}
253+
}
254+
255+
export function validateComponentName (name: string) {
256+
if (!/^[a-zA-Z][\w-]*$/.test(name)) {
257+
warn(
258+
'Invalid component name: "' + name + '". Component names ' +
259+
'can only contain alphanumeric characters and the hyphen, ' +
260+
'and must start with a letter.'
261+
)
262+
}
263+
const lower = name.toLowerCase()
264+
if (isBuiltInTag(lower) || config.isReservedTag(lower)) {
265+
warn(
266+
'Do not use built-in or reserved HTML elements as component ' +
267+
'id: ' + name
268+
)
258269
}
259270
}
260271

0 commit comments

Comments
 (0)