Skip to content

Commit

Permalink
fix(runtime-vapor): component self-reference
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 13, 2024
1 parent 114d501 commit fab9917
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 45 deletions.
23 changes: 1 addition & 22 deletions packages/runtime-vapor/__tests__/helpers/resolveAssets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 4 additions & 21 deletions packages/runtime-vapor/src/helpers/resolveAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,30 +24,21 @@ function resolveAsset(
type: typeof COMPONENTS,
name: string,
warnMissing?: boolean,
maybeSelfReference?: boolean,
): Component | undefined
// overload 2: directives
function resolveAsset(
type: typeof DIRECTIVES,
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 ||
Expand All @@ -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
Expand Down

0 comments on commit fab9917

Please sign in to comment.