Skip to content

Commit

Permalink
fix: support defineModel without defineEmits
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanzmj committed May 7, 2024
1 parent 8f9e23e commit 8a0a754
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ export function* generateScriptSetup(
+ ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],${newLine}`
+ ` __VLS_setup = (async () => {${newLine}`;
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, undefined, definePropMirrors);
const emitName = scriptSetupRanges.emits.define ? `typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'} & ` : '';
yield ` return {} as {${newLine}`
+ ` props: ${ctx.helperTypes.Prettify.name}<typeof __VLS_functionalComponentProps & __VLS_PublicProps> & __VLS_BuiltInPublicProps,${newLine}`
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.expose.define ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}`
+ ` attrs: any,${newLine}`
+ ` slots: ReturnType<typeof __VLS_template>,${newLine}`
+ ` emit: typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'} & typeof __VLS_modelEmitsType,${newLine}`
+ ` emit: ${emitName}typeof __VLS_modelEmitsType,${newLine}`
+ ` }${endOfLine}`;
yield ` })(),${newLine}`; // __VLS_setup = (async () => {
yield `) => ({} as import('${options.vueCompilerOptions.lib}').VNode & { __ctx?: Awaited<typeof __VLS_setup> }))`;
Expand Down Expand Up @@ -263,17 +264,21 @@ function* generateComponentProps(
scriptSetupRanges: ScriptSetupRanges,
definePropMirrors: Map<string, number>,
): Generator<Code> {
if (scriptSetupRanges.props.define?.arg || scriptSetupRanges.emits.define) {
const hasEmit = scriptSetupRanges.emits.define || scriptSetupRanges.defineProp.some(p => p.isModel);
if (scriptSetupRanges.props.define?.arg || hasEmit) {
yield `const __VLS_fnComponent = `
+ `(await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
if (scriptSetupRanges.props.define?.arg) {
yield ` props: `;
yield generateSfcBlockSection(scriptSetup, scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end, codeFeatures.navigation);
yield `,${newLine}`;
}
if (scriptSetupRanges.emits.define) {
if (hasEmit) {
yield ` emits: ({} as __VLS_NormalizeEmits<typeof __VLS_modelEmitsType`;
yield scriptSetupRanges.emits.name ?? ' & typeof __VLS_emit';
if (scriptSetupRanges.emits.define) {
yield ` & typeof `;
yield scriptSetupRanges.emits.name ?? '__VLS_emit';
}
yield `>),${newLine}`;
}
yield `})${endOfLine}`;
Expand Down
2 changes: 1 addition & 1 deletion test-workspace/component-meta/generic/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const title = ref('')
</script>

<template>
<Comp :foo="1" v-model:="title" />
<Comp :foo="1" v-model:title="title" />
</template>

0 comments on commit 8a0a754

Please sign in to comment.