Skip to content

Commit 03a6c94

Browse files
yusufkandemirjohnsoncodehk
andauthoredOct 1, 2021
fix: Pick up the correct slot scope type from $slots (#540)
* fix: Pick up the correct slot scope type from `$slots` See: #539 (comment) * fix: fixed slots type edge cases Co-authored-by: johnsoncodehk <[email protected]>
1 parent d5c222d commit 03a6c94

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed
 

‎packages/vscode-vue-languageservice/src/generators/template.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export function generate(
7575
}>,
7676
}> = {};
7777
const tagResolves: Record<string, {
78-
name: string,
7978
rawComponent: string,
8079
baseProps: string,
8180
emit: string,
81+
slots: string,
8282
events: Record<string, string>,
8383
offsets: number[],
8484
}> = {};
@@ -94,13 +94,18 @@ export function generate(
9494
const var_rawComponent = `__VLS_${elementIndex++}`;
9595
const var_baseProps = `__VLS_${elementIndex++}`;
9696
const var_emit = `__VLS_${elementIndex++}`;
97+
const var_slots = `__VLS_${elementIndex++}`;
9798
const var_events: Record<string, string> = {};
9899

99100
tsCodeGen.addText(`declare const ${var_correctTagName}: __VLS_GetComponentName<typeof __VLS_rawComponents, '${tag}'>;\n`);
100101
tsCodeGen.addText(`declare const ${var_wrapComponent}: __VLS_GetProperty<typeof __VLS_wrapComponents, typeof ${var_correctTagName}, any>;\n`);
101102
tsCodeGen.addText(`declare const ${var_rawComponent}: __VLS_GetProperty<typeof __VLS_rawComponents, typeof ${var_correctTagName}, any>;\n`);
102103
tsCodeGen.addText(`declare const ${var_baseProps}: __VLS_ExtractComponentProps<typeof ${var_rawComponent}>;\n`);
103104
tsCodeGen.addText(`declare const ${var_emit}: __VLS_ExtractEmit2<typeof ${var_rawComponent}>;\n`);
105+
tsCodeGen.addText(`declare const ${var_slots}:
106+
__VLS_TemplateSlots<typeof ${var_wrapComponent}>
107+
& __VLS_ScriptSlots<typeof ${var_rawComponent}>
108+
& __VLS_DefaultSlots<typeof ${var_wrapComponent}, typeof ${var_rawComponent}>;\n`);
104109

105110
const resolvedTag = tags[tag];
106111
const tagRanges = resolvedTag.offsets.map(offset => ({ start: offset, end: offset + tag.length }));
@@ -212,10 +217,10 @@ export function generate(
212217
}
213218

214219
tagResolves[tag] = {
215-
name: var_correctTagName,
216220
rawComponent: var_rawComponent,
217221
baseProps: var_baseProps,
218222
emit: var_emit,
223+
slots: var_slots,
219224
events: var_events,
220225
offsets: tags[tag].offsets.map(offset => htmlToTemplate(offset, offset)).filter(shared.notEmpty),
221226
};
@@ -1123,7 +1128,7 @@ export function generate(
11231128
slotName = prop.arg.content;
11241129
}
11251130
const diagStart = tsCodeGen.getText().length;
1126-
tsCodeGen.addText(`({ ...__VLS_getTemplateSlots(__VLS_wrapComponents[${tagResolves[parentEl.tag].name}]), ...__VLS_getScriptSlots(__VLS_rawComponents[${tagResolves[parentEl.tag].name}])})`);
1131+
tsCodeGen.addText(tagResolves[parentEl.tag].slots);
11271132
const argRange = prop.arg
11281133
? {
11291134
start: prop.arg.loc.start.offset,

‎packages/vscode-vue-languageservice/src/utils/globalDoc.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ declare global {
7979
function __VLS_directiveFunction<T>(dir: T): T extends ObjectDirective<infer E, infer V> ? V extends { value: infer V_2 } ? (value: V_2) => void : (value: V) => void
8080
: T extends FunctionDirective<infer E, infer V> ? V extends { value: infer V_2 } ? (value: V_2) => void : (value: V) => void : T;
8181
82-
function __VLS_getTemplateSlots<T>(t: T): T extends { __VLS_slots: infer S } ? S : {};
83-
function __VLS_getScriptSlots<T>(t: T): T extends new (...args: any) => { $slots?: infer S } ? (S extends object ? S : {}) : {};
82+
type __VLS_TemplateSlots<T> = T extends { __VLS_slots: infer S } ? S : {};
83+
type __VLS_ScriptSlots<T> = T extends new (...args: any) => { $slots?: infer S }
84+
? { [K in keyof S]: S[K] extends (obj: infer O) => any ? O : S[K] }
85+
: {};
86+
type __VLS_DefaultSlots<W, R> = W extends { __VLS_slots: infer _ }
87+
? {} : R extends new (...args: any) => { $slots?: infer _ }
88+
? {} : Record<string, any>;
8489
8590
type __VLS_GetComponentName<T, K extends string> = K extends keyof T ? IsAny<T[K]> extends false ? K : __VLS_GetComponentName_CamelCase<T, CamelCase<K>> : __VLS_GetComponentName_CamelCase<T, CamelCase<K>>;
8691
type __VLS_GetComponentName_CamelCase<T, K extends string> = K extends keyof T ? IsAny<T[K]> extends false ? K : __VLS_GetComponentName_CapitalCase<T, Capitalize<K>> : __VLS_GetComponentName_CapitalCase<T, Capitalize<K>>;

0 commit comments

Comments
 (0)
Please sign in to comment.