Skip to content

Commit 2bb3cc1

Browse files
authored
refactor(language-core): split __VLS_templateResult (#4781)
1 parent 7a0337b commit 2bb3cc1

File tree

4 files changed

+55
-75
lines changed

4 files changed

+55
-75
lines changed

packages/language-core/lib/codegen/script/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function* generatePropsOption(
142142
optionExpCodes.push(generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation));
143143
}
144144
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size) {
145-
let attrsType = `typeof __VLS_templateResult['attrs']`;
145+
let attrsType = `typeof __VLS_templateAttrs`;
146146
if (hasEmitsOption) {
147147
attrsType = `Omit<${attrsType}, \`on\${string}\`>`;
148148
}

packages/language-core/lib/codegen/script/scriptSetup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ function* generateSetupFunction(
288288
yield* generateModelEmits(options, scriptSetup, scriptSetupRanges);
289289
yield* generateStyleModules(options, ctx);
290290
yield* generateTemplate(options, ctx, false);
291-
yield `type __VLS_Refs = typeof __VLS_templateResult['refs']${endOfLine}`;
292-
yield `type __VLS_Slots = typeof __VLS_templateResult['slots']${endOfLine}`;
291+
yield `type __VLS_Refs = typeof __VLS_templateRefs${endOfLine}`;
292+
yield `type __VLS_Slots = typeof __VLS_templateSlots${endOfLine}`;
293293

294294
if (syntax) {
295295
if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges?.slots.define)) {

packages/language-core/lib/codegen/script/template.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ export function* generateTemplate(
110110
const templateUsageVars = [...getTemplateUsageVars(options, ctx)];
111111
yield `// @ts-ignore${newLine}`;
112112
yield `[${templateUsageVars.join(', ')}]${newLine}`;
113-
yield `const __VLS_templateResult { slots: {}, refs: {}, attrs: {} }${endOfLine}`;
113+
yield `const __VLS_templateSlots = {}${endOfLine}`;
114+
yield `const __VLS_templateRefs = {}${endOfLine}`;
115+
yield `const __VLS_templateAttrs = {}${endOfLine}`;
114116
}
115117
}
116118

@@ -162,11 +164,9 @@ function* generateTemplateBody(
162164
}
163165
}
164166

165-
yield `const __VLS_templateResult = {`;
166-
yield `slots: ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'},${newLine}`;
167-
yield `refs: $refs,${newLine}`;
168-
yield `attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${newLine}`;
169-
yield `}${endOfLine}`;
167+
yield `const __VLS_templateSlots = ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'}${endOfLine}`;
168+
yield `const __VLS_templateRefs = $refs${endOfLine}`;
169+
yield `const __VLS_templateAttrs = {} as Partial<typeof __VLS_inheritedAttrs>${endOfLine}`;
170170
}
171171

