From 51bd9307a6770fdd84cc049f7cf315181a06c27d Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Tue, 27 Aug 2024 18:02:51 +0800 Subject: [PATCH] fix(language-core): fallthrough attributes break component type when root tag type is unknown (#4729) --- .../lib/codegen/script/component.ts | 17 ++++++++++++----- .../inheritAttrs_unknownTag/basic.vue | 7 +++++++ .../inheritAttrs_unknownTag/main.vue | 7 +++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/basic.vue create mode 100644 test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/main.vue diff --git a/packages/language-core/lib/codegen/script/component.ts b/packages/language-core/lib/codegen/script/component.ts index ca7cf0b44d..6ed34d5a3c 100644 --- a/packages/language-core/lib/codegen/script/component.ts +++ b/packages/language-core/lib/codegen/script/component.ts @@ -128,10 +128,6 @@ export function* generatePropsOption( const optionExpCodes: Code[] = []; const typeOptionExpCodes: Code[] = []; - if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size && !hasEmitsOption) { - optionExpCodes.push(`{} as ${ctx.helperTypes.TypePropsToOption.name}<__VLS_PickNotAny<${ctx.helperTypes.OmitIndexSignature.name}['attrs']>, {}>>`); - typeOptionExpCodes.push(`{} as ReturnType['attrs']`); - } if (ctx.generatedPropsType) { optionExpCodes.push([ `{} as `, @@ -141,11 +137,22 @@ export function* generatePropsOption( ].join('')); typeOptionExpCodes.push(`{} as __VLS_PublicProps`); } - if (scriptSetupRanges.props.define?.arg) { const { arg } = scriptSetupRanges.props.define; optionExpCodes.push(generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation)); } + if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size && !hasEmitsOption) { + const attrsType = `ReturnType['attrs']`; + const optionType = `${ctx.helperTypes.TypePropsToOption.name}<__VLS_PickNotAny<${ctx.helperTypes.OmitIndexSignature.name}<${attrsType}>, {}>>`; + if (optionExpCodes.length) { + optionExpCodes.unshift(`{} as ${optionType}`); + } + else { + // workaround for https://github.com/vuejs/core/pull/7419 + optionExpCodes.unshift(`{} as keyof ${attrsType} extends never ? never: ${optionType}`); + } + typeOptionExpCodes.unshift(`{} as ${attrsType}`); + } const useTypeOption = options.vueCompilerOptions.target >= 3.5 && typeOptionExpCodes.length; const useOption = (!useTypeOption || scriptSetupRanges.props.withDefaults) && optionExpCodes.length; diff --git a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/basic.vue b/test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/basic.vue new file mode 100644 index 0000000000..88fa3d63aa --- /dev/null +++ b/test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/basic.vue @@ -0,0 +1,7 @@ + + + diff --git a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/main.vue b/test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/main.vue new file mode 100644 index 0000000000..e177ec22aa --- /dev/null +++ b/test-workspace/tsc/passedFixtures/vue3_strictTemplate/inheritAttrs_unknownTag/main.vue @@ -0,0 +1,7 @@ + + +