Skip to content

Commit

Permalink
refactor(language-core): extract event normalization logic to global …
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
KazariEX committed Dec 17, 2024
1 parent 06b98f5 commit f94d7a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
11 changes: 10 additions & 1 deletion packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } :
N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } :
N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } :
${strictTemplates ? '{}' : '{ [K in N0]: unknown }'}
${strictTemplates ? '{}' : '{ [K in N0]: unknown }'};
type __VLS_FunctionalComponentProps<T, K> =
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: { props?: infer P } } ? NonNullable<P> : never
: T extends (props: infer P, ...args: any) => any ? P :
Expand All @@ -59,6 +59,15 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates
: true
: false
: false;
type __VLS_NormalizeComponentEvent<Props, Events, onEvent extends keyof Props, Event extends keyof Events, CamelizedEvent extends keyof Events> = (
__VLS_IsFunction<Props, onEvent> extends true
? Props
: __VLS_IsFunction<Events, Event> extends true
? { [K in onEvent]?: Events[Event] }
: __VLS_IsFunction<Events, CamelizedEvent> extends true
? { [K in onEvent]?: Events[CamelizedEvent] }
: Props
)${ strictTemplates ? '' : ' & Record<string, unknown>' };
// fix https://github.com/vuejs/language-tools/issues/926
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function* generateComponent(
}
}

const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEmit, var_componentEvents);
const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEvents);
if (usedComponentEventsVar) {
ctx.usedComponentCtxVars.add(var_defineComponentCtx);
yield `let ${var_componentEmit}!: typeof ${var_defineComponentCtx}.emit${endOfLine}`;
Expand Down
26 changes: 1 addition & 25 deletions packages/language-core/lib/codegen/template/elementEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function* generateElementEvents(
node: CompilerDOM.ElementNode,
componentVar: string,
componentInstanceVar: string,
emitVar: string,
eventsVar: string
): Generator<Code, boolean> {
let usedComponentEventsVar = false;
Expand All @@ -34,30 +33,7 @@ export function* generateElementEvents(
yield `let ${propsVar}!: __VLS_FunctionalComponentProps<typeof ${componentVar}, typeof ${componentInstanceVar}>${endOfLine}`;
}
const originalPropName = camelize('on-' + prop.arg.loc.source);
const originalPropNameObjectKey = variableNameRegex.test(originalPropName)
? originalPropName
: `'${originalPropName}'`;
yield `const ${ctx.getInternalVariable()}: `;
if (!options.vueCompilerOptions.strictTemplates) {
yield `Record<string, unknown> & `;
}
yield `(${newLine}`;
yield `__VLS_IsFunction<typeof ${propsVar}, '${originalPropName}'> extends true${newLine}`;
yield `? typeof ${propsVar}${newLine}`;
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${prop.arg.loc.source}'> extends true${newLine}`;
yield `? {${newLine}`;
yield `/**__VLS_emit,${emitVar},${prop.arg.loc.source}*/${newLine}`;
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${prop.arg.loc.source}']${newLine}`;
yield `}${newLine}`;
if (prop.arg.loc.source !== camelize(prop.arg.loc.source)) {
yield `: __VLS_IsFunction<typeof ${eventsVar}, '${camelize(prop.arg.loc.source)}'> extends true${newLine}`;
yield `? {${newLine}`;
yield `/**__VLS_emit,${emitVar},${camelize(prop.arg.loc.source)}*/${newLine}`;
yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${camelize(prop.arg.loc.source)}']${newLine}`;
yield `}${newLine}`;
}
yield `: typeof ${propsVar}${newLine}`;
yield `) = {${newLine}`;
yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${eventsVar}, '${originalPropName}', '${prop.arg.loc.source}', '${camelize(prop.arg.loc.source)}'> = {${newLine}`;
yield* generateEventArg(ctx, prop.arg, true);
yield `: `;
yield* generateEventExpression(options, ctx, prop);
Expand Down

0 comments on commit f94d7a1

Please sign in to comment.