From: 三咲智子 Kevin Deng Date: Wed, 13 Nov 2024 02:57:22 +0000 (+0800) Subject: fix(runtime-vapor): component self-reference X-Git-Tag: v3.6.0-alpha.1~16^2~290 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fab9917ae4907aee34ab5709c17fa3242b42d99f;p=thirdparty%2Fvuejs%2Fcore.git fix(runtime-vapor): component self-reference --- diff --git a/packages/runtime-vapor/__tests__/helpers/resolveAssets.spec.ts b/packages/runtime-vapor/__tests__/helpers/resolveAssets.spec.ts index 366edc83dc..fb4a4aa04d 100644 --- a/packages/runtime-vapor/__tests__/helpers/resolveAssets.spec.ts +++ b/packages/runtime-vapor/__tests__/helpers/resolveAssets.spec.ts @@ -54,28 +54,7 @@ describe('resolveAssets', () => { expect(directive3!).toBe(BarBaz) expect(directive4!).toBe(BarBaz) }) - test('maybeSelfReference', async () => { - let component1: Component | string - let component2: Component | string - let component3: Component | string - const Foo = () => [] - const Root = define({ - name: 'Root', - render() { - component1 = resolveComponent('Root', true) - component2 = resolveComponent('Foo', true) - component3 = resolveComponent('Bar', true) - return [] - }, - }) - const app = createVaporApp(Root.component) - app.component('Foo', Foo) - const root = document.createElement('div') - app.mount(root) - expect(component1!).toMatchObject(Root.component) // explicit self name reference - expect(component2!).toBe(Foo) // successful resolve take higher priority - expect(component3!).toMatchObject(Root.component) // fallback when resolve fails - }) + describe('warning', () => { test('used outside render() or setup()', () => { resolveComponent('foo') diff --git a/packages/runtime-vapor/src/component.ts b/packages/runtime-vapor/src/component.ts index 0c9901da0d..ca0cb7fa04 100644 --- a/packages/runtime-vapor/src/component.ts +++ b/packages/runtime-vapor/src/component.ts @@ -425,11 +425,10 @@ function getSlotsProxy(instance: ComponentInternalInstance): StaticSlots { export function getComponentName( Component: Component, - includeInferred = true, ): string | false | undefined { return isFunction(Component) ? Component.displayName || Component.name - : Component.name || (includeInferred && Component.__name) + : Component.name || Component.__name } export function formatComponentName( diff --git a/packages/runtime-vapor/src/helpers/resolveAssets.ts b/packages/runtime-vapor/src/helpers/resolveAssets.ts index 493b11a63d..06bd31f57f 100644 --- a/packages/runtime-vapor/src/helpers/resolveAssets.ts +++ b/packages/runtime-vapor/src/helpers/resolveAssets.ts @@ -8,11 +8,8 @@ export const DIRECTIVES = 'directives' export type AssetTypes = typeof COMPONENTS | typeof DIRECTIVES -export function resolveComponent( - name: string, - maybeSelfReference?: boolean, -): string | Component { - return resolveAsset(COMPONENTS, name, true, maybeSelfReference) || name +export function resolveComponent(name: string): string | Component { + return resolveAsset(COMPONENTS, name, true) || name } export function resolveDirective(name: string): Directive | undefined { @@ -27,7 +24,6 @@ function resolveAsset( type: typeof COMPONENTS, name: string, warnMissing?: boolean, - maybeSelfReference?: boolean, ): Component | undefined // overload 2: directives function resolveAsset( @@ -35,22 +31,14 @@ function resolveAsset( name: string, ): Directive | undefined // implementation -function resolveAsset( - type: AssetTypes, - name: string, - warnMissing = true, - maybeSelfReference = false, -) { +function resolveAsset(type: AssetTypes, name: string, warnMissing = true) { const instance = currentInstance if (instance) { const Component = instance.type // explicit self name has highest priority if (type === COMPONENTS) { - const selfName = getComponentName( - Component, - false /* do not include inferred name to avoid breaking existing code */, - ) + const selfName = getComponentName(Component) if ( selfName && (selfName === name || @@ -65,11 +53,6 @@ function resolveAsset( // global registration resolve(instance.appContext[type], name) - if (!res && maybeSelfReference) { - // fallback to implicit self-reference - return Component - } - if (__DEV__ && warnMissing && !res) { const extra = type === COMPONENTS