Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 26 additions & 14 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function* generateTemplate(
): Generator<Code> {
yield* generateSetupExposed(options, ctx);
yield* generateTemplateCtx(options, ctx, selfType);
yield* generateTemplateComponents(options);
yield* generateTemplateDirectives(options);
yield* generateTemplateComponents(options, ctx);
yield* generateTemplateDirectives(options, ctx);

if (options.styleCodegen) {
yield* options.styleCodegen.codes;
Expand Down Expand Up @@ -82,14 +82,20 @@ function* generateTemplateCtx(
yield endOfLine;
}

function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<Code> {
const types: string[] = [`typeof ${names.ctx}`];
function* generateTemplateComponents(
{ script, scriptRanges }: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
): Generator<Code> {
const types: string[] = [];

if (options.script && options.scriptRanges?.componentOptions?.components) {
const { components } = options.scriptRanges.componentOptions;
if (ctx.generatedTypes.has(names.SetupExposed)) {
types.push(names.SetupExposed);
}
if (script && scriptRanges?.componentOptions?.components) {
const { components } = scriptRanges.componentOptions;
yield `const __VLS_componentsOption = `;
yield* generateSfcBlockSection(
options.script,
script,
components.start,
components.end,
codeFeatures.navigation,
Expand All @@ -98,18 +104,24 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
types.push(`typeof __VLS_componentsOption`);
}

yield `type __VLS_LocalComponents = ${types.join(` & `)}${endOfLine}`;
yield `type __VLS_LocalComponents = ${types.length ? types.join(` & `) : `{}`}${endOfLine}`;
yield `let ${names.components}!: __VLS_LocalComponents & __VLS_GlobalComponents${endOfLine}`;
}

function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<Code> {
const types: string[] = [`typeof ${names.ctx}`];
function* generateTemplateDirectives(
{ script, scriptRanges }: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
): Generator<Code> {
const types: string[] = [];

if (options.script && options.scriptRanges?.componentOptions?.directives) {
const { directives } = options.scriptRanges.componentOptions;
if (ctx.generatedTypes.has(names.SetupExposed)) {
types.push(names.SetupExposed);
}
if (script && scriptRanges?.componentOptions?.directives) {
const { directives } = scriptRanges.componentOptions;
yield `const __VLS_directivesOption = `;
yield* generateSfcBlockSection(
options.script,
script,
directives.start,
directives.end,
codeFeatures.navigation,
Expand All @@ -118,7 +130,7 @@ function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<C
types.push(`__VLS_ResolveDirectives<typeof __VLS_directivesOption>`);
}

yield `type __VLS_LocalDirectives = ${types.join(` & `)}${endOfLine}`;
yield `type __VLS_LocalDirectives = ${types.length ? types.join(` & `) : `{}`}${endOfLine}`;
yield `let ${names.directives}!: __VLS_LocalDirectives & __VLS_GlobalDirectives${endOfLine}`;
}

Expand Down
1 change: 1 addition & 0 deletions test-workspace/tsc/passedFixtures/vue3.4/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"../vue3/#4828",
"../vue3/#4878",
"../vue3/#5120",
"../vue3/#5840",
"../vue3/rootEl",
"../vue3/templateRef",
"../vue3/templateRef_native",
Expand Down
12 changes: 12 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#5840/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script setup lang="ts" generic="T extends number">
import { useTemplateRef } from 'vue';

const props = defineProps<{ generic?: T }>();
const CompRef = useTemplateRef('CompRef');

CompRef.value?.exposedFunction1();
</script>

<template>
<Comp ref="CompRef" />
</template>