From f2df923d4c10a70d38c54fef8fd27964eff2a6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Fri, 24 Mar 2023 01:30:04 +0800 Subject: [PATCH] fix(api): resolve union event name fix https://github.com/vuejs/core/issues/7943 --- .changeset/blue-falcons-crash.md | 6 +++ packages/api/src/vue/emits.ts | 26 ++++++---- .../tests/__snapshots__/fixtures.test.ts.snap | 52 +++++++++++++++++++ .../tests/fixtures/union-emits.vue | 7 +++ 4 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 .changeset/blue-falcons-crash.md create mode 100644 packages/better-define/tests/fixtures/union-emits.vue diff --git a/.changeset/blue-falcons-crash.md b/.changeset/blue-falcons-crash.md new file mode 100644 index 000000000..0b66386b6 --- /dev/null +++ b/.changeset/blue-falcons-crash.md @@ -0,0 +1,6 @@ +--- +'@vue-macros/better-define': patch +'@vue-macros/api': patch +--- + +resolve union event name for `api` and `betterDefine`, fix https://github.com/vuejs/core/issues/7943 diff --git a/packages/api/src/vue/emits.ts b/packages/api/src/vue/emits.ts index 11991277d..3ea2c6422 100644 --- a/packages/api/src/vue/emits.ts +++ b/packages/api/src/vue/emits.ts @@ -154,16 +154,24 @@ export async function handleTSEmitsDefinition({ type: evtArg.typeAnnotation.typeAnnotation, scope: signature.scope, }) - if (isTSExports(evtType) || evtType?.type.type !== 'TSLiteralType') - continue - const literal = evtType.type.literal - if (!isStaticExpression(literal)) continue - const evt = String( - resolveLiteral(literal as Exclude) - ) - if (!definitions[evt]) definitions[evt] = [] - definitions[evt].push(buildDefinition(signature)) + if (isTSExports(evtType) || !evtType?.type) continue + + const types = + evtType.type.type === 'TSUnionType' + ? evtType.type.types + : [evtType.type] + + for (const type of types) { + if (type.type !== 'TSLiteralType') continue + const literal = type.literal + if (!isStaticExpression(literal)) continue + const evt = String( + resolveLiteral(literal as Exclude) + ) + if (!definitions[evt]) definitions[evt] = [] + definitions[evt].push(buildDefinition(signature)) + } } return { diff --git a/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap b/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap index 0ae9ab50e..0549b4439 100644 --- a/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap +++ b/packages/better-define/tests/__snapshots__/fixtures.test.ts.snap @@ -706,6 +706,58 @@ export { union as default }; " `; +exports[`fixtures > tests/fixtures/union-emits.vue > isProduction = false 1`] = ` +"import { defineComponent } from 'vue'; + +var _sfc_main = /* @__PURE__ */ defineComponent({ + __name: \\"union-emits\\", + emits: [\\"some\\", \\"emit\\", \\"another\\"], + setup(__props, { emit }) { + return () => { + }; + } +}); + +var _export_sfc = (sfc, props) => { + const target = sfc.__vccOpts || sfc; + for (const [key, val] of props) { + target[key] = val; + } + return target; +}; + +var unionEmits = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]); + +export { unionEmits as default }; +" +`; + +exports[`fixtures > tests/fixtures/union-emits.vue > isProduction = true 1`] = ` +"import { defineComponent } from 'vue'; + +var _sfc_main = /* @__PURE__ */ defineComponent({ + __name: \\"union-emits\\", + emits: [\\"some\\", \\"emit\\", \\"another\\"], + setup(__props, { emit }) { + return () => { + }; + } +}); + +var _export_sfc = (sfc, props) => { + const target = sfc.__vccOpts || sfc; + for (const [key, val] of props) { + target[key] = val; + } + return target; +}; + +var unionEmits = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]); + +export { unionEmits as default }; +" +`; + exports[`fixtures > tests/fixtures/unresolved.vue > isProduction = false 1`] = ` "import { defineComponent } from 'vue'; diff --git a/packages/better-define/tests/fixtures/union-emits.vue b/packages/better-define/tests/fixtures/union-emits.vue new file mode 100644 index 000000000..680524c04 --- /dev/null +++ b/packages/better-define/tests/fixtures/union-emits.vue @@ -0,0 +1,7 @@ +