]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(runtime-core): extract common getComponentName function (#2454)
authoredison <daiwei521@126.com>
Fri, 4 Dec 2020 22:03:03 +0000 (06:03 +0800)
committerGitHub <noreply@github.com>
Fri, 4 Dec 2020 22:03:03 +0000 (17:03 -0500)
packages/runtime-core/src/component.ts
packages/runtime-core/src/components/KeepAlive.ts
packages/runtime-core/src/helpers/resolveAssets.ts

index 9e416586fe05ee04d0d99582ee3639e3d892be31..788a20b43433f0507af80a357a29185514971ee5 100644 (file)
@@ -801,15 +801,21 @@ const classifyRE = /(?:^|[-_])(\w)/g
 const classify = (str: string): string =>
   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 */
 export function formatComponentName(
   instance: ComponentInternalInstance | null,
   Component: ConcreteComponent,
   isRoot = false
 ): string {
-  let name = isFunction(Component)
-    ? Component.displayName || Component.name
-    : Component.name
+  let name = getComponentName(Component)
   if (!name && Component.__file) {
     const match = Component.__file.match(/([^/\\]+)\.\w+$/)
     if (match) {
index 0630e3d16d1495cec2585073b25053643faae732..f5c0bfd61bdc63c7a4e9fa7731acf3ff805e2790 100644 (file)
@@ -1,11 +1,11 @@
 import {
   ConcreteComponent,
   getCurrentInstance,
-  FunctionalComponent,
   SetupContext,
   ComponentInternalInstance,
   LifecycleHooks,
-  currentInstance
+  currentInstance,
+  getComponentName
 } from '../component'
 import { VNode, cloneVNode, isVNode, VNodeProps } from '../vnode'
 import { warn } from '../warning'
@@ -151,7 +151,7 @@ const KeepAliveImpl = {
 
     function pruneCache(filter?: (name: string) => boolean) {
       cache.forEach((vnode, key) => {
-        const name = getName(vnode.type as ConcreteComponent)
+        const name = getComponentName(vnode.type as ConcreteComponent)
         if (name && (!filter || !filter(name))) {
           pruneCacheEntry(key)
         }
@@ -235,7 +235,7 @@ const KeepAliveImpl = {
 
       let vnode = getInnerChild(rawVNode)
       const comp = vnode.type as ConcreteComponent
-      const name = getName(comp)
+      const name = getComponentName(comp)
       const { include, exclude, max } = props
 
       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 {
   if (isArray(pattern)) {
     return pattern.some((p: string | RegExp) => matches(p, name))
index d94422c32fdc9f4b3c5874dbce641aa9b0765af3..986079a952294354e4cab691c74b4449bcf03172 100644 (file)
@@ -2,8 +2,8 @@ import { currentRenderingInstance } from '../componentRenderUtils'
 import {
   currentInstance,
   ConcreteComponent,
-  FunctionalComponent,
-  ComponentOptions
+  ComponentOptions,
+  getComponentName
 } from '../component'
 import { Directive } from '../directives'
 import { camelize, capitalize, isString } from '@vue/shared'
@@ -73,8 +73,7 @@ function resolveAsset(
         return Component
       }
 
-      const selfName =
-        (Component as FunctionalComponent).displayName || Component.name
+      const selfName = getComponentName(Component)
       if (
         selfName &&
         (selfName === name ||