Skip to content

Commit e36b3f0

Browse files
committed
fix(language-core): handle edge case of default slot name mismatch
1 parent adfa3f8 commit e36b3f0

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

packages/language-core/lib/codegen/globalTypes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ export function generateGlobalTypes({
172172
: T extends () => any ? (props: {}, ctx?: any) => ReturnType<T>
173173
: T extends (...args: any) => any ? T
174174
: (_: {}${checkUnknownProps ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${checkUnknownProps ? '' : ' & Record<string, unknown>'} } };
175-
function __VLS_asFunctionalElement<T>(tag: T, endTag?: T): (_: T${checkUnknownComponents ? '' : ' & Record<string, unknown>'}) => void;
176175
function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
177-
function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
176+
function __VLS_asFunctionalElement<T>(tag: T, endTag?: T): (attrs: T${checkUnknownComponents ? '' : ' & Record<string, unknown>'}) => void;
177+
function __VLS_asFunctionalSlot<S>(slot: S): (props: NonNullable<S> extends (props: infer P) => any ? P : {}) => void;
178178
function __VLS_tryAsConstant<const T>(t: T): T;
179179
}
180180
`;

packages/language-core/lib/codegen/template/slotOutlet.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function* generateSlotOutlet(
1515
node: CompilerDOM.SlotOutletNode
1616
): Generator<Code> {
1717
const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag);
18+
const startTagEndOffset = startTagOffset + node.tag.length;
1819
const propsVar = ctx.getInternalVariable();
1920
const nameProp = node.props.find(prop => {
2021
if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
@@ -30,7 +31,7 @@ export function* generateSlotOutlet(
3031
});
3132

3233
if (options.hasDefineSlots) {
33-
yield `__VLS_normalizeSlot(`;
34+
yield `__VLS_asFunctionalSlot(`;
3435
if (nameProp) {
3536
let codes: Generator<Code> | Code[];
3637
if (nameProp.type === CompilerDOM.NodeTypes.ATTRIBUTE && nameProp.value) {
@@ -78,16 +79,23 @@ export function* generateSlotOutlet(
7879
}
7980
else {
8081
yield* wrapWith(
81-
node.loc.start.offset,
82-
node.loc.end.offset,
82+
startTagOffset,
83+
startTagEndOffset,
8384
ctx.codeFeatures.verification,
84-
`${options.slotsAssignName ?? '__VLS_slots'}['default']`
85+
`${options.slotsAssignName ?? '__VLS_slots'}[`,
86+
...wrapWith(
87+
startTagOffset,
88+
startTagEndOffset,
89+
ctx.codeFeatures.verification,
90+
`'default'`
91+
),
92+
`]`
8593
);
8694
}
87-
yield `)?.(`;
95+
yield `)(`;
8896
yield* wrapWith(
8997
startTagOffset,
90-
startTagOffset + node.tag.length,
98+
startTagEndOffset,
9199
ctx.codeFeatures.verification,
92100
`{${newLine}`,
93101
...generateElementProps(
@@ -154,7 +162,7 @@ export function* generateSlotOutlet(
154162
else {
155163
ctx.slots.push({
156164
name: 'default',
157-
tagRange: [startTagOffset, startTagOffset + node.tag.length],
165+
tagRange: [startTagOffset, startTagEndOffset],
158166
nodeLoc: node.loc,
159167
propsVar,
160168
});

0 commit comments

Comments
 (0)