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')
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(
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 {
type: typeof COMPONENTS,
name: string,
warnMissing?: boolean,
- maybeSelfReference?: boolean,
): Component | undefined
// overload 2: directives
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 ||
// 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