forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(language-core): do not generate element for
<template>
with `v-…
…slot` (vuejs#5077)
- Loading branch information
Showing
6 changed files
with
132 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import * as CompilerDOM from '@vue/compiler-dom'; | ||
import type { Code } from '../../types'; | ||
import { collectVars, createTsAst, endOfLine, newLine, wrapWith } from '../utils'; | ||
import type { TemplateCodegenContext } from './context'; | ||
import type { TemplateCodegenOptions } from './index'; | ||
import { generateObjectProperty } from './objectProperty'; | ||
import { generateTemplateChild } from './templateChild'; | ||
|
||
export function* generateVSlot( | ||
options: TemplateCodegenOptions, | ||
ctx: TemplateCodegenContext, | ||
node: CompilerDOM.ElementNode, | ||
slotDir: CompilerDOM.DirectiveNode | ||
): Generator<Code> { | ||
if (!ctx.currentComponent) { | ||
return; | ||
} | ||
ctx.currentComponent.used = true; | ||
const slotBlockVars: string[] = []; | ||
yield `{${newLine}`; | ||
|
||
yield `const { `; | ||
if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && slotDir.arg.content) { | ||
yield* generateObjectProperty( | ||
options, | ||
ctx, | ||
slotDir.arg.loc.source, | ||
slotDir.arg.loc.start.offset, | ||
slotDir.arg.isStatic ? ctx.codeFeatures.withoutHighlight : ctx.codeFeatures.all, | ||
slotDir.arg.loc, | ||
false, | ||
true | ||
); | ||
} | ||
else { | ||
yield* wrapWith( | ||
slotDir.loc.start.offset, | ||
slotDir.loc.start.offset + (slotDir.rawName?.length ?? 0), | ||
ctx.codeFeatures.withoutHighlightAndCompletion, | ||
`default` | ||
); | ||
} | ||
yield `: __VLS_thisSlot } = ${ctx.currentComponent.ctxVar}.slots!${endOfLine}`; | ||
|
||
if (slotDir.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) { | ||
const slotAst = createTsAst(options.ts, slotDir, `(${slotDir.exp.content}) => {}`); | ||
collectVars(options.ts, slotAst, slotAst, slotBlockVars); | ||
if (!slotDir.exp.content.includes(':')) { | ||
yield `const [`; | ||
yield [ | ||
slotDir.exp.content, | ||
'template', | ||
slotDir.exp.loc.start.offset, | ||
ctx.codeFeatures.all, | ||
]; | ||
yield `] = __VLS_getSlotParams(__VLS_thisSlot)${endOfLine}`; | ||
} | ||
else { | ||
yield `const `; | ||
yield [ | ||
slotDir.exp.content, | ||
'template', | ||
slotDir.exp.loc.start.offset, | ||
ctx.codeFeatures.all, | ||
]; | ||
yield ` = __VLS_getSlotParam(__VLS_thisSlot)${endOfLine}`; | ||
} | ||
} | ||
|
||
for (const varName of slotBlockVars) { | ||
ctx.addLocalVariable(varName); | ||
} | ||
|
||
yield* ctx.resetDirectiveComments('end of slot children start'); | ||
|
||
let prev: CompilerDOM.TemplateChildNode | undefined; | ||
for (const childNode of node.children) { | ||
yield* generateTemplateChild(options, ctx, childNode, prev); | ||
prev = childNode; | ||
} | ||
|
||
for (const varName of slotBlockVars) { | ||
ctx.removeLocalVariable(varName); | ||
} | ||
|
||
let isStatic = true; | ||
if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) { | ||
isStatic = slotDir.arg.isStatic; | ||
} | ||
if (isStatic && !slotDir.arg) { | ||
yield `${ctx.currentComponent.ctxVar}.slots!['`; | ||
yield [ | ||
'', | ||
'template', | ||
slotDir.loc.start.offset + ( | ||
slotDir.loc.source.startsWith('#') | ||
? '#'.length | ||
: slotDir.loc.source.startsWith('v-slot:') | ||
? 'v-slot:'.length | ||
: 0 | ||
), | ||
ctx.codeFeatures.completion, | ||
]; | ||
yield `'/* empty slot name completion */]${endOfLine}`; | ||
} | ||
|
||
yield* ctx.generateAutoImportCompletion(); | ||
yield `}${newLine}`; | ||
} |