From 19228a469a461406e16ec18c868972c6e842e093 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 16 Mar 2020 12:46:15 -0400 Subject: [PATCH] refactor: simplify resolveDyanmicComponent --- .../__tests__/transforms/transformElement.spec.ts | 3 +-- .../src/transforms/transformElement.ts | 8 +++----- packages/runtime-core/src/helpers/resolveAssets.ts | 13 ++++--------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index cc3cbd48..85800fef 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -814,8 +814,7 @@ describe('compiler: element transform', () => { { type: NodeTypes.SIMPLE_EXPRESSION, content: 'foo' - }, - '$' + } ] } }) diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 905268de..3123d8ec 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -215,11 +215,9 @@ export function resolveComponentType( } // dynamic else if (isProp.exp) { - return createCallExpression( - context.helper(RESOLVE_DYNAMIC_COMPONENT), - // _ctx.$ exposes the owner instance of current render function - [isProp.exp, context.prefixIdentifiers ? `_ctx.$` : `$`] - ) + return createCallExpression(context.helper(RESOLVE_DYNAMIC_COMPONENT), [ + isProp.exp + ]) } } diff --git a/packages/runtime-core/src/helpers/resolveAssets.ts b/packages/runtime-core/src/helpers/resolveAssets.ts index dd12b473..9c3dee57 100644 --- a/packages/runtime-core/src/helpers/resolveAssets.ts +++ b/packages/runtime-core/src/helpers/resolveAssets.ts @@ -23,16 +23,11 @@ export function resolveComponent(name: string): Component | undefined { } export function resolveDynamicComponent( - component: unknown, - // Dynamic component resolution has to be called inline due to potential - // access to scope variables. When called inside slots it will be inside - // a different component's render cycle, so the owner instance must be passed - // in explicitly. - instance: ComponentInternalInstance + component: unknown ): Component | undefined { if (!component) return if (isString(component)) { - return resolveAsset(COMPONENTS, component, instance) + return resolveAsset(COMPONENTS, component, currentRenderingInstance) } else if (isFunction(component) || isObject(component)) { return component } @@ -46,13 +41,13 @@ export function resolveDirective(name: string): Directive | undefined { function resolveAsset( type: typeof COMPONENTS, name: string, - instance?: ComponentInternalInstance + instance?: ComponentInternalInstance | null ): Component | undefined // overload 2: directives function resolveAsset( type: typeof DIRECTIVES, name: string, - instance?: ComponentInternalInstance + instance?: ComponentInternalInstance | null ): Directive | undefined function resolveAsset(