Skip to content

Commit f94d7a1

Browse files
committed
refactor(language-core): extract event normalization logic to global types
1 parent 06b98f5 commit f94d7a1

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

packages/language-core/lib/codegen/globalTypes.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
4747
N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } :
4848
N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } :
4949
N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } :
50-
${strictTemplates ? '{}' : '{ [K in N0]: unknown }'}
50+
${strictTemplates ? '{}' : '{ [K in N0]: unknown }'};
5151
type __VLS_FunctionalComponentProps<T, K> =
5252
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: { props?: infer P } } ? NonNullable<P> : never
5353
: T extends (props: infer P, ...args: any) => any ? P :
@@ -59,6 +59,15 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
5959
: true
6060
: false
6161
: false;
62+
type __VLS_NormalizeComponentEvent<Props, Events, onEvent extends keyof Props, Event extends keyof Events, CamelizedEvent extends keyof Events> = (
63+
__VLS_IsFunction<Props, onEvent> extends true
64+
? Props
65+
: __VLS_IsFunction<Events, Event> extends true
66+
? { [K in onEvent]?: Events[Event] }
67+
: __VLS_IsFunction<Events, CamelizedEvent> extends true
68+
? { [K in onEvent]?: Events[CamelizedEvent] }
69+
: Props
70+
)${ strictTemplates ? '' : ' & Record<string, unknown>' };
6271
// fix https://github.com/vuejs/language-tools/issues/926
6372
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
6473
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export function* generateComponent(
245245
}
246246
}
247247

248-
const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEmit, var_componentEvents);
248+
const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEvents);
249249
if (usedComponentEventsVar) {
250250
ctx.usedComponentCtxVars.add(var_defineComponentCtx);
251251
yield `let ${var_componentEmit}!: typeof ${var_defineComponentCtx}.emit${endOfLine}`;

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export function* generateElementEvents(
1515
node: CompilerDOM.ElementNode,
1616
componentVar: string,
1717
componentInstanceVar: string,
18-
emitVar: string,
1918
eventsVar: string
2019
): Generator<Code, boolean> {
2120
let usedComponentEventsVar = false;
@@ -34,30 +33,7 @@ export function* generateElementEvents(
3433
yield `let ${propsVar}!: __VLS_FunctionalComponentProps<typeof ${componentVar}, typeof ${componentInstanceVar}>${endOfLine}`;
3534
}
3635
const originalPropName = camelize('on-' + prop.arg.loc.source);
37-
const originalPropNameObjectKey = variableNameRegex.test(originalPropName)
38-
? originalPropName
39-
: `'${originalPropName}'`;
40-
yield `const ${ctx.getInternalVariable()}: `;
41-
if (!options.vueCompilerOptions.strictTemplates) {
42-
yield `Record<string, unknown> & `;
43-
}
44-
yield `(${newLine}`;
45-
yield `__VLS_IsFunction<typeof ${propsVar}, '${originalPropName}'> extends true${newLine}`;
46-
yield `? typeof ${propsVar}${newLine}`;
47-
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${prop.arg.loc.source}'> extends true${newLine}`;
48-
yield `? {${newLine}`;
49-
yield `/**__VLS_emit,${emitVar},${prop.arg.loc.source}*/${newLine}`;
50-
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${prop.arg.loc.source}']${newLine}`;
51-
yield `}${newLine}`;
52-
if (prop.arg.loc.source !== camelize(prop.arg.loc.source)) {
53-
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${camelize(prop.arg.loc.source)}'> extends true${newLine}`;
54-
yield `? {${newLine}`;
55-
yield `/**__VLS_emit,${emitVar},${camelize(prop.arg.loc.source)}*/${newLine}`;
56-
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${camelize(prop.arg.loc.source)}']${newLine}`;
57-
yield `}${newLine}`;
58-
}
59-
yield `: typeof ${propsVar}${newLine}`;
60-
yield `) = {${newLine}`;
36+
yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${eventsVar}, '${originalPropName}', '${prop.arg.loc.source}', '${camelize(prop.arg.loc.source)}'> = {${newLine}`;
6137
yield* generateEventArg(ctx, prop.arg, true);
6238
yield `: `;
6339
yield* generateEventExpression(options, ctx, prop);

0 commit comments

Comments
 (0)