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

feat(language-core): support defineSlots destructuring #4312

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,17 @@ function* generateSetupFunction(
}
}
}
if (scriptSetupRanges.slots.define && !scriptSetupRanges.slots.name) {
setupCodeModifies.push([[`const __VLS_slots = `], scriptSetupRanges.slots.define.start, scriptSetupRanges.slots.define.start]);
if (scriptSetupRanges.slots.define) {
if (!scriptSetupRanges.slots.name) {
setupCodeModifies.push([[`const __VLS_slots = `], scriptSetupRanges.slots.define.start, scriptSetupRanges.slots.define.start]);
}
else if (scriptSetupRanges.slots.isObjectBindingPattern) {
setupCodeModifies.push([
[`__VLS_slots;\nconst __VLS_slots = `],
scriptSetupRanges.slots.define.start,
scriptSetupRanges.slots.define.start,
]);
}
}
if (scriptSetupRanges.emits.define && !scriptSetupRanges.emits.name) {
setupCodeModifies.push([[`const __VLS_emit = `], scriptSetupRanges.emits.define.start, scriptSetupRanges.emits.define.start]);
Expand Down
8 changes: 7 additions & 1 deletion packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function parseScriptSetupRanges(
} = {};
const slots: {
name?: string;
isObjectBindingPattern?: boolean;
define?: ReturnType<typeof parseDefineFunction>;
} = {};
const emits: {
Expand Down Expand Up @@ -185,7 +186,12 @@ export function parseScriptSetupRanges(
else if (vueCompilerOptions.macros.defineSlots.includes(callText)) {
slots.define = parseDefineFunction(node);
if (ts.isVariableDeclaration(parent)) {
slots.name = getNodeText(ts, parent.name, ast);
if (ts.isIdentifier(parent.name)) {
slots.name = getNodeText(ts, parent.name, ast);
}
else {
slots.isObjectBindingPattern = ts.isObjectBindingPattern(parent.name);
}
}
}
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
Expand Down
19 changes: 19 additions & 0 deletions packages/tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,25 @@ type __VLS_WithTemplateSlots<T, S> = T & {
"
`;

exports[`vue-tsc-dts > Input: template-slots/component-destructuring.vue, Output: template-slots/component-destructuring.vue.d.ts 1`] = `
"declare const _default: __VLS_WithTemplateSlots<import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>, Readonly<{
bottom: (props: {
num: number;
}) => any[];
}> & {
bottom: (props: {
num: number;
}) => any[];
}>;
export default _default;
type __VLS_WithTemplateSlots<T, S> = T & {
new (): {
$slots: S;
};
};
"
`;

exports[`vue-tsc-dts > Input: template-slots/component-no-script.vue, Output: template-slots/component-no-script.vue.d.ts 1`] = `
"declare const _default: __VLS_WithTemplateSlots<import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>, {
"no-bind"?(_: {}): any;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<component-destructure #bottom="{ num }">
{{ num }}
</component-destructure>

<bottom :num="1" />
</template>

<script setup lang="ts">
const { bottom } = defineSlots<{
bottom: (props: { num: number }) => any[],
}>()
</script>