@vue/language-core 2.0.28 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/index.d.ts +4 -3
  2. package/index.js +4 -3
  3. package/lib/codegen/common.d.ts +2 -1
  4. package/lib/codegen/common.js +16 -5
  5. package/lib/codegen/globalTypes.d.ts +1 -0
  6. package/lib/codegen/globalTypes.js +123 -0
  7. package/lib/codegen/localTypes.d.ts +14 -0
  8. package/lib/codegen/localTypes.js +120 -0
  9. package/lib/codegen/script/component.d.ts +1 -4
  10. package/lib/codegen/script/component.js +112 -63
  11. package/lib/codegen/script/context.d.ts +14 -10
  12. package/lib/codegen/script/context.js +5 -107
  13. package/lib/codegen/script/index.d.ts +3 -2
  14. package/lib/codegen/script/index.js +37 -53
  15. package/lib/codegen/script/internalComponent.d.ts +1 -1
  16. package/lib/codegen/script/internalComponent.js +13 -5
  17. package/lib/codegen/script/scriptSetup.js +144 -62
  18. package/lib/codegen/script/template.d.ts +3 -0
  19. package/lib/codegen/script/template.js +80 -79
  20. package/lib/codegen/template/context.d.ts +6 -1
  21. package/lib/codegen/template/context.js +9 -2
  22. package/lib/codegen/template/element.d.ts +1 -1
  23. package/lib/codegen/template/element.js +214 -92
  24. package/lib/codegen/template/elementChildren.js +1 -0
  25. package/lib/codegen/template/elementDirectives.js +8 -4
  26. package/lib/codegen/template/elementEvents.js +13 -14
  27. package/lib/codegen/template/elementProps.js +43 -22
  28. package/lib/codegen/template/index.d.ts +3 -0
  29. package/lib/codegen/template/index.js +29 -41
  30. package/lib/codegen/template/interpolation.d.ts +1 -1
  31. package/lib/codegen/template/interpolation.js +24 -15
  32. package/lib/codegen/template/objectProperty.d.ts +1 -1
  33. package/lib/codegen/template/objectProperty.js +7 -2
  34. package/lib/codegen/template/styleScopedClasses.d.ts +3 -0
  35. package/lib/codegen/template/styleScopedClasses.js +72 -0
  36. package/lib/codegen/template/templateChild.js +5 -1
  37. package/lib/codegen/types.d.ts +9 -0
  38. package/lib/codegen/types.js +3 -0
  39. package/lib/languagePlugin.d.ts +2 -4
  40. package/lib/languagePlugin.js +4 -57
  41. package/lib/parsers/scriptRanges.d.ts +1 -0
  42. package/lib/parsers/scriptRanges.js +5 -0
  43. package/lib/parsers/scriptSetupRanges.d.ts +17 -3
  44. package/lib/parsers/scriptSetupRanges.js +127 -44
  45. package/lib/plugins/file-md.js +9 -6
  46. package/lib/plugins/vue-root-tags.js +51 -0
  47. package/lib/plugins/vue-tsx.d.ts +40 -3
  48. package/lib/plugins/vue-tsx.js +17 -4
  49. package/lib/plugins.js +2 -0
  50. package/lib/types.d.ts +14 -10
  51. package/lib/utils/findDestructuredProps.js +3 -0
  52. package/lib/utils/parseCssClassNames.js +4 -6
  53. package/lib/utils/parseCssVars.js +5 -7
  54. package/lib/utils/parseSfc.js +4 -1
  55. package/lib/utils/ts.js +8 -2
  56. package/lib/utils/vue2TemplateCompiler.js +1 -1
  57. package/lib/virtualFile/{computedFiles.d.ts → computedEmbeddedCodes.d.ts} +1 -1
  58. package/lib/virtualFile/{computedFiles.js → computedEmbeddedCodes.js} +3 -3
  59. package/lib/virtualFile/computedSfc.d.ts +1 -1
  60. package/lib/virtualFile/computedSfc.js +14 -3
  61. package/lib/virtualFile/vueFile.d.ts +13 -3
  62. package/lib/virtualFile/vueFile.js +17 -9
  63. package/package.json +6 -6
  64. package/lib/codegen/script/globalTypes.d.ts +0 -2
  65. package/lib/codegen/script/globalTypes.js +0 -134
  66. package/lib/codegen/template/objectKey.js +0 -34
  67. package/lib/languageModule.d.ts +0 -5
  68. package/lib/languageModule.js +0 -159
  69. package/lib/plugins/file-dot-setup.js +0 -34
  70. package/lib/virtualFile/computedMappings.d.ts +0 -4
  71. package/lib/virtualFile/computedMappings.js +0 -65
  72. /package/lib/plugins/{file-dot-setup.d.ts → vue-root-tags.d.ts} +0 -0
  73. /package/lib/{codegen/template/objectKey.d.ts → utils/findDestructuredProps.d.ts} +0 -0
