Skip to content

Commit

Permalink
refactor: split __VLS_templateResult
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Sep 1, 2024
1 parent 0fd9a7a commit efa3bf0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 75 deletions.
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function* generatePropsOption(
optionExpCodes.push(generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation));
}
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size) {
let attrsType = `typeof __VLS_templateResult['attrs']`;
let attrsType = `typeof __VLS_templateAttrs`;
if (hasEmitsOption) {
attrsType = `Omit<${attrsType}, \`on\${string}\`>`;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ function* generateSetupFunction(
yield* generateModelEmits(options, scriptSetup, scriptSetupRanges);
yield* generateStyleModules(options, ctx);
yield* generateTemplate(options, ctx, false);
yield `type __VLS_Refs = typeof __VLS_templateResult['refs']${endOfLine}`;
yield `type __VLS_Slots = typeof __VLS_templateResult['slots']${endOfLine}`;
yield `type __VLS_Refs = typeof __VLS_templateRefs${endOfLine}`;
yield `type __VLS_Slots = typeof __VLS_templateSlots${endOfLine}`;

if (syntax) {
if (!options.vueCompilerOptions.skipTemplateCodegen && (options.templateCodegen?.hasSlot || scriptSetupRanges?.slots.define)) {
Expand Down
12 changes: 6 additions & 6 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export function* generateTemplate(
const templateUsageVars = [...getTemplateUsageVars(options, ctx)];
yield `// @ts-ignore${newLine}`;
yield `[${templateUsageVars.join(', ')}]${newLine}`;
yield `const __VLS_templateResult { slots: {}, refs: {}, attrs: {} }${endOfLine}`;
yield `const __VLS_templateSlots = {}${endOfLine}`;
yield `const __VLS_templateRefs = {}${endOfLine}`;
yield `const __VLS_templateAttrs = {}${endOfLine}`;
}
}

Expand Down Expand Up @@ -162,11 +164,9 @@ function* generateTemplateBody(
}
}

yield `const __VLS_templateResult = {`;
yield `slots: ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'},${newLine}`;
yield `refs: $refs,${newLine}`;
yield `attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${newLine}`;
yield `}${endOfLine}`;
yield `const __VLS_templateSlots = ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'}${endOfLine}`;
yield `const __VLS_templateRefs = $refs${endOfLine}`;
yield `const __VLS_templateAttrs = {} as Partial<typeof __VLS_inheritedAttrs>${endOfLine}`;
}

export function* generateCssClassProperty(
Expand Down
112 changes: 46 additions & 66 deletions packages/tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -644,18 +644,13 @@ declare var __VLS_3: {
num: number;
str: string;
};
declare var __VLS_inheritedAttrs: {};
declare const __VLS_templateResult: {
slots: {
"no-bind"?(_: typeof __VLS_0): any;
default?(_: typeof __VLS_1): any;
"named-slot"?(_: typeof __VLS_2): any;
vbind?(_: typeof __VLS_3): any;
};
refs: {};
attrs: Partial<typeof __VLS_inheritedAttrs>;
declare const __VLS_templateSlots: {
"no-bind"?(_: typeof __VLS_0): any;
default?(_: typeof __VLS_1): any;
"named-slot"?(_: typeof __VLS_2): any;
vbind?(_: typeof __VLS_3): any;
};
type __VLS_Slots = typeof __VLS_templateResult['slots'];
type __VLS_Slots = typeof __VLS_templateSlots;
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
export default _default;
Expand All @@ -669,37 +664,32 @@ type __VLS_WithTemplateSlots<T, S> = T & {
exports[`vue-tsc-dts > Input: template-slots/component-define-slots.vue, Output: template-slots/component-define-slots.vue.d.ts 1`] = `
"import { VNode } from 'vue';
declare var __VLS_inheritedAttrs: {};
declare const __VLS_templateResult: {
slots: Readonly<{
default: (props: {
num: number;
}) => VNode[];
'named-slot': (props: {
str: string;
}) => VNode[];
vbind: (props: {
num: number;
str: string;
}) => VNode[];
'no-bind': () => VNode[];
}> & {
default: (props: {
num: number;
}) => VNode[];
'named-slot': (props: {
str: string;
}) => VNode[];
vbind: (props: {
num: number;
str: string;
}) => VNode[];
'no-bind': () => VNode[];
};
refs: {};
attrs: Partial<typeof __VLS_inheritedAttrs>;
declare const __VLS_templateSlots: Readonly<{
default: (props: {
num: number;
}) => VNode[];
'named-slot': (props: {
str: string;
}) => VNode[];
vbind: (props: {
num: number;
str: string;
}) => VNode[];
'no-bind': () => VNode[];
}> & {
default: (props: {
num: number;
}) => VNode[];
'named-slot': (props: {
str: string;
}) => VNode[];
vbind: (props: {
num: number;
str: string;
}) => VNode[];
'no-bind': () => VNode[];
};
type __VLS_Slots = typeof __VLS_templateResult['slots'];
type __VLS_Slots = typeof __VLS_templateSlots;
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
export default _default;
Expand All @@ -712,21 +702,16 @@ type __VLS_WithTemplateSlots<T, S> = T & {
`;
exports[`vue-tsc-dts > Input: template-slots/component-destructuring.vue, Output: template-slots/component-destructuring.vue.d.ts 1`] = `
"declare var __VLS_inheritedAttrs: {};
declare const __VLS_templateResult: {
slots: Readonly<{
bottom: (props: {
num: number;
}) => any[];
}> & {
bottom: (props: {
num: number;
}) => any[];
};
refs: {};
attrs: Partial<typeof __VLS_inheritedAttrs>;
"declare const __VLS_templateSlots: Readonly<{
bottom: (props: {
num: number;
}) => any[];
}> & {
bottom: (props: {
num: number;
}) => any[];
};
type __VLS_Slots = typeof __VLS_templateResult['slots'];
type __VLS_Slots = typeof __VLS_templateSlots;
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
export default _default;
Expand All @@ -750,18 +735,13 @@ declare var __VLS_3: {
num: number;
str: string;
};
declare var __VLS_inheritedAttrs: {};
declare const __VLS_templateResult: {
slots: {
"no-bind"?(_: typeof __VLS_0): any;
default?(_: typeof __VLS_1): any;
"named-slot"?(_: typeof __VLS_2): any;
vbind?(_: typeof __VLS_3): any;
};
refs: {};
attrs: Partial<typeof __VLS_inheritedAttrs>;
declare const __VLS_templateSlots: {
"no-bind"?(_: typeof __VLS_0): any;
default?(_: typeof __VLS_1): any;
"named-slot"?(_: typeof __VLS_2): any;
vbind?(_: typeof __VLS_3): any;
};
type __VLS_Slots = typeof __VLS_templateResult['slots'];
type __VLS_Slots = typeof __VLS_templateSlots;
declare const __VLS_component: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_Slots>;
export default _default;
Expand Down

0 comments on commit efa3bf0

Please sign in to comment.