Skip to content

Commit a1deea2

Browse files
committed
fix(language-core): generate prefix for events defined by emits correctly
1 parent ecefdb8 commit a1deea2

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import * as CompilerDOM from '@vue/compiler-dom';
22
import { camelize, capitalize } from '@vue/shared';
33
import type * as ts from 'typescript';
4-
import type { Code } from '../../types';
4+
import type { Code, VueCodeInformation } from '../../types';
55
import { combineLastMapping, createTsAst, endOfLine, identifierRegex, newLine } from '../utils';
66
import { generateCamelized } from '../utils/camelized';
77
import { wrapWith } from '../utils/wrapWith';
88
import type { TemplateCodegenContext } from './context';
99
import type { TemplateCodegenOptions } from './index';
1010
import { generateInterpolation } from './interpolation';
11-
import { generateObjectProperty } from './objectProperty';
1211

1312
export function* generateElementEvents(
1413
options: TemplateCodegenOptions,
@@ -40,6 +39,7 @@ export function* generateElementEvents(
4039
yield `let ${emitsVar}!: __VLS_ResolveEmits<typeof ${componentOriginalVar}, typeof ${componentCtxVar}.emit>${endOfLine}`;
4140
yield `let ${propsVar}!: __VLS_FunctionalComponentProps<typeof ${componentFunctionalVar}, typeof ${componentVNodeVar}>${endOfLine}`;
4241
}
42+
4343
let source = prop.arg?.loc.source ?? 'model-value';
4444
let start = prop.arg?.loc.start.offset;
4545
let propPrefix = 'on-';
@@ -54,18 +54,14 @@ export function* generateElementEvents(
5454
propPrefix = 'onVnode-';
5555
emitPrefix = 'vnode-';
5656
}
57-
yield `(): __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${emitsVar}, '${camelize(propPrefix + source)}', '${emitPrefix + source}', '${camelize(emitPrefix + source)}'> => (${newLine}`;
57+
const propName = camelize(propPrefix + source);
58+
const emitName = emitPrefix + source;
59+
const camelizedEmitName = camelize(emitName);
60+
61+
yield `(): __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${emitsVar}, '${propName}', '${emitName}', '${camelizedEmitName}'> => (${newLine}`;
5862
if (prop.name === 'on') {
5963
yield `{ `;
60-
yield* generateObjectProperty(
61-
options,
62-
ctx,
63-
emitPrefix + source,
64-
start!,
65-
ctx.codeFeatures.navigation,
66-
undefined,
67-
true
68-
);
64+
yield* generateEventArg(ctx, source, start!, emitPrefix.slice(0, -1), ctx.codeFeatures.navigation);
6965
yield `: {} as any } as typeof ${emitsVar},${newLine}`;
7066
}
7167
yield `{ `;
@@ -75,7 +71,7 @@ export function* generateElementEvents(
7571
yield* generateEventExpression(options, ctx, prop);
7672
}
7773
else {
78-
yield `'${camelize(propPrefix + source)}': `;
74+
yield `'${propName}': `;
7975
yield* generateModelEventExpression(options, ctx, prop);
8076
}
8177
yield `})${endOfLine}`;
@@ -87,21 +83,19 @@ export function* generateEventArg(
8783
ctx: TemplateCodegenContext,
8884
name: string,
8985
start: number,
90-
directive = 'on'
91-
): Generator<Code> {
92-
const features = {
86+
directive = 'on',
87+
features: VueCodeInformation = {
9388
...ctx.codeFeatures.withoutHighlightAndCompletion,
9489
...ctx.codeFeatures.navigationWithoutRename,
95-
};
90+
}
91+
): Generator<Code> {
92+
if (directive.length) {
93+
name = capitalize(name);
94+
}
9695
if (identifierRegex.test(camelize(name))) {
9796
yield ['', 'template', start, features];
9897
yield directive;
99-
yield* generateCamelized(
100-
capitalize(name),
101-
'template',
102-
start,
103-
combineLastMapping
104-
);
98+
yield* generateCamelized(name, 'template', start, combineLastMapping);
10599
}
106100
else {
107101
yield* wrapWith(
@@ -110,12 +104,7 @@ export function* generateEventArg(
110104
features,
111105
`'`,
112106
directive,
113-
...generateCamelized(
114-
capitalize(name),
115-
'template',
116-
start,
117-
combineLastMapping
118-
),
107+
...generateCamelized(name, 'template', start, combineLastMapping),
119108
`'`
120109
);
121110
}

0 commit comments

Comments
 (0)