Skip to content

Commit a27caa1

Browse files
committed
feat(language-core): use internal options for directly exposing user emit types
close #3893
1 parent 68edc1d commit a27caa1

File tree

7 files changed

+86
-7
lines changed

7 files changed

+86
-7
lines changed

packages/component-meta/lib/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ function createSchemaResolvers(
565565

566566
return {
567567
name: (typeChecker.getTypeOfSymbolAtLocation(call.parameters[0], symbolNode) as ts.StringLiteralType).value,
568+
description: ts.displayPartsToString(call.getDocumentationComment(typeChecker)),
568569
type: typeChecker.typeToString(subtype),
569570
rawType: rawType ? subtype : undefined,
570571
signature: typeChecker.signatureToString(call),

packages/component-meta/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface PropertyMeta {
3636

3737
export interface EventMeta {
3838
name: string;
39+
description: string;
3940
type: string;
4041
rawType?: ts.Type;
4142
signature: string;

packages/component-meta/tests/index.spec.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
2626
// expect(meta.type).toEqual(TypeMeta.Class);
2727

2828
const foo = meta.props.find(prop => prop.name === 'foo');
29-
const onUpdateFoo = meta.events.find(event => event.name === 'update:foo')
29+
const onUpdateFoo = meta.events.find(event => event.name === 'update:foo');
3030

3131
const bar = meta.props.find(prop => prop.name === 'bar');
32-
const onUpdateBar = meta.events.find(event => event.name === 'update:bar')
32+
const onUpdateBar = meta.events.find(event => event.name === 'update:bar');
3333

3434
const qux = meta.props.find(prop => prop.name === 'qux');
3535
const quxModifiers = meta.props.find(prop => prop.name === 'quxModifiers');
36-
const onUpdateQux = meta.events.find(event => event.name === 'update:qux')
36+
const onUpdateQux = meta.events.find(event => event.name === 'update:qux');
3737

3838
expect(foo).toBeDefined();
3939
expect(bar).toBeDefined();
@@ -42,7 +42,7 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
4242
expect(onUpdateFoo).toBeDefined();
4343
expect(onUpdateBar).toBeDefined();
4444
expect(onUpdateQux).toBeDefined();
45-
})
45+
});
4646

4747
test('reference-type-props', () => {
4848
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/reference-type-props/component.vue');
@@ -724,6 +724,23 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => describ
724724
expect(b).toBeDefined();
725725
});
726726

727+
test('emits-generic', () => {
728+
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/events/component-generic.vue');
729+
const meta = checker.getComponentMeta(componentPath);
730+
const foo = meta.events.find(event =>event.name === 'foo');
731+
732+
expect(foo?.description).toBe('Emitted when foo...');
733+
});
734+
735+
// Wait for https://github.com/vuejs/core/pull/10801
736+
test.skip('emits-class', () => {
737+
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/events/component-class.vue');
738+
const meta = checker.getComponentMeta(componentPath);
739+
const foo = meta.events.find(event =>event.name === 'foo');
740+
741+
expect(foo?.description).toBe('Emitted when foo...');
742+
});
743+
727744
test('ts-named-exports', () => {
728745
const componentPath = path.resolve(__dirname, '../../../test-workspace/component-meta/ts-named-export/component.ts');
729746
const exportNames = checker.getExportNames(componentPath);

packages/language-core/lib/codegen/script/component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,7 @@ export function* generateScriptSetupOptions(
117117
if (scriptSetupRanges.props.define?.typeArg) {
118118
yield `__typeProps: typeof __VLS_typeProps,${newLine}`;
119119
}
120+
if (scriptSetupRanges.emits.define) {
121+
yield `__typeEmits: typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'},${newLine}`;
122+
}
120123
}

packages/tsc/tests/__snapshots__/dts.spec.ts.snap

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,51 @@ export default _default;
2828
"
2929
`;
3030

31+
exports[`vue-tsc-dts > Input: events/component-class.vue, Output: events/component-class.vue.d.ts 1`] = `
32+
"declare const _default: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
33+
foo: (value: string) => void;
34+
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>> & {
35+
onFoo?: (value: string) => any;
36+
}, {}, {}>;
37+
export default _default;
38+
"
39+
`;
40+
41+
exports[`vue-tsc-dts > Input: events/component-generic.vue, Output: events/component-generic.vue.d.ts 1`] = `
42+
"declare const _default: <T>(__VLS_props: {
43+
onFoo?: (value: string) => any;
44+
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, __VLS_ctx?: {
45+
slots: {};
46+
attrs: any;
47+
emit: (evt: "foo", value: string) => void;
48+
}, __VLS_expose?: (exposed: import('vue').ShallowUnwrapRef<{}>) => void, __VLS_setup?: Promise<{
49+
props: {
50+
onFoo?: (value: string) => any;
51+
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps;
52+
expose(exposed: import('vue').ShallowUnwrapRef<{}>): void;
53+
attrs: any;
54+
slots: {};
55+
emit: (evt: "foo", value: string) => void;
56+
}>) => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
57+
[key: string]: any;
58+
}> & {
59+
__ctx?: {
60+
props: {
61+
onFoo?: (value: string) => any;
62+
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps;
63+
expose(exposed: import('vue').ShallowUnwrapRef<{}>): void;
64+
attrs: any;
65+
slots: {};
66+
emit: (evt: "foo", value: string) => void;
67+
};
68+
};
69+
export default _default;
70+
type __VLS_Prettify<T> = {
71+
[K in keyof T]: T[K];
72+
} & {};
73+
"
74+
`;
75+
3176
exports[`vue-tsc-dts > Input: generic/component.vue, Output: generic/component.vue.d.ts 1`] = `
3277
"declare const _default: <T>(__VLS_props: {
3378
onBar?: (data: number) => any;
@@ -268,13 +313,13 @@ exports[`vue-tsc-dts > Input: reference-type-events/component.vue, Output: refer
268313
}) => void;
269314
baz: () => void;
270315
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>> & {
316+
onFoo?: (data?: {
317+
foo: string;
318+
}) => any;
271319
onBar?: (value: {
272320
arg1: number;
273321
arg2?: any;
274322
}) => any;
275-
onFoo?: (data?: {
276-
foo: string;
277-
}) => any;
278323
onBaz?: () => any;
279324
}, {}, {}>;
280325
export default _default;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script setup lang="ts">
2+
const emit = defineEmits<{
3+
/** Emitted when foo... */
4+
(evt: "foo", value: string): void
5+
}>();
6+
</script>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script setup lang="ts" generic="T">
2+
const emit = defineEmits<{
3+
/** Emitted when foo... */
4+
(evt: "foo", value: string): void
5+
}>();
6+
</script>

0 commit comments

Comments
 (0)