Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-core): prevent circular reference of templateRef #4768

Merged
merged 8 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { generateStyleScopedClasses } from '../template/styleScopedClasses';
import type { ScriptCodegenContext } from './context';
import { codeFeatures, type ScriptCodegenOptions } from './index';
import { generateInternalComponent } from './internalComponent';
import { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';

export function* generateTemplateCtx(
options: ScriptCodegenOptions,
Expand All @@ -26,6 +27,9 @@ export function* generateTemplateCtx(
if (options.sfc.styles.some(style => style.module)) {
exps.push(`{} as __VLS_StyleModules`);
}
if (options.scriptSetupRanges?.templateRefs.length) {
exps.push(getRefsType(options, options.scriptSetupRanges));
}

yield `const __VLS_ctx = `;
if (exps.length === 1) {
Expand Down Expand Up @@ -76,7 +80,7 @@ export function* generateTemplateComponents(options: ScriptCodegenOptions): Gene

exps.push(`{} as NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}>`);
exps.push(`{} as __VLS_GlobalComponents`);
exps.push(`{} as typeof __VLS_ctx`);
exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>>`);

yield `const __VLS_components = {${newLine}`;
for (const type of exps) {
Expand Down Expand Up @@ -257,3 +261,15 @@ export function getTemplateUsageVars(options: ScriptCodegenOptions, ctx: ScriptC

return usageVars;
}

function getRefsType(options: ScriptCodegenOptions, scriptSetupRanges: ScriptSetupRanges) {
KazariEX marked this conversation as resolved.
Show resolved Hide resolved
let result = '';
result += (`{} as import('${options.vueCompilerOptions.lib}').UnwrapRef<{${newLine}`);
for (const { name } of scriptSetupRanges.templateRefs) {
if (name) {
result += (`${name}: typeof ${name}${newLine}`);
}
}
result += (`}>${newLine}`);
return result;
}
9 changes: 4 additions & 5 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,14 @@ export function* generateComponent(
options.templateRefNames.set(refName, varName);
ctx.usedComponentCtxVars.add(var_defineComponentCtx);

yield `// @ts-ignore${newLine}`;
yield `var ${varName} = {} as (Parameters<typeof ${var_defineComponentCtx}['expose']>[0] | null)`;
if (node.codegenNode?.type === CompilerDOM.NodeTypes.VNODE_CALL
&& node.codegenNode.props?.type === CompilerDOM.NodeTypes.JS_OBJECT_EXPRESSION
&& node.codegenNode.props.properties.find(({ key }) => key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && key.content === 'ref_for')
&& node.codegenNode.props.properties.some(({ key }) => key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && key.content === 'ref_for')
) {
yield `var ${varName} = [{} as Parameters<typeof ${var_defineComponentCtx}['expose']>[0]]${endOfLine}`;
} else {
yield `var ${varName} = {} as Parameters<typeof ${var_defineComponentCtx}['expose']>[0]${endOfLine}`;
yield `[]`;
}
yield `${endOfLine}`;
}

const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEmit, var_componentEvents);
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
function* generateRefs(): Generator<Code> {
yield `const __VLS_refs = {${newLine}`;
for (const [name, varName] of options.templateRefNames) {
yield `'${name}': ${varName}!,${newLine}`;
yield `'${name}': ${varName},${newLine}`;
}
yield `}${endOfLine}`;
yield `declare var $refs: typeof __VLS_refs${endOfLine}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { exactType } from '../../shared';
<template>
<TemplateRef ref="templateRef" />

{{ exactType($refs.templateRef.$refs.generic.foo, {} as 1) }}
{{ exactType($refs.templateRef?.$refs.generic?.foo, {} as (1 | undefined)) }}
</template>
KazariEX marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
<script setup lang="ts">
import { useTemplateRef } from 'vue';
import { exactType } from '../../shared';
import { useTemplateRef } from "vue";
import { exactType } from "../../shared";

const comp1 = useTemplateRef('generic');
const comp1 = useTemplateRef("generic");
if (comp1.value) {
exactType(comp1.value.foo, 1)
exactType(comp1.value.foo, 1);
}

const comp2 = useTemplateRef('v-for');
const comp2 = useTemplateRef("v-for");
if (comp2.value) {
exactType(comp2.value[0]?.foo, {} as number | undefined);
exactType(comp2.value[0]?.foo, {} as number | undefined);
}

const comp3 = useTemplateRef('a');
const comp3 = useTemplateRef("a");
if (comp3.value) {
exactType(comp3.value.href, {} as string | undefined);
exactType(comp3.value.href, {} as string | undefined);
}
</script>

<template>
<Generic ref="generic" :foo="1"></Generic>

<Generic v-for="i in 4" ref="v-for" :foo="i"></Generic>
<Generic ref="generic" :foo="1"></Generic>
{{ exactType(comp1?.foo, {} as 1 | undefined) }}

<a ref="a"></a>
<Generic v-for="i in 4" ref="v-for" :foo="i"></Generic>
{{ exactType(comp2?.[0]?.foo, {} as number | undefined) }}

<a ref="a"></a>
{{ exactType(comp3?.href, {} as string | undefined) }}
</template>