Skip to content

Commit c5beb97

Browse files
authored
fix(language-core): should try casting dynamic slot name into constant (#4669)
1 parent eef948d commit c5beb97

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

packages/language-core/lib/codegen/script/globalTypes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ declare global {
100100
: false;
101101
102102
function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
103+
function __VLS_tryAsConstant<const T>(t: T): T;
103104
104105
/**
105106
* emit

packages/language-core/lib/codegen/template/element.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ function* generateComponentSlot(
444444
slotDir.arg.loc.source,
445445
slotDir.arg.loc.start.offset,
446446
slotDir.arg.isStatic ? ctx.codeFeatures.withoutHighlight : ctx.codeFeatures.all,
447-
slotDir.arg.loc
447+
slotDir.arg.loc,
448+
false,
449+
true,
448450
);
449451
yield ': __VLS_thisSlot';
450452
}

packages/language-core/lib/codegen/template/objectProperty.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,25 @@ export function* generateObjectProperty(
1414
offset: number,
1515
features: VueCodeInformation,
1616
astHolder?: any,
17-
shouldCamelize = false
17+
shouldCamelize = false,
18+
shouldBeConstant = false
1819
): Generator<Code> {
1920
if (code.startsWith('[') && code.endsWith(']') && astHolder) {
20-
yield* generateInterpolation(options, ctx, code, astHolder, offset, features, '', '');
21+
if (shouldBeConstant) {
22+
yield* generateInterpolation(
23+
options,
24+
ctx,
25+
code.slice(1, -1),
26+
astHolder,
27+
offset + 1,
28+
features,
29+
'[__VLS_tryAsConstant(',
30+
')]',
31+
);
32+
}
33+
else {
34+
yield* generateInterpolation(options, ctx, code, astHolder, offset, features, '', '');
35+
}
2136
}
2237
else if (shouldCamelize) {
2338
if (variableNameRegex.test(camelize(code))) {
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template></template>
2+
3+
<script lang="ts" setup>
4+
defineSlots<{
5+
action1: (props: { a: string }) => void
6+
action2: (props: { a: string }) => void
7+
}>()
8+
</script>
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<Child>
3+
<template #[`action${n}`]="data">
4+
{{ exactType(data.a, {} as string) }}
5+
</template>
6+
<template #action2="data">
7+
{{ exactType(data.a, {} as string) }}
8+
</template>
9+
</Child>
10+
</template>
11+
<script lang="ts" setup>
12+
import { exactType } from 'tsc/shared'
13+
import Child from './child.vue'
14+
15+
const n = 1 as const
16+
</script>

0 commit comments

Comments
 (0)