Skip to content

Commit

Permalink
fix: slot binding type annotation not working if parent component mis…
Browse files Browse the repository at this point in the history
…sing

close #1425
  • Loading branch information
johnsoncodehk committed Jun 12, 2022
1 parent 420d1f2 commit 2180f29
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/vue-code-gen/src/generators/template.ts
Expand Up @@ -542,14 +542,12 @@ export function generate(
let slotBlockVars: string[] | undefined;

writeInlineCss(node);
if (parentEl) {

slotBlockVars = [];
writeImportSlots(node, parentEl, slotBlockVars);
slotBlockVars = [];
writeImportSlots(node, parentEl, slotBlockVars);

for (const varName of slotBlockVars)
localVars[varName] = (localVars[varName] ?? 0) + 1;
}
for (const varName of slotBlockVars)
localVars[varName] = (localVars[varName] ?? 0) + 1;

const vScope = node.props.find(prop => prop.type === CompilerDOM.NodeTypes.DIRECTIVE && prop.name === 'scope');
let inScope = false;
Expand Down Expand Up @@ -1265,13 +1263,9 @@ export function generate(
}
}
}
function writeImportSlots(node: CompilerDOM.ElementNode, parentEl: CompilerDOM.ElementNode, slotBlockVars: string[]) {

const tag = tagResolves[parentEl.tag];

if (!tag)
return;
function writeImportSlots(node: CompilerDOM.ElementNode, parentEl: CompilerDOM.ElementNode | undefined, slotBlockVars: string[]) {

const tag = parentEl ? tagResolves[parentEl.tag] : undefined;

for (const prop of node.props) {
if (
Expand All @@ -1282,10 +1276,12 @@ export function generate(
const varComponentInstance = `__VLS_${elementIndex++}`;
const varSlots = `__VLS_${elementIndex++}`;

tsCodeGen.addText(`const ${varComponentInstance} = new ${tag.component}({ `);
writeProps(parentEl, false, 'slots');
tsCodeGen.addText(`});\n`);
tsCodeGen.addText(`declare const ${varSlots}: __VLS_types.ExtractComponentSlots<typeof ${varComponentInstance}>;\n`);
if (tag && parentEl) {
tsCodeGen.addText(`const ${varComponentInstance} = new ${tag.component}({ `);
writeProps(parentEl, false, 'slots');
tsCodeGen.addText(`});\n`);
tsCodeGen.addText(`declare const ${varSlots}: __VLS_types.ExtractComponentSlots<typeof ${varComponentInstance}>;\n`);
}

if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
tsCodeGen.addText(`const `);
Expand Down Expand Up @@ -1313,6 +1309,13 @@ export function generate(

tsCodeGen.addText(` = `);
}

if (!tag || !parentEl) {
// fix https://github.com/johnsoncodehk/volar/issues/1425
tsCodeGen.addText(`{} as any;\n`);
continue;
}

let slotName = 'default';
let isStatic = true;
if (prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.content !== '') {
Expand Down

0 comments on commit 2180f29

Please sign in to comment.