@vue/language-core 2.2.8 → 3.0.0-alpha.2

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 (47) hide show
  1. package/index.d.ts +1 -2
  2. package/index.js +1 -1
  3. package/lib/codegen/globalTypes.js +3 -9
  4. package/lib/codegen/script/component.js +21 -9
  5. package/lib/codegen/script/index.d.ts +0 -1
  6. package/lib/codegen/script/index.js +1 -4
  7. package/lib/codegen/script/scriptSetup.js +1 -10
  8. package/lib/codegen/script/src.js +1 -20
  9. package/lib/codegen/script/styleModulesType.d.ts +1 -1
  10. package/lib/codegen/script/styleModulesType.js +2 -2
  11. package/lib/codegen/script/template.js +4 -1
  12. package/lib/codegen/template/context.d.ts +26 -17
  13. package/lib/codegen/template/context.js +89 -53
  14. package/lib/codegen/template/element.d.ts +2 -2
  15. package/lib/codegen/template/element.js +24 -57
  16. package/lib/codegen/template/elementChildren.d.ts +1 -1
  17. package/lib/codegen/template/elementChildren.js +4 -6
  18. package/lib/codegen/template/elementDirectives.js +1 -11
  19. package/lib/codegen/template/elementEvents.d.ts +1 -0
  20. package/lib/codegen/template/elementEvents.js +39 -17
  21. package/lib/codegen/template/elementProps.js +4 -8
  22. package/lib/codegen/template/index.d.ts +0 -1
  23. package/lib/codegen/template/index.js +22 -10
  24. package/lib/codegen/template/interpolation.d.ts +1 -1
  25. package/lib/codegen/template/interpolation.js +53 -53
  26. package/lib/codegen/template/slotOutlet.js +1 -2
  27. package/lib/codegen/template/styleScopedClasses.js +2 -2
  28. package/lib/codegen/template/templateChild.d.ts +1 -1
  29. package/lib/codegen/template/templateChild.js +12 -46
  30. package/lib/codegen/template/vFor.js +5 -10
  31. package/lib/codegen/template/vIf.js +2 -10
  32. package/lib/codegen/template/vSlot.d.ts +1 -2
  33. package/lib/codegen/template/vSlot.js +84 -62
  34. package/lib/codegen/utils/index.js +2 -2
  35. package/lib/parsers/scriptRanges.js +7 -3
  36. package/lib/parsers/scriptSetupRanges.d.ts +0 -3
  37. package/lib/parsers/scriptSetupRanges.js +18 -28
  38. package/lib/plugins/vue-tsx.d.ts +24 -15
  39. package/lib/plugins/vue-tsx.js +31 -23
  40. package/lib/types.d.ts +2 -2
  41. package/lib/utils/forEachElementNode.d.ts +1 -0
  42. package/lib/utils/forEachElementNode.js +3 -0
  43. package/lib/utils/shared.d.ts +5 -1
  44. package/lib/utils/shared.js +16 -4
  45. package/lib/utils/ts.d.ts +0 -4
  46. package/lib/utils/ts.js +1 -10
  47. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -5,9 +5,8 @@ export * from './lib/parsers/scriptSetupRanges';
5
5
  export * from './lib/plugins';
6
6
  export * from './lib/types';
7
7
  export * from './lib/utils/parseSfc';
8
+ export * from './lib/utils/shared';
8
9
  export * from './lib/utils/ts';
9
10
  export * from './lib/virtualFile/vueFile';
10
11
  export { tsCodegen } from './lib/plugins/vue-tsx';
11
- export * from './lib/utils/shared';
12
12
  export * from '@volar/language-core';
13
- export type * as CompilerDOM from '@vue/compiler-dom';
package/index.js CHANGED
@@ -22,10 +22,10 @@ __exportStar(require("./lib/parsers/scriptSetupRanges"), exports);
22
22
  __exportStar(require("./lib/plugins"), exports);
23
23
  __exportStar(require("./lib/types"), exports);
24
24
  __exportStar(require("./lib/utils/parseSfc"), exports);
25
+ __exportStar(require("./lib/utils/shared"), exports);
25
26
  __exportStar(require("./lib/utils/ts"), exports);
26
27
  __exportStar(require("./lib/virtualFile/vueFile"), exports);
27
28
  var vue_tsx_1 = require("./lib/plugins/vue-tsx");
28
29
  Object.defineProperty(exports, "tsCodegen", { enumerable: true, get: function () { return vue_tsx_1.tsCodegen; } });
