From 638d6534d8e7d2bbb0a045bbee64f4ba25dbdaf0 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Thu, 8 Aug 2024 23:19:32 +0800 Subject: [PATCH 1/2] fix: should try casting dynamic slot name into constant --- .../lib/codegen/script/globalTypes.ts | 1 + .../lib/codegen/template/element.ts | 4 +++- .../lib/codegen/template/objectProperty.ts | 14 ++++++++++++-- test-workspace/tsc/vue3/#4668/child.vue | 8 ++++++++ test-workspace/tsc/vue3/#4668/main.vue | 16 ++++++++++++++++ 5 files changed, 40 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..622bb7d3df 100644 --- a/packages/language-core/lib/codegen/template/objectProperty.ts +++ b/packages/language-core/lib/codegen/template/objectProperty.ts @@ -14,10 +14,20 @@ 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, '', ''); + yield* generateInterpolation( + options, + ctx, + code.slice(1, -1), + astHolder, + offset + 1, + features, + shouldBeConstant ? '[__VLS_tryAsConstant(' : '[', + shouldBeConstant ? ')]' : ']', + ); } 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 @@ + + From 2ebd13764f2d25689a8c8e1678330ae080fa1105 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Thu, 8 Aug 2024 23:32:36 +0800 Subject: [PATCH 2/2] chore: update --- .../lib/codegen/template/objectProperty.ts | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/language-core/lib/codegen/template/objectProperty.ts b/packages/language-core/lib/codegen/template/objectProperty.ts index 622bb7d3df..eadf2a3a97 100644 --- a/packages/language-core/lib/codegen/template/objectProperty.ts +++ b/packages/language-core/lib/codegen/template/objectProperty.ts @@ -15,19 +15,24 @@ export function* generateObjectProperty( features: VueCodeInformation, astHolder?: any, shouldCamelize = false, - shouldBeConstant = false, + shouldBeConstant = false ): Generator { if (code.startsWith('[') && code.endsWith(']') && astHolder) { - yield* generateInterpolation( - options, - ctx, - code.slice(1, -1), - astHolder, - offset + 1, - features, - shouldBeConstant ? '[__VLS_tryAsConstant(' : '[', - shouldBeConstant ? ')]' : ']', - ); + 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))) {