Skip to content

Commit

Permalink
fix: more consistent component naming warnings across the API
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and aJean committed Aug 19, 2020
1 parent 386e909 commit 4f432be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
12 changes: 3 additions & 9 deletions src/core/global-api/assets.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */

import config from '../config'
import { ASSET_TYPES } from 'shared/constants'
import { warn, isPlainObject } from '../util/index'
import { isPlainObject, validateComponentName } from '../util/index'

export function initAssetRegisters (Vue: GlobalAPI) {
/**
Expand All @@ -17,13 +16,8 @@ export function initAssetRegisters (Vue: GlobalAPI) {
return this.options[type + 's'][id]
} else {
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'production') {
if (type === 'component' && config.isReservedTag(id)) {
warn(
'Do not use built-in or reserved HTML elements as component ' +
'id: ' + id
)
}
if (process.env.NODE_ENV !== 'production' && type === 'component') {
validateComponentName(id)
}
if (type === 'component' && isPlainObject(definition)) {
definition.name = definition.name || id
Expand Down
12 changes: 3 additions & 9 deletions src/core/global-api/extend.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* @flow */

import { ASSET_TYPES } from 'shared/constants'
import { warn, extend, mergeOptions } from '../util/index'
import { defineComputed, proxy } from '../instance/state'
import { extend, mergeOptions, validateComponentName } from '../util/index'

export function initExtend (Vue: GlobalAPI) {
/**
Expand All @@ -26,14 +26,8 @@ export function initExtend (Vue: GlobalAPI) {
}

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

const Sub = function VueComponent (options) {
Expand Down
25 changes: 18 additions & 7 deletions src/core/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,24 @@ const defaultStrat = function (parentVal: any, childVal: any): any {
*/
function checkComponents (options: Object) {
for (const key in options.components) {
const lower = key.toLowerCase()
if (isBuiltInTag(lower) || config.isReservedTag(lower)) {
warn(
'Do not use built-in or reserved HTML elements as component ' +
'id: ' + key
)
}
validateComponentName(key)
}
}

export function validateComponentName (name: string) {
if (!/^[a-zA-Z][\w-]*$/.test(name)) {
warn(
'Invalid component name: "' + name + '". Component names ' +
'can only contain alphanumeric characters and the hyphen, ' +
'and must start with a letter.'
)
}
const lower = name.toLowerCase()
if (isBuiltInTag(lower) || config.isReservedTag(lower)) {
warn(
'Do not use built-in or reserved HTML elements as component ' +
'id: ' + name
)
}
}

Expand Down

0 comments on commit 4f432be

Please sign in to comment.