172172
export function* generateCssClassProperty(

packages/tsc/tests/__snapshots__/dts.spec.ts.snap

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -644,18 +644,13 @@ declare var __VLS_3: {
644644
num: number;
645645
str: string;
646646
};
647-
declare var __VLS_inheritedAttrs: {};
648-
declare const __VLS_templateResult: {
649-
slots: {
650-
"no-bind"?(_: typeof __VLS_0): any;
651-
default?(_: typeof __VLS_1): any;
652-
"named-slot"?(_: typeof __VLS_2): any;
653-
vbind?(_: typeof __VLS_3): any;
654-
};
655-
refs: {};
656-
attrs: Partial<typeof __VLS_inheritedAttrs>;
647+
declare const __VLS_templateSlots: {
648+
"no-bind"?(_: typeof __VLS_0): any;
649+
default?(_: typeof __VLS_1): any;
650+
"named-slot"?(_: typeof __VLS_2): any;
651+
vbind?(_: typeof __VLS_3): any;
657652
};
658-
type __VLS_Slots = typeof __VLS_templateResult['slots'];
653+
type __VLS_Slots = typeof __VLS_templateSlots;
659654
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
660655
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
661656
export default _default;
@@ -669,37 +664,32 @@ type __VLS_WithTemplateSlots<T, S> = T & {
669664
670665
exports[`vue-tsc-dts > Input: template-slots/component-define-slots.vue, Output: template-slots/component-define-slots.vue.d.ts 1`] = `
671666
"import { VNode } from 'vue';
672-
declare var __VLS_inheritedAttrs: {};
673-
declare const __VLS_templateResult: {
674-
slots: Readonly<{
675-
default: (props: {
676-
num: number;
677-
}) => VNode[];
678-
'named-slot': (props: {
679-
str: string;
680-
}) => VNode[];
681-
vbind: (props: {
682-
num: number;
683-
str: string;
684-
}) => VNode[];
685-
'no-bind': () => VNode[];
686-
}> & {
687-
default: (props: {
688-
num: number;
689-
}) => VNode[];
690-
'named-slot': (props: {
691-
str: string;
692-
}) => VNode[];
693-
vbind: (props: {
694-
num: number;
695-
str: string;
696-
}) => VNode[];
697-
'no-bind': () => VNode[];
698-
};
699-
refs: {};
700-
attrs: Partial<typeof __VLS_inheritedAttrs>;
667+
declare const __VLS_templateSlots: Readonly<{
668+
default: (props: {
669+
num: number;
670+
}) => VNode[];
671+
'named-slot': (props: {
672+
str: string;
673+
}) => VNode[];
674+
vbind: (props: {
675+
num: number;
676+
str: string;
677+
}) => VNode[];
678+
'no-bind': () => VNode[];
679+
}> & {
680+
default: (props: {
681+
num: number;
682+
}) => VNode[];
683+
'named-slot': (props: {
684+
str: string;
685+
}) => VNode[];
686+
vbind: (props: {
687+
num: number;
688+
str: string;
689+
}) => VNode[];
690+
'no-bind': () => VNode[];
701691
};
702-
type __VLS_Slots = typeof __VLS_templateResult['slots'];
692+
type __VLS_Slots = typeof __VLS_templateSlots;
703693
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
704694
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
705695
export default _default;
@@ -712,21 +702,16 @@ type __VLS_WithTemplateSlots<T, S> = T & {
712702
`;
713703
714704
exports[`vue-tsc-dts > Input: template-slots/component-destructuring.vue, Output: template-slots/component-destructuring.vue.d.ts 1`] = `
715-
"declare var __VLS_inheritedAttrs: {};
716-
declare const __VLS_templateResult: {
717-
slots: Readonly<{
718-
bottom: (props: {
719-
num: number;
720-
}) => any[];
721-
}> & {
722-
bottom: (props: {
723-
num: number;
724-
}) => any[];
725-
};
726-
refs: {};
727-
attrs: Partial<typeof __VLS_inheritedAttrs>;
705+
"declare const __VLS_templateSlots: Readonly<{
706+
bottom: (props: {
707+
num: number;
708+
}) => any[];
709+
}> & {
710+
bottom: (props: {
711+
num: number;
712+
}) => any[];
728713
};
729-
type __VLS_Slots = typeof __VLS_templateResult['slots'];
714+
type __VLS_Slots = typeof __VLS_templateSlots;
730715
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
731716
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
732717
export default _default;
@@ -750,18 +735,13 @@ declare var __VLS_3: {
750735
num: number;
751736
str: string;
752737
};
753-
declare var __VLS_inheritedAttrs: {};
754-
declare const __VLS_templateResult: {
755-
slots: {
756-
"no-bind"?(_: typeof __VLS_0): any;
757-
default?(_: typeof __VLS_1): any;
758-
"named-slot"?(_: typeof __VLS_2): any;
759-
vbind?(_: typeof __VLS_3): any;
760-
};
761-
refs: {};
762-
attrs: Partial<typeof __VLS_inheritedAttrs>;
738+
declare const __VLS_templateSlots: {
739+
"no-bind"?(_: typeof __VLS_0): any;
740+
default?(_: typeof __VLS_1): any;
741+
"named-slot"?(_: typeof __VLS_2): any;
742+
vbind?(_: typeof __VLS_3): any;
763743
};
764-
type __VLS_Slots = typeof __VLS_templateResult['slots'];
744+
type __VLS_Slots = typeof __VLS_templateSlots;
765745
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
766746
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
767747
export default _default;

0 commit comments

Comments
 (0)