package/index.d.ts CHANGED
@@ -1,13 +1,14 @@
1
+ export * from './lib/codegen/globalTypes';
1
2
  export * from './lib/codegen/template';
2
3
  export * from './lib/languagePlugin';
3
4
  export * from './lib/parsers/scriptSetupRanges';
4
5
  export * from './lib/plugins';
5
- export * from './lib/virtualFile/vueFile';
6
6
  export * from './lib/types';
7
- export * from './lib/utils/ts';
8
7
  export * from './lib/utils/parseSfc';
8
+ export * from './lib/utils/ts';
9
+ export * from './lib/virtualFile/vueFile';
9
10
  export * as scriptRanges from './lib/parsers/scriptRanges';
10
- export * from './lib/utils/shared';
11
11
  export { tsCodegen } from './lib/plugins/vue-tsx';
12
+ export * from './lib/utils/shared';
12
13
  export * from '@volar/language-core';
13
14
  export type * as CompilerDOM from '@vue/compiler-dom';
package/index.js CHANGED
@@ -15,17 +15,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.tsCodegen = exports.scriptRanges = void 0;
18
+ __exportStar(require("./lib/codegen/globalTypes"), exports);
18
19
  __exportStar(require("./lib/codegen/template"), exports);
19
20
  __exportStar(require("./lib/languagePlugin"), exports);
20
21
  __exportStar(require("./lib/parsers/scriptSetupRanges"), exports);
21
22
  __exportStar(require("./lib/plugins"), exports);
22
- __exportStar(require("./lib/virtualFile/vueFile"), exports);
23
23
  __exportStar(require("./lib/types"), exports);
24
- __exportStar(require("./lib/utils/ts"), exports);
25
24
  __exportStar(require("./lib/utils/parseSfc"), exports);
25
+ __exportStar(require("./lib/utils/ts"), exports);
26
+ __exportStar(require("./lib/virtualFile/vueFile"), exports);
26
27
  exports.scriptRanges = require("./lib/parsers/scriptRanges");
27
- __exportStar(require("./lib/utils/shared"), exports);
28
28
  var vue_tsx_1 = require("./lib/plugins/vue-tsx");
29
29
  Object.defineProperty(exports, "tsCodegen", { enumerable: true, get: function () { return vue_tsx_1.tsCodegen; } });
30
+ __exportStar(require("./lib/utils/shared"), exports);
30
31
  __exportStar(require("@volar/language-core"), exports);
31
32
  //# sourceMappingURL=index.js.map
@@ -6,6 +6,7 @@ export declare const combineLastMapping: VueCodeInformation;
6
6
  export declare const variableNameRegex: RegExp;
7
7
  export declare function conditionWrapWith(condition: boolean, startOffset: number, endOffset: number, features: VueCodeInformation, ...wrapCodes: Code[]): Generator<Code>;
8
8
  export declare function wrapWith(startOffset: number, endOffset: number, features: VueCodeInformation, ...wrapCodes: Code[]): Generator<Code>;
9
- export declare function collectVars(ts: typeof import('typescript'), node: ts.Node, ast: ts.SourceFile, result: string[]): void;
9
+ export declare function collectVars(ts: typeof import('typescript'), node: ts.Node, ast: ts.SourceFile, results?: string[], includesRest?: boolean): string[];
10
+ export declare function collectIdentifiers(ts: typeof import('typescript'), node: ts.Node, results?: ts.Identifier[], includesRest?: boolean): ts.Identifier[];
10
11
  export declare function createTsAst(ts: typeof import('typescript'), astHolder: any, text: string): ts.SourceFile;
11
12
  export declare function generateSfcBlockSection(block: SfcBlock, start: number, end: number, features: VueCodeInformation): Code;
@@ -4,6 +4,7 @@ exports.variableNameRegex = exports.combineLastMapping = exports.endOfLine = exp
4
4
  exports.conditionWrapWith = conditionWrapWith;
5
5
  exports.wrapWith = wrapWith;
6
6
  exports.collectVars = collectVars;
7
+ exports.collectIdentifiers = collectIdentifiers;
7
8
  exports.createTsAst = createTsAst;
8
9
  exports.generateSfcBlockSection = generateSfcBlockSection;
9
10
  const scriptSetupRanges_1 = require("../parsers/scriptSetupRanges");
@@ -32,25 +33,35 @@ function* wrapWith(startOffset, endOffset, features, ...wrapCodes) {
32
33
  }
33
34
  yield ['', 'template', endOffset, { __combineOffsetMapping: offset }];
34
35
  }
