@vue/language-core 2.1.6 → 2.1.8

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 (49) hide show
  1. package/lib/codegen/common.d.ts +2 -2
  2. package/lib/codegen/common.js +8 -10
  3. package/lib/codegen/globalTypes.js +13 -8
  4. package/lib/codegen/inlayHints.d.ts +11 -0
  5. package/lib/codegen/inlayHints.js +17 -0
  6. package/lib/codegen/script/component.d.ts +1 -1
  7. package/lib/codegen/script/component.js +8 -5
  8. package/lib/codegen/script/componentSelf.js +1 -1
  9. package/lib/codegen/script/context.d.ts +1 -1
  10. package/lib/codegen/script/index.d.ts +1 -0
  11. package/lib/codegen/script/index.js +23 -10
  12. package/lib/codegen/script/scriptSetup.js +103 -112
  13. package/lib/codegen/script/styleModulesType.js +3 -5
  14. package/lib/codegen/script/template.d.ts +1 -1
  15. package/lib/codegen/script/template.js +9 -12
  16. package/lib/codegen/template/context.d.ts +3 -2
  17. package/lib/codegen/template/context.js +1 -0
  18. package/lib/codegen/template/element.d.ts +1 -1
  19. package/lib/codegen/template/element.js +31 -11
  20. package/lib/codegen/template/elementDirectives.js +63 -31
  21. package/lib/codegen/template/elementProps.js +7 -17
  22. package/lib/codegen/template/index.d.ts +1 -0
  23. package/lib/codegen/template/index.js +63 -53
  24. package/lib/codegen/template/interpolation.d.ts +1 -1
  25. package/lib/codegen/template/interpolation.js +34 -28
  26. package/lib/codegen/template/slotOutlet.js +5 -0
  27. package/lib/codegen/template/templateChild.d.ts +1 -1
  28. package/lib/codegen/template/templateChild.js +6 -2
  29. package/lib/codegen/template/vFor.js +1 -1
  30. package/lib/parsers/scriptSetupRanges.d.ts +14 -4
  31. package/lib/parsers/scriptSetupRanges.js +39 -26
  32. package/lib/plugins/vue-tsx.d.ts +27 -16
  33. package/lib/plugins/vue-tsx.js +43 -31
  34. package/lib/types.d.ts +3 -1
  35. package/lib/utils/parseCssClassNames.d.ts +1 -1
  36. package/lib/utils/parseCssClassNames.js +5 -4
  37. package/lib/utils/parseCssVars.d.ts +3 -2
  38. package/lib/utils/parseCssVars.js +12 -11
  39. package/lib/utils/ts.d.ts +1 -1
  40. package/lib/utils/ts.js +3 -5
  41. package/lib/virtualFile/computedEmbeddedCodes.d.ts +2 -1
  42. package/lib/virtualFile/computedEmbeddedCodes.js +11 -11
  43. package/lib/virtualFile/computedSfc.d.ts +2 -1
  44. package/lib/virtualFile/computedSfc.js +77 -76
  45. package/lib/virtualFile/computedVueSfc.d.ts +2 -1
  46. package/lib/virtualFile/computedVueSfc.js +8 -8
  47. package/lib/virtualFile/vueFile.d.ts +6 -7
  48. package/lib/virtualFile/vueFile.js +13 -12
  49. package/package.json +9 -8
@@ -6,7 +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, results?: string[], includesRest?: boolean): string[];
10
- export declare function collectIdentifiers(ts: typeof import('typescript'), node: ts.Node, results?: ts.Identifier[], includesRest?: boolean): ts.Identifier[];
9
+ export declare function collectVars(ts: typeof import('typescript'), node: ts.Node, ast: ts.SourceFile, results?: string[]): string[];
10
+ export declare function collectIdentifiers(ts: typeof import('typescript'), node: ts.Node, results?: [id: ts.Identifier, isRest: boolean][], isRest?: boolean): [id: ts.Identifier, isRest: boolean][];
11
11
  export declare function createTsAst(ts: typeof import('typescript'), astHolder: any, text: string): ts.SourceFile;
12
12
  export declare function generateSfcBlockSection(block: SfcBlock, start: number, end: number, features: VueCodeInformation): Code;
@@ -33,33 +33,31 @@ function* wrapWith(startOffset, endOffset, features, ...wrapCodes) {
33
33
  }
34
34
  yield ['', 'template', endOffset, { __combineOffsetMapping: offset }];
35
35
  }
