Skip to content

Commit

Permalink
fix(language-core): should try casting dynamic slot name into constant (
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX authored Aug 9, 2024
1 parent eef948d commit c5beb97
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/language-core/lib/codegen/script/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ declare global {
: false;
function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
function __VLS_tryAsConstant<const T>(t: T): T;
/**
* emit
Expand Down
4 changes: 3 additions & 1 deletion packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
19 changes: 17 additions & 2 deletions packages/language-core/lib/codegen/template/objectProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ export function* generateObjectProperty(
offset: number,
features: VueCodeInformation,
astHolder?: any,
shouldCamelize = false
shouldCamelize = false,
shouldBeConstant = false
): Generator<Code> {
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))) {
Expand Down
8 changes: 8 additions & 0 deletions test-workspace/tsc/vue3/#4668/child.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template></template>

<script lang="ts" setup>
defineSlots<{
action1: (props: { a: string }) => void
action2: (props: { a: string }) => void
}>()
</script>
16 changes: 16 additions & 0 deletions test-workspace/tsc/vue3/#4668/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<template>
<Child>
<template #[`action${n}`]="data">
{{ exactType(data.a, {} as string) }}
</template>
<template #action2="data">
{{ exactType(data.a, {} as string) }}
</template>
</Child>
</template>
<script lang="ts" setup>
import { exactType } from 'tsc/shared'
import Child from './child.vue'
const n = 1 as const
</script>

0 comments on commit c5beb97

Please sign in to comment.