From efa3bf0099ae476b737ce2c9f0a43d97c42daf1d Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Mon, 2 Sep 2024 01:42:30 +0800 Subject: [PATCH] refactor: split `__VLS_templateResult` --- .../lib/codegen/script/component.ts | 2 +- .../lib/codegen/script/scriptSetup.ts | 4 +- .../lib/codegen/script/template.ts | 12 +- .../tsc/tests/__snapshots__/dts.spec.ts.snap | 112 +++++++----------- 4 files changed, 55 insertions(+), 75 deletions(-) diff --git a/packages/language-core/lib/codegen/script/component.ts b/packages/language-core/lib/codegen/script/component.ts index 929d1f370d..c0a92e2997 100644 --- a/packages/language-core/lib/codegen/script/component.ts +++ b/packages/language-core/lib/codegen/script/component.ts @@ -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}\`>`; } diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index 5e4a8f684c..d259697360 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -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)) { diff --git a/packages/language-core/lib/codegen/script/template.ts b/packages/language-core/lib/codegen/script/template.ts index 56f366a37b..b2cc7d1f73 100644 --- a/packages/language-core/lib/codegen/script/template.ts +++ b/packages/language-core/lib/codegen/script/template.ts @@ -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}`; } } @@ -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,${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${endOfLine}`; } export function* generateCssClassProperty( diff --git a/packages/tsc/tests/__snapshots__/dts.spec.ts.snap b/packages/tsc/tests/__snapshots__/dts.spec.ts.snap index 0b12b2b53a..d53c99917d 100644 --- a/packages/tsc/tests/__snapshots__/dts.spec.ts.snap +++ b/packages/tsc/tests/__snapshots__/dts.spec.ts.snap @@ -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; +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>, {}, {}>; declare const _default: __VLS_WithTemplateSlots; export default _default; @@ -669,37 +664,32 @@ type __VLS_WithTemplateSlots = 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; +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>, {}, {}>; declare const _default: __VLS_WithTemplateSlots; export default _default; @@ -712,21 +702,16 @@ type __VLS_WithTemplateSlots = 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; +"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>, {}, {}>; declare const _default: __VLS_WithTemplateSlots; export default _default; @@ -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; +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>, {}, {}>; declare const _default: __VLS_WithTemplateSlots; export default _default;