Skip to content

Commit

Permalink
fix(language-core): ObjectDirective does not work with defineSlots
Browse files Browse the repository at this point in the history
close #4327
  • Loading branch information
johnsoncodehk committed Apr 30, 2024
1 parent d523035 commit c24c6a7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
Expand Up @@ -22,7 +22,8 @@ export function* generateElementChildren(

// fix https://github.com/vuejs/language-tools/issues/932
if (
!ctx.hasSlotElements.has(node)
componentCtxVar
&& !ctx.hasSlotElements.has(node)
&& node.children.length
&& node.tagType !== CompilerDOM.ElementTypes.ELEMENT
&& node.tagType !== CompilerDOM.ElementTypes.TEMPLATE
Expand Down
85 changes: 42 additions & 43 deletions packages/language-core/lib/codegen/template/slotOutlet.ts
Expand Up @@ -58,55 +58,54 @@ export function* generateSlotOutlet(
`}`,
);
yield `)${endOfLine}`;
return;
}
else {
yield `var ${varSlot} = {${newLine}`;
yield* generateElementProps(options, ctx, node, node.props.filter(prop => prop !== nameProp), true);
yield `}${endOfLine}`;
}

if (
nameProp?.type === CompilerDOM.NodeTypes.ATTRIBUTE
&& nameProp.value
) {
ctx.slots.push({
name: nameProp.value.content,
loc: nameProp.loc.start.offset + nameProp.loc.source.indexOf(nameProp.value.content, nameProp.name.length),
tagRange: [startTagOffset, startTagOffset + node.tag.length],
varName: varSlot,
nodeLoc: node.loc,
});
}
else if (
nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE
&& nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
const slotExpVar = ctx.getInternalVariable();
yield `var ${slotExpVar} = `;
yield* generateInterpolation(
options,
ctx,
nameProp.exp.content,
nameProp.exp,
nameProp.exp.loc.start.offset,
ctx.codeFeatures.all,
'(',
')',
);
yield ` as const${endOfLine}`;
ctx.dynamicSlots.push({
expVar: slotExpVar,
varName: varSlot,
});
}
else {
ctx.slots.push({
name: 'default',
tagRange: [startTagOffset, startTagOffset + node.tag.length],
varName: varSlot,
nodeLoc: node.loc,
});
if (
nameProp?.type === CompilerDOM.NodeTypes.ATTRIBUTE
&& nameProp.value
) {
ctx.slots.push({
name: nameProp.value.content,
loc: nameProp.loc.start.offset + nameProp.loc.source.indexOf(nameProp.value.content, nameProp.name.length),
tagRange: [startTagOffset, startTagOffset + node.tag.length],
varName: varSlot,
nodeLoc: node.loc,
});
}
else if (
nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE
&& nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
const slotExpVar = ctx.getInternalVariable();
yield `var ${slotExpVar} = `;
yield* generateInterpolation(
options,
ctx,
nameProp.exp.content,
nameProp.exp,
nameProp.exp.loc.start.offset,
ctx.codeFeatures.all,
'(',
')',
);
yield ` as const${endOfLine}`;
ctx.dynamicSlots.push({
expVar: slotExpVar,
varName: varSlot,
});
}
else {
ctx.slots.push({
name: 'default',
tagRange: [startTagOffset, startTagOffset + node.tag.length],
varName: varSlot,
nodeLoc: node.loc,
});
}
}
yield* ctx.generateAutoImportCompletion();
yield* generateElementChildren(options, ctx, node, currentComponent, componentCtxVar);
Expand Down
1 change: 1 addition & 0 deletions test-workspace/tsc/vue2/tsconfig.json
Expand Up @@ -17,6 +17,7 @@
"../vue3/#3615",
"../vue3/#3656",
"../vue3/#3672",
"../vue3/#4327",
"../vue3/components",
"../vue3/defineEmits",
"../vue3/defineModel",
Expand Down
17 changes: 17 additions & 0 deletions test-workspace/tsc/vue3/#4327/main.vue
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { ObjectDirective } from 'vue';
const vTest: ObjectDirective<HTMLElement, any | undefined> = {
mounted() { },
};
defineSlots<{
default(): any;
}>();
</script>

<template>
<slot>
<p v-test="{}">Default slot content</p>
</slot>
</template>

0 comments on commit c24c6a7

Please sign in to comment.