Skip to content

Commit e765d81

Browse files
committed
fix(runtime-core): fix component name inference in warnings
Should not pollute component definition name property fix #1418
1 parent 1c4e1b6 commit e765d81

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

packages/runtime-core/src/component.ts

+13
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ const classify = (str: string): string =>
682682
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
683683

684684
export function formatComponentName(
685+
instance: ComponentInternalInstance | null,
685686
Component: Component,
686687
isRoot = false
687688
): string {
@@ -694,5 +695,17 @@ export function formatComponentName(
694695
name = match[1]
695696
}
696697
}
698+
699+
if (!name && instance && instance.parent) {
700+
// try to infer the name based on local resolution
701+
const registry = instance.parent.components
702+
for (const key in registry) {
703+
if (registry[key] === Component) {
704+
name = key
705+
break
706+
}
707+
}
708+
}
709+
697710
return name ? classify(name) : isRoot ? `App` : `Anonymous`
698711
}

packages/runtime-core/src/helpers/resolveAssets.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,8 @@ function resolveAsset(
8282
res = self
8383
}
8484
}
85-
if (__DEV__) {
86-
if (res) {
87-
// in dev, infer anonymous component's name based on registered name
88-
if (
89-
type === COMPONENTS &&
90-
isObject(res) &&
91-
!(res as ComponentOptions).name
92-
) {
93-
;(res as ComponentOptions).name = name
94-
}
95-
} else if (warnMissing) {
96-
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
97-
}
85+
if (__DEV__ && warnMissing && !res) {
86+
warn(`Failed to resolve ${type.slice(0, -1)}: ${name}`)
9887
}
9988
return res
10089
} else if (__DEV__) {

packages/runtime-core/src/profiling.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ export function endMeasure(instance: ComponentInternalInstance, type: string) {
1717
const startTag = `vue-${type}-${instance.uid}`
1818
const endTag = startTag + `:end`
1919
perf.mark(endTag)
20-
perf.measure(
21-
`<${formatComponentName(instance.type)}> ${type}`,
22-
startTag,
23-
endTag
24-
)
20+
perf.measure(`<${formatComponentName(instance)}> ${type}`, startTag, endTag)
2521
perf.clearMarks(startTag)
2622
perf.clearMarks(endTag)
2723
}

packages/runtime-core/src/warning.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export function warn(msg: string, ...args: any[]) {
4848
msg + args.join(''),
4949
instance && instance.proxy,
5050
trace
51-
.map(({ vnode }) => `at <${formatComponentName(vnode.type)}>`)
51+
.map(
52+
({ vnode }) => `at <${formatComponentName(instance, vnode.type)}>`
53+
)
5254
.join('\n'),
5355
trace
5456
]
@@ -109,7 +111,11 @@ function formatTraceEntry({ vnode, recurseCount }: TraceEntry): any[] {
109111
const postfix =
110112
recurseCount > 0 ? `... (${recurseCount} recursive calls)` : ``
111113
const isRoot = vnode.component ? vnode.component.parent == null : false
112-
const open = ` at <${formatComponentName(vnode.type, isRoot)}`
114+
const open = ` at <${formatComponentName(
115+
vnode.component,
116+
vnode.type,
117+
isRoot
118+
)}`
113119
const close = `>` + postfix
114120
return vnode.props
115121
? [open, ...formatProps(vnode.props), close]

0 commit comments

Comments
 (0)