Skip to content

fix(language-core): intersect __VLS_slots with __VLS_ctx.$slots #5083

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 12 commits into from
Feb 12, 2025
Merged
Prev Previous commit
Next Next commit
refactor: rename some properties
  • Loading branch information
KazariEX committed Dec 24, 2024
commit ff251712f9ed14d30c52269b048e8ddf7fd2a8c7
6 changes: 3 additions & 3 deletions packages/language-core/lib/codegen/template/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
const accessExternalVariables = new Map<string, Set<number>>();
const slots: {
name: string;
loc?: number;
offset?: number;
tagRange: [number, number];
varName: string;
nodeLoc: any;
propsVar: string;
}[] = [];
const dynamicSlots: {
expVar: string;
varName: string;
propsVar: string;
}[] = [];
const hasSlotElements = new Set<CompilerDOM.ElementNode>();;
const blockConditions: string[] = [];
Expand Down
10 changes: 5 additions & 5 deletions packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ function* generateSlots(options: TemplateCodegenOptions, ctx: TemplateCodegenCon

if (!options.hasDefineSlots) {
yield `var __VLS_slots!: __VLS_OmitStringIndex<typeof __VLS_ctx.${name}> & `;
for (const { expVar, varName } of ctx.dynamicSlots) {
for (const { expVar, propsVar } of ctx.dynamicSlots) {
ctx.hasSlot = true;
yield `Partial<Record<NonNullable<typeof ${expVar}>, (_: typeof ${varName}) => any>> &${newLine}`;
yield `Partial<Record<NonNullable<typeof ${expVar}>, (props: typeof ${propsVar}) => any>> &${newLine}`;
}
yield `{${newLine}`;
for (const slot of ctx.slots) {
ctx.hasSlot = true;
if (slot.name && slot.loc !== undefined) {
if (slot.name && slot.offset !== undefined) {
yield* generateObjectProperty(
options,
ctx,
slot.name,
slot.loc,
slot.offset,
ctx.codeFeatures.withoutHighlightAndCompletion,
slot.nodeLoc
);
Expand All @@ -83,7 +83,7 @@ function* generateSlots(options: TemplateCodegenOptions, ctx: TemplateCodegenCon
`default`
);
}
yield `?(_: typeof ${slot.varName}): any,${newLine}`;
yield `?(props: typeof ${slot.propsVar}): any,${newLine}`;
}
yield `}${endOfLine}`;
}
Expand Down
26 changes: 12 additions & 14 deletions packages/language-core/lib/codegen/template/slotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function* generateSlotOutlet(
node: CompilerDOM.SlotOutletNode
): Generator<Code> {
const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag);
const varSlot = ctx.getInternalVariable();
const propsVar = ctx.getInternalVariable();
const nameProp = node.props.find(prop => {
if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
return prop.name === 'name';
Expand Down Expand Up @@ -43,7 +43,7 @@ export function* generateSlotOutlet(
? `'${nameProp.value.content}'`
: nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
? nameProp.exp.content
: `('default' as const)`
: `'default'`
),
`]`
);
Expand All @@ -59,7 +59,7 @@ export function* generateSlotOutlet(
yield `)${endOfLine}`;
}
else {
yield `var ${varSlot} = {${newLine}`;
yield `var ${propsVar} = {${newLine}`;
yield* generateElementProps(options, ctx, node, node.props.filter(prop => prop !== nameProp), options.vueCompilerOptions.strictTemplates, true);
yield `}${endOfLine}`;

Expand All @@ -69,10 +69,10 @@ export function* generateSlotOutlet(
) {
ctx.slots.push({
name: nameProp.value.content,
loc: nameProp.loc.start.offset + nameProp.loc.source.indexOf(nameProp.value.content, nameProp.name.length),
offset: 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,
propsVar,
});
}
else if (
Expand All @@ -83,31 +83,29 @@ export function* generateSlotOutlet(
if (isShortHand) {
ctx.inlayHints.push(createVBindShorthandInlayHintInfo(nameProp.exp.loc, 'name'));
}
const slotExpVar = ctx.getInternalVariable();
yield `var ${slotExpVar} = `;
const expVar = ctx.getInternalVariable();
yield `var ${expVar} = __VLS_tryAsConstant(`;
yield* generateInterpolation(
options,
ctx,
'template',
ctx.codeFeatures.all,
nameProp.exp.content,
nameProp.exp.loc.start.offset,
nameProp.exp,
'(',
')'
nameProp.exp
);
yield ` as const${endOfLine}`;
yield `)${endOfLine}`;
ctx.dynamicSlots.push({
expVar: slotExpVar,
varName: varSlot,
expVar,
propsVar,
});
}
else {
ctx.slots.push({
name: 'default',
tagRange: [startTagOffset, startTagOffset + node.tag.length],
varName: varSlot,
nodeLoc: node.loc,
propsVar,
});
}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare const _default: <Row extends BaseRow>(__VLS_props: NonNullable<Awaited<t
slots: __VLS_OmitStringIndex<Readonly<{
[name: string]: import("vue").Slot<any>;
}>> & {
default?(_: {
default?(props: {
row: Row;
}): any;
};
Expand Down Expand Up @@ -624,14 +624,14 @@ exports[`vue-tsc-dts > Input: template-slots/component.vue, Output: template-slo
slots: __VLS_OmitStringIndex<Readonly<{
[name: string]: import("vue").Slot<any>;
}>> & {
'no-bind'?(_: {}): any;
default?(_: {
'no-bind'?(props: {}): any;
default?(props: {
num: number;
}): any;
'named-slot'?(_: {
'named-slot'?(props: {
str: string;
}): any;
vbind?(_: {
vbind?(props: {
num: number;
str: string;
}): any;
Expand Down Expand Up @@ -728,14 +728,14 @@ exports[`vue-tsc-dts > Input: template-slots/component-no-script.vue, Output: te
slots: __VLS_OmitStringIndex<Readonly<{
[name: string]: import("vue").Slot<any>;
}>> & {
'no-bind'?(_: {}): any;
default?(_: {
'no-bind'?(props: {}): any;
default?(props: {
num: number;
}): any;
'named-slot'?(_: {
'named-slot'?(props: {
str: string;
}): any;
vbind?(_: {
vbind?(props: {
num: number;
str: string;
}): any;
Expand Down
Loading