36
- function collectVars(ts, node, ast, results = [], includesRest = true) {
37
- const identifiers = collectIdentifiers(ts, node, [], includesRest);
38
- for (const id of identifiers) {
36
+ function collectVars(ts, node, ast, results = []) {
37
+ const identifiers = collectIdentifiers(ts, node, []);
38
+ for (const [id] of identifiers) {
39
39
  results.push((0, scriptSetupRanges_1.getNodeText)(ts, id, ast));
40
40
  }
41
41
  return results;
42
42
  }
43
- function collectIdentifiers(ts, node, results = [], includesRest = true) {
43
+ function collectIdentifiers(ts, node, results = [], isRest = false) {
44
44
  if (ts.isIdentifier(node)) {
45
- results.push(node);
45
+ results.push([node, isRest]);
46
46
  }
47
47
  else if (ts.isObjectBindingPattern(node)) {
48
48
  for (const el of node.elements) {
49
- if (includesRest || !el.dotDotDotToken) {
50
- collectIdentifiers(ts, el.name, results, includesRest);
51
- }
49
+ collectIdentifiers(ts, el.name, results, !!el.dotDotDotToken);
52
50
  }
53
51
  }
54
52
  else if (ts.isArrayBindingPattern(node)) {
55
53
  for (const el of node.elements) {
56
54
  if (ts.isBindingElement(el)) {
57
- collectIdentifiers(ts, el.name, results, includesRest);
55
+ collectIdentifiers(ts, el.name, results, !!el.dotDotDotToken);
58
56
  }
59
57
  }
60
58
  }
61
59
  else {
62
- ts.forEachChild(node, node => collectIdentifiers(ts, node, results, includesRest));
60
+ ts.forEachChild(node, node => collectIdentifiers(ts, node, results, false));
63
61
  }
64
62
  return results;
65
63
  }
@@ -79,11 +79,18 @@ function generateGlobalTypes(lib, target, strictTemplates) {
79
79
  >
80
80
  >;
81
81
  type __VLS_PrettifyGlobal<T> = { [K in keyof T]: T[K]; } & {};
82
+ type __VLS_PickFunctionalComponentCtx<T, K> = NonNullable<__VLS_PickNotAny<
83
+ '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
84
+ , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
85
+ >>;
86
+ type __VLS_UseTemplateRef<T> = Readonly<import('${lib}').ShallowRef<T | null>>;
82
87
 
83
88
  function __VLS_getVForSourceType(source: number): [number, number, number][];
84
89
  function __VLS_getVForSourceType(source: string): [string, number, number][];
85
90
  function __VLS_getVForSourceType<T extends any[]>(source: T): [
86
- item: T[number],
91
+ item: ${(target >= 3 ? `import('${lib}').Reactive<T[number]>` :
92
+ target >= 2.7 ? `import('${lib}').UnwrapNestedRefs<T[number]>` :
93
+ `T[number]`)},
87
94
  key: number,
88
95
  index: number,
89
96
  ][];
@@ -107,9 +114,11 @@ function generateGlobalTypes(lib, target, strictTemplates) {
107
114
  function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>;
108
115
  // @ts-ignore
109
116
  function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0];
110
- function __VLS_directiveAsFunction<T extends import('${lib}').Directive>(dir: T): T extends (...args: any) => any
111
- ? T | __VLS_unknownDirective
112
- : NonNullable<(T & Record<string, __VLS_unknownDirective>)['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>;
117
+ function __VLS_asFunctionalDirective<T>(dir: T): T extends import('${lib}').ObjectDirective
118
+ ? NonNullable<T['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>
119
+ : T extends (...args: any) => any
120
+ ? T
121
+ : __VLS_unknownDirective;
113
122
  function __VLS_withScope<T, K>(ctx: T, scope: K): ctx is T & K;
114
123
  function __VLS_makeOptional<T>(t: T): { [K in keyof T]?: T[K] };
115
124
  function __VLS_nonNullable<T>(t: T): T extends null | undefined ? never : T;
@@ -125,10 +134,6 @@ function generateGlobalTypes(lib, target, strictTemplates) {
125
134
  : (_: {}${strictTemplates ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${strictTemplates ? '' : ' & Record<string, unknown>'} } };
126
135
  function __VLS_elementAsFunction<T>(tag: T, endTag?: T): (_: T${strictTemplates ? '' : ' & Record<string, unknown>'}) => void;
127
136
  function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
128
- function __VLS_pickFunctionalComponentCtx<T, K>(comp: T, compInstance: K): NonNullable<__VLS_PickNotAny<
129
- '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
130
- , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
131
- >>;
132
137
  function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S;
133
138
  function __VLS_tryAsConstant<const T>(t: T): T;
134
139
  }
@@ -0,0 +1,11 @@
1
+ import type * as CompilerDOM from '@vue/compiler-dom';
2
+ export interface InlayHintInfo {
3
+ blockName: string;
4
+ offset: number;
5
+ setting: string;
6
+ label: string;
7
+ tooltip?: string;
8
+ paddingRight?: boolean;
9
+ paddingLeft?: boolean;
10
+ }
11
+ export declare function createVBindShorthandInlayHintInfo(loc: CompilerDOM.SourceLocation, variableName: string): InlayHintInfo;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createVBindShorthandInlayHintInfo = createVBindShorthandInlayHintInfo;
4
+ function createVBindShorthandInlayHintInfo(loc, variableName) {
5
+ return {
6
+ blockName: 'template',
7
+ offset: loc.end.offset,
8
+ setting: 'vue.inlayHints.vBindShorthand',
9
+ label: `="${variableName}"`,
10
+ tooltip: [
11
+ `This is a shorthand for \`${loc.source}="${variableName}"\`.`,
12
+ 'To hide this hint, set `vue.inlayHints.vBindShorthand` to `false` in IDE settings.',
13
+ '[More info](https://github.com/vuejs/core/pull/9451)',
14
+ ].join('\n\n'),
15
+ };
16
+ }
17
+ //# sourceMappingURL=inlayHints.js.map
@@ -4,5 +4,5 @@ import type { ScriptCodegenContext } from './context';
4
4
  import { ScriptCodegenOptions } from './index';
5
5
  export declare function generateComponent(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
6
6
  export declare function generateComponentSetupReturns(scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
7
- export declare function generateEmitsOption(options: ScriptCodegenOptions, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
7
+ export declare function generateEmitsOption(options: ScriptCodegenOptions, scriptSetupRanges: ScriptSetupRanges): Generator<Code>;
8
8
  export declare function generatePropsOption(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, scriptSetup: NonNullable<Sfc['scriptSetup']>, scriptSetupRanges: ScriptSetupRanges, hasEmitsOption: boolean, inheritAttrs: boolean): Generator<Code>;
@@ -26,7 +26,7 @@ function* generateComponent(options, ctx, scriptSetup, scriptSetupRanges) {
26
26
  yield `}${common_1.endOfLine}`;
27
27
  yield `},${common_1.newLine}`;
28
28
  if (!ctx.bypassDefineComponent) {
29
- const emitOptionCodes = [...generateEmitsOption(options, scriptSetup, scriptSetupRanges)];
29
+ const emitOptionCodes = [...generateEmitsOption(options, scriptSetupRanges)];
30
30
  for (const code of emitOptionCodes) {
31
31
  yield code;
32
32
  }
@@ -39,6 +39,9 @@ function* generateComponent(options, ctx, scriptSetup, scriptSetupRanges) {
39
39
  if (options.vueCompilerOptions.target >= 3.5 && scriptSetupRanges.templateRefs.length) {
40
40
  yield `__typeRefs: {} as __VLS_TemplateResult['refs'],${common_1.newLine}`;
41
41
  }
42
+ if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.singleRootElType) {
43
+ yield `__typeEl: {} as __VLS_TemplateResult['rootEl'],${common_1.newLine}`;
44
+ }
42
45
  yield `})`;
43
46
  }
44
47
  function* generateComponentSetupReturns(scriptSetupRanges) {
@@ -53,12 +56,12 @@ function* generateComponentSetupReturns(scriptSetupRanges) {
53
56
  yield `$emit: ${scriptSetupRanges.emits.name ?? '__VLS_emit'},${common_1.newLine}`;
54
57
  }
55
58
  }
56
- function* generateEmitsOption(options, scriptSetup, scriptSetupRanges) {
59
+ function* generateEmitsOption(options, scriptSetupRanges) {
57
60
  const codes = [];
58
61
  if (scriptSetupRanges.defineProp.some(p => p.isModel)) {
59
62
  codes.push({
60
- optionExp: `{} as __VLS_NormalizeEmits<__VLS_ModelEmitsType>`,
61
- typeOptionType: `__VLS_ModelEmitsType`,
63
+ optionExp: `{} as __VLS_NormalizeEmits<typeof __VLS_modelEmit>`,
64
+ typeOptionType: `__VLS_ModelEmit`,
62
65
  });
63
66
  }
64
67
  if (scriptSetupRanges.emits.define) {
@@ -66,7 +69,7 @@ function* generateEmitsOption(options, scriptSetup, scriptSetupRanges) {
66
69
  codes.push({
67
70
  optionExp: `{} as __VLS_NormalizeEmits<typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'}>`,
68
71
  typeOptionType: typeArg && !hasUnionTypeArg
69
- ? scriptSetup.content.slice(typeArg.start, typeArg.end)
72
+ ? `__VLS_Emit`
70
73
  : undefined,
71
74
  });
72
75
  }
@@ -41,7 +41,7 @@ function* generateComponentSelf(options, ctx, templateCodegenCtx) {
41
41
  yield `}${common_1.endOfLine}`; // return {
42
42
  yield `},${common_1.newLine}`; // setup() {
43
43
  if (options.sfc.scriptSetup && options.scriptSetupRanges && !ctx.bypassDefineComponent) {
44
- const emitOptionCodes = [...(0, component_1.generateEmitsOption)(options, options.sfc.scriptSetup, options.scriptSetupRanges)];
44
+ const emitOptionCodes = [...(0, component_1.generateEmitsOption)(options, options.scriptSetupRanges)];
45
45
  for (const code of emitOptionCodes) {
46
46
  yield code;
47
47
  }
@@ -1,4 +1,4 @@
1
- import { InlayHintInfo } from '../types';
1
+ import { InlayHintInfo } from '../inlayHints';
2
2
  import type { ScriptCodegenOptions } from './index';
3
3
  export interface HelperType {
4
4
  name: string;
@@ -30,3 +30,4 @@ export interface ScriptCodegenOptions {
30
30
  linkedCodeMappings: Mapping[];
31
31
  }
32
32
  export declare function generateScript(options: ScriptCodegenOptions): Generator<Code, ScriptCodegenContext>;
33
+ export declare function generateScriptSectionPartiallyEnding(source: string, end: number, mark: string): Generator<Code>;
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.codeFeatures = void 0;
4
4
  exports.generateScript = generateScript;
5
+ exports.generateScriptSectionPartiallyEnding = generateScriptSectionPartiallyEnding;
5
6
  const common_1 = require("../common");
6
7
  const globalTypes_1 = require("../globalTypes");
7
- const context_1 = require("./context");
8
8
  const componentSelf_1 = require("./componentSelf");
9
+ const context_1 = require("./context");
9
10
  const scriptSetup_1 = require("./scriptSetup");
10
11
  const src_1 = require("./src");
11
12
  const styleModulesType_1 = require("./styleModulesType");
@@ -35,7 +36,13 @@ exports.codeFeatures = {
35
36
  function* generateScript(options) {
36
37
  const ctx = (0, context_1.createScriptCodegenContext)(options);
37
38
  if (options.vueCompilerOptions.__setupedGlobalTypes) {
38
- yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${common_1.newLine}`;
39
+ const globalTypes = options.vueCompilerOptions.__setupedGlobalTypes;
40
+ if (typeof globalTypes === 'object') {
41
+ yield `/// <reference types="${globalTypes.absolutePath}" />${common_1.newLine}`;
42
+ }
43
+ else {
44
+ yield `/// <reference types=".vue-global-types/${options.vueCompilerOptions.lib}_${options.vueCompilerOptions.target}_${options.vueCompilerOptions.strictTemplates}.d.ts" />${common_1.newLine}`;
45
+ }
39
46
  }
40
47
  else {
41
48
  yield `/* placeholder */`;
@@ -57,6 +64,7 @@ function* generateScript(options) {
57
64
  }
58
65
  else {
59
66
  yield (0, common_1.generateSfcBlockSection)(options.sfc.script, 0, options.sfc.script.content.length, exports.codeFeatures.all);
67
+ yield* generateScriptSectionPartiallyEnding(options.sfc.script.name, options.sfc.script.content.length, '#3632/both.vue');
60
68
  yield* (0, scriptSetup_1.generateScriptSetup)(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
61
69
  }
62
70
  }
@@ -92,10 +100,10 @@ function* generateScript(options) {
92
100
  }
93
101
  else {
94
102
  yield (0, common_1.generateSfcBlockSection)(options.sfc.script, 0, classBlockEnd, exports.codeFeatures.all);
95
- yield `__VLS_template = () => {`;
96
- const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx, true);
103
+ yield `__VLS_template = () => {${common_1.newLine}`;
104
+ const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
97
105
  yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
98
- yield `},${common_1.newLine}`;
106
+ yield `}${common_1.endOfLine}`;
99
107
  yield (0, common_1.generateSfcBlockSection)(options.sfc.script, classBlockEnd, options.sfc.script.content.length, exports.codeFeatures.all);
100
108
  }
101
109
  }
@@ -108,15 +116,15 @@ function* generateScript(options) {
108
116
  yield* generateDefineProp(options, options.sfc.scriptSetup);
109
117
  yield* (0, scriptSetup_1.generateScriptSetup)(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
110
118
  }
111
- yield `;`;
119
+ if (options.sfc.script) {
120
+ yield* generateScriptSectionPartiallyEnding(options.sfc.script.name, options.sfc.script.content.length, '#3632/script.vue');
121
+ }
112
122
  if (options.sfc.scriptSetup) {
113
- // #4569
114
- yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, exports.codeFeatures.verification];
123
+ yield* generateScriptSectionPartiallyEnding(options.sfc.scriptSetup.name, options.sfc.scriptSetup.content.length, '#4569/main.vue');
115
124
  }
116
- yield common_1.newLine;
117
125
  if (!ctx.generatedTemplate) {
118
126
  yield `function __VLS_template() {${common_1.newLine}`;
119
- const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx, false);
127
+ const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
120
128
  yield `}${common_1.endOfLine}`;
121
129
  yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
122
130
  }
@@ -134,6 +142,11 @@ function* generateScript(options) {
134
142
  }
135
143
  return ctx;
136
144
  }
145
+ function* generateScriptSectionPartiallyEnding(source, end, mark) {
146
+ yield `;`;
147
+ yield ['', source, end, exports.codeFeatures.verification];
148
+ yield `/* PartiallyEnd: ${mark} */${common_1.newLine}`;
149
+ }
137
150
  function* generateDefineProp(options, scriptSetup) {
138
151
  const definePropProposalA = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition') || options.vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition';
139
152
  const definePropProposalB = scriptSetup.content.trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition') || options.vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition';
@@ -4,8 +4,8 @@ exports.generateScriptSetupImports = generateScriptSetupImports;
4
4
  exports.generateScriptSetup = generateScriptSetup;
5
5
  const common_1 = require("../common");
6
6
  const component_1 = require("./component");
7
- const index_1 = require("./index");
8
7
  const componentSelf_1 = require("./componentSelf");
8
+ const index_1 = require("./index");
9
9
  const template_1 = require("./template");
10
10
  function* generateScriptSetupImports(scriptSetup, scriptSetupRanges) {
11
11
  yield [
@@ -52,10 +52,10 @@ function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
52
52
  emitTypes.push(`typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'}`);
53
53
  }
54
54
  if (scriptSetupRanges.defineProp.some(p => p.isModel)) {
55
- emitTypes.push(`__VLS_ModelEmitsType`);
55
+ emitTypes.push(`typeof __VLS_modelEmit`);
56
56
  }
57
57
  yield ` return {} as {${common_1.newLine}`
58
- + ` props: ${ctx.localTypes.PrettifyLocal}<typeof __VLS_functionalComponentProps & __VLS_PublicProps> & __VLS_BuiltInPublicProps,${common_1.newLine}`
58
+ + ` props: ${ctx.localTypes.PrettifyLocal}<typeof __VLS_functionalComponentProps & __VLS_TemplateResult['attrs'] & __VLS_PublicProps> & __VLS_BuiltInPublicProps,${common_1.newLine}`
59
59
  + ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.expose.define ? 'typeof __VLS_exposed' : '{}'}>): void,${common_1.newLine}`
60
60
  + ` attrs: any,${common_1.newLine}`
61
61
  + ` slots: __VLS_TemplateResult['slots'],${common_1.newLine}`
@@ -106,45 +106,8 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
106
106
  }
107
107
  ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;
108
108
  let setupCodeModifies = [];
109
- const propsRange = scriptSetupRanges.props.withDefaults ?? scriptSetupRanges.props.define;
110
- if (propsRange && scriptSetupRanges.props.define) {
111
- const statement = scriptSetupRanges.props.define.statement;
112
- if (scriptSetupRanges.props.define.typeArg) {
113
- setupCodeModifies.push([[
114
- `let __VLS_typeProps!: `,
115
- (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.define.typeArg.start, scriptSetupRanges.props.define.typeArg.end, index_1.codeFeatures.all),
116
- common_1.endOfLine,
117
- ], statement.start, statement.start]);
118
- setupCodeModifies.push([[`typeof __VLS_typeProps`], scriptSetupRanges.props.define.typeArg.start, scriptSetupRanges.props.define.typeArg.end]);
119
- }
120
- if (!scriptSetupRanges.props.name) {
121
- if (statement.start === propsRange.start && statement.end === propsRange.end) {
122
- setupCodeModifies.push([[`const __VLS_props = `], propsRange.start, propsRange.start]);
123
- }
124
- else {
125
- if (scriptSetupRanges.props.define.typeArg) {
126
- setupCodeModifies.push([[
127
- `const __VLS_props = `,
128
- (0, common_1.generateSfcBlockSection)(scriptSetup, propsRange.start, scriptSetupRanges.props.define.typeArg.start, index_1.codeFeatures.all),
129
- ], statement.start, scriptSetupRanges.props.define.typeArg.start]);
130
- setupCodeModifies.push([[
131
- (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.define.typeArg.end, propsRange.end, index_1.codeFeatures.all),
132
- `${common_1.endOfLine}`,
133
- (0, common_1.generateSfcBlockSection)(scriptSetup, statement.start, propsRange.start, index_1.codeFeatures.all),
134
- `__VLS_props`,
135
- ], scriptSetupRanges.props.define.typeArg.end, propsRange.end]);
136
- }
137
- else {
138
- setupCodeModifies.push([[
139
- `const __VLS_props = `,
140
- (0, common_1.generateSfcBlockSection)(scriptSetup, propsRange.start, propsRange.end, index_1.codeFeatures.all),
141
- `${common_1.endOfLine}`,
142
- (0, common_1.generateSfcBlockSection)(scriptSetup, statement.start, propsRange.start, index_1.codeFeatures.all),
143
- `__VLS_props`,
144
- ], statement.start, propsRange.end]);
145
- }
146
- }
147
- }
109
+ if (scriptSetupRanges.props.define) {
110
+ setupCodeModifies.push(...generateDefineWithType(scriptSetup, scriptSetupRanges.props.name, scriptSetupRanges.props.define, scriptSetupRanges.props.withDefaults ?? scriptSetupRanges.props.define, '__VLS_props', '__VLS_Props'));
148
111
  }
149
112
  if (scriptSetupRanges.slots.define) {
150
113
  if (scriptSetupRanges.slots.isObjectBindingPattern) {
@@ -158,8 +121,8 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
158
121
  setupCodeModifies.push([[`const __VLS_slots = `], scriptSetupRanges.slots.define.start, scriptSetupRanges.slots.define.start]);
159
122
  }
160
123
  }
161
- if (scriptSetupRanges.emits.define && !scriptSetupRanges.emits.name) {
162
- setupCodeModifies.push([[`const __VLS_emit = `], scriptSetupRanges.emits.define.start, scriptSetupRanges.emits.define.start]);
124
+ if (scriptSetupRanges.emits.define) {
125
+ setupCodeModifies.push(...generateDefineWithType(scriptSetup, scriptSetupRanges.emits.name, scriptSetupRanges.emits.define, scriptSetupRanges.emits.define, '__VLS_emit', '__VLS_Emit'));
163
126
  }
164
127
  if (scriptSetupRanges.expose.define) {
165
128
  if (scriptSetupRanges.expose.define?.typeArg) {
@@ -193,66 +156,67 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
193
156
  }
194
157
  }
195
158
  if (scriptSetupRanges.cssModules.length) {
196
- for (const { exp, arg } of scriptSetupRanges.cssModules) {
197
- if (arg) {
198
- setupCodeModifies.push([
199
- [
200
- ` as Omit<__VLS_StyleModules, '$style'>[`,
201
- (0, common_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, index_1.codeFeatures.all),
202
- `]`
203
- ],
204
- exp.end,
205
- exp.end
206
- ]);
207
- }
208
- else {
209
- setupCodeModifies.push([
210
- [
211
- ` as __VLS_StyleModules[`,
212
- ['', scriptSetup.name, exp.start, index_1.codeFeatures.verification],
213
- `'$style'`,
214
- ['', scriptSetup.name, exp.end, index_1.codeFeatures.verification],
215
- `]`
216
- ],
217
- exp.end,
218
- exp.end
219
- ]);
220
- }
159
+ for (const { define: { arg, exp } } of scriptSetupRanges.cssModules) {
160
+ setupCodeModifies.push([
161
+ arg ? [
162
+ ` as Omit<__VLS_StyleModules, '$style'>[`,
163
+ (0, common_1.generateSfcBlockSection)(scriptSetup, arg.start, arg.end, index_1.codeFeatures.all),
164
+ `]`
165
+ ] : [
166
+ ` as __VLS_StyleModules[`,
167
+ ['', scriptSetup.name, exp.start, index_1.codeFeatures.verification],
168
+ `'$style'`,
169
+ ['', scriptSetup.name, exp.end, index_1.codeFeatures.verification],
170
+ `]`
171
+ ],
172
+ exp.end,
173
+ exp.end
174
+ ]);
221
175
  }
222
176
  }
177
+ const isTs = options.lang !== 'js' && options.lang !== 'jsx';
223
178
  for (const { define } of scriptSetupRanges.templateRefs) {
224
- if (define?.arg) {
179
+ if (!define?.arg) {
180
+ continue;
181
+ }
182
+ if (isTs) {
225
183
  setupCodeModifies.push([
226
184
  [
227
185
  `<__VLS_TemplateResult['refs'][`,
228
186
  (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.navigation),
229
187
  `], keyof __VLS_TemplateResult['refs']>`
230
188
  ],
231
- define.arg.start - 1,
232
- define.arg.start - 1
189
+ define.exp.end,
190
+ define.exp.end
191
+ ]);
192
+ }
193
+ else {
194
+ setupCodeModifies.push([
195
+ [`(`],
196
+ define.start,
197
+ define.start
198
+ ], [
199
+ [
200
+ ` as __VLS_UseTemplateRef<__VLS_TemplateResult['refs'][`,
201
+ (0, common_1.generateSfcBlockSection)(scriptSetup, define.arg.start, define.arg.end, index_1.codeFeatures.navigation),
202
+ `]>)`
203
+ ],
204
+ define.end,
205
+ define.end
233
206
  ]);
234
207
  }
235
208
  }
236
209
  setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);
237
- if (setupCodeModifies.length) {
238
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1], index_1.codeFeatures.all);
239
- while (setupCodeModifies.length) {
240
- const [codes, _start, end] = setupCodeModifies.shift();
241
- for (const code of codes) {
242
- yield code;
243
- }
244
- if (setupCodeModifies.length) {
245
- const nextStart = setupCodeModifies[0][1];
246
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, end, nextStart, index_1.codeFeatures.all);
247
- }
248
- else {
249
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, end, scriptSetup.content.length, index_1.codeFeatures.all);
250
- }
210
+ let nextStart = scriptSetupRanges.importSectionEndOffset;
211
+ for (const [codes, start, end] of setupCodeModifies) {
212
+ yield (0, common_1.generateSfcBlockSection)(scriptSetup, nextStart, start, index_1.codeFeatures.all);
213
+ for (const code of codes) {
214
+ yield code;
251
215
  }
216
+ nextStart = end;
252
217
  }
253
- else {
254
- yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.importSectionEndOffset, scriptSetup.content.length, index_1.codeFeatures.all);
255
- }
218
+ yield (0, common_1.generateSfcBlockSection)(scriptSetup, nextStart, scriptSetup.content.length, index_1.codeFeatures.all);
219
+ yield* (0, index_1.generateScriptSectionPartiallyEnding)(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');
256
220
  if (scriptSetupRanges.props.define?.typeArg && scriptSetupRanges.props.withDefaults?.arg) {
257
221
  // fix https://github.com/vuejs/language-tools/issues/1187
258
222
  yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
@@ -260,9 +224,9 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
260
224
  yield `)${common_1.endOfLine}`;
261
225
  }
262
226
  yield* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors);
263
- yield* generateModelEmits(options, scriptSetup, scriptSetupRanges);
227
+ yield* generateModelEmit(scriptSetup, scriptSetupRanges);
264
228
  yield `function __VLS_template() {${common_1.newLine}`;
265
- const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx, false);
229
+ const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
266
230
  yield `}${common_1.endOfLine}`;
267
231
  yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
268
232
  yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${common_1.endOfLine}`;
@@ -281,6 +245,43 @@ function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, sy
281
245
  }
282
246
  }
283
247
  }
248
+ function* generateDefineWithType(scriptSetup, name, define, expression, defaultName, typeName) {
249
+ const { statement, typeArg } = define;
250
+ if (typeArg) {
251
+ yield [[
252
+ `type ${typeName} = `,
253
+ (0, common_1.generateSfcBlockSection)(scriptSetup, typeArg.start, typeArg.end, index_1.codeFeatures.all),
254
+ common_1.endOfLine,
255
+ ], statement.start, statement.start];
256
+ yield [[typeName], typeArg.start, typeArg.end];
257
+ }
258
+ if (!name) {
259
+ if (statement.start === expression.start && statement.end === expression.end) {
260
+ yield [[`const ${defaultName} = `], expression.start, expression.start];
261
+ }
262
+ else if (typeArg) {
263
+ yield [[
264
+ `const ${defaultName} = `,
265
+ (0, common_1.generateSfcBlockSection)(scriptSetup, expression.start, typeArg.start, index_1.codeFeatures.all)
266
+ ], statement.start, typeArg.start];
267
+ yield [[
268
+ (0, common_1.generateSfcBlockSection)(scriptSetup, typeArg.end, expression.end, index_1.codeFeatures.all),
269
+ common_1.endOfLine,
270
+ (0, common_1.generateSfcBlockSection)(scriptSetup, statement.start, expression.start, index_1.codeFeatures.all),
271
+ defaultName
272
+ ], typeArg.end, expression.end];
273
+ }
274
+ else {
275
+ yield [[
276
+ `const ${defaultName} = `,
277
+ (0, common_1.generateSfcBlockSection)(scriptSetup, expression.start, expression.end, index_1.codeFeatures.all),
278
+ common_1.endOfLine,
279
+ (0, common_1.generateSfcBlockSection)(scriptSetup, statement.start, expression.start, index_1.codeFeatures.all),
280
+ defaultName
281
+ ], statement.start, expression.end];
282
+ }
283
+ }
284
+ }
284
285
  function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, definePropMirrors) {
285
286
  yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${common_1.newLine}`;
286
287
  if (scriptSetupRanges.props.define?.arg) {
@@ -288,7 +289,7 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
288
289
  yield (0, common_1.generateSfcBlockSection)(scriptSetup, scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end, index_1.codeFeatures.navigation);
289
290
  yield `,${common_1.newLine}`;
290
291
  }
291
- yield* (0, component_1.generateEmitsOption)(options, scriptSetup, scriptSetupRanges);
292
+ yield* (0, component_1.generateEmitsOption)(options, scriptSetupRanges);
292
293
  yield `})${common_1.endOfLine}`;
293
294
  yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
294
295
  ? `import('${options.vueCompilerOptions.lib}').PublicProps;`
@@ -374,35 +375,25 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges, d
374
375
  yield ` & `;
375
376
  }
376
377
  ctx.generatedPropsType = true;
377
- yield `typeof __VLS_typeProps`;
378
+ yield `__VLS_Props`;
378
379
  }
379
380
  if (!ctx.generatedPropsType) {
380
381
  yield `{}`;
381
382
  }
382
383
  yield common_1.endOfLine;
383
384
  }
384
- function* generateModelEmits(options, scriptSetup, scriptSetupRanges) {
385
+ function* generateModelEmit(scriptSetup, scriptSetupRanges) {
385
386
  const defineModels = scriptSetupRanges.defineProp.filter(p => p.isModel);
386
387
  if (defineModels.length) {
387
- const generateDefineModels = function* () {
388
- for (const defineModel of defineModels) {
389
- const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
390
- yield `'update:${propName}': [${propName}:`;
391
- yield* generateDefinePropType(scriptSetup, propName, localName, defineModel);
392
- yield `]${common_1.endOfLine}`;
393
- }
394
- };
395
- if (options.vueCompilerOptions.target >= 3.5) {
396
- yield `type __VLS_ModelEmitsType = {${common_1.newLine}`;
397
- yield* generateDefineModels();
398
- yield `}${common_1.endOfLine}`;
399
- }
400
- else {
401
- yield `const __VLS_modelEmitsType = (await import('${options.vueCompilerOptions.lib}')).defineEmits<{${common_1.newLine}`;
402
- yield* generateDefineModels();
403
- yield `}>()${common_1.endOfLine}`;
404
- yield `type __VLS_ModelEmitsType = typeof __VLS_modelEmitsType${common_1.endOfLine}`;
388
+ yield `type __VLS_ModelEmit = {${common_1.newLine}`;
389
+ for (const defineModel of defineModels) {
390
+ const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
391
+ yield `'update:${propName}': [value:`;
392
+ yield* generateDefinePropType(scriptSetup, propName, localName, defineModel);
393
+ yield `]${common_1.endOfLine}`;
405
394
  }
395
+ yield `}${common_1.endOfLine}`;
396
+ yield `const __VLS_modelEmit = defineEmits<__VLS_ModelEmit>()${common_1.endOfLine}`;
406
397
  }
407
398
  }
408
399
  function* generateDefinePropType(scriptSetup, propName, localName, defineProp) {
@@ -5,13 +5,12 @@ const index_1 = require("./index");
5
5
  const template_1 = require("./template");
6
6
  const common_1 = require("../common");
7
7
  function* generateStyleModulesType(options, ctx) {
8
- const styles = options.sfc.styles.filter(style => style.module);
8
+ const styles = options.sfc.styles.map((style, i) => [style, i]).filter(([style]) => style.module);
9
9
  if (!styles.length) {
10
10
  return;
11
11
  }
12
12
  yield `type __VLS_StyleModules = {${common_1.newLine}`;
13
- for (let i = 0; i < styles.length; i++) {
14
- const style = styles[i];
13
+ for (const [style, i] of styles) {
15
14
  const { name, offset } = style.module;
16
15
  if (offset) {
17
16
  yield [
@@ -30,7 +29,6 @@ function* generateStyleModulesType(options, ctx) {
30
29
  }
31
30
  yield `>${common_1.endOfLine}`;
32
31
  }
33
- yield `}`;
34
- yield common_1.endOfLine;
32
+ yield `}${common_1.endOfLine}`;
35
33
  }
36
34
  //# sourceMappingURL=styleModulesType.js.map
@@ -3,6 +3,6 @@ import { TemplateCodegenContext } from '../template/context';
3
3
  import type { ScriptCodegenContext } from './context';
4
4
  import { type ScriptCodegenOptions } from './index';
5
5
  export declare function generateTemplateDirectives(options: ScriptCodegenOptions): Generator<Code>;
6
- export declare function generateTemplate(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, isClassComponent: boolean): Generator<Code, TemplateCodegenContext>;
6
+ export declare function generateTemplate(options: ScriptCodegenOptions, ctx: ScriptCodegenContext): Generator<Code, TemplateCodegenContext>;
7
7
  export declare function generateCssClassProperty(styleIndex: number, classNameWithDot: string, offset: number, propertyType: string, optional: boolean): Generator<Code>;
8
8
  export declare function getTemplateUsageVars(options: ScriptCodegenOptions, ctx: ScriptCodegenContext): Set<string>;