Skip to content

Commit

Permalink
fix(runtime-core): fix component name inference in warnings
Browse files Browse the repository at this point in the history
Should not pollute component definition name property
fix #1418
  • Loading branch information
yyx990803 committed Jun 26, 2020
1 parent 1c4e1b6 commit e765d81
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
13 changes: 13 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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`
}
15 changes: 2 additions & 13 deletions packages/runtime-core/src/helpers/resolveAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__) {
Expand Down
6 changes: 1 addition & 5 deletions packages/runtime-core/src/profiling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime-core/src/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit e765d81

Please sign in to comment.