From 1e408b8d568469ca0966bee18b4bc1508fd12659 Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Fri, 20 Dec 2024 04:26:20 +0800 Subject: [PATCH] fix(language-core): only generate the props it needs in generic components --- .../lib/codegen/script/scriptSetup.ts | 62 ++++++++++--------- .../tsc/tests/__snapshots__/dts.spec.ts.snap | 14 ++--- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index bd4b7345e8..3f2a959ee1 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -66,7 +66,7 @@ export function* generateScriptSetup( } yield `return {} as {${newLine}` - + ` props: ${ctx.localTypes.PrettifyLocal} & __VLS_BuiltInPublicProps,${newLine}` + + ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & __VLS_TemplateResult['attrs']> & __VLS_BuiltInPublicProps,${newLine}` + ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}` + ` attrs: any,${newLine}` + ` slots: __VLS_TemplateResult['slots'],${newLine}` @@ -368,37 +368,39 @@ function* generateComponentProps( scriptSetup: NonNullable, scriptSetupRanges: ScriptSetupRanges ): Generator { - yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`; - - if (scriptSetupRanges.defineProps?.arg) { - yield `props: `; - yield generateSfcBlockSection( - scriptSetup, - scriptSetupRanges.defineProps.arg.start, - scriptSetupRanges.defineProps.arg.end, - codeFeatures.navigation - ); - yield `,${newLine}`; + if (scriptSetup.generic) { + yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`; + + if (scriptSetupRanges.defineProps?.arg) { + yield `props: `; + yield generateSfcBlockSection( + scriptSetup, + scriptSetupRanges.defineProps.arg.start, + scriptSetupRanges.defineProps.arg.end, + codeFeatures.navigation + ); + yield `,${newLine}`; + } + + yield* generateEmitsOption(options, scriptSetupRanges); + + yield `})${endOfLine}`; + + yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4 + ? `import('${options.vueCompilerOptions.lib}').PublicProps` + : options.vueCompilerOptions.target >= 3.0 + ? `import('${options.vueCompilerOptions.lib}').VNodeProps` + + ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps` + + ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps` + : `globalThis.JSX.IntrinsicAttributes` + }`; + yield endOfLine; + + yield `type __VLS_OwnProps = `; + yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}['$props'], keyof __VLS_BuiltInPublicProps>`; + yield endOfLine; } - yield* generateEmitsOption(options, scriptSetupRanges); - - yield `})${endOfLine}`; - - yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4 - ? `import('${options.vueCompilerOptions.lib}').PublicProps` - : options.vueCompilerOptions.target >= 3.0 - ? `import('${options.vueCompilerOptions.lib}').VNodeProps` - + ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps` - + ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps` - : `globalThis.JSX.IntrinsicAttributes` - }`; - yield endOfLine; - - yield `let __VLS_functionalComponentProps!: `; - yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}['$props'], keyof __VLS_BuiltInPublicProps>`; - yield endOfLine; - if (scriptSetupRanges.defineProp.length) { yield `const __VLS_defaults = {${newLine}`; for (const defineProp of scriptSetupRanges.defineProp) { diff --git a/packages/tsc/tests/__snapshots__/dts.spec.ts.snap b/packages/tsc/tests/__snapshots__/dts.spec.ts.snap index 7c6e412db5..6959c4430d 100644 --- a/packages/tsc/tests/__snapshots__/dts.spec.ts.snap +++ b/packages/tsc/tests/__snapshots__/dts.spec.ts.snap @@ -5,10 +5,10 @@ exports[`vue-tsc-dts > Input: #4577/main.vue, Output: #4577/main.vue.d.ts 1`] = value: string; }; declare const _default: (__VLS_props: NonNullable>["props"], __VLS_ctx?: __VLS_PrettifyLocal>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable>["expose"], __VLS_setup?: Promise<{ - props: __VLS_PrettifyLocal & Omit<{} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, never> & Partial<{}> & { + props: __VLS_PrettifyLocal & Omit<{} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, never> & { nonGeneric: string; rows: Row[]; - }> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); + } & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); expose(exposed: import("vue").ShallowUnwrapRef<{}>): void; attrs: any; slots: { @@ -69,7 +69,7 @@ exports[`vue-tsc-dts > Input: events/component-generic.vue, Output: events/compo "declare const _default: (__VLS_props: NonNullable>["props"], __VLS_ctx?: __VLS_PrettifyLocal>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable>["expose"], __VLS_setup?: Promise<{ props: __VLS_PrettifyLocal & Omit<{ readonly onFoo?: (value: string) => any; - } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onFoo"> & Partial<{}> & {}> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); + } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onFoo"> & {} & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); expose(exposed: import("vue").ShallowUnwrapRef<{}>): void; attrs: any; slots: {}; @@ -89,11 +89,11 @@ exports[`vue-tsc-dts > Input: generic/component.vue, Output: generic/component.v props: __VLS_PrettifyLocal & Omit<{ readonly "onUpdate:title"?: (value: string) => any; readonly onBar?: (data: number) => any; - } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & Partial<{}> & ({ + } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & ({ title?: string; } & { foo: number; - })> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); + }) & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); expose(exposed: import("vue").ShallowUnwrapRef<{ baz: number; }>): void; @@ -123,11 +123,11 @@ exports[`vue-tsc-dts > Input: generic/custom-extension-component.cext, Output: g props: __VLS_PrettifyLocal & Omit<{ readonly "onUpdate:title"?: (value: string) => any; readonly onBar?: (data: number) => any; - } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & Partial<{}> & ({ + } & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & ({ title?: string; } & { foo: number; - })> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); + }) & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps); expose(exposed: import("vue").ShallowUnwrapRef<{ baz: number; }>): void;