From c5beb97fffb6aafeaabb3f88a942ca9b86ca8568 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Fri, 9 Aug 2024 14:00:28 +0800 Subject: [PATCH] fix(language-core): should try casting dynamic slot name into constant (#4669) --- .../lib/codegen/script/globalTypes.ts | 1 + .../lib/codegen/template/element.ts | 4 +++- .../lib/codegen/template/objectProperty.ts | 19 +++++++++++++++++-- test-workspace/tsc/vue3/#4668/child.vue | 8 ++++++++ test-workspace/tsc/vue3/#4668/main.vue | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test-workspace/tsc/vue3/#4668/child.vue create mode 100644 test-workspace/tsc/vue3/#4668/main.vue diff --git a/packages/language-core/lib/codegen/script/globalTypes.ts b/packages/language-core/lib/codegen/script/globalTypes.ts index 7148e107b0..923d971aa9 100644 --- a/packages/language-core/lib/codegen/script/globalTypes.ts +++ b/packages/language-core/lib/codegen/script/globalTypes.ts @@ -100,6 +100,7 @@ declare global { : false; function __VLS_normalizeSlot(s: S): S extends () => infer R ? (props: {}) => R : S; + function __VLS_tryAsConstant(t: T): T; /** * emit diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index a7a55b479f..763d9237e2 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -444,7 +444,9 @@ function* generateComponentSlot( slotDir.arg.loc.source, slotDir.arg.loc.start.offset, slotDir.arg.isStatic ? ctx.codeFeatures.withoutHighlight : ctx.codeFeatures.all, - slotDir.arg.loc + slotDir.arg.loc, + false, + true, ); yield ': __VLS_thisSlot'; } diff --git a/packages/language-core/lib/codegen/template/objectProperty.ts b/packages/language-core/lib/codegen/template/objectProperty.ts index 702908d19b..eadf2a3a97 100644 --- a/packages/language-core/lib/codegen/template/objectProperty.ts +++ b/packages/language-core/lib/codegen/template/objectProperty.ts @@ -14,10 +14,25 @@ export function* generateObjectProperty( offset: number, features: VueCodeInformation, astHolder?: any, - shouldCamelize = false + shouldCamelize = false, + shouldBeConstant = false ): Generator { if (code.startsWith('[') && code.endsWith(']') && astHolder) { - yield* generateInterpolation(options, ctx, code, astHolder, offset, features, '', ''); + if (shouldBeConstant) { + yield* generateInterpolation( + options, + ctx, + code.slice(1, -1), + astHolder, + offset + 1, + features, + '[__VLS_tryAsConstant(', + ')]', + ); + } + else { + yield* generateInterpolation(options, ctx, code, astHolder, offset, features, '', ''); + } } else if (shouldCamelize) { if (variableNameRegex.test(camelize(code))) { diff --git a/test-workspace/tsc/vue3/#4668/child.vue b/test-workspace/tsc/vue3/#4668/child.vue new file mode 100644 index 0000000000..3163f4d0d3 --- /dev/null +++ b/test-workspace/tsc/vue3/#4668/child.vue @@ -0,0 +1,8 @@ + + + diff --git a/test-workspace/tsc/vue3/#4668/main.vue b/test-workspace/tsc/vue3/#4668/main.vue new file mode 100644 index 0000000000..5e701d317a --- /dev/null +++ b/test-workspace/tsc/vue3/#4668/main.vue @@ -0,0 +1,16 @@ + +