From 9e91d26939fd0c92035ffb51bb40d37653d646b9 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Fri, 29 Mar 2024 01:36:09 +0800 Subject: [PATCH] chore(language-core): move global types code --- .../lib/generators/globalTypes.ts | 129 +++++++++++++++++ .../language-core/lib/generators/script.ts | 130 +----------------- 2 files changed, 131 insertions(+), 128 deletions(-) create mode 100644 packages/language-core/lib/generators/globalTypes.ts diff --git a/packages/language-core/lib/generators/globalTypes.ts b/packages/language-core/lib/generators/globalTypes.ts new file mode 100644 index 0000000000..48fb161d8c --- /dev/null +++ b/packages/language-core/lib/generators/globalTypes.ts @@ -0,0 +1,129 @@ +import type { VueCompilerOptions } from '../types'; +import { getSlotsPropertyName } from '../utils/shared'; + +export function generateGlobalTypes(vueCompilerOptions: VueCompilerOptions) { + const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${vueCompilerOptions.strictTemplates ? '' : ' & Record'}`; + return ` +; declare global { +// @ts-ignore +type __VLS_IntrinsicElements = __VLS_PickNotAny>>; +// @ts-ignore +type __VLS_Element = __VLS_PickNotAny; +// @ts-ignore +type __VLS_GlobalComponents = ${[ + `__VLS_PickNotAny`, + `__VLS_PickNotAny`, + `__VLS_PickNotAny`, + `Pick` + ].join(' & ')}; +type __VLS_IsAny = 0 extends 1 & T ? true : false; +type __VLS_PickNotAny = __VLS_IsAny extends true ? B : A; + +const __VLS_intrinsicElements: __VLS_IntrinsicElements; + +// v-for +function __VLS_getVForSourceType(source: number): [number, number, number][]; +function __VLS_getVForSourceType(source: string): [string, number, number][]; +function __VLS_getVForSourceType(source: T): [ + item: T[number], + key: number, + index: number, +][]; +function __VLS_getVForSourceType }>(source: T): [ + item: T extends { [Symbol.iterator](): Iterator } ? T1 : never, + key: number, + index: undefined, +][]; +// #3845 +function __VLS_getVForSourceType }>(source: T): [ + item: number | (Exclude extends { [Symbol.iterator](): Iterator } ? T1 : never), + key: number, + index: undefined, +][]; +function __VLS_getVForSourceType(source: T): [ + item: T[keyof T], + key: keyof T, + index: number, +][]; + +// @ts-ignore +function __VLS_getSlotParams(slot: T): Parameters<__VLS_PickNotAny, (...args: any[]) => any>>; +// @ts-ignore +function __VLS_getSlotParam(slot: T): Parameters<__VLS_PickNotAny, (...args: any[]) => any>>[0]; +function __VLS_directiveFunction(dir: T): + T extends import('${vueCompilerOptions.lib}').ObjectDirective | import('${vueCompilerOptions.lib}').FunctionDirective ? (value: V) => void + : T; +function __VLS_withScope(ctx: T, scope: K): ctx is T & K; +function __VLS_makeOptional(t: T): { [K in keyof T]?: T[K] }; + +type __VLS_SelfComponent = string extends N ? {} : N extends string ? { [P in N]: C } : {}; +type __VLS_WithComponent = + N1 extends keyof LocalComponents ? N1 extends N0 ? Pick : { [K in N0]: LocalComponents[N1] } : + N2 extends keyof LocalComponents ? N2 extends N0 ? Pick : { [K in N0]: LocalComponents[N2] } : + N3 extends keyof LocalComponents ? N3 extends N0 ? Pick : { [K in N0]: LocalComponents[N3] } : + 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] } : + ${vueCompilerOptions.strictTemplates ? '{}' : '{ [K in N0]: unknown }'} + +type __VLS_FillingEventArg_ParametersLength any> = __VLS_IsAny> extends true ? -1 : Parameters['length']; +type __VLS_FillingEventArg = E extends (...args: any) => any ? __VLS_FillingEventArg_ParametersLength extends 0 ? ($event?: undefined) => ReturnType : E : E; +function __VLS_asFunctionalComponent any ? InstanceType : unknown>(t: T, instance?: K): + T extends new (...args: any) => any + ? (props: ${fnPropsType}, ctx?: any) => __VLS_Element & { __ctx?: { + attrs?: any, + slots?: K extends { ${getSlotsPropertyName(vueCompilerOptions.target)}: infer Slots } ? Slots : any, + emit?: K extends { $emit: infer Emit } ? Emit : any + } & { props?: ${fnPropsType}; expose?(exposed: K): void; } } + : T extends () => any ? (props: {}, ctx?: any) => ReturnType + : T extends (...args: any) => any ? T + : (_: {}${vueCompilerOptions.strictTemplates ? '' : ' & Record'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${vueCompilerOptions.strictTemplates ? '' : ' & Record'} } }; +function __VLS_elementAsFunctionalComponent(t: T): (_: T${vueCompilerOptions.strictTemplates ? '' : ' & Record'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: T${vueCompilerOptions.strictTemplates ? '' : ' & Record'} } }; +function __VLS_functionalComponentArgsRest any>(t: T): Parameters['length'] extends 2 ? [any] : []; +function __VLS_pickEvent(emitEvent: E1, propEvent: E2): __VLS_FillingEventArg< + __VLS_PickNotAny< + __VLS_AsFunctionOrAny, + __VLS_AsFunctionOrAny + > +> | undefined; +function __VLS_pickFunctionalComponentCtx(comp: T, compInstance: K): __VLS_PickNotAny< + '__ctx' extends keyof __VLS_PickNotAny ? K extends { __ctx?: infer Ctx } ? Ctx : never : any + , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any +>; +type __VLS_FunctionalComponentProps = + '__ctx' extends keyof __VLS_PickNotAny ? K extends { __ctx?: { props?: infer P } } ? NonNullable

: never + : T extends (props: infer P, ...args: any) => any ? P : + {}; +type __VLS_AsFunctionOrAny = unknown extends F ? any : ((...args: any) => any) extends F ? F : any; + +function __VLS_normalizeSlot(s: S): S extends () => infer R ? (props: {}) => R : S; + +/** + * emit + */ +// fix https://github.com/vuejs/language-tools/issues/926 +type __VLS_UnionToIntersection = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never; +type __VLS_OverloadUnionInner = U & T extends (...args: infer A) => infer R + ? U extends T + ? never + : __VLS_OverloadUnionInner & U & ((...args: A) => R)> | ((...args: A) => R) + : never; +type __VLS_OverloadUnion = Exclude< + __VLS_OverloadUnionInner<(() => never) & T>, + T extends () => never ? never : () => never +>; +type __VLS_ConstructorOverloads = __VLS_OverloadUnion extends infer F + ? F extends (event: infer E, ...args: infer A) => any + ? { [K in E & string]: (...args: A) => void; } + : never + : never; +type __VLS_NormalizeEmits = __VLS_PrettifyGlobal< + __VLS_UnionToIntersection< + __VLS_ConstructorOverloads & { + [K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never + } + > +>; +type __VLS_PrettifyGlobal = { [K in keyof T]: T[K]; } & {}; +}`; +}; diff --git a/packages/language-core/lib/generators/script.ts b/packages/language-core/lib/generators/script.ts index fe30740f7d..420df25917 100644 --- a/packages/language-core/lib/generators/script.ts +++ b/packages/language-core/lib/generators/script.ts @@ -7,6 +7,7 @@ import type { Code, CodeAndStack, Sfc, SfcBlock, VueCompilerOptions } from '../t import { getSlotsPropertyName, hyphenateTag } from '../utils/shared'; import { eachInterpolationSegment } from '../utils/transform'; import { disableAllFeatures, enableAllFeatures, withStack } from './utils'; +import { generateGlobalTypes } from './globalTypes'; interface HelperType { name: string; @@ -174,7 +175,7 @@ export function* generate( yield* generateScriptSetupAndTemplate(); yield* generateScriptContentAfterExportDefault(); if (globalTypesHolder === fileName) { - yield* generateGlobalHelperTypes(); + yield _(generateGlobalTypes(vueCompilerOptions)); } yield* generateLocalHelperTypes(); yield _(`\ntype __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements;\n`); @@ -187,133 +188,6 @@ export function* generate( yield _(['', 'scriptSetup', scriptSetup.content.length, disableAllFeatures({ verification: true })]); } - function* generateGlobalHelperTypes(): Generator { - const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${vueCompilerOptions.strictTemplates ? '' : ' & Record'}`; - yield _(` -; declare global { -// @ts-ignore -type __VLS_IntrinsicElements = __VLS_PickNotAny>>; -// @ts-ignore -type __VLS_Element = __VLS_PickNotAny; -// @ts-ignore -type __VLS_GlobalComponents = ${[ - `__VLS_PickNotAny`, - `__VLS_PickNotAny`, - `__VLS_PickNotAny`, - `Pick` - ].join(' & ')}; -type __VLS_IsAny = 0 extends 1 & T ? true : false; -type __VLS_PickNotAny = __VLS_IsAny extends true ? B : A; - -const __VLS_intrinsicElements: __VLS_IntrinsicElements; - -// v-for -function __VLS_getVForSourceType(source: number): [number, number, number][]; -function __VLS_getVForSourceType(source: string): [string, number, number][]; -function __VLS_getVForSourceType(source: T): [ - item: T[number], - key: number, - index: number, -][]; -function __VLS_getVForSourceType }>(source: T): [ - item: T extends { [Symbol.iterator](): Iterator } ? T1 : never, - key: number, - index: undefined, -][]; -// #3845 -function __VLS_getVForSourceType }>(source: T): [ - item: number | (Exclude extends { [Symbol.iterator](): Iterator } ? T1 : never), - key: number, - index: undefined, -][]; -function __VLS_getVForSourceType(source: T): [ - item: T[keyof T], - key: keyof T, - index: number, -][]; - -// @ts-ignore -function __VLS_getSlotParams(slot: T): Parameters<__VLS_PickNotAny, (...args: any[]) => any>>; -// @ts-ignore -function __VLS_getSlotParam(slot: T): Parameters<__VLS_PickNotAny, (...args: any[]) => any>>[0]; -function __VLS_directiveFunction(dir: T): - T extends import('${vueCompilerOptions.lib}').ObjectDirective | import('${vueCompilerOptions.lib}').FunctionDirective ? (value: V) => void - : T; -function __VLS_withScope(ctx: T, scope: K): ctx is T & K; -function __VLS_makeOptional(t: T): { [K in keyof T]?: T[K] }; - -type __VLS_SelfComponent = string extends N ? {} : N extends string ? { [P in N]: C } : {}; -type __VLS_WithComponent = - N1 extends keyof LocalComponents ? N1 extends N0 ? Pick : { [K in N0]: LocalComponents[N1] } : - N2 extends keyof LocalComponents ? N2 extends N0 ? Pick : { [K in N0]: LocalComponents[N2] } : - N3 extends keyof LocalComponents ? N3 extends N0 ? Pick : { [K in N0]: LocalComponents[N3] } : - 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] } : - ${vueCompilerOptions.strictTemplates ? '{}' : '{ [K in N0]: unknown }'} - -type __VLS_FillingEventArg_ParametersLength any> = __VLS_IsAny> extends true ? -1 : Parameters['length']; -type __VLS_FillingEventArg = E extends (...args: any) => any ? __VLS_FillingEventArg_ParametersLength extends 0 ? ($event?: undefined) => ReturnType : E : E; -function __VLS_asFunctionalComponent any ? InstanceType : unknown>(t: T, instance?: K): - T extends new (...args: any) => any - ? (props: ${fnPropsType}, ctx?: any) => __VLS_Element & { __ctx?: { - attrs?: any, - slots?: K extends { ${getSlotsPropertyName(vueCompilerOptions.target)}: infer Slots } ? Slots : any, - emit?: K extends { $emit: infer Emit } ? Emit : any - } & { props?: ${fnPropsType}; expose?(exposed: K): void; } } - : T extends () => any ? (props: {}, ctx?: any) => ReturnType - : T extends (...args: any) => any ? T - : (_: {}${vueCompilerOptions.strictTemplates ? '' : ' & Record'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${vueCompilerOptions.strictTemplates ? '' : ' & Record'} } }; -function __VLS_elementAsFunctionalComponent(t: T): (_: T${vueCompilerOptions.strictTemplates ? '' : ' & Record'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: T${vueCompilerOptions.strictTemplates ? '' : ' & Record'} } }; -function __VLS_functionalComponentArgsRest any>(t: T): Parameters['length'] extends 2 ? [any] : []; -function __VLS_pickEvent(emitEvent: E1, propEvent: E2): __VLS_FillingEventArg< - __VLS_PickNotAny< - __VLS_AsFunctionOrAny, - __VLS_AsFunctionOrAny - > -> | undefined; -function __VLS_pickFunctionalComponentCtx(comp: T, compInstance: K): __VLS_PickNotAny< - '__ctx' extends keyof __VLS_PickNotAny ? K extends { __ctx?: infer Ctx } ? Ctx : never : any - , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any ->; -type __VLS_FunctionalComponentProps = - '__ctx' extends keyof __VLS_PickNotAny ? K extends { __ctx?: { props?: infer P } } ? NonNullable

: never - : T extends (props: infer P, ...args: any) => any ? P : - {}; -type __VLS_AsFunctionOrAny = unknown extends F ? any : ((...args: any) => any) extends F ? F : any; - -function __VLS_normalizeSlot(s: S): S extends () => infer R ? (props: {}) => R : S; - -/** - * emit - */ -// fix https://github.com/vuejs/language-tools/issues/926 -type __VLS_UnionToIntersection = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never; -type __VLS_OverloadUnionInner = U & T extends (...args: infer A) => infer R - ? U extends T - ? never - : __VLS_OverloadUnionInner & U & ((...args: A) => R)> | ((...args: A) => R) - : never; -type __VLS_OverloadUnion = Exclude< - __VLS_OverloadUnionInner<(() => never) & T>, - T extends () => never ? never : () => never ->; -type __VLS_ConstructorOverloads = __VLS_OverloadUnion extends infer F - ? F extends (event: infer E, ...args: infer A) => any - ? { [K in E & string]: (...args: A) => void; } - : never - : never; -type __VLS_NormalizeEmits = __VLS_PrettifyGlobal< - __VLS_UnionToIntersection< - __VLS_ConstructorOverloads & { - [K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never - } - > ->; -type __VLS_PrettifyGlobal = { [K in keyof T]: T[K]; } & {}; -} -`); - } function* generateLocalHelperTypes(): Generator { let shouldCheck = true; while (shouldCheck) {