Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should try casting dynamic slot name into constant #4669

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading