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, '') str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
export function formatComponentName( export function formatComponentName(
instance: ComponentInternalInstance | null,
Component: Component, Component: Component,
isRoot = false isRoot = false
): string { ): string {
@ -694,5 +695,17 @@ export function formatComponentName(
name = match[1] 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` return name ? classify(name) : isRoot ? `App` : `Anonymous`
} }

View File

@ -82,20 +82,9 @@ function resolveAsset(
res = self res = self
} }
} }
if (__DEV__) { if (__DEV__ && warnMissing && !res) {
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) {
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`) warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
} }
}
return res return res
} else if (__DEV__) { } else if (__DEV__) {
warn( warn(

View File

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

View File

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