35
- function collectVars(ts, node, ast, result) {
36
+ function collectVars(ts, node, ast, results = [], includesRest = true) {
37
+ const identifiers = collectIdentifiers(ts, node, [], includesRest);
38
+ for (const id of identifiers) {
39
+ results.push((0, scriptSetupRanges_1.getNodeText)(ts, id, ast));
40
+ }
41
+ return results;
42
+ }
43
+ function collectIdentifiers(ts, node, results = [], includesRest = true) {
36
44
  if (ts.isIdentifier(node)) {
37
- result.push((0, scriptSetupRanges_1.getNodeText)(ts, node, ast));
45
+ results.push(node);
38
46
  }
39
47
  else if (ts.isObjectBindingPattern(node)) {
40
48
  for (const el of node.elements) {
41
- collectVars(ts, el.name, ast, result);
49
+ if (includesRest || !el.dotDotDotToken) {
50
+ collectIdentifiers(ts, el.name, results, includesRest);
51
+ }
42
52
  }
43
53
  }
44
54
  else if (ts.isArrayBindingPattern(node)) {
45
55
  for (const el of node.elements) {
46
56
  if (ts.isBindingElement(el)) {
47
- collectVars(ts, el.name, ast, result);
57
+ collectIdentifiers(ts, el.name, results, includesRest);
48
58
  }
49
59
  }
50
60
  }
51
61
  else {
52
- ts.forEachChild(node, node => collectVars(ts, node, ast, result));
62
+ ts.forEachChild(node, node => collectIdentifiers(ts, node, results, includesRest));
53
63
  }
64
+ return results;
54
65
  }
55
66
  function createTsAst(ts, astHolder, text) {
56
67
  if (astHolder.__volar_ast_text !== text) {
@@ -0,0 +1 @@
1
+ export declare function generateGlobalTypes(lib: string, target: number, strictTemplates: boolean): string;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateGlobalTypes = generateGlobalTypes;
4
+ const shared_1 = require("../utils/shared");
5
+ function generateGlobalTypes(lib, target, strictTemplates) {
6
+ const fnPropsType = `(K extends { $props: infer Props } ? Props : any)${strictTemplates ? '' : ' & Record<string, unknown>'}`;
7
+ return `
8
+ const __VLS_globalComponents = { ...{} as import('${lib}').GlobalComponents };
9
+
10
+ declare const __VLS_intrinsicElements: __VLS_IntrinsicElements;
11
+ declare const __VLS_directiveBindingRestFields = { instance: null, oldValue: null, modifiers: null as any, dir: null as any };
12
+
13
+ type __VLS_IntrinsicElements = ${(target >= 3.3
14
+ ? `import('${lib}/jsx-runtime').JSX.IntrinsicElements;`
15
+ : `globalThis.JSX.IntrinsicElements;`)}
16
+ type __VLS_Element = ${(target >= 3.3
17
+ ? `import('${lib}/jsx-runtime').JSX.Element;`
18
+ : `globalThis.JSX.Element;`)}
19
+ type __VLS_GlobalComponents = ${(target >= 3.5
20
+ ? `void extends typeof __VLS_globalComponents ? {} : typeof __VLS_globalComponents;`
21
+ : `(void extends typeof __VLS_globalComponents ? {} : typeof __VLS_globalComponents) & Pick<typeof import('${lib}'), 'Transition' | 'TransitionGroup' | 'KeepAlive' | 'Suspense' | 'Teleport'>;`)}
22
+ type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
23
+ type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
24
+ type __VLS_unknownDirective = (arg1: unknown, arg2: unknown, arg3: unknown, arg4: unknown) => void;
25
+ type __VLS_WithComponent<N0 extends string, LocalComponents, N1 extends string, N2 extends string, N3 extends string> =
26
+ N1 extends keyof LocalComponents ? N1 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N1] } :
27
+ N2 extends keyof LocalComponents ? N2 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N2] } :
28
+ N3 extends keyof LocalComponents ? N3 extends N0 ? Pick<LocalComponents, N0 extends keyof LocalComponents ? N0 : never> : { [K in N0]: LocalComponents[N3] } :
29
+ N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } :
30
+ N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } :
31
+ N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } :
32
+ ${strictTemplates ? '{}' : '{ [K in N0]: unknown }'}
33
+ type __VLS_FunctionalComponentProps<T, K> =
34
+ '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: { props?: infer P } } ? NonNullable<P> : never
35
+ : T extends (props: infer P, ...args: any) => any ? P :
36
+ {};
37
+ type __VLS_IsFunction<T, K> = K extends keyof T
38
+ ? __VLS_IsAny<T[K]> extends false
39
+ ? unknown extends T[K]
40
+ ? false
41
+ : true
42
+ : false
43
+ : false;
44
+ // fix https://github.com/vuejs/language-tools/issues/926
45
+ type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
46
+ type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R
47
+ ? U extends T
48
+ ? never
49
+ : __VLS_OverloadUnionInner<T, Pick<T, keyof T> & U & ((...args: A) => R)> | ((...args: A) => R)
50
+ : never;
51
+ type __VLS_OverloadUnion<T> = Exclude<
52
+ __VLS_OverloadUnionInner<(() => never) & T>,
53
+ T extends () => never ? never : () => never
54
+ >;
55
+ type __VLS_ConstructorOverloads<T> = __VLS_OverloadUnion<T> extends infer F
56
+ ? F extends (event: infer E, ...args: infer A) => any
57
+ ? { [K in E & string]: (...args: A) => void; }
58
+ : never
59
+ : never;
60
+ type __VLS_NormalizeEmits<T> = __VLS_PrettifyGlobal<
61
+ __VLS_UnionToIntersection<
62
+ __VLS_ConstructorOverloads<T> & {
63
+ [K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never
64
+ }
65
+ >
66
+ >;
67
+ type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {};
68
+
69
+ declare function __VLS_getVForSourceType(source: number): [number, number, number][];
70
+ declare function __VLS_getVForSourceType(source: string): [string, number, number][];
71
+ declare function __VLS_getVForSourceType<T extends any[]>(source: T): [
72
+ item: T[number],
73
+ key: number,
74
+ index: number,
75
+ ][];
76
+ declare function __VLS_getVForSourceType<T extends { [Symbol.iterator](): Iterator<any> }>(source: T): [
77
+ item: T extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never,
78
+ key: number,
79
+ index: undefined,
80
+ ][];
81
+ // #3845
82
+ declare function __VLS_getVForSourceType<T extends number | { [Symbol.iterator](): Iterator<any> }>(source: T): [
83
+ item: number | (Exclude<T, number> extends { [Symbol.iterator](): Iterator<infer T1> } ? T1 : never),
84
+ key: number,
85
+ index: undefined,
86
+ ][];
87
+ declare function __VLS_getVForSourceType<T>(source: T): [
88
+ item: T[keyof T],
89
+ key: keyof T,
90
+ index: number,
91
+ ][];
92
+ // @ts-ignore
93
+ declare function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>;
94
+ // @ts-ignore
95
+ declare function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0];
96
+ declare function __VLS_directiveAsFunction<T extends import('${lib}').Directive>(dir: T): T extends (...args: any) => any
97
+ ? T | __VLS_unknownDirective
98
+ : NonNullable<(T & Record<string, __VLS_unknownDirective>)['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>;
99
+ declare function __VLS_withScope<T, K>(ctx: T, scope: K): ctx is T & K;
100
+ declare function __VLS_makeOptional<T>(t: T): { [K in keyof T]?: T[K] };
101
+ declare function __VLS_nonNullable<T>(t: T): T extends null | undefined ? never : T;
102
+ declare function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K):
103
+ T extends new (...args: any) => any
104
+ ? (props: ${fnPropsType}, ctx?: any) => __VLS_Element & { __ctx?: {
105
+ attrs?: any,
106
+ slots?: K extends { ${(0, shared_1.getSlotsPropertyName)(target)}: infer Slots } ? Slots : any,
107
+ emit?: K extends { $emit: infer Emit } ? Emit : any
108
+ } & { props?: ${fnPropsType}; expose?(exposed: K): void; } }
109
+ : T extends () => any ? (props: {}, ctx?: any) => ReturnType<T>
110
+ : T extends (...args: any) => any ? T
111
+ : (_: {}${strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${strictTemplates ? '' : ' & Record<string, unknown>'} } };
112
+ declare function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void;
113
+ declare function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): Parameters<T>['length'] extends 2 ? [any] : [];
114
+ declare function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): NonNullable<__VLS_PickNotAny<
115
+ '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
116
+ , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
117
+ >>;
118
+ declare function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
119
+ declare function __VLS_tryAsConstant<const T>(t: T): T;
120
+ `;
121
+ }
122
+ ;
123
+ //# sourceMappingURL=globalTypes.js.map
@@ -0,0 +1,14 @@
1
+ import type * as ts from 'typescript';
2
+ import { VueCompilerOptions } from '../types';
3
+ export declare function getLocalTypesGenerator(compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions): {
4
+ generate: (names: string[]) => Generator<string, void, unknown>;
5
+ getUsedNames(): Set<string>;
6
+ readonly PrettifyLocal: string;
7
+ readonly OmitKeepDiscriminatedUnion: string;
8
+ readonly WithDefaults: string;
9
+ readonly WithTemplateSlots: string;
10
+ readonly PropsChildren: string;
11
+ readonly TypePropsToOption: string;
12
+ readonly OmitIndexSignature: string;
13
+ readonly PickRefsExpose: string;
14
+ };
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLocalTypesGenerator = getLocalTypesGenerator;
4
+ const shared_1 = require("../utils/shared");
5
+ const common_1 = require("./common");
6
+ function getLocalTypesGenerator(compilerOptions, vueCompilerOptions) {
7
+ const used = new Set();
8
+ const OmitKeepDiscriminatedUnion = defineHelper(`__VLS_OmitKeepDiscriminatedUnion`, () => `
9
+ type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
10
+ ? Pick<T, Exclude<keyof T, K>>
11
+ : never;
12
+ `.trimStart());
13
+ const WithDefaults = defineHelper(`__VLS_WithDefaults`, () => `
14
+ type __VLS_WithDefaults<P, D> = {
15
+ [K in keyof Pick<P, keyof P>]: K extends keyof D
16
+ ? ${PrettifyLocal.name}<P[K] & { default: D[K]}>
17
+ : P[K]
18
+ };
19
+ `.trimStart());
20
+ const PrettifyLocal = defineHelper(`__VLS_PrettifyLocal`, () => `type __VLS_PrettifyLocal<T> = { [K in keyof T]: T[K]; } & {}${common_1.endOfLine}`);
21
+ const WithTemplateSlots = defineHelper(`__VLS_WithTemplateSlots`, () => `
22
+ type __VLS_WithTemplateSlots<T, S> = T & {
23
+ new(): {
24
+ ${(0, shared_1.getSlotsPropertyName)(vueCompilerOptions.target)}: S;
25
+ ${vueCompilerOptions.jsxSlots ? `$props: ${PropsChildren.name}<S>;` : ''}
26
+ }
27
+ };
28
+ `.trimStart());
29
+ const PropsChildren = defineHelper(`__VLS_PropsChildren`, () => `
30
+ type __VLS_PropsChildren<S> = {
31
+ [K in keyof (
32
+ boolean extends (
33
+ // @ts-ignore
34
+ JSX.ElementChildrenAttribute extends never
35
+ ? true
36
+ : false
37
+ )
38
+ ? never
39
+ // @ts-ignore
40
+ : JSX.ElementChildrenAttribute
41
+ )]?: S;
42
+ };
43
+ `.trimStart());
44
+ const TypePropsToOption = defineHelper(`__VLS_TypePropsToOption`, () => compilerOptions.exactOptionalPropertyTypes ?
45
+ `
46
+ type __VLS_TypePropsToOption<T> = {
47
+ [K in keyof T]-?: {} extends Pick<T, K>
48
+ ? { type: import('${vueCompilerOptions.lib}').PropType<T[K]> }
49
+ : { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
50
+ };
51
+ `.trimStart() :
52
+ `
53
+ type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
54
+ type __VLS_TypePropsToOption<T> = {
55
+ [K in keyof T]-?: {} extends Pick<T, K>
56
+ ? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable<T[K]>> }
57
+ : { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
58
+ };
59
+ `.trimStart());
60
+ const OmitIndexSignature = defineHelper(`__VLS_OmitIndexSignature`, () => `type __VLS_OmitIndexSignature<T> = { [K in keyof T as {} extends Record<K, unknown> ? never : K]: T[K]; }${common_1.endOfLine}`);
61
+ const PickRefsExpose = defineHelper(`__VLS_PickRefsExpose`, () => `
62
+ type __VLS_PickRefsExpose<T> = T extends object
63
+ ? { [K in keyof T]: (T[K] extends any[]
64
+ ? Parameters<T[K][0]['expose']>[0][]
65
+ : T[K] extends { expose?: (exposed: infer E) => void }
66
+ ? E
67
+ : T[K]) | null }
68
+ : never;
69
+ `.trimStart());
70
+ const helpers = {
71
+ [PrettifyLocal.name]: PrettifyLocal,
72
+ [OmitKeepDiscriminatedUnion.name]: OmitKeepDiscriminatedUnion,
73
+ [WithDefaults.name]: WithDefaults,
74
+ [WithTemplateSlots.name]: WithTemplateSlots,
75
+ [PropsChildren.name]: PropsChildren,
76
+ [TypePropsToOption.name]: TypePropsToOption,
77
+ [OmitIndexSignature.name]: OmitIndexSignature,
78
+ [PickRefsExpose.name]: PickRefsExpose,
79
+ };
80
+ used.clear();
81
+ return {
82
+ generate,
83
+ getUsedNames() {
84
+ return used;
85
+ },
86
+ get PrettifyLocal() { return PrettifyLocal.name; },
87
+ get OmitKeepDiscriminatedUnion() { return OmitKeepDiscriminatedUnion.name; },
88
+ get WithDefaults() { return WithDefaults.name; },
89
+ get WithTemplateSlots() { return WithTemplateSlots.name; },
90
+ get PropsChildren() { return PropsChildren.name; },
91
+ get TypePropsToOption() { return TypePropsToOption.name; },
92
+ get OmitIndexSignature() { return OmitIndexSignature.name; },
93
+ get PickRefsExpose() { return PickRefsExpose.name; },
94
+ };
95
+ function* generate(names) {
96
+ const generated = new Set();
97
+ while (names.length) {
98
+ used.clear();
99
+ for (const name of names) {
100
+ if (generated.has(name)) {
101
+ continue;
102
+ }
103
+ const helper = helpers[name];
104
+ yield helper.generate();
105
+ generated.add(name);
106
+ }
107
+ names = [...used].filter(name => !generated.has(name));
108
+ }
109
+ }
110
+ function defineHelper(name, generate) {
111
+ return {
112
+ get name() {
113
+ used.add(name);
114
+ return name;
115
+ },
116
+ generate,
117
+ };
118
+ }
119
+ }
120
+ //# sourceMappingURL=localTypes.js.map
@@ -1,11 +1,8 @@
1
- import type { ScriptRanges } from '../../parsers/scriptRanges';
2
1
  import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
3
2
  import type { Code, Sfc } from '../../types';
4
3
  import type { ScriptCodegenContext } from './context';
5
4
  import { ScriptCodegenOptions } from './index';
6
5
  export declare function generateComponent(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
7
6
  export declare function generateComponentSetupReturns(scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
8
- export declare function generateScriptOptions(script: NonNullable<Sfc['script']>, scriptRanges: ScriptRanges): Generator<Code>;
9
- export declare function generateScriptSetupOptions(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
10
- export declare function generatePropsOption(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges): Generator<Code, void, unknown>;
11
7
  export declare function generateEmitsOption(options: ScriptCodegenOptions, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
8
+ export declare function generatePropsOption(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges, hasEmitsOption: boolean, inheritAttrs: boolean): Generator<Code>;
@@ -2,10 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateComponent = generateComponent;
4
4
  exports.generateComponentSetupReturns = generateComponentSetupReturns;
5
- exports.generateScriptOptions = generateScriptOptions;
6
- exports.generateScriptSetupOptions = generateScriptSetupOptions;
7
- exports.generatePropsOption = generatePropsOption;
8
5
  exports.generateEmitsOption = generateEmitsOption;
6
+ exports.generatePropsOption = generatePropsOption;
9
7
  const common_1 = require("../common");
10
8
  const index_1 = require("./index");
11
9
  function* generateComponent(options, ctx, scriptSetup, scriptSetupRanges) {
@@ -28,10 +26,18 @@ function* generateComponent(options, ctx, scriptSetup, scriptSetupRanges) {
28
26
  yield `}${common_1.endOfLine}`;
29
27
  yield `},${common_1.newLine}`;
30
28
  if (!ctx.bypassDefineComponent) {
31
- yield* generateScriptSetupOptions(options, ctx, scriptSetup, scriptSetupRanges);
29
+ const emitOptionCodes = [...generateEmitsOption(options, scriptSetup, scriptSetupRanges)];
30
+ for (const code of emitOptionCodes) {
31
+ yield code;
32
+ }
33
+ yield* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges, !!emitOptionCodes.length, true);
32
34
  }
33
- if (options.sfc.script && options.scriptRanges) {
34
- yield* generateScriptOptions(options.sfc.script, options.scriptRanges);
35
+ if (options.sfc.script && options.scriptRanges?.exportDefault?.args) {
36
+ const { args } = options.scriptRanges.exportDefault;
37
+ yield (0, common_1.generateSfcBlockSection)(options.sfc.script, args.start + 1, args.end - 1, index_1.codeFeatures.all);
38
+ }
39
+ if (options.vueCompilerOptions.target >= 3.5 && scriptSetupRanges.templateRefs.length) {
40
+ yield `__typeRefs: {} as __VLS_Refs,${common_1.newLine}`;
35
41
  }
36
42
  yield `})`;
37
43
  }
@@ -47,78 +53,121 @@ function* generateComponentSetupReturns(scriptSetupRanges) {
47
53
  yield `$emit: ${scriptSetupRanges.emits.name ?? '__VLS_emit'},${common_1.newLine}`;
48
54
  }
49
55
  }
50
- function* generateScriptOptions(script, scriptRanges) {
51
- if (scriptRanges.exportDefault?.args) {
52
- yield (0, common_1.generateSfcBlockSection)(script, scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1, index_1.codeFeatures.all);
56
+ function* generateEmitsOption(options, scriptSetup, scriptSetupRanges) {
57
+ const codes = [];
58
+ if (scriptSetupRanges.defineProp.some(p => p.isModel)) {
59
+ codes.push({
60
+ optionExp: `{} as __VLS_NormalizeEmits<__VLS_ModelEmitsType>`,
61
+ typeOptionType: `__VLS_ModelEmitsType`,
62
+ });
53
63
  }
54
- }
55
- function* generateScriptSetupOptions(options, ctx, scriptSetup, scriptSetupRanges) {
56
- yield* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges);
57
- yield* generateEmitsOption(options, scriptSetup, scriptSetupRanges);
58
- }
59
- function* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges) {
60
- if (options.vueCompilerOptions.target >= 3.5 && ctx.generatedPropsType) {
61
- yield `__typeProps: {} as __VLS_PublicProps,${common_1.newLine}`;
62
- }
63
- if (options.vueCompilerOptions.target < 3.5 || !ctx.generatedPropsType || scriptSetupRanges.props.withDefaults) {
64
- const codegens = [];
65
- if (ctx.generatedPropsType) {
66
- codegens.push(function* () {
67
- yield `{} as `;
68
- if (scriptSetupRanges.props.withDefaults?.arg) {
69
- yield `${ctx.helperTypes.WithDefaults.name}<`;
70
- }
71
- yield `${ctx.helperTypes.TypePropsToOption.name}<`;
72
- yield `__VLS_PublicProps>`;
73
- if (scriptSetupRanges.props.withDefaults?.arg) {
74
- yield `, typeof __VLS_withDefaultsArg>`;
75
- }
76
- });
77
- }
78
- if (scriptSetupRanges.props.define?.arg) {
79
- const { arg } = scriptSetupRanges.props.define;
80
- codegens.push(function* () {
81
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, index_1.codeFeatures.navigation);
82
- });
64
+ if (scriptSetupRanges.emits.define) {
65
+ const { typeArg, hasUnionTypeArg } = scriptSetupRanges.emits.define;
66
+ codes.push({
67
+ optionExp: `{} as __VLS_NormalizeEmits<typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'}>`,
68
+ typeOptionType: typeArg && !hasUnionTypeArg
69
+ ? scriptSetup.content.slice(typeArg.start, typeArg.end)
70
+ : undefined,
71
+ });
72
+ }
73
+ if (options.vueCompilerOptions.target >= 3.5 && codes.every(code => code.typeOptionType)) {
74
+ if (codes.length === 1) {
75
+ yield `__typeEmits: {} as `;
76
+ yield codes[0].typeOptionType;
77
+ yield `,${common_1.newLine}`;
83
78
  }
84
- if (codegens.length === 1) {
85
- yield `props: `;
86
- for (const generate of codegens) {
87
- yield* generate();
79
+ else if (codes.length >= 2) {
80
+ yield `__typeEmits: {} as `;
81
+ yield codes[0].typeOptionType;
82
+ for (let i = 1; i < codes.length; i++) {
83
+ yield ` & `;
84
+ yield codes[i].typeOptionType;
88
85
  }
89
86
  yield `,${common_1.newLine}`;
90
87
  }
91
- else if (codegens.length >= 2) {
92
- yield `props: {${common_1.newLine}`;
93
- for (const generate of codegens) {
88
+ }
89
+ else if (codes.every(code => code.optionExp)) {
90
+ if (codes.length === 1) {
91
+ yield `emits: `;
92
+ yield codes[0].optionExp;
93
+ yield `,${common_1.newLine}`;
94
+ }
95
+ else if (codes.length >= 2) {
96
+ yield `emits: {${common_1.newLine}`;
97
+ for (const code of codes) {
94
98
  yield `...`;
95
- yield* generate();
99
+ yield code.optionExp;
96
100
  yield `,${common_1.newLine}`;
97
101
  }
98
102
  yield `},${common_1.newLine}`;
99
103
  }
100
104
  }
101
105
  }
102
- function* generateEmitsOption(options, scriptSetup, scriptSetupRanges) {
103
- if (!scriptSetupRanges.emits.define && !scriptSetupRanges.defineProp.some(p => p.isModel)) {
104
- return;
105
- }
106
- if (options.vueCompilerOptions.target < 3.5 || scriptSetupRanges.emits.define?.arg || scriptSetupRanges.emits.define?.hasUnionTypeArg) {
107
- yield `emits: ({} as __VLS_NormalizeEmits<__VLS_ModelEmitsType`;
108
- if (scriptSetupRanges?.emits.define) {
109
- yield ` & typeof `;
110
- yield scriptSetupRanges.emits.name ?? '__VLS_emit';
106
+ function* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges, hasEmitsOption, inheritAttrs) {
107
+ const optionExpCodes = [];
108
+ const typeOptionExpCodes = [];
109
+ if (ctx.generatedPropsType) {
110
+ optionExpCodes.push([
111
+ `{} as `,
112
+ scriptSetupRanges.props.withDefaults?.arg ? `${ctx.localTypes.WithDefaults}<` : '',
113
+ `${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`,
114
+ scriptSetupRanges.props.withDefaults?.arg ? `, typeof __VLS_withDefaultsArg>` : '',
115
+ ].join(''));
116
+ typeOptionExpCodes.push(`{} as __VLS_PublicProps`);
117
+ }
118
+ if (scriptSetupRanges.props.define?.arg) {
119
+ const { arg } = scriptSetupRanges.props.define;
120
+ optionExpCodes.push((0, common_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, index_1.codeFeatures.navigation));
121
+ }
122
+ if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size) {
123
+ let attrsType = `typeof __VLS_templateResult['attrs']`;
124
+ if (hasEmitsOption) {
125
+ attrsType = `Omit<${attrsType}, \`on\${string}\`>`;
111
126
  }
112
- yield `>),${common_1.newLine}`;
127
+ const propsType = `__VLS_PickNotAny<${ctx.localTypes.OmitIndexSignature}<${attrsType}>, {}>`;
128
+ const optionType = `${ctx.localTypes.TypePropsToOption}<${propsType}>`;
129
+ if (optionExpCodes.length) {
130
+ optionExpCodes.unshift(`{} as ${optionType}`);
131
+ }
132
+ else {
133
+ // workaround for https://github.com/vuejs/core/pull/7419
134
+ optionExpCodes.unshift(`{} as keyof ${propsType} extends never ? never: ${optionType}`);
135
+ }
136
+ typeOptionExpCodes.unshift(`{} as ${attrsType}`);
113
137
  }
114
- else {
115
- yield `__typeEmits: {} as __VLS_ModelEmitsType`;
116
- const typeArg = scriptSetupRanges.emits.define?.typeArg;
117
- if (typeArg) {
118
- yield ` & `;
119
- yield scriptSetup.content.slice(typeArg.start, typeArg.end);
138
+ const useTypeOption = options.vueCompilerOptions.target >= 3.5 && typeOptionExpCodes.length;
139
+ const useOption = (!useTypeOption || scriptSetupRanges.props.withDefaults) && optionExpCodes.length;
140
+ if (useTypeOption) {
141
+ if (typeOptionExpCodes.length === 1) {
142
+ yield `__typeProps: `;
143
+ yield typeOptionExpCodes[0];
144
+ yield `,${common_1.newLine}`;
145
+ }
146
+ else if (typeOptionExpCodes.length >= 2) {
147
+ yield `__typeProps: {${common_1.newLine}`;
148
+ for (const code of typeOptionExpCodes) {
149
+ yield `...`;
150
+ yield code;
151
+ yield `,${common_1.newLine}`;
152
+ }
153
+ yield `},${common_1.newLine}`;
154
+ }
155
+ }
156
+ if (useOption) {
157
+ if (optionExpCodes.length === 1) {
158
+ yield `props: `;
159
+ yield optionExpCodes[0];
160
+ yield `,${common_1.newLine}`;
161
+ }
162
+ else if (optionExpCodes.length >= 2) {
163
+ yield `props: {${common_1.newLine}`;
164
+ for (const code of optionExpCodes) {
165
+ yield `...`;
166
+ yield code;
167
+ yield `,${common_1.newLine}`;
168
+ }
169
+ yield `},${common_1.newLine}`;
120
170
  }
121
- yield `,${common_1.newLine}`;
122
171
  }
123
172
  }
124
173
  //# sourceMappingURL=component.js.map
@@ -1,5 +1,6 @@
1
+ import { InlayHintInfo } from '../types';
1
2
  import type { ScriptCodegenOptions } from './index';
2
- interface HelperType {
3
+ export interface HelperType {
3
4
  name: string;
4
5
  used?: boolean;
5
6
  generated?: boolean;
@@ -12,14 +13,17 @@ export declare function createScriptCodegenContext(options: ScriptCodegenOptions
12
13
  scriptSetupGeneratedOffset: number | undefined;
13
14
  bypassDefineComponent: boolean;
14
15
  bindingNames: Set<string>;
15
- helperTypes: {
16
- OmitKeepDiscriminatedUnion: HelperType;
17
- WithDefaults: HelperType;
18
- Prettify: HelperType;
19
- WithTemplateSlots: HelperType;
20
- PropsChildren: HelperType;
21
- TypePropsToOption: HelperType;
16
+ localTypes: {
17
+ generate: (names: string[]) => Generator<string, void, unknown>;
18
+ getUsedNames(): Set<string>;
19
+ readonly PrettifyLocal: string;
20
+ readonly OmitKeepDiscriminatedUnion: string;
21
+ readonly WithDefaults: string;
22
+ readonly WithTemplateSlots: string;
23
+ readonly PropsChildren: string;
24
+ readonly TypePropsToOption: string;
25
+ readonly OmitIndexSignature: string;
26
+ readonly PickRefsExpose: string;
22
27
  };
23
- generateHelperTypes: () => Generator<string, void, unknown>;
28
+ inlayHints: InlayHintInfo[];
24
29
  };
25
- export {};