Skip to content

Commit dcfe727

Browse files
authored
fix(language-core): do not generate redundant function scopes to affect type narrowing (#5430)
1 parent 894c5e8 commit dcfe727

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/language-core/lib/codegen/template/elementEvents.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function* generateElementEvents(
5858
const emitName = emitPrefix + source;
5959
const camelizedEmitName = camelize(emitName);
6060

61-
yield `(): __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${emitsVar}, '${propName}', '${emitName}', '${camelizedEmitName}'> => (${newLine}`;
61+
yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${emitsVar}, '${propName}', '${emitName}', '${camelizedEmitName}'> = (${newLine}`;
6262
if (prop.name === 'on') {
6363
yield `{ `;
6464
yield* generateEventArg(ctx, source, start!, emitPrefix.slice(0, -1), ctx.codeFeatures.navigation);
@@ -121,10 +121,12 @@ export function* generateEventExpression(
121121
let isFirstMapping = true;
122122

123123
const ast = createTsAst(options.ts, prop.exp, prop.exp.content);
124-
const _isCompoundExpression = isCompoundExpression(options.ts, ast);
125-
if (_isCompoundExpression) {
126-
yield `(...[$event]) => {${newLine}`;
124+
const isCompound = isCompoundExpression(options.ts, ast);
125+
126+
if (isCompound) {
127127
ctx.addLocalVariable('$event');
128+
129+
yield `(...[$event]) => {${newLine}`;
128130
yield* ctx.generateConditionGuards();
129131
prefix = ``;
130132
suffix = ``;
@@ -135,7 +137,7 @@ export function* generateEventExpression(
135137
ctx,
136138
'template',
137139
offset => {
138-
if (_isCompoundExpression && isFirstMapping) {
140+
if (isCompound && isFirstMapping) {
139141
isFirstMapping = false;
140142
ctx.inlayHints.push({
141143
blockName: 'template',
@@ -159,7 +161,7 @@ export function* generateEventExpression(
159161
suffix
160162
);
161163

162-
if (_isCompoundExpression) {
164+
if (isCompound) {
163165
ctx.removeLocalVariable('$event');
164166

165167
yield endOfLine;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script setup lang="ts">
2+
let foo!: {
3+
bar?: {
4+
baz: () => void;
5+
}
6+
};
7+
const Comp = () => {};
8+
</script>
9+
10+
<template>
11+
<div v-if="foo.bar">
12+
<Comp @click="foo.bar.baz" />
13+
</div>
14+
</template>

0 commit comments

Comments
 (0)