fix(runtime-core): fix component name inference in warnings

Should not pollute component definition name property
fix #1418
This commit is contained in:
Evan You 2020-06-26 09:28:15 -04:00
parent 1c4e1b6792
commit e765d81404
4 changed files with 24 additions and 20 deletions

View File

@ -682,6 +682,7 @@ const classify = (str: string): string =>
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
export function formatComponentName(
instance: ComponentInternalInstance | null,
Component: Component,
isRoot = false
): string {
@ -694,5 +695,17 @@ export function formatComponentName(
name = match[1]
}
}
if (!name && instance && instance.parent) {
// try to infer the name based on local resolution
const registry = instance.parent.components
for (const key in registry) {
if (registry[key] === Component) {
name = key
break
}
}
}
return name ? classify(name) : isRoot ? `App` : `Anonymous`
}

View File

@ -82,20 +82,9 @@ function resolveAsset(
res = self
}
}
if (__DEV__) {
if (res) {
// in dev, infer anonymous component's name based on registered name
if (
type === COMPONENTS &&
isObject(res) &&
!(res as ComponentOptions).name
) {
;(res as ComponentOptions).name = name
}
} else if (warnMissing) {
if (__DEV__ && warnMissing && !res) {
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
}
}
return res
} else if (__DEV__) {
warn(

View File

@ -17,11 +17,7 @@ export function endMeasure(instance: ComponentInternalInstance, type: string) {
const startTag = `vue-${type}-${instance.uid}`
const endTag = startTag + `:end`
perf.mark(endTag)
perf.measure(
`<${formatComponentName(instance.type)}> ${type}`,
startTag,
endTag
)
perf.measure(`<${formatComponentName(instance)}> ${type}`, startTag, endTag)
perf.clearMarks(startTag)
perf.clearMarks(endTag)
}

View File

@ -48,7 +48,9 @@ export function warn(msg: string, ...args: any[]) {
msg + args.join(''),
instance && instance.proxy,
trace
.map(({ vnode }) => `at <${formatComponentName(vnode.type)}>`)
.map(
({ vnode }) => `at <${formatComponentName(instance, vnode.type)}>`
)
.join('\n'),
trace
]
@ -109,7 +111,11 @@ function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
const postfix =
recurseCount > 0 ? `... (${recurseCount} recursive calls)` : ``
const isRoot = vnode.component ? vnode.component.parent == null : false
const open = ` at <${formatComponentName(vnode.type, isRoot)}`
const open = ` at <${formatComponentName(
vnode.component,
vnode.type,
isRoot
)}`
const close = `>` + postfix
return vnode.props
? [open, ...formatProps(vnode.props), close]