]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(sfc): avoid auto name inference leading to unwanted recursion
authorEvan You <yyx990803@gmail.com>
Mon, 6 Jun 2022 09:36:47 +0000 (17:36 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 6 Jun 2022 09:36:49 +0000 (17:36 +0800)
fix #5965
fix #6027
fix #6029

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts
packages/runtime-core/src/component.ts
packages/runtime-core/src/helpers/resolveAssets.ts

index e8026dc129c6be5671da743078f012895aa71d92..b7fc9304b26dbcc09a0a864408883d3f7f643b76 100644 (file)
@@ -2,7 +2,7 @@
 
 exports[`SFC analyze <script> bindings auto name inference basic 1`] = `
 "export default {
-  name: 'FooBar',
+  __name: 'FooBar',
   setup(__props, { expose }) {
   expose();
 const a = 1
index 77a26a6f5db06928487a66d7359d7d82e4fb187b..1a48a8f8ebecd13ef8f08f86ac5c84b9fd4a438d 100644 (file)
@@ -1650,7 +1650,7 @@ describe('SFC analyze <script> bindings', () => {
         }
       )
       expect(content).toMatch(`export default {
-  name: 'FooBar'`)
+  __name: 'FooBar'`)
       assertCode(content)
     })
 
index ca82dc909bd83189cd32cc377647936b76379552..81be4e12a006aea11e4b9bf0fef0d1abbfa28dc1 100644 (file)
@@ -1463,7 +1463,7 @@ export function compileScript(
   if (!hasDefaultExportName && filename && filename !== DEFAULT_FILENAME) {
     const match = filename.match(/([^/\\]+)\.\w+$/)
     if (match) {
-      runtimeOptions += `\n  name: '${match[1]}',`
+      runtimeOptions += `\n  __name: '${match[1]}',`
     }
   }
   if (hasInlinedSsrRenderFn) {
index 11a6cafbcf14784fbfd0671b5aed282866a7e09a..eededdf34d0222ece173a67a6a0519538646b3fc 100644 (file)
@@ -106,6 +106,10 @@ export interface ComponentInternalOptions {
    * This one should be exposed so that devtools can make use of it
    */
   __file?: string
+  /**
+   * name inferred from filename
+   */
+  __name?: string
 }
 
 export interface FunctionalComponent<P = {}, E extends EmitsOptions = {}>
@@ -949,11 +953,12 @@ const classify = (str: string): string =>
   str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
 
 export function getComponentName(
-  Component: ConcreteComponent
-): string | undefined {
+  Component: ConcreteComponent,
+  includeInferred = true
+): string | false | undefined {
   return isFunction(Component)
     ? Component.displayName || Component.name
-    : Component.name
+    : Component.name || (includeInferred && Component.__name)
 }
 
 /* istanbul ignore next */
index 878f824150b635783385c63ddf0d0089925cf74c..214c4f446b381b5d51fe36721f2b1e9345ced30a 100644 (file)
@@ -86,7 +86,10 @@ function resolveAsset(
 
     // explicit self name has highest priority
     if (type === COMPONENTS) {
-      const selfName = getComponentName(Component)
+      const selfName = getComponentName(
+        Component,
+        false /* do not include inferred name to avoid breaking existing code */
+      )
       if (
         selfName &&
         (selfName === name ||