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(language-core): type-checking not working with hyphen in slot name with JS #4478

Merged
merged 9 commits into from
Jul 20, 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
73 changes: 32 additions & 41 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generatePropertyAccess } from './propertyAccess';
import { generateTemplateChild } from './templateChild';
import { generateObjectProperty } from './objectProperty';

const colonReg = /:/g;

Expand Down Expand Up @@ -419,12 +420,38 @@ function* generateComponentSlot(
ctx.hasSlotElements.add(currentComponent);
}
const slotBlockVars: string[] = [];
let hasProps = false;
if (slotDir?.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
yield `const {`;
if (slotDir?.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content) {
yield* generateObjectProperty(
options,
ctx,
slotDir.arg.loc.source,
slotDir.arg.loc.start.offset,
slotDir.arg.isStatic ? ctx.codeFeatures.withoutHighlight : ctx.codeFeatures.all,
slotDir.arg.loc
);
yield ': __VLS_thisSlot';
}
else {
yield `default: `;
yield* wrapWith(
slotDir.loc.start.offset,
slotDir.loc.start.offset + (
slotDir.loc.source.startsWith('#')
? '#'.length
: slotDir.loc.source.startsWith('v-slot:')
? 'v-slot:'.length
: 0
),
ctx.codeFeatures.withoutHighlightAndCompletion,
`__VLS_thisSlot`
);
}
yield `} = ${componentCtxVar}.slots!${endOfLine}`;

if (slotDir?.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
const slotAst = createTsAst(options.ts, slotDir, `(${slotDir.exp.content}) => {}`);
collectVars(options.ts, slotAst, slotAst, slotBlockVars);
hasProps = true;
if (!slotDir.exp.content.includes(':')) {
yield `const [`;
yield [
Expand All @@ -433,7 +460,7 @@ function* generateComponentSlot(
slotDir.exp.loc.start.offset,
ctx.codeFeatures.all,
];
yield `] = __VLS_getSlotParams(`;
yield `] = __VLS_getSlotParams(__VLS_thisSlot)${endOfLine}`;
}
else {
yield `const `;
Expand All @@ -443,45 +470,9 @@ function* generateComponentSlot(
slotDir.exp.loc.start.offset,
ctx.codeFeatures.all,
];
yield ` = __VLS_getSlotParam(`;
yield ` = __VLS_getSlotParam(__VLS_thisSlot)${endOfLine}`;
}
}
yield* wrapWith(
(slotDir.arg ?? slotDir).loc.start.offset,
(slotDir.arg ?? slotDir).loc.end.offset,
ctx.codeFeatures.verification,
`(${componentCtxVar}.slots!)`,
...(
slotDir?.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content
? generatePropertyAccess(
options,
ctx,
slotDir.arg.loc.source,
slotDir.arg.loc.start.offset,
slotDir.arg.isStatic ? ctx.codeFeatures.withoutHighlight : ctx.codeFeatures.all,
slotDir.arg.loc
)
: [
`.`,
...wrapWith(
slotDir.loc.start.offset,
slotDir.loc.start.offset + (
slotDir.loc.source.startsWith('#')
? '#'.length
: slotDir.loc.source.startsWith('v-slot:')
? 'v-slot:'.length
: 0
),
ctx.codeFeatures.withoutHighlightAndCompletion,
`default`
)
]
)
);
if (hasProps) {
yield `)`;
}
yield endOfLine;

for (const varName of slotBlockVars) {
ctx.addLocalVariable(varName);
Expand Down
7 changes: 7 additions & 0 deletions test-workspace/tsc/vue3/#3819/comp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts" setup generic="T">
type ISlots = {
"last-columns": (props: string) => void;
"lastColumns": (props: string) => void;
};
defineSlots<ISlots>()
</script>
21 changes: 21 additions & 0 deletions test-workspace/tsc/vue3/#3819/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script setup>
import Comp from './comp.vue';

const config = {
a: 'a',
b: 'b',
c: 'c',
d: 'd',
};
</script>

<template>
<Comp :config="config">
<template #last-columns></template>
<!-- @vue-expect-error -->
<template #last-columnsasd></template>
<template #lastColumns></template>
<!-- @vue-expect-error -->
<template #lastColumnsasd></template>
</Comp>
</template>
Loading