fix(warn): fix component name inference in warning trace

This commit is contained in:
Evan You 2020-04-20 16:06:51 -04:00
parent 054ccecd58
commit 0278992f78
2 changed files with 10 additions and 12 deletions

View File

@ -52,6 +52,7 @@ export interface SFCInternalOptions {
__cssModules?: Data __cssModules?: Data
__hmrId?: string __hmrId?: string
__hmrUpdated?: boolean __hmrUpdated?: boolean
__file?: string
} }
export interface FunctionalComponent< export interface FunctionalComponent<
@ -540,16 +541,16 @@ const classify = (str: string): string =>
export function formatComponentName( export function formatComponentName(
Component: Component, Component: Component,
file?: string isRoot = false
): string { ): string {
let name = isFunction(Component) let name = isFunction(Component)
? Component.displayName || Component.name ? Component.displayName || Component.name
: Component.name : Component.name
if (!name && file) { if (!name && Component.__file) {
const match = file.match(/([^/\\]+)\.vue$/) const match = Component.__file.match(/([^/\\]+)\.vue$/)
if (match) { if (match) {
name = match[1] name = match[1]
} }
} }
return name ? classify(name) : 'Anonymous' return name ? classify(name) : isRoot ? `App` : `Anonymous`
} }

View File

@ -48,10 +48,7 @@ export function warn(msg: string, ...args: any[]) {
msg + args.join(''), msg + args.join(''),
instance && instance.proxy, instance && instance.proxy,
trace trace
.map( .map(({ vnode }) => `at <${formatComponentName(vnode.type)}>`)
({ vnode }) =>
`at <${formatComponentName(vnode.type as Component)}>`
)
.join('\n'), .join('\n'),
trace trace
] ]
@ -111,12 +108,12 @@ function formatTrace(trace: ComponentTraceStack): any[] {
function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] { function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
const postfix = const postfix =
recurseCount > 0 ? `... (${recurseCount} recursive calls)` : `` 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 close = `>` + postfix
const rootLabel = vnode.component!.parent == null ? `(Root)` : ``
return vnode.props return vnode.props
? [open, ...formatProps(vnode.props), close, rootLabel] ? [open, ...formatProps(vnode.props), close]
: [open + close, rootLabel] : [open + close]
} }
function formatProps(props: Data): any[] { function formatProps(props: Data): any[] {