Skip to content

fix(language-core): do not generate element for <template> with v-slot #5077

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

Merged
merged 9 commits into from
Dec 31, 2024
Prev Previous commit
Next Next commit
refactor: remove hasSlotElements
  • Loading branch information
KazariEX committed Dec 23, 2024
commit e1c024fcd40b6b48983a2414e8c8ae6f6c1ba6ef
2 changes: 0 additions & 2 deletions packages/language-core/lib/codegen/template/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
expVar: string;
varName: string;
}[] = [];
const hasSlotElements = new Set<CompilerDOM.ElementNode>();;
const blockConditions: string[] = [];
const scopedClasses: {
source: string;
Expand All @@ -134,7 +133,6 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
codeFeatures,
accessExternalVariables,
lastGenericComment,
hasSlotElements,
blockConditions,
scopedClasses,
emptyClassOffsets,
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export function* generateComponent(
yield* generateVSlot(options, ctx, node, slotDir);
}
else {
yield* generateElementChildren(options, ctx, node);
yield* generateElementChildren(options, ctx, node, true);
}

if (ctx.currentComponent.used) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { generateTemplateChild } from './templateChild';
export function* generateElementChildren(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext,
node: CompilerDOM.ElementNode
node: CompilerDOM.ElementNode,
isDefaultSlot: boolean = false
): Generator<Code> {
yield* ctx.resetDirectiveComments('end of element children start');
let prev: CompilerDOM.TemplateChildNode | undefined;
Expand All @@ -21,10 +22,9 @@ export function* generateElementChildren(
// fix https://github.com/vuejs/language-tools/issues/932
if (
ctx.currentComponent
&& !ctx.hasSlotElements.has(node)
&& isDefaultSlot
&& node.children.length
&& node.tagType !== CompilerDOM.ElementTypes.ELEMENT
&& node.tagType !== CompilerDOM.ElementTypes.TEMPLATE
&& node.tagType === CompilerDOM.ElementTypes.COMPONENT
) {
ctx.currentComponent.used = true;
yield `${ctx.currentComponent.ctxVar}.slots!.`;
Expand Down
4 changes: 1 addition & 3 deletions packages/language-core/lib/codegen/template/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export function* generateVSlot(
if (!ctx.currentComponent) {
return;
}

const slotBlockVars: string[] = [];
ctx.currentComponent.used = true;
ctx.hasSlotElements.add(ctx.currentComponent.node);
const slotBlockVars: string[] = [];
yield `{${newLine}`;

yield `const { `;
Expand Down
Loading