From 0278992f78834bc8df677c4e8ec891bb79510edb Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 20 Apr 2020 16:06:51 -0400 Subject: [PATCH] fix(warn): fix component name inference in warning trace --- packages/runtime-core/src/component.ts | 9 +++++---- packages/runtime-core/src/warning.ts | 13 +++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index eeadd982..6ba7c0ff 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -52,6 +52,7 @@ export interface SFCInternalOptions { __cssModules?: Data __hmrId?: string __hmrUpdated?: boolean + __file?: string } export interface FunctionalComponent< @@ -540,16 +541,16 @@ const classify = (str: string): string => export function formatComponentName( Component: Component, - file?: string + isRoot = false ): string { let name = isFunction(Component) ? Component.displayName || Component.name : Component.name - if (!name && file) { - const match = file.match(/([^/\\]+)\.vue$/) + if (!name && Component.__file) { + const match = Component.__file.match(/([^/\\]+)\.vue$/) if (match) { name = match[1] } } - return name ? classify(name) : 'Anonymous' + return name ? classify(name) : isRoot ? `App` : `Anonymous` } diff --git a/packages/runtime-core/src/warning.ts b/packages/runtime-core/src/warning.ts index c31ecfed..7b84d814 100644 --- a/packages/runtime-core/src/warning.ts +++ b/packages/runtime-core/src/warning.ts @@ -48,10 +48,7 @@ export function warn(msg: string, ...args: any[]) { msg + args.join(''), instance && instance.proxy, trace - .map( - ({ vnode }) => - `at <${formatComponentName(vnode.type as Component)}>` - ) + .map(({ vnode }) => `at <${formatComponentName(vnode.type)}>`) .join('\n'), trace ] @@ -111,12 +108,12 @@ function formatTrace(trace: ComponentTraceStack): any[] { function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] { const postfix = recurseCount > 0 ? `... (${recurseCount} recursive calls)` : `` - const open = ` at <${formatComponentName(vnode)}` + const isRoot = vnode.component!.parent == null + const open = ` at <${formatComponentName(vnode.type, isRoot)}` const close = `>` + postfix - const rootLabel = vnode.component!.parent == null ? `(Root)` : `` return vnode.props - ? [open, ...formatProps(vnode.props), close, rootLabel] - : [open + close, rootLabel] + ? [open, ...formatProps(vnode.props), close] + : [open + close] } function formatProps(props: Data): any[] {