diff --git a/.changeset/popular-rivers-itch.md b/.changeset/popular-rivers-itch.md new file mode 100644 index 000000000..b23ab51f6 --- /dev/null +++ b/.changeset/popular-rivers-itch.md @@ -0,0 +1,5 @@ +--- +'@vue-macros/api': patch +--- + +support short-emits syntax diff --git a/packages/api/src/vue/emits.ts b/packages/api/src/vue/emits.ts index 6e13a4622..fbeb3cb16 100644 --- a/packages/api/src/vue/emits.ts +++ b/packages/api/src/vue/emits.ts @@ -26,6 +26,8 @@ import type { TSFunctionType, TSInterfaceDeclaration, TSIntersectionType, + TSMappedType, + TSPropertySignature, TSType, TSTypeLiteral, UnaryExpression, @@ -179,6 +181,13 @@ export async function handleTSEmitsDefinition({ } } + for (const evt of Object.keys(properties.properties)) { + if (!definitions[evt]) definitions[evt] = [] + definitions[evt].push( + buildDefinition(properties.properties[evt].signature), + ) + } + return { definitions, definitionsAst: buildDefinition({ scope, type: definitionsAst }), @@ -212,7 +221,12 @@ export interface TSEmits extends EmitsBase { definitions: Record< string, - ASTDefinition[] + ASTDefinition< + | TSCallSignatureDeclaration + | TSFunctionType + | TSPropertySignature + | TSMappedType + >[] > definitionsAst: ASTDefinition< TSTypeLiteral | TSIntersectionType | TSInterfaceDeclaration | TSFunctionType diff --git a/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap b/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap index 00ff60e35..9e8eb8de2 100644 --- a/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap +++ b/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap @@ -796,6 +796,52 @@ export { resolveFailed as default }; " `; +exports[`fixtures > tests/fixtures/short-emits.vue > isProduction = false 1`] = ` +"import { openBlock, createElementBlock, defineComponent } from 'vue'; +import _export_sfc from '[NULL]/plugin-vue/export-helper'; + +var _sfc_main = /* @__PURE__ */ defineComponent({ + __name: "short-emits", + emits: ["click"], + setup(__props, { emit: __emit }) { + const emit = __emit; + return (_ctx, _cache) => { + return openBlock(), createElementBlock("button", { + onClick: _cache[0] || (_cache[0] = ($event) => emit("click")) + }, "click"); + }; + } +}); + +var shortEmits = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]); + +export { shortEmits as default }; +" +`; + +exports[`fixtures > tests/fixtures/short-emits.vue > isProduction = true 1`] = ` +"import { openBlock, createElementBlock, defineComponent } from 'vue'; +import _export_sfc from '[NULL]/plugin-vue/export-helper'; + +var _sfc_main = /* @__PURE__ */ defineComponent({ + __name: "short-emits", + emits: ["click"], + setup(__props, { emit: __emit }) { + const emit = __emit; + return (_ctx, _cache) => { + return openBlock(), createElementBlock("button", { + onClick: _cache[0] || (_cache[0] = ($event) => emit("click")) + }, "click"); + }; + } +}); + +var shortEmits = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]); + +export { shortEmits as default }; +" +`; + exports[`fixtures > tests/fixtures/special-key.vue > isProduction = false 1`] = ` "import { defineComponent } from 'vue'; import _export_sfc from '[NULL]/plugin-vue/export-helper'; diff --git a/packages/better-define/tests/fixtures/short-emits.vue b/packages/better-define/tests/fixtures/short-emits.vue new file mode 100644 index 000000000..258c21c76 --- /dev/null +++ b/packages/better-define/tests/fixtures/short-emits.vue @@ -0,0 +1,10 @@ + + + diff --git a/playground/vue3/src/examples/better-define/index.vue b/playground/vue3/src/examples/better-define/index.vue index 6d7daafe3..6f6e3a110 100644 --- a/playground/vue3/src/examples/better-define/index.vue +++ b/playground/vue3/src/examples/better-define/index.vue @@ -2,10 +2,12 @@ import Child from './child.vue' import ReactivityTransform from './reactivity-transform.vue' import QuasarDemo from './quasar-demo.vue' +import ShortEmits from './short-emits.vue' diff --git a/playground/vue3/src/examples/better-define/short-emits.vue b/playground/vue3/src/examples/better-define/short-emits.vue new file mode 100644 index 000000000..87f3caf82 --- /dev/null +++ b/playground/vue3/src/examples/better-define/short-emits.vue @@ -0,0 +1,11 @@ + + +