refactor(runtime-core): extract common getComponentName function (#2454)

This commit is contained in:
edison 2020-12-05 06:03:03 +08:00 committed by GitHub
parent b2189ba2f3
commit 82bf7ebf36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 15 deletions

View File

@ -801,15 +801,21 @@ const classifyRE = /(?:^|[-_])(\w)/g
const classify = (str: string): string => const classify = (str: string): string =>
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '') str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
export function getComponentName(
Component: ConcreteComponent
): string | undefined {
return isFunction(Component)
? Component.displayName || Component.name
: Component.name
}
/* istanbul ignore next */ /* istanbul ignore next */
export function formatComponentName( export function formatComponentName(
instance: ComponentInternalInstance | null, instance: ComponentInternalInstance | null,
Component: ConcreteComponent, Component: ConcreteComponent,
isRoot = false isRoot = false
): string { ): string {
let name = isFunction(Component) let name = getComponentName(Component)
? Component.displayName || Component.name
: Component.name
if (!name && Component.__file) { if (!name && Component.__file) {
const match = Component.__file.match(/([^/\\]+)\.\w+$/) const match = Component.__file.match(/([^/\\]+)\.\w+$/)
if (match) { if (match) {

View File

@ -1,11 +1,11 @@
import { import {
ConcreteComponent, ConcreteComponent,
getCurrentInstance, getCurrentInstance,
FunctionalComponent,
SetupContext, SetupContext,
ComponentInternalInstance, ComponentInternalInstance,
LifecycleHooks, LifecycleHooks,
currentInstance currentInstance,
getComponentName
} from '../component' } from '../component'
import { VNode, cloneVNode, isVNode, VNodeProps } from '../vnode' import { VNode, cloneVNode, isVNode, VNodeProps } from '../vnode'
import { warn } from '../warning' import { warn } from '../warning'
@ -151,7 +151,7 @@ const KeepAliveImpl = {
function pruneCache(filter?: (name: string) => boolean) { function pruneCache(filter?: (name: string) => boolean) {
cache.forEach((vnode, key) => { cache.forEach((vnode, key) => {
const name = getName(vnode.type as ConcreteComponent) const name = getComponentName(vnode.type as ConcreteComponent)
if (name && (!filter || !filter(name))) { if (name && (!filter || !filter(name))) {
pruneCacheEntry(key) pruneCacheEntry(key)
} }
@ -235,7 +235,7 @@ const KeepAliveImpl = {
let vnode = getInnerChild(rawVNode) let vnode = getInnerChild(rawVNode)
const comp = vnode.type as ConcreteComponent const comp = vnode.type as ConcreteComponent
const name = getName(comp) const name = getComponentName(comp)
const { include, exclude, max } = props const { include, exclude, max } = props
if ( if (
@ -301,10 +301,6 @@ export const KeepAlive = (KeepAliveImpl as any) as {
} }
} }
function getName(comp: ConcreteComponent): string | void {
return (comp as FunctionalComponent).displayName || comp.name
}
function matches(pattern: MatchPattern, name: string): boolean { function matches(pattern: MatchPattern, name: string): boolean {
if (isArray(pattern)) { if (isArray(pattern)) {
return pattern.some((p: string | RegExp) => matches(p, name)) return pattern.some((p: string | RegExp) => matches(p, name))

View File

@ -2,8 +2,8 @@ import { currentRenderingInstance } from '../componentRenderUtils'
import { import {
currentInstance, currentInstance,
ConcreteComponent, ConcreteComponent,
FunctionalComponent, ComponentOptions,
ComponentOptions getComponentName
} from '../component' } from '../component'
import { Directive } from '../directives' import { Directive } from '../directives'
import { camelize, capitalize, isString } from '@vue/shared' import { camelize, capitalize, isString } from '@vue/shared'
@ -73,8 +73,7 @@ function resolveAsset(
return Component return Component
} }
const selfName = const selfName = getComponentName(Component)
(Component as FunctionalComponent).displayName || Component.name
if ( if (
selfName && selfName &&
(selfName === name || (selfName === name ||