Skip to content

Commit

Permalink
fix: JSDoc comments not emitted when using type-only defineProps macro
Browse files Browse the repository at this point in the history
close #3645
  • Loading branch information
johnsoncodehk committed Oct 12, 2023
1 parent 2e17f3c commit 177055f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 57 deletions.
58 changes: 41 additions & 17 deletions packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,18 +617,25 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
}
function generateComponentOptions(functional: boolean) {
if (scriptSetupRanges && !bypassDefineComponent) {
if (scriptSetupRanges.props.define?.arg || scriptSetupRanges.props.define?.typeArg || (!functional && scriptSetupRanges.defineProp.length)) {
codes.push(`props: {\n`);
if (scriptSetupRanges.props.define?.arg) {
codes.push('...');
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end);
codes.push(',\n');
}
if (scriptSetupRanges.props.define?.typeArg) {

const ranges = scriptSetupRanges;
const propsCodegens: (() => void)[] = [];

if (ranges.props.define?.arg) {
const arg = ranges.props.define.arg;
propsCodegens.push(() => {
addExtraReferenceVirtualCode('scriptSetup', arg.start, arg.end);
});
}
if (ranges.props.define?.typeArg) {
const typeArg = ranges.props.define.typeArg;
propsCodegens.push(() => {

usedHelperTypes.DefinePropsToOptions = true;
codes.push('...{} as ');

if (scriptSetupRanges.props.withDefaults?.arg) {
codes.push(`{} as `);

if (ranges.props.withDefaults?.arg) {
usedHelperTypes.MergePropDefaults = true;
codes.push(`__VLS_WithDefaults<`);
}
Expand All @@ -638,25 +645,42 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
codes.push(`typeof __VLS_fnPropsTypeOnly`);
}
else {
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.props.define.typeArg.start, scriptSetupRanges.props.define.typeArg.end);
addExtraReferenceVirtualCode('scriptSetup', typeArg.start, typeArg.end);
}
codes.push(`>`);

if (scriptSetupRanges.props.withDefaults?.arg) {
if (ranges.props.withDefaults?.arg) {
codes.push(`, typeof __VLS_withDefaultsArg`);
codes.push(`>`);
}
codes.push(',\n');
});
}
if (!functional && ranges.defineProp.length) {
propsCodegens.push(() => {
codes.push(`__VLS_propsOption_defineProp`);
});
}

if (propsCodegens.length === 1) {
codes.push(`props: `);
for (const generate of propsCodegens) {
generate();
}
if (!functional && scriptSetupRanges.defineProp.length) {
codes.push(`...__VLS_propsOption_defineProp,\n`);
codes.push(`,\n`);
}
else if (propsCodegens.length >= 2) {
codes.push(`props: {\n`);
for (const generate of propsCodegens) {
codes.push('...');
generate();
codes.push(',\n');
}
codes.push(`},\n`);
}
if (scriptSetupRanges.emits.define) {
if (ranges.emits.define) {
codes.push(
`emits: ({} as __VLS_NormalizeEmits<typeof `,
scriptSetupRanges.emits.name ?? '__VLS_emit',
ranges.emits.name ?? '__VLS_emit',
`>),\n`,
);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/vue-test-workspace/vue-tsc-dts/#3645/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script setup lang="ts">
defineProps<{
/**
* This is an example prop
*/
foo?: string;
}>();
</script>
106 changes: 66 additions & 40 deletions packages/vue-tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ export default _default;
"
`;

exports[`vue-tsc-dts > Input: #3645/main.vue, Output: #3645/main.vue.d.ts 1`] = `
"declare const _default: import(\\"vue\\").DefineComponent<__VLS_TypePropsToRuntimeProps<{
/**
* This is an example prop
*/
foo?: string;
}>, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<__VLS_TypePropsToRuntimeProps<{
/**
* This is an example prop
*/
foo?: string;
}>>>, {}, {}>;
export default _default;
type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
type __VLS_TypePropsToRuntimeProps<T> = {
[K in keyof T]-?: {} extends Pick<T, K> ? {
type: import('vue').PropType<__VLS_NonUndefinedable<T[K]>>;
} : {
type: import('vue').PropType<T[K]>;
required: true;
};
};
"
`;
exports[`vue-tsc-dts > Input: components/script-setup.vue, Output: components/script-setup.vue.d.ts 1`] = `
"declare const _default: import(\\"vue\\").DefineComponent<{
foo: StringConstructor;
Expand All @@ -49,29 +74,31 @@ exports[`vue-tsc-dts > Input: components/script-setup-default-props.vue, Output:
msg?: string;
labels?: string[];
}
declare const _default: import(\\"vue\\").DefineComponent<{
msg: {
type: import(\\"vue\\").PropType<string>;
default: string;
};
labels: {
type: import(\\"vue\\").PropType<string[]>;
default: () => string[];
};
}, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<{
msg: {
type: import(\\"vue\\").PropType<string>;
default: string;
};
labels: {
type: import(\\"vue\\").PropType<string[]>;
default: () => string[];
};
}>>, {
declare const _default: import(\\"vue\\").DefineComponent<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, {
msg: string;
labels: () => string[];
}>, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, {
msg: string;
labels: () => string[];
}>>>, {
msg: string;
labels: string[];
}, {}>;
export default _default;
type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
type __VLS_TypePropsToRuntimeProps<T> = {
[K in keyof T]-?: {} extends Pick<T, K> ? {
type: import('vue').PropType<__VLS_NonUndefinedable<T[K]>>;
} : {
type: import('vue').PropType<T[K]>;
required: true;
};
};
type __VLS_WithDefaults<P, D> = {
[K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & {
default: D[K];
}> : P[K];
};
"
`;
Expand All @@ -86,8 +113,8 @@ export default _default;
exports[`vue-tsc-dts > Input: components/script-setup-generic.vue, Output: components/script-setup-generic.vue.d.ts 1`] = `
"declare const _default: <T>(__VLS_props: {
onBar?: (data: T) => any;
foo: T;
onBar?: (data: T) => any;
} & import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, __VLS_ctx?: {
slots: Readonly<{
default?(data: T): any;
Expand All @@ -98,8 +125,8 @@ exports[`vue-tsc-dts > Input: components/script-setup-generic.vue, Output: compo
baz: T;
}>) => void, __VLS_setup?: Promise<{
props: {
onBar?: (data: T) => any;
foo: T;
onBar?: (data: T) => any;
} & import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps;
expose(exposed: import(\\"vue\\").ShallowUnwrapRef<{
baz: T;
Expand All @@ -114,8 +141,8 @@ exports[`vue-tsc-dts > Input: components/script-setup-generic.vue, Output: compo
}> & {
__ctx?: {
props: {
onBar?: (data: T) => any;
foo: T;
onBar?: (data: T) => any;
} & import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps;
expose(exposed: import(\\"vue\\").ShallowUnwrapRef<{
baz: T;
Expand All @@ -132,30 +159,29 @@ export default _default;
`;
exports[`vue-tsc-dts > Input: components/script-setup-type-only.vue, Output: components/script-setup-type-only.vue.d.ts 1`] = `
"declare const _default: import(\\"vue\\").DefineComponent<{
foo: {
type: import(\\"vue\\").PropType<string>;
required: true;
};
bar: {
type: import(\\"vue\\").PropType<number>;
};
}, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {
"declare const _default: import(\\"vue\\").DefineComponent<__VLS_TypePropsToRuntimeProps<{
foo: string;
bar?: number;
}>, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {
change: (id: number) => void;
update: (value: string) => void;
}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<{
foo: {
type: import(\\"vue\\").PropType<string>;
required: true;
};
bar: {
type: import(\\"vue\\").PropType<number>;
};
}>> & {
}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<__VLS_TypePropsToRuntimeProps<{
foo: string;
bar?: number;
}>>> & {
onChange?: (id: number) => any;
onUpdate?: (value: string) => any;
}, {}, {}>;
export default _default;
type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
type __VLS_TypePropsToRuntimeProps<T> = {
[K in keyof T]-?: {} extends Pick<T, K> ? {
type: import('vue').PropType<__VLS_NonUndefinedable<T[K]>>;
} : {
type: import('vue').PropType<T[K]>;
required: true;
};
};
"
`;
Expand Down

0 comments on commit 177055f

Please sign in to comment.