Skip to content

Commit 0278992

Browse files
committed
fix(warn): fix component name inference in warning trace
1 parent 054ccec commit 0278992

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

packages/runtime-core/src/component.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface SFCInternalOptions {
5252
__cssModules?: Data
5353
__hmrId?: string
5454
__hmrUpdated?: boolean
55+
__file?: string
5556
}
5657

5758
export interface FunctionalComponent<
@@ -540,16 +541,16 @@ const classify = (str: string): string =>
540541

541542
export function formatComponentName(
542543
Component: Component,
543-
file?: string
544+
isRoot = false
544545
): string {
545546
let name = isFunction(Component)
546547
? Component.displayName || Component.name
547548
: Component.name
548-
if (!name && file) {
549-
const match = file.match(/([^/\\]+)\.vue$/)
549+
if (!name && Component.__file) {
550+
const match = Component.__file.match(/([^/\\]+)\.vue$/)
550551
if (match) {
551552
name = match[1]
552553
}
553554
}
554-
return name ? classify(name) : 'Anonymous'
555+
return name ? classify(name) : isRoot ? `App` : `Anonymous`
555556
}

packages/runtime-core/src/warning.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ export function warn(msg: string, ...args: any[]) {
4848
msg + args.join(''),
4949
instance && instance.proxy,
5050
trace
51-
.map(
52-
({ vnode }) =>
53-
`at <${formatComponentName(vnode.type as Component)}>`
54-
)
51+
.map(({ vnode }) => `at <${formatComponentName(vnode.type)}>`)
5552
.join('\n'),
5653
trace
5754
]
@@ -111,12 +108,12 @@ function formatTrace(trace: ComponentTraceStack): any[] {
111108
function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
112109
const postfix =
113110
recurseCount > 0 ? `... (${recurseCount} recursive calls)` : ``
114-
const open = ` at <${formatComponentName(vnode)}`
111+
const isRoot = vnode.component!.parent == null
112+
const open = ` at <${formatComponentName(vnode.type, isRoot)}`
115113
const close = `>` + postfix
116-
const rootLabel = vnode.component!.parent == null ? `(Root)` : ``
117114
return vnode.props
118-
? [open, ...formatProps(vnode.props), close, rootLabel]
119-
: [open + close, rootLabel]
115+
? [open, ...formatProps(vnode.props), close]
116+
: [open + close]
120117
}
121118

122119
function formatProps(props: Data): any[] {

0 commit comments

Comments
 (0)