Skip to content

Commit 8d8e069

Browse files
committed
fix(language-core): using interface merging for GlobalComponents
1 parent 05a8020 commit 8d8e069

File tree

7 files changed

+46
-31
lines changed

7 files changed

+46
-31
lines changed

packages/component-meta/lib/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function baseCreate(
142142
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost);
143143
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost);
144144
const globalTypesName = `${commandLine.vueOptions.lib}_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`;
145-
const globalTypesContents = vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates);
145+
const globalTypesContents = vue.generateGlobalTypes('global', commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates);
146146
const globalTypesSnapshot: ts.IScriptSnapshot = {
147147
getText: (start, end) => globalTypesContents.substring(start, end),
148148
getLength: () => globalTypesContents.length,

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

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { getSlotsPropertyName } from '../utils/shared';
2+
import { endOfLine, newLine } from './common';
23

3-
export function generateGlobalTypes(lib: string, target: number, strictTemplates: boolean) {
4+
export function generateGlobalTypes(mode: 'global' | 'local', lib: string, target: number, strictTemplates: boolean) {
45
const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${strictTemplates ? '' : ' & Record<string, unknown>'}`;
5-
return `// @ts-nocheck
66

7-
const __VLS_globalComponents = { ...{} as import('${lib}').GlobalComponents };
7+
let str = '';
88

9-
declare const __VLS_intrinsicElements: __VLS_IntrinsicElements;
10-
declare const __VLS_directiveBindingRestFields = { instance: null, oldValue: null, modifiers: null as any, dir: null as any };
9+
if (mode === 'global') {
10+
str += `// @ts-nocheck${newLine}`;
11+
str += `export {}${endOfLine}`;
12+
str += `declare module '${lib}' {${newLine}`;
13+
str += ` export interface GlobalComponents { }${newLine}`;
14+
str += `}${newLine}`;
15+
str += `declare global {${newLine}`;
16+
}
17+
18+
str += `
19+
const __VLS_intrinsicElements: __VLS_IntrinsicElements;
20+
const __VLS_directiveBindingRestFields = { instance: null, oldValue: null, modifiers: null as any, dir: null as any };
1121
1222
type __VLS_IntrinsicElements = ${(
1323
target >= 3.3
@@ -21,8 +31,8 @@ type __VLS_Element = ${(
2131
)}
2232
type __VLS_GlobalComponents = ${(
2333
target >= 3.5
24-
? `void extends typeof __VLS_globalComponents ? {} : typeof __VLS_globalComponents;`
25-
: `(void extends typeof __VLS_globalComponents ? {} : typeof __VLS_globalComponents) & Pick<typeof import('${lib}'), 'Transition' | 'TransitionGroup' | 'KeepAlive' | 'Suspense' | 'Teleport'>;`
34+
? `import('vue').GlobalComponents;`
35+
: `import('vue').GlobalComponents & Pick<typeof import('${lib}'), 'Transition' | 'TransitionGroup' | 'KeepAlive' | 'Suspense' | 'Teleport'>;`
2636
)}
2737
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
2838
type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
@@ -71,40 +81,40 @@ type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal<
7181
>;
7282
type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {};
7383
74-
declare function __VLS_getVForSourceType(source: number): [number, number, number][];
75-
declare function __VLS_getVForSourceType(source: string): [string, number, number][];
76-
declare function __VLS_getVForSourceType<T extends any[]>(source: T): [
84+
function __VLS_getVForSourceType(source: number): [number, number, number][];
85+
function __VLS_getVForSourceType(source: string): [string, number, number][];
86+
function __VLS_getVForSourceType<T extends any[]>(source: T): [
7787
item: T[number],
7888
key: number,
7989
index: number,
8090
][];
81-
declare function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [
91+
function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [
8292
item: T extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never,
8393
key: number,
8494
index: undefined,
8595
][];
8696
// #3845
87-
declare function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [
97+
function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [
8898
item: number | (Exclude<T, number> extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never),
8999
key: number,
90100
index: undefined,
91101
][];
92-
declare function __VLS_getVForSourceType<T>(source: T): [
102+
function __VLS_getVForSourceType<T>(source: T): [
93103
item: T[keyof T],
94104
key: keyof T,
95105
index: number,
96106
][];
97107
// @ts-ignore
98-
declare function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>;
108+
function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>;
99109
// @ts-ignore
100-
declare function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0];
101-
declare function __VLS_directiveAsFunction<T extends import('${lib}').Directive>(dir: T): T extends (...args: any) => any
110+
function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0];
111+
function __VLS_directiveAsFunction<T extends import('${lib}').Directive>(dir: T): T extends (...args: any) => any
102112
? T | __VLS_unknownDirective
103113
: NonNullable<(T & Record<string, __VLS_unknownDirective>)['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>;
104-
declare function __VLS_withScope<T, K>(ctx: T, scope: K): ctx is T & K;
105-
declare function __VLS_makeOptional<T>(t: T): { [K in keyof T]?: T[K] };
106-
declare function __VLS_nonNullable<T>(t: T): T extends null | undefined ? never : T;
107-
declare function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K):
114+
function __VLS_withScope<T, K>(ctx: T, scope: K): ctx is T & K;
115+
function __VLS_makeOptional<T>(t: T): { [K in keyof T]?: T[K] };
116+
function __VLS_nonNullable<T>(t: T): T extends null | undefined ? never : T;
117+
function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K):
108118
T extends new (...args: any) => any
109119
? (props: ${fnPropsType}, ctx?: any) => __VLS_Element & { __ctx?: {
110120
attrs?: any,
@@ -114,13 +124,18 @@ declare function __VLS_asFunctionalComponent<T, K = T extends new (...args: any)
114124
: T extends () => any ? (props: {}, ctx?: any) => ReturnType<T>
115125
: T extends (...args: any) => any ? T
116126
: (_: {}${strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${strictTemplates ? '' : ' & Record<string, unknown>'} } };
117-
declare function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void;
118-
declare function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
119-
declare function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): NonNullable<__VLS_PickNotAny<
127+
function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void;
128+
function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
129+
function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): NonNullable<__VLS_PickNotAny<
120130
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
121131
, T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
122132
>>;
123-
declare function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
124-
declare function __VLS_tryAsConstant<const T>(t: T): T;
133+
function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
134+
function __VLS_tryAsConstant<const T>(t: T): T;
125135
`;
136+
137+
if (mode === 'global') {
138+
str += `}${newLine}`;
139+
}
140+
return str;
126141
};

packages/language-core/lib/codegen/script/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
143143
}
144144
yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
145145
if (!options.vueCompilerOptions.__setupedGlobalTypes?.()) {
146-
yield generateGlobalTypes(options.vueCompilerOptions.lib, options.vueCompilerOptions.target, options.vueCompilerOptions.strictTemplates);
146+
yield generateGlobalTypes('local', options.vueCompilerOptions.lib, options.vueCompilerOptions.target, options.vueCompilerOptions.strictTemplates);
147147
}
148148

149149
if (options.sfc.scriptSetup) {

packages/language-server/lib/initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function initialize(
5959
const fileExists = project.typescript.languageServiceHost.fileExists.bind(project.typescript.languageServiceHost);
6060
const getScriptSnapshot = project.typescript.languageServiceHost.getScriptSnapshot.bind(project.typescript.languageServiceHost);
6161
const globalTypesName = `${vueCompilerOptions.lib}_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
62-
const globalTypesContents = generateGlobalTypes(vueCompilerOptions.lib, vueCompilerOptions.target, vueCompilerOptions.strictTemplates);
62+
const globalTypesContents = generateGlobalTypes('global', vueCompilerOptions.lib, vueCompilerOptions.target, vueCompilerOptions.strictTemplates);
6363
const globalTypesSnapshot: ts.IScriptSnapshot = {
6464
getText: (start, end) => globalTypesContents.substring(start, end),
6565
getLength: () => globalTypesContents.length,

packages/tsc/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
3535
dir = parentDir;
3636
}
3737
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${vueOptions.lib}_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`);
38-
const globalTypesContents = vue.generateGlobalTypes(vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
38+
const globalTypesContents = vue.generateGlobalTypes('global', vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
3939
ts.sys.writeFile(globalTypesPath, globalTypesContents);
4040
setupedGlobalTypes = true;
4141
} catch { }

packages/tsc/tests/dts.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('vue-tsc-dts', () => {
4545
dir = parentDir;
4646
}
4747
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${vueOptions.lib}_${vueOptions.target}_${vueOptions.strictTemplates}.d.ts`);
48-
const globalTypesContents = vue.generateGlobalTypes(vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
48+
const globalTypesContents = vue.generateGlobalTypes('global', vueOptions.lib, vueOptions.target, vueOptions.strictTemplates);
4949
ts.sys.writeFile(globalTypesPath, globalTypesContents);
5050
setupedGlobalTypes = true;
5151
} catch { }

packages/typescript-plugin/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const plugin: ts.server.PluginModuleFactory = mods => {
7474
dir = parentDir;
7575
}
7676
const globalTypesPath = path.resolve(dir, `node_modules/.vue-global-types/${options.lib}_${options.target}_${options.strictTemplates}.d.ts`);
77-
const globalTypesContents = vue.generateGlobalTypes(options.lib, options.target, options.strictTemplates);
77+
const globalTypesContents = vue.generateGlobalTypes('global', options.lib, options.target, options.strictTemplates);
7878
proj.writeFile(globalTypesPath, globalTypesContents);
7979
setupedProjects.add(proj);
8080
} catch { }

0 commit comments

Comments
 (0)