29
- __exportStar(require("./lib/utils/shared"), exports);
30
30
  __exportStar(require("@volar/language-core"), exports);
31
31
  //# sourceMappingURL=index.js.map
@@ -24,7 +24,6 @@ function generateGlobalTypes({ lib, target, checkUnknownProps, checkUnknownEvent
24
24
  }
25
25
  text += `
26
26
  ; declare global {
27
- const __VLS_intrinsicElements: __VLS_IntrinsicElements;
28
27
  const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any };
29
28
  const __VLS_unref: typeof import('${lib}').unref;
30
29
  const __VLS_placeholder: any;
@@ -100,9 +99,6 @@ function generateGlobalTypes({ lib, target, checkUnknownProps, checkUnknownEvent
100
99
  '__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any
101
100
  , T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
102
101
  >>;
103
- type __VLS_OmitStringIndex<T> = {
104
- [K in keyof T as string extends K ? never : K]: T[K];
105
- };
106
102
  type __VLS_UseTemplateRef<T> = Readonly<import('${lib}').ShallowRef<T | null>>;
107
103
 
108
104
  function __VLS_getVForSourceType<T extends number | string | any[] | Iterable<any>>(source: T): [
@@ -118,10 +114,8 @@ function generateGlobalTypes({ lib, target, checkUnknownProps, checkUnknownEvent
118
114
  key: keyof T,
119
115
  index: number,
120
116
  ][];
121
- // @ts-ignore
122
- function __VLS_getSlotParams<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>;
123
- // @ts-ignore
124
- function __VLS_getSlotParam<T>(slot: T): Parameters<__VLS_PickNotAny<NonNullable<T>, (...args: any[]) => any>>[0];
117
+ function __VLS_getSlotParameters<S, D extends S>(slot: S, decl?: D):
118
+ __VLS_PickNotAny<NonNullable<D>, (...args: any) => any> extends (...args: infer P) => any ? P : any[];
125
119
  function __VLS_asFunctionalDirective<T>(dir: T): T extends import('${lib}').ObjectDirective
126
120
  ? NonNullable<T['created' | 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated' | 'beforeUnmount' | 'unmounted']>
127
121
  : T extends (...args: any) => any
@@ -144,7 +138,7 @@ function generateGlobalTypes({ lib, target, checkUnknownProps, checkUnknownEvent
144
138
  : (_: {}${checkUnknownProps ? '' : ' & Record<string, unknown>'}, ctx?: any) => { __ctx?: { attrs?: any, expose?: any, slots?: any, emit?: any, props?: {}${checkUnknownProps ? '' : ' & Record<string, unknown>'} } };
145
139
  function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
146
140
  function __VLS_asFunctionalElement<T>(tag: T, endTag?: T): (attrs: T${checkUnknownComponents ? '' : ' & Record<string, unknown>'}) => void;
147
- function __VLS_asFunctionalSlot<S>(slot: S): (props: NonNullable<S> extends (props: infer P) => any ? P : {}) => void;
141
+ function __VLS_asFunctionalSlot<S>(slot: S): S extends () => infer R ? (props: {}) => R : NonNullable<S>;
148
142
  function __VLS_tryAsConstant<const T>(t: T): T;
149
143
  }
150
144
  `;
@@ -111,15 +111,23 @@ function* generateEmitsOption(options, scriptSetupRanges) {
111
111
  function* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges, hasEmitsOption, inheritAttrs) {
112
112
  const codes = [];
113
113
  if (ctx.generatedPropsType) {
114
- codes.push({
115
- optionExp: [
116
- `{} as `,
117
- scriptSetupRanges.withDefaults?.arg ? `${ctx.localTypes.WithDefaults}<` : '',
118
- `${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`,
119
- scriptSetupRanges.withDefaults?.arg ? `, typeof __VLS_withDefaultsArg>` : '',
120
- ].join(''),
121
- typeOptionExp: `{} as __VLS_PublicProps`,
122
- });
114
+ if (options.vueCompilerOptions.target >= 3.6) {
115
+ codes.push({
116
+ optionExp: '{}',
117
+ typeOptionExp: `{} as __VLS_PublicProps`,
118
+ });
119
+ }
120
+ else {
121
+ codes.push({
122
+ optionExp: [
123
+ `{} as `,
124
+ scriptSetupRanges.withDefaults?.arg ? `${ctx.localTypes.WithDefaults}<` : '',
125
+ `${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`,
126
+ scriptSetupRanges.withDefaults?.arg ? `, typeof __VLS_withDefaultsArg>` : '',
127
+ ].join(''),
128
+ typeOptionExp: `{} as __VLS_PublicProps`,
129
+ });
130
+ }
123
131
  }
124
132
  if (scriptSetupRanges.defineProps?.arg) {
125
133
  const { arg } = scriptSetupRanges.defineProps;
@@ -146,6 +154,10 @@ function* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges, hasE
146
154
  const useTypeOption = options.vueCompilerOptions.target >= 3.5 && codes.every(code => code.typeOptionExp);
147
155
  const useOption = !useTypeOption || scriptSetupRanges.withDefaults;
148
156
  if (useTypeOption) {
157
+ if (options.vueCompilerOptions.target >= 3.6
158
+ && scriptSetupRanges.withDefaults?.arg) {
159
+ yield `__defaults: __VLS_withDefaultsArg,${utils_1.newLine}`;
160
+ }
149
161
  if (codes.length === 1) {
150
162
  yield `__typeProps: `;
151
163
  yield codes[0].typeOptionExp;
@@ -9,7 +9,6 @@ export interface ScriptCodegenOptions {
9
9
  compilerOptions: ts.CompilerOptions;
10
10
  vueCompilerOptions: VueCompilerOptions;
11
11
  sfc: Sfc;
12
- edited: boolean;
13
12
  fileName: string;
14
13
  lang: string;
15
14
  scriptRanges: ScriptRanges | undefined;
@@ -27,7 +27,7 @@ function* generateScript(options) {
27
27
  }
28
28
  }
29
29
  else {
30
- yield `/* placeholder */`;
30
+ yield `/// <reference path="./__VLS_fake.d.ts" />`;
31
31
  }
32
32
  if (options.sfc.script?.src) {
33
33
  yield* (0, src_1.generateSrc)(options.sfc.script.src);
@@ -105,9 +105,6 @@ function* generateScript(options) {
105
105
  const templateCodegenCtx = yield* (0, template_1.generateTemplate)(options, ctx);
106
106
  yield* (0, componentSelf_1.generateComponentSelf)(options, ctx, templateCodegenCtx);
107
107
  }
108
- if (options.edited) {
109
- yield `type __VLS_IntrinsicElementsCompletion = __VLS_IntrinsicElements${utils_1.endOfLine}`;
110
- }
111
108
  yield* ctx.localTypes.generate([...ctx.localTypes.getUsedNames()]);
112
109
  if (options.appendGlobalTypes) {
113
110
  yield (0, globalTypes_1.generateGlobalTypes)(options.vueCompilerOptions);
@@ -78,15 +78,6 @@ function* generateScriptSetup(options, ctx, scriptSetup, scriptSetupRanges) {
78
78
  }
79
79
  function* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, syntax) {
80
80
  let setupCodeModifies = [];
81
- for (const { comments } of scriptSetupRanges.defineProp) {
82
- if (comments) {
83
- setupCodeModifies.push([
84
- [``],
85
- comments.start,
86
- comments.end,
87
- ]);
88
- }
89
- }
90
81
  if (scriptSetupRanges.defineProps) {
91
82
  const { name, statement, callExp, typeArg } = scriptSetupRanges.defineProps;
92
83
  setupCodeModifies.push(...generateDefineWithType(scriptSetup, statement, scriptSetupRanges.withDefaults?.callExp ?? callExp, typeArg, name, `__VLS_props`, `__VLS_Props`));
@@ -405,7 +396,7 @@ function* generateComponentProps(options, ctx, scriptSetup, scriptSetupRanges) {
405
396
  for (const defineProp of scriptSetupRanges.defineProp) {
406
397
  const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);
407
398
  if (defineProp.comments) {
408
- yield (0, utils_1.generateSfcBlockSection)(scriptSetup, defineProp.comments.start, defineProp.comments.end, codeFeatures_1.codeFeatures.all);
399
+ yield scriptSetup.content.slice(defineProp.comments.start, defineProp.comments.end);
409
400
  yield utils_1.newLine;
410
401
  }
411
402
  if (defineProp.isModel && !defineProp.name) {
@@ -23,26 +23,7 @@ function* generateSrc(src) {
23
23
  yield `export * from `;
24
24
  yield* (0, utils_1.generateSfcBlockAttrValue)(src, text, {
25
25
  ...codeFeatures_1.codeFeatures.all,
26
- navigation: text === src.text
27
- ? true
28
- : {
29
- shouldRename: () => false,
30
- resolveRenameEditText(newName) {
31
- if (newName.endsWith('.jsx') || newName.endsWith('.js')) {
32
- newName = newName.split('.').slice(0, -1).join('.');
33
- }
34
- if (src?.text.endsWith('.d.ts')) {
35
- newName = newName + '.d.ts';
36
- }
37
- else if (src?.text.endsWith('.ts')) {
38
- newName = newName + '.ts';
39
- }
40
- else if (src?.text.endsWith('.tsx')) {
41
- newName = newName + '.tsx';
42
- }
43
- return newName;
44
- },
45
- },
26
+ ...text === src.text ? codeFeatures_1.codeFeatures.navigation : codeFeatures_1.codeFeatures.navigationWithoutRename,
46
27
  });
47
28
  yield utils_1.endOfLine;
48
29
  yield `export { default } from '${text}'${utils_1.endOfLine}`;
@@ -1,4 +1,4 @@
1
1
  import type { Code } from '../../types';
2
2
  import type { ScriptCodegenContext } from './context';
3
- import { ScriptCodegenOptions } from './index';
3
+ import type { ScriptCodegenOptions } from './index';
4
4
  export declare function generateStyleModulesType(options: ScriptCodegenOptions, ctx: ScriptCodegenContext): Generator<Code>;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateStyleModulesType = generateStyleModulesType;
4
+ const codeFeatures_1 = require("../codeFeatures");
4
5
  const utils_1 = require("../utils");
5
- const index_1 = require("./index");
6
6
  const template_1 = require("./template");
7
7
  function* generateStyleModulesType(options, ctx) {
8
8
  const styles = options.sfc.styles.map((style, i) => [style, i]).filter(([style]) => style.module);
@@ -17,7 +17,7 @@ function* generateStyleModulesType(options, ctx) {
17
17
  name,
18
18
  'main',
19
19
  offset + 1,
20
- index_1.codeFeatures.all
20
+ codeFeatures_1.codeFeatures.all
21
21
  ];
22
22
  }
23
23
  else {
@@ -15,9 +15,9 @@ function* generateTemplate(options, ctx) {
15
15
  ctx.generatedTemplate = true;
16
16
  const templateCodegenCtx = (0, context_1.createTemplateCodegenContext)({
17
17
  scriptSetupBindingNames: new Set(),
18
- edited: options.edited,
19
18
  });
20
19
  yield* generateTemplateCtx(options);
20
+ yield* generateTemplateElements();
21
21
  yield* generateTemplateComponents(options);
22
22
  yield* generateTemplateDirectives(options);
23
23
  yield* generateTemplateBody(options, templateCodegenCtx);
@@ -47,6 +47,9 @@ function* generateTemplateCtx(options) {
47
47
  yield `}${utils_1.endOfLine}`;
48
48
  }
49
49
  }
50
+ function* generateTemplateElements() {
51
+ yield `let __VLS_elements!: __VLS_IntrinsicElements${utils_1.endOfLine}`;
52
+ }
50
53
  function* generateTemplateComponents(options) {
51
54
  const types = [];
52
55
  if (options.sfc.script && options.scriptRanges?.exportDefault?.componentsOption) {
@@ -1,4 +1,4 @@
1
- import type * as CompilerDOM from '@vue/compiler-dom';
1
+ import * as CompilerDOM from '@vue/compiler-dom';
2
2
  import type { Code, VueCodeInformation } from '../../types';
3
3
  import { InlayHintInfo } from '../inlayHints';
4
4
  import type { TemplateCodegenOptions } from './index';
@@ -100,7 +100,18 @@ export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenCont
100
100
  * an error/diagnostic was encountered for a region of code covered by a `@vue-expect-error` directive,
101
101
  * and additionally how we use that to determine whether to propagate diagnostics back upward.
102
102
  */
103
- export declare function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'edited'>): {
103
+ export declare function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames'>): {
104
+ readonly currentInfo: {
105
+ ignoreError?: boolean;
106
+ expectError?: {
107
+ token: number;
108
+ node: CompilerDOM.CommentNode;
109
+ };
110
+ generic?: {
111
+ content: string;
112
+ offset: number;
113
+ };
114
+ };
104
115
  codeFeatures: {
105
116
  all: VueCodeInformation;
106
117
  none: VueCodeInformation;
@@ -120,6 +131,7 @@ export declare function createTemplateCodegenContext(options: Pick<TemplateCodeg
120
131
  withoutHighlightAndCompletionAndNavigation: VueCodeInformation;
121
132
  };
122
133
  resolveCodeFeatures: (features: VueCodeInformation) => VueCodeInformation;
134
+ inVFor: boolean;
123
135
  slots: {
124
136
  name: string;
125
137
  offset?: number;
@@ -133,10 +145,6 @@ export declare function createTemplateCodegenContext(options: Pick<TemplateCodeg
133
145
  }[];
134
146
  dollarVars: Set<string>;
135
147
  accessExternalVariables: Map<string, Set<number>>;
136
- lastGenericComment: {
137
- content: string;
138
- offset: number;
139
- } | undefined;
140
148
  blockConditions: string[];
141
149
  scopedClasses: {
142
150
  source: string;
@@ -150,22 +158,23 @@ export declare function createTemplateCodegenContext(options: Pick<TemplateCodeg
150
158
  templateRefs: Map<string, {
151
159
  typeExp: string;
152
160
  offset: number;
153
- }>;
161
+ }[]>;
154
162
  currentComponent: {
155
163
  ctxVar: string;
156
164
  used: boolean;
157
165
  } | undefined;
158
166
  singleRootElTypes: string[];
159
167
  singleRootNodes: Set<CompilerDOM.ElementNode | null>;
168
+ addTemplateRef(name: string, typeExp: string, offset: number): void;
160
169
  accessExternalVariable(name: string, offset?: number): void;
161
- hasLocalVariable: (name: string) => boolean;
162
- addLocalVariable: (name: string) => void;
163
- removeLocalVariable: (name: string) => void;
164
- getInternalVariable: () => string;
165
- getHoistVariable: (originalVar: string) => string;
166
- generateHoistVariables: () => Generator<string, void, unknown>;
167
- ignoreError: () => Generator<Code>;
168
- expectError: (prevNode: CompilerDOM.CommentNode) => Generator<Code>;
169
- resetDirectiveComments: (endStr: string) => Generator<Code>;
170
- generateAutoImportCompletion: () => Generator<Code>;
170
+ hasLocalVariable(name: string): boolean;
171
+ addLocalVariable(name: string): void;
172
+ removeLocalVariable(name: string): void;
173
+ getInternalVariable(): string;
174
+ getHoistVariable(originalVar: string): string;
175
+ generateHoistVariables(): Generator<string, void, unknown>;
176
+ generateConditionGuards(): Generator<string, void, unknown>;
177
+ generateAutoImportCompletion(): Generator<Code>;
178
+ enter(node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode | CompilerDOM.SimpleExpressionNode): boolean;
179
+ exit(): Generator<Code>;
171
180
  };
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createTemplateCodegenContext = createTemplateCodegenContext;
4
+ const CompilerDOM = require("@vue/compiler-dom");
4
5
  const codeFeatures_1 = require("../codeFeatures");
5
6
  const utils_1 = require("../utils");
6
7
  const wrapWith_1 = require("../utils/wrapWith");
8
+ const commentDirectiveRegex = /^<!--\s*@vue-(?<name>[-\w]+)\b(?<content>[\s\S]*)-->$/;
7
9
  /**
8
10
  * Creates and returns a Context object used for generating type-checkable TS code
9
11
  * from the template section of a .vue file.
@@ -102,13 +104,11 @@ const wrapWith_1 = require("../utils/wrapWith");
102
104
  * and additionally how we use that to determine whether to propagate diagnostics back upward.
103
105
  */
104
106
  function createTemplateCodegenContext(options) {
105
- let ignoredError = false;
106
- let expectErrorToken;
107
- let lastGenericComment;
108
107
  let variableId = 0;
109
108
  function resolveCodeFeatures(features) {
110
- if (features.verification) {
111
- if (ignoredError) {
109
+ if (features.verification && stack.length) {
110
+ const data = stack[stack.length - 1];
111
+ if (data.ignoreError) {
112
112
  // We are currently in a region of code covered by a @vue-ignore directive, so don't
113
113
  // even bother performing any type-checking: set verification to false.
114
114
  return {
@@ -116,17 +116,16 @@ function createTemplateCodegenContext(options) {
116
116
  verification: false,
117
117
  };
118
118
  }
119
- if (expectErrorToken) {
119
+ if (data.expectError !== undefined) {
120
120
  // We are currently in a region of code covered by a @vue-expect-error directive. We need to
121
121
  // keep track of the number of errors encountered within this region so that we can know whether
122
122
  // we will need to propagate an "unused ts-expect-error" diagnostic back to the original
123
123
  // .vue file or not.
124
- const token = expectErrorToken;
125
124
  return {
126
125
  ...features,
127
126
  verification: {
128
127
  shouldReport: () => {
129
- token.errors++;
128
+ data.expectError.token++;
130
129
  return false;
131
130
  },
132
131
  },
@@ -148,7 +147,12 @@ function createTemplateCodegenContext(options) {
148
147
  const bindingAttrLocs = [];
149
148
  const inheritedAttrVars = new Set();
150
149
  const templateRefs = new Map();
150
+ const stack = [];
151
+ const commentBuffer = [];
151
152
  return {
153
+ get currentInfo() {
154
+ return stack[stack.length - 1];
155
+ },
152
156
  codeFeatures: new Proxy(codeFeatures_1.codeFeatures, {
153
157
  get(target, key) {
154
158
  const data = target[key];
@@ -156,11 +160,11 @@ function createTemplateCodegenContext(options) {
156
160
  },
157
161
  }),
158
162
  resolveCodeFeatures,
163
+ inVFor: false,
159
164
  slots,
160
165
  dynamicSlots,
161
166
  dollarVars,
162
167
  accessExternalVariables,
163
- lastGenericComment,
164
168
  blockConditions,
165
169
  scopedClasses,
166
170
  emptyClassOffsets,
@@ -171,6 +175,13 @@ function createTemplateCodegenContext(options) {
171
175
  currentComponent: undefined,
172
176
  singleRootElTypes: [],
173
177
  singleRootNodes: new Set(),
178
+ addTemplateRef(name, typeExp, offset) {
179
+ let refs = templateRefs.get(name);
180
+ if (!refs) {
181
+ templateRefs.set(name, refs = []);
182
+ }
183
+ refs.push({ typeExp, offset });
184
+ },
174
185
  accessExternalVariable(name, offset) {
175
186
  let arr = accessExternalVariables.get(name);
176
187
  if (!arr) {
@@ -180,26 +191,26 @@ function createTemplateCodegenContext(options) {
180
191
  arr.add(offset);
181
192
  }
182
193
  },
183
- hasLocalVariable: (name) => {
194
+ hasLocalVariable(name) {
184
195
  return !!localVars.get(name);
185
196
  },
186
- addLocalVariable: (name) => {
197
+ addLocalVariable(name) {
187
198
  localVars.set(name, (localVars.get(name) ?? 0) + 1);
188
199
  },
189
- removeLocalVariable: (name) => {
200
+ removeLocalVariable(name) {
190
201
  localVars.set(name, localVars.get(name) - 1);
191
202
  },
192
- getInternalVariable: () => {
203
+ getInternalVariable() {
193
204
  return `__VLS_${variableId++}`;
194
205
  },
195
- getHoistVariable: (originalVar) => {
206
+ getHoistVariable(originalVar) {
196
207
  let name = hoistVars.get(originalVar);
197
208
  if (name === undefined) {
198
209
  hoistVars.set(originalVar, name = `__VLS_${variableId++}`);
199
210
  }
200
211
  return name;
201
212
  },
202
- generateHoistVariables: function* () {
213
+ *generateHoistVariables() {
203
214
  // trick to avoid TS 4081 (#5186)
204
215
  if (hoistVars.size) {
205
216
  yield `// @ts-ignore${utils_1.newLine}`;
@@ -210,45 +221,12 @@ function createTemplateCodegenContext(options) {
210
221
  yield utils_1.endOfLine;
211
222
  }
212
223
  },
213
- ignoreError: function* () {
214
- if (!ignoredError) {
215
- ignoredError = true;
216
- yield `// @vue-ignore start${utils_1.newLine}`;
224
+ *generateConditionGuards() {
225
+ for (const condition of blockConditions) {
226
+ yield `if (!${condition}) return${utils_1.endOfLine}`;
217
227
  }
218
228
  },
219
- expectError: function* (prevNode) {
220
- if (!expectErrorToken) {
221
- expectErrorToken = {
222
- errors: 0,
223
- node: prevNode,
224
- };
225
- yield `// @vue-expect-error start${utils_1.newLine}`;
226
- }
227
- },
228
- resetDirectiveComments: function* (endStr) {
229
- if (expectErrorToken) {
230
- const token = expectErrorToken;
231
- yield* (0, wrapWith_1.wrapWith)(expectErrorToken.node.loc.start.offset, expectErrorToken.node.loc.end.offset, {
232
- verification: {
233
- // If no errors/warnings/diagnostics were reported within the region of code covered
234
- // by the @vue-expect-error directive, then we should allow any `unused @ts-expect-error`
235
- // diagnostics to be reported upward.
236
- shouldReport: () => token.errors === 0,
237
- },
238
- }, `// @ts-expect-error __VLS_TS_EXPECT_ERROR`);
239
- yield `${utils_1.newLine}${utils_1.endOfLine}`;
240
- expectErrorToken = undefined;
241
- yield `// @vue-expect-error ${endStr}${utils_1.newLine}`;
242
- }
243
- if (ignoredError) {
244
- ignoredError = false;
245
- yield `// @vue-ignore ${endStr}${utils_1.newLine}`;
246
- }
247
- },
248
- generateAutoImportCompletion: function* () {
249
- if (!options.edited) {
250
- return;
251
- }
229
+ *generateAutoImportCompletion() {
252
230
  const all = [...accessExternalVariables.entries()];
253
231
  if (!all.some(([_, offsets]) => offsets.size)) {
254
232
  return;
@@ -282,7 +260,65 @@ function createTemplateCodegenContext(options) {
282
260
  offsets.clear();
283
261
  }
284
262
  yield `]${utils_1.endOfLine}`;
285
- }
263
+ },
264
+ enter(node) {
265
+ if (node.type === CompilerDOM.NodeTypes.COMMENT) {
266
+ commentBuffer.push(node);
267
+ return false;
268
+ }
269
+ const data = {};
270
+ const comments = [...commentBuffer];
271
+ commentBuffer.length = 0;
272
+ for (const comment of comments) {
273
+ const match = comment.loc.source.match(commentDirectiveRegex);
274
+ if (match) {
275
+ const { name, content } = match.groups;
276
+ switch (name) {
277
+ case 'skip': {
278
+ return false;
279
+ }
280
+ case 'ignore': {
281
+ data.ignoreError = true;
282
+ break;
283
+ }
284
+ case 'expect-error': {
285
+ data.expectError = {
286
+ token: 0,
287
+ node: comment,
288
+ };
289
+ break;
290
+ }
291
+ case 'generic': {
292
+ const text = content.trim();
293
+ if (text.startsWith('{') && text.endsWith('}')) {
294
+ data.generic = {
295
+ content: text.slice(1, -1),
296
+ offset: comment.loc.start.offset + comment.loc.source.indexOf('{') + 1,
297
+ };
298
+ }
299
+ break;
300
+ }
301
+ }
302
+ }
303
+ }
304
+ stack.push(data);
305
+ return true;
306
+ },
307
+ *exit() {
308
+ const data = stack.pop();
309
+ commentBuffer.length = 0;
310
+ if (data.expectError !== undefined) {
311
+ yield* (0, wrapWith_1.wrapWith)(data.expectError.node.loc.start.offset, data.expectError.node.loc.end.offset, {
312
+ verification: {
313
+ // If no errors/warnings/diagnostics were reported within the region of code covered
314
+ // by the @vue-expect-error directive, then we should allow any `unused @ts-expect-error`
315
+ // diagnostics to be reported upward.
316
+ shouldReport: () => data.expectError.token === 0,
317
+ },
318
+ }, `// @ts-expect-error`);
319
+ yield `${utils_1.newLine}${utils_1.endOfLine}`;
320
+ }
321
+ },
286
322
  };
287
323
  }
288
324
  //# sourceMappingURL=context.js.map
@@ -2,5 +2,5 @@ import * as CompilerDOM from '@vue/compiler-dom';
2
2
  import type { Code } from '../../types';
3
3
  import type { TemplateCodegenContext } from './context';
4
4
  import type { TemplateCodegenOptions } from './index';
5
- export declare function generateComponent(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode, isVForChild: boolean): Generator<Code>;
6
- export declare function generateElement(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode, isVForChild: boolean): Generator<Code>;
5
+ export declare function generateComponent(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode): Generator<Code>;
6
+ export declare function generateElement(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode): Generator<Code>;