From e765d814048c2cdc3cc32bdffb73c6e59b0d747d Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 26 Jun 2020 09:28:15 -0400 Subject: [PATCH] fix(runtime-core): fix component name inference in warnings Should not pollute component definition name property fix #1418 --- packages/runtime-core/src/component.ts | 13 +++++++++++++ .../runtime-core/src/helpers/resolveAssets.ts | 15 ++------------- packages/runtime-core/src/profiling.ts | 6 +----- packages/runtime-core/src/warning.ts | 10 ++++++++-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 2e8e56aa8b1..ebd46ae6d57 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -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` } diff --git a/packages/runtime-core/src/helpers/resolveAssets.ts b/packages/runtime-core/src/helpers/resolveAssets.ts index 3f11cc9aa71..268de161c36 100644 --- a/packages/runtime-core/src/helpers/resolveAssets.ts +++ b/packages/runtime-core/src/helpers/resolveAssets.ts @@ -82,19 +82,8 @@ 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) { - warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`) - } + if (__DEV__ && warnMissing && !res) { + warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`) } return res } else if (__DEV__) { diff --git a/packages/runtime-core/src/profiling.ts b/packages/runtime-core/src/profiling.ts index 4dd77f14a30..6dbc8c29627 100644 --- a/packages/runtime-core/src/profiling.ts +++ b/packages/runtime-core/src/profiling.ts @@ -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) } diff --git a/packages/runtime-core/src/warning.ts b/packages/runtime-core/src/warning.ts index aeeefe3988d..07f8a27b7f9 100644 --- a/packages/runtime-core/src/warning.ts +++ b/packages/runtime-core/src/warning.ts @@ -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]