Skip to content
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

fix(language-core): transform slot parameter list into equivalent binding pattern #5245

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,8 @@ export function generateGlobalTypes({
key: keyof T,
index: number,
][];
// @ts-ignore
function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>;
// @ts-ignore
function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0];
function __VLS_getSlotParameters<S, D extends S>(slot: S, decl?: D):
__VLS_PickNotAny<NonNullable<D>, (...args: any) => any> extends (...args: infer P) => any ? P : any[];
function __VLS_asFunctionalDirective<T>(dir: T): T extends import('${lib}').ObjectDirective
? NonNullable<T['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>
: T extends (...args: any) => any
Expand Down
89 changes: 68 additions & 21 deletions packages/language-core/lib/codegen/template/vSlot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as CompilerDOM from '@vue/compiler-dom';
import type * as ts from 'typescript';
import type { Code } from '../../types';
import { collectVars, createTsAst, endOfLine, newLine } from '../utils';
import { wrapWith } from '../utils/wrapWith';
Expand Down Expand Up @@ -41,31 +42,12 @@ export function* generateVSlot(
`default`
);
}
yield `: __VLS_thisSlot } = ${ctx.currentComponent.ctxVar}.slots!${endOfLine}`;
yield `: __VLS_slot } = ${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}`;
}
yield* generateSlotParameters(options, ctx, slotAst, slotDir.exp);
}

for (const varName of slotBlockVars) {
Expand Down Expand Up @@ -109,6 +91,71 @@ export function* generateVSlot(
yield `}${newLine}`;
}

function* generateSlotParameters(
options: TemplateCodegenOptions,
ctx: TemplateCodegenContext,
ast: ts.SourceFile,
exp: CompilerDOM.SimpleExpressionNode
): Generator<Code> {
const { ts } = options;

const statement = ast.statements[0];
if (!ts.isExpressionStatement(statement) || !ts.isArrowFunction(statement.expression)) {
return;
}

const { expression } = statement;
const startOffset = exp.loc.start.offset - 1;
const modifies: [Code[], number, number][] = [];
const types: (Code | null)[] = [];

for (const { name, type } of expression.parameters) {
if (type) {
modifies.push([
[``],
name.end,
type.end,
]);
types.push(chunk(type.getStart(ast), type.end));
}
else {
types.push(null);
}
}

yield `const [`;
let nextStart = 1;
for (const [codes, start, end] of modifies) {
yield chunk(nextStart, start);
yield* codes;
nextStart = end;
}
yield chunk(nextStart, expression.equalsGreaterThanToken.pos - 1);
yield `] = __VLS_getSlotParameters(__VLS_slot`;

if (types.some(t => t)) {
yield `, `;
yield* wrapWith(
exp.loc.start.offset,
exp.loc.end.offset,
ctx.codeFeatures.verification,
`(`,
...types.flatMap(type => type ? [`_: `, type, `, `] : `_, `),
`) => [] as any`
);
}
yield `)${endOfLine}`;

function chunk(start: number, end: number): Code {
return [
ast.text.slice(start, end),
'template',
startOffset + start,
ctx.codeFeatures.all,
];
}
}

export function* generateImplicitDefaultSlot(
ctx: TemplateCodegenContext,
node: CompilerDOM.ElementNode
Expand Down