Skip to content

Commit

Permalink
feat(better-define): support short-emits syntax (#582)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
  • Loading branch information
zhiyuanzmj and sxzz committed Dec 14, 2023
1 parent 20d3e08 commit 2b3a405
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/popular-rivers-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vue-macros/api': patch
---

support short-emits syntax
16 changes: 15 additions & 1 deletion packages/api/src/vue/emits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import type {
TSFunctionType,
TSInterfaceDeclaration,
TSIntersectionType,
TSMappedType,
TSPropertySignature,
TSType,
TSTypeLiteral,
UnaryExpression,
Expand Down Expand Up @@ -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 }),
Expand Down Expand Up @@ -212,7 +221,12 @@ export interface TSEmits extends EmitsBase {

definitions: Record<
string,
ASTDefinition<TSCallSignatureDeclaration | TSFunctionType>[]
ASTDefinition<
| TSCallSignatureDeclaration
| TSFunctionType
| TSPropertySignature
| TSMappedType
>[]
>
definitionsAst: ASTDefinition<
TSTypeLiteral | TSIntersectionType | TSInterfaceDeclaration | TSFunctionType
Expand Down
46 changes: 46 additions & 0 deletions packages/better-define/tests/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 10 additions & 0 deletions packages/better-define/tests/fixtures/short-emits.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script setup lang="ts">
type ShortEmits = {
click: []
}
const emit = defineEmits<ShortEmits>()
</script>

<template>
<button @click="emit('click')">click</button>
</template>
2 changes: 2 additions & 0 deletions playground/vue3/src/examples/better-define/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
</script>

<template>
<Child name="" msg="" :age="10" union="" />
<ReactivityTransform />
<QuasarDemo model-value="" />
<ShortEmits />
</template>
11 changes: 11 additions & 0 deletions playground/vue3/src/examples/better-define/short-emits.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup lang="ts">
type ShortEmits = {
click: []
}
const emit = defineEmits<ShortEmits>()
</script>

<template>
<button @click="emit('click')">click</button>
</template>

0 comments on commit 2b3a405

Please sign in to comment.