Skip to content

Commit

Permalink
fix(api): resolve union event name
Browse files Browse the repository at this point in the history
  • Loading branch information
topsmart20 committed Mar 23, 2023
1 parent b4e29ed commit 63abe1d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .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
26 changes: 17 additions & 9 deletions packages/api/src/vue/emits.ts
Expand Up @@ -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<typeof literal, UnaryExpression>)
)
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<typeof literal, UnaryExpression>)
)
if (!definitions[evt]) definitions[evt] = []
definitions[evt].push(buildDefinition(signature))
}
}

return {
Expand Down
52 changes: 52 additions & 0 deletions packages/better-define/tests/__snapshots__/fixtures.test.ts.snap
Expand Up @@ -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';
Expand Down
7 changes: 7 additions & 0 deletions packages/better-define/tests/fixtures/union-emits.vue
@@ -0,0 +1,7 @@
<script setup lang="ts">
type Emits = 'some' | 'emit'
const emit = defineEmits<{
(e: Emits): void
(e: 'another', val: string): void
}>()
</script>

0 comments on commit 63abe1d

Please sign in to comment.