@vue/language-core 2.1.5 → 2.1.6

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 (36) hide show
  1. package/lib/codegen/globalTypes.d.ts +1 -1
  2. package/lib/codegen/globalTypes.js +123 -125
  3. package/lib/codegen/script/component.js +35 -32
  4. package/lib/codegen/script/{internalComponent.d.ts → componentSelf.d.ts} +1 -1
  5. package/lib/codegen/script/{internalComponent.js → componentSelf.js} +6 -6
  6. package/lib/codegen/script/index.d.ts +1 -0
  7. package/lib/codegen/script/index.js +14 -6
  8. package/lib/codegen/script/scriptSetup.js +10 -37
  9. package/lib/codegen/script/styleModulesType.d.ts +4 -0
  10. package/lib/codegen/script/styleModulesType.js +36 -0
  11. package/lib/codegen/script/template.d.ts +3 -3
  12. package/lib/codegen/script/template.js +53 -66
  13. package/lib/codegen/template/context.d.ts +1 -0
  14. package/lib/codegen/template/context.js +2 -0
  15. package/lib/codegen/template/element.js +4 -4
  16. package/lib/codegen/template/elementDirectives.js +1 -1
  17. package/lib/codegen/template/elementEvents.js +4 -1
  18. package/lib/codegen/template/elementProps.js +27 -18
  19. package/lib/codegen/template/index.d.ts +2 -2
  20. package/lib/codegen/template/index.js +2 -2
  21. package/lib/codegen/template/interpolation.d.ts +1 -1
  22. package/lib/codegen/template/interpolation.js +26 -12
  23. package/lib/parsers/scriptRanges.d.ts +1 -0
  24. package/lib/parsers/scriptRanges.js +7 -2
  25. package/lib/parsers/scriptSetupRanges.js +1 -1
  26. package/lib/plugins/vue-sfc-template.js +2 -2
  27. package/lib/plugins/vue-tsx.d.ts +2 -0
  28. package/lib/plugins/vue-tsx.js +25 -10
  29. package/lib/types.d.ts +2 -2
  30. package/lib/utils/ts.d.ts +8 -2
  31. package/lib/utils/ts.js +45 -8
  32. package/package.json +2 -2
  33. package/lib/codegen/script/globalTypes.d.ts +0 -2
  34. package/lib/codegen/script/globalTypes.js +0 -147
  35. package/lib/codegen/template/vBindShorthand.d.ts +0 -3
  36. package/lib/codegen/template/vBindShorthand.js +0 -18
@@ -1,8 +1,8 @@
1
1
  import type { Code } from '../../types';
2
+ import { TemplateCodegenContext } from '../template/context';
2
3
  import type { ScriptCodegenContext } from './context';
3
4
  import { type ScriptCodegenOptions } from './index';
4
- export declare function generateTemplateCtx(options: ScriptCodegenOptions, isClassComponent: boolean): Generator<Code>;
5
- export declare function generateTemplateComponents(options: ScriptCodegenOptions): Generator<Code>;
6
- export declare function generateTemplate(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, isClassComponent: boolean): Generator<Code>;
5
+ export declare function generateTemplateDirectives(options: ScriptCodegenOptions): Generator<Code>;
6
+ export declare function generateTemplate(options: ScriptCodegenOptions, ctx: ScriptCodegenContext, isClassComponent: boolean): 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>;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateTemplateCtx = generateTemplateCtx;
4
- exports.generateTemplateComponents = generateTemplateComponents;
3
+ exports.generateTemplateDirectives = generateTemplateDirectives;
5
4
  exports.generateTemplate = generateTemplate;
6
5
  exports.generateCssClassProperty = generateCssClassProperty;
7
6
  exports.getTemplateUsageVars = getTemplateUsageVars;
@@ -11,54 +10,28 @@ const context_1 = require("../template/context");
11
10
  const interpolation_1 = require("../template/interpolation");
12
11
  const styleScopedClasses_1 = require("../template/styleScopedClasses");
13
12
  const index_1 = require("./index");
14
- const internalComponent_1 = require("./internalComponent");
15
13
  function* generateTemplateCtx(options, isClassComponent) {
16
- const baseExps = [];
17
- const extraExps = [];
14
+ const exps = [];
18
15
  if (isClassComponent) {
19
- baseExps.push(`this`);
16
+ exps.push(`this`);
20
17
  }
21
18
  else {
22
- baseExps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>>`);
19
+ exps.push(`{} as InstanceType<__VLS_PickNotAny<typeof __VLS_self, new () => {}>>`);
23
20
  }
24
21
  if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileBaseName.endsWith(ext))) {
25
- extraExps.push(`globalThis`);
22
+ exps.push(`globalThis`);
26
23
  }
27
24
  if (options.sfc.styles.some(style => style.module)) {
28
- extraExps.push(`{} as __VLS_StyleModules`);
29
- }
30
- if (options.scriptSetupRanges?.templateRefs.length) {
31
- let exp = `{} as import('${options.vueCompilerOptions.lib}').UnwrapRef<{${common_1.newLine}`;
32
- for (const { name } of options.scriptSetupRanges.templateRefs) {
33
- if (name) {
34
- exp += `${name}: typeof ${name}${common_1.newLine}`;
35
- }
36
- }
37
- exp += `}>${common_1.newLine}`;
38
- extraExps.push(exp);
39
- }
40
- yield `const __VLS_ctxBase = `;
41
- if (baseExps.length === 1) {
42
- yield baseExps[0];
43
- yield common_1.endOfLine;
44
- }
45
- else {
46
- yield `{${common_1.newLine}`;
47
- for (const exp of baseExps) {
48
- yield `...`;
49
- yield exp;
50
- yield `,${common_1.newLine}`;
51
- }
52
- yield `}${common_1.endOfLine}`;
25
+ exps.push(`{} as __VLS_StyleModules`);
53
26
  }
54
27
  yield `const __VLS_ctx = `;
55
- if (extraExps.length === 0) {
56
- yield `__VLS_ctxBase${common_1.endOfLine}`;
28
+ if (exps.length === 1) {
29
+ yield exps[0];
30
+ yield `${common_1.endOfLine}`;
57
31
  }
58
32
  else {
59
33
  yield `{${common_1.newLine}`;
60
- yield `...__VLS_ctxBase,${common_1.newLine}`;
61
- for (const exp of extraExps) {
34
+ for (const exp of exps) {
62
35
  yield `...`;
63
36
  yield exp;
64
37
  yield `,${common_1.newLine}`;
@@ -83,19 +56,18 @@ function* generateTemplateComponents(options) {
83
56
  nameType = options.sfc.script.content.substring(nameOption.start, nameOption.end);
84
57
  }
85
58
  else if (options.sfc.scriptSetup) {
86
- yield `let __VLS_name!: '${options.scriptSetupRanges?.options.name ?? options.fileBaseName.substring(0, options.fileBaseName.lastIndexOf('.'))}'${common_1.endOfLine}`;
87
- nameType = 'typeof __VLS_name';
59
+ nameType = `'${options.scriptSetupRanges?.options.name ?? options.fileBaseName.substring(0, options.fileBaseName.lastIndexOf('.'))}'`;
88
60
  }
89
61
  if (nameType) {
90
62
  exps.push(`{} as {
91
- [K in ${nameType}]: typeof __VLS_internalComponent
63
+ [K in ${nameType}]: typeof __VLS_self
92
64
  & (new () => {
93
65
  ${(0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target)}: typeof ${options.scriptSetupRanges?.slots?.name ?? '__VLS_slots'}
94
66
  })
95
67
  }`);
96
68
  }
97
- exps.push(`{} as NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}>`);
98
- exps.push(`__VLS_ctxBase`);
69
+ exps.push(`{} as NonNullable<typeof __VLS_self extends { components: infer C } ? C : {}>`);
70
+ exps.push(`__VLS_ctx`);
99
71
  yield `const __VLS_localComponents = {${common_1.newLine}`;
100
72
  for (const type of exps) {
101
73
  yield `...`;
@@ -103,26 +75,41 @@ function* generateTemplateComponents(options) {
103
75
  yield `,${common_1.newLine}`;
104
76
  }
105
77
  yield `}${common_1.endOfLine}`;
106
- yield `let __VLS_components: typeof __VLS_localComponents & __VLS_GlobalComponents${common_1.endOfLine}`;
78
+ yield `let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents${common_1.endOfLine}`;
107
79
  }
108
- function* generateTemplate(options, ctx, isClassComponent) {
109
- ctx.generatedTemplate = true;
110
- if (!options.vueCompilerOptions.skipTemplateCodegen) {
111
- const templateCodegenCtx = (0, context_1.createTemplateCodegenContext)({
112
- scriptSetupBindingNames: new Set(),
113
- edited: options.edited,
114
- });
115
- yield* generateTemplateCtx(options, isClassComponent);
116
- yield* generateTemplateComponents(options);
117
- yield* generateTemplateBody(options, templateCodegenCtx);
118
- yield* (0, internalComponent_1.generateInternalComponent)(options, ctx, templateCodegenCtx);
80
+ function* generateTemplateDirectives(options) {
81
+ const exps = [];
82
+ if (options.sfc.script && options.scriptRanges?.exportDefault?.directivesOption) {
83
+ const { directivesOption } = options.scriptRanges.exportDefault;
84
+ exps.push([
85
+ options.sfc.script.content.substring(directivesOption.start, directivesOption.end),
86
+ 'script',
87
+ directivesOption.start,
88
+ index_1.codeFeatures.navigation,
89
+ ]);
119
90
  }
120
- else {
121
- const templateUsageVars = [...getTemplateUsageVars(options, ctx)];
122
- yield `// @ts-ignore${common_1.newLine}`;
123
- yield `[${templateUsageVars.join(', ')}]${common_1.newLine}`;
124
- yield `const __VLS_templateResult { slots: {}, refs: {}, attrs: {} }${common_1.endOfLine}`;
91
+ exps.push(`{} as NonNullable<typeof __VLS_self extends { directives: infer D } ? D : {}>`);
92
+ exps.push(`__VLS_ctx`);
93
+ yield `const __VLS_localDirectives = {${common_1.newLine}`;
94
+ for (const type of exps) {
95
+ yield `...`;
96
+ yield type;
97
+ yield `,${common_1.newLine}`;
125
98
  }
99
+ yield `}${common_1.endOfLine}`;
100
+ yield `let __VLS_directives!: typeof __VLS_localDirectives & __VLS_GlobalDirectives${common_1.endOfLine}`;
101
+ }
102
+ function* generateTemplate(options, ctx, isClassComponent) {
103
+ ctx.generatedTemplate = true;
104
+ const templateCodegenCtx = (0, context_1.createTemplateCodegenContext)({
105
+ scriptSetupBindingNames: new Set(),
106
+ edited: options.edited,
107
+ });
108
+ yield* generateTemplateCtx(options, isClassComponent);
109
+ yield* generateTemplateComponents(options);
110
+ yield* generateTemplateDirectives(options);
111
+ yield* generateTemplateBody(options, templateCodegenCtx);
112
+ return templateCodegenCtx;
126
113
  }
127
114
  function* generateTemplateBody(options, templateCodegenCtx) {
128
115
  const firstClasses = new Set();
@@ -157,14 +144,14 @@ function* generateTemplateBody(options, templateCodegenCtx) {
157
144
  yield `// no template${common_1.newLine}`;
158
145
  if (!options.scriptSetupRanges?.slots.define) {
159
146
  yield `const __VLS_slots = {}${common_1.endOfLine}`;
160
- yield `const $refs = {}${common_1.endOfLine}`;
161
- yield `const __VLS_inheritedAttrs = {}${common_1.endOfLine}`;
162
147
  }
148
+ yield `const $refs = {}${common_1.endOfLine}`;
149
+ yield `const __VLS_inheritedAttrs = {}${common_1.endOfLine}`;
163
150
  }
164
- yield `const __VLS_templateResult = {`;
165
- yield `slots: ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'},${common_1.newLine}`;
166
- yield `refs: $refs,${common_1.newLine}`;
167
- yield `attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${common_1.newLine}`;
151
+ yield `return {${common_1.newLine}`;
152
+ yield ` slots: ${options.scriptSetupRanges?.slots.name ?? '__VLS_slots'},${common_1.newLine}`;
153
+ yield ` refs: $refs,${common_1.newLine}`;
154
+ yield ` attrs: {} as Partial<typeof __VLS_inheritedAttrs>,${common_1.newLine}`;
168
155
  yield `}${common_1.endOfLine}`;
169
156
  }
170
157
  function* generateCssClassProperty(styleIndex, classNameWithDot, offset, propertyType, optional) {
@@ -199,7 +186,7 @@ function* generateCssVars(options, ctx) {
199
186
  yield `// CSS variable injection ${common_1.newLine}`;
200
187
  for (const style of options.sfc.styles) {
201
188
  for (const cssBind of style.cssVars) {
202
- for (const [segment, offset, onlyError] of (0, interpolation_1.forEachInterpolationSegment)(options.ts, ctx, cssBind.text, cssBind.offset, options.ts.createSourceFile('/a.txt', cssBind.text, 99))) {
189
+ for (const [segment, offset, onlyError] of (0, interpolation_1.forEachInterpolationSegment)(options.ts, undefined, ctx, cssBind.text, cssBind.offset, options.ts.createSourceFile('/a.txt', cssBind.text, 99))) {
203
190
  if (offset === undefined) {
204
191
  yield segment;
205
192
  }
@@ -41,6 +41,7 @@ export declare function createTemplateCodegenContext(options: Pick<TemplateCodeg
41
41
  inlayHints: InlayHintInfo[];
42
42
  hasSlot: boolean;
43
43
  inheritedAttrVars: Set<unknown>;
44
+ templateRefs: Map<string, [string, number]>;
44
45
  singleRootNode: CompilerDOM.ElementNode | undefined;
45
46
  accessExternalVariable(name: string, offset?: number): void;
46
47
  hasLocalVariable: (name: string) => boolean;
@@ -95,6 +95,7 @@ function createTemplateCodegenContext(options) {
95
95
  const scopedClasses = [];
96
96
  const emptyClassOffsets = [];
97
97
  const inlayHints = [];
98
+ const templateRefs = new Map();
98
99
  return {
99
100
  slots,
100
101
  dynamicSlots,
@@ -108,6 +109,7 @@ function createTemplateCodegenContext(options) {
108
109
  inlayHints,
109
110
  hasSlot: false,
110
111
  inheritedAttrVars: new Set(),
112
+ templateRefs,
111
113
  singleRootNode: undefined,
112
114
  accessExternalVariable(name, offset) {
113
115
  let arr = accessExternalVariables.get(name);
@@ -153,9 +153,9 @@ function* generateComponent(options, ctx, node, currentComponent) {
153
153
  const [refName, offset] = yield* generateVScope(options, ctx, node, props);
154
154
  if (refName) {
155
155
  const varName = ctx.getInternalVariable();
156
- options.templateRefNames.set(refName, [varName, offset]);
156
+ ctx.templateRefs.set(refName, [varName, offset]);
157
157
  ctx.usedComponentCtxVars.add(var_defineComponentCtx);
158
- yield `var ${varName} = {} as (Parameters<typeof ${var_defineComponentCtx}['expose']>[0] | null)`;
158
+ yield `var ${varName} = {} as (Parameters<NonNullable<typeof ${var_defineComponentCtx}['expose']>>[0] | null)`;
159
159
  if (node.codegenNode?.type === CompilerDOM.NodeTypes.VNODE_CALL
160
160
  && node.codegenNode.props?.type === CompilerDOM.NodeTypes.JS_OBJECT_EXPRESSION
161
161
  && node.codegenNode.props.properties.some(({ key }) => key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && key.content === 'ref_for')) {
@@ -208,7 +208,7 @@ function* generateElement(options, ctx, node, currentComponent, componentCtxVar)
208
208
  }
209
209
  const [refName, offset] = yield* generateVScope(options, ctx, node, node.props);
210
210
  if (refName) {
211
- options.templateRefNames.set(refName, [`__VLS_intrinsicElements['${node.tag}']`, offset]);
211
+ ctx.templateRefs.set(refName, [`__VLS_nativeElements['${node.tag}']`, offset]);
212
212
  }
213
213
  const slotDir = node.props.find(p => p.type === CompilerDOM.NodeTypes.DIRECTIVE && p.name === 'slot');
214
214
  if (slotDir && componentCtxVar) {
@@ -360,7 +360,7 @@ function* generateReferencesForElements(options, ctx, node) {
360
360
  && prop.name === 'ref'
361
361
  && prop.value) {
362
362
  const [content, startOffset] = normalizeAttributeValue(prop.value);
363
- yield `// @ts-ignore${common_1.newLine}`;
363
+ yield `// @ts-ignore navigation for \`const ${content} = ref()\`${common_1.newLine}`;
364
364
  yield `__VLS_ctx`;
365
365
  yield* (0, propertyAccess_1.generatePropertyAccess)(options, ctx, content, startOffset, ctx.codeFeatures.navigation, prop.value.loc);
366
366
  yield common_1.endOfLine;
@@ -21,7 +21,7 @@ function* generateElementDirectives(options, ctx, node) {
21
21
  yield* (0, interpolation_1.generateInterpolation)(options, ctx, prop.arg.content, prop.arg.loc, prop.arg.loc.start.offset + prop.arg.loc.source.indexOf(prop.arg.content), ctx.codeFeatures.all, '(', ')');
22
22
  yield common_1.endOfLine;
23
23
  }
24
- yield* (0, common_1.wrapWith)(prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, `__VLS_directiveAsFunction(__VLS_ctx.`, ...(0, camelized_1.generateCamelized)('v-' + prop.name, prop.loc.start.offset, {
24
+ yield* (0, common_1.wrapWith)(prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, `__VLS_directiveAsFunction(__VLS_directives.`, ...(0, camelized_1.generateCamelized)('v-' + prop.name, prop.loc.start.offset, {
25
25
  ...ctx.codeFeatures.all,
26
26
  verification: false,
27
27
  completion: {
@@ -136,7 +136,10 @@ function* generateEventExpression(options, ctx, prop) {
136
136
  }
137
137
  function isCompoundExpression(ts, ast) {
138
138
  let result = true;
139
- if (ast.statements.length === 1) {
139
+ if (ast.statements.length === 0) {
140
+ result = false;
141
+ }
142
+ else if (ast.statements.length === 1) {
140
143
  ts.forEachChild(ast, child_1 => {
141
144
  if (ts.isExpressionStatement(child_1)) {
142
145
  ts.forEachChild(child_1, child_2 => {
@@ -99,7 +99,7 @@ function* generateElementProps(options, ctx, node, props, enableCodeFeatures, pr
99
99
  }
100
100
  : false,
101
101
  }, prop.loc.name_2 ?? (prop.loc.name_2 = {}), shouldCamelize)
102
- : (0, common_1.wrapWith)(prop.loc.start.offset, prop.loc.start.offset + 'v-model'.length, ctx.codeFeatures.verification, propName)), `: (`, ...genereatePropExp(options, ctx, prop, prop.exp, ctx.codeFeatures.all, prop.arg?.loc.start.offset === prop.exp?.loc.start.offset, enableCodeFeatures), `)`);
102
+ : (0, common_1.wrapWith)(prop.loc.start.offset, prop.loc.start.offset + 'v-model'.length, ctx.codeFeatures.verification, propName)), `: (`, ...generatePropExp(options, ctx, prop, prop.exp, ctx.codeFeatures.all, prop.arg?.loc.start.offset === prop.exp?.loc.start.offset, enableCodeFeatures), `)`);
103
103
  if (!enableCodeFeatures) {
104
104
  yield (0, muggle_string_1.toString)([...codes]);
105
105
  }
@@ -126,22 +126,9 @@ function* generateElementProps(options, ctx, node, props, enableCodeFeatures, pr
126
126
  if (shouldSpread) {
127
127
  yield `...{ `;
128
128
  }
129
- const codeInfo = ctx.codeFeatures.withoutHighlightAndCompletion;
130
- const codes = (0, common_1.conditionWrapWith)(enableCodeFeatures, prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, ...(0, objectProperty_1.generateObjectProperty)(options, ctx, prop.name, prop.loc.start.offset, shouldCamelize
129
+ const codeInfo = shouldCamelize
131
130
  ? {
132
- ...codeInfo,
133
- verification: options.vueCompilerOptions.strictTemplates
134
- ? codeInfo.verification
135
- : {
136
- shouldReport(_source, code) {
137
- if (String(code) === '2353' || String(code) === '2561') {
138
- return false;
139
- }
140
- return typeof codeInfo.verification === 'object'
141
- ? codeInfo.verification.shouldReport?.(_source, code) ?? true
142
- : true;
143
- },
144
- },
131
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
145
132
  navigation: ctx.codeFeatures.withoutHighlightAndCompletion.navigation
146
133
  ? {
147
134
  resolveRenameNewName: shared_1.camelize,
@@ -149,7 +136,23 @@ function* generateElementProps(options, ctx, node, props, enableCodeFeatures, pr
149
136
  }
150
137
  : false,
151
138
  }
152
- : ctx.codeFeatures.withoutHighlightAndCompletion, prop.loc.name_1 ?? (prop.loc.name_1 = {}), shouldCamelize), `: (`, ...(prop.value
139
+ : {
140
+ ...ctx.codeFeatures.withoutHighlightAndCompletion,
141
+ };
142
+ if (!options.vueCompilerOptions.strictTemplates) {
143
+ const verification = codeInfo.verification;
144
+ codeInfo.verification = {
145
+ shouldReport(_source, code) {
146
+ if (String(code) === '2353' || String(code) === '2561') {
147
+ return false;
148
+ }
149
+ return typeof verification === 'object'
150
+ ? verification.shouldReport?.(_source, code) ?? true
151
+ : true;
152
+ },
153
+ };
154
+ }
155
+ const codes = (0, common_1.conditionWrapWith)(enableCodeFeatures, prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, ...(0, objectProperty_1.generateObjectProperty)(options, ctx, prop.name, prop.loc.start.offset, codeInfo, prop.loc.name_1 ?? (prop.loc.name_1 = {}), shouldCamelize), `: (`, ...(prop.value
153
156
  ? generateAttrValue(prop.value, ctx.codeFeatures.all)
154
157
  : [`true`]), `)`);
155
158
  if (!enableCodeFeatures) {
@@ -182,7 +185,13 @@ function* generateElementProps(options, ctx, node, props, enableCodeFeatures, pr
182
185
  }
183
186
  }
184
187
  }
185
- function* genereatePropExp(options, ctx, prop, exp, features, isShorthand, enableCodeFeatures) {
188
+ function* generatePropExp(options, ctx, prop, exp, features, isShorthand, enableCodeFeatures) {
189
+ if (isShorthand && features.completion) {
190
+ features = {
191
+ ...features,
192
+ completion: undefined,
193
+ };
194
+ }
186
195
  if (exp && exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) { // style='z-index: 2' will compile to {'z-index':'2'}
187
196
  if (!isShorthand) { // vue 3.4+
188
197
  yield* (0, interpolation_1.generateInterpolation)(options, ctx, exp.loc.source, exp.loc, exp.loc.start.offset, features, '(', ')');
@@ -7,10 +7,10 @@ export interface TemplateCodegenOptions {
7
7
  compilerOptions: ts.CompilerOptions;
8
8
  vueCompilerOptions: VueCompilerOptions;
9
9
  template: NonNullable<Sfc['template']>;
10
+ edited: boolean;
10
11
  scriptSetupBindingNames: Set<string>;
11
12
  scriptSetupImportComponentNames: Set<string>;
12
- edited: boolean;
13
- templateRefNames: Map<string, [varName: string, offset: number]>;
13
+ templateRefNames: Set<string>;
14
14
  hasDefineSlots?: boolean;
15
15
  slotsAssignName?: string;
16
16
  propsAssignName?: string;
@@ -35,12 +35,12 @@ function* generateTemplate(options) {
35
35
  return ctx;
36
36
  function* generateRefs() {
37
37
  yield `const __VLS_refs = {${common_1.newLine}`;
38
- for (const [name, [varName, offset]] of options.templateRefNames) {
38
+ for (const [name, [varName, offset]] of ctx.templateRefs) {
39
39
  yield* (0, stringLiteralKey_1.generateStringLiteralKey)(name, offset, ctx.codeFeatures.navigationAndCompletion);
40
40
  yield `: ${varName},${common_1.newLine}`;
41
41
  }
42
42
  yield `}${common_1.endOfLine}`;
43
- yield `declare var $refs: typeof __VLS_refs${common_1.endOfLine}`;
43
+ yield `var $refs!: typeof __VLS_refs${common_1.endOfLine}`;
44
44
  }
45
45
  function* generateSlotsType() {
46
46
  for (const { expVar, varName } of ctx.dynamicSlots) {
@@ -3,4 +3,4 @@ import type { Code, VueCodeInformation } from '../../types';
3
3
  import type { TemplateCodegenContext } from './context';
4
4
  import type { TemplateCodegenOptions } from './index';
5
5
  export declare function generateInterpolation(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, _code: string, astHolder: any, start: number | undefined, data: VueCodeInformation | ((offset: number) => VueCodeInformation) | undefined, prefix: string, suffix: string): Generator<Code>;
6
- export declare function forEachInterpolationSegment(ts: typeof import('typescript'), ctx: TemplateCodegenContext, code: string, offset: number | undefined, ast: ts.SourceFile): Generator<[fragment: string, offset: number | undefined, isJustForErrorMapping?: boolean]>;
6
+ export declare function forEachInterpolationSegment(ts: typeof import('typescript'), templateRefNames: Set<string> | undefined, ctx: TemplateCodegenContext, code: string, offset: number | undefined, ast: ts.SourceFile): Generator<[fragment: string, offset: number | undefined, errorMappingOnly?: boolean]>;
@@ -9,7 +9,7 @@ function* generateInterpolation(options, ctx, _code, astHolder, start, data, pre
9
9
  const code = prefix + _code + suffix;
10
10
  const ast = (0, common_1.createTsAst)(options.ts, astHolder, code);
11
11
  const vars = [];
12
- for (let [section, offset, onlyError] of forEachInterpolationSegment(options.ts, ctx, code, start !== undefined ? start - prefix.length : undefined, ast)) {
12
+ for (let [section, offset, onlyError] of forEachInterpolationSegment(options.ts, options.templateRefNames, ctx, code, start !== undefined ? start - prefix.length : undefined, ast)) {
13
13
  if (offset === undefined) {
14
14
  yield section;
15
15
  }
@@ -48,7 +48,7 @@ function* generateInterpolation(options, ctx, _code, astHolder, start, data, pre
48
48
  }
49
49
  }
50
50
  }
51
- function* forEachInterpolationSegment(ts, ctx, code, offset, ast) {
51
+ function* forEachInterpolationSegment(ts, templateRefNames, ctx, code, offset, ast) {
52
52
  let ctxVars = [];
53
53
  const varCb = (id, isShorthand) => {
54
54
  const text = (0, scriptSetupRanges_1.getNodeText)(ts, id, ast);
@@ -84,26 +84,40 @@ function* forEachInterpolationSegment(ts, ctx, code, offset, ast) {
84
84
  yield [code.substring(0, ctxVars[0].offset), 0];
85
85
  }
86
86
  for (let i = 0; i < ctxVars.length - 1; i++) {
87
- // fix https://github.com/vuejs/language-tools/issues/1205
88
- // fix https://github.com/vuejs/language-tools/issues/1264
89
- yield ['', ctxVars[i + 1].offset, true];
90
- yield ['__VLS_ctx.', undefined];
91
- if (ctxVars[i + 1].isShorthand) {
92
- yield [code.substring(ctxVars[i].offset, ctxVars[i + 1].offset + ctxVars[i + 1].text.length), ctxVars[i].offset];
87
+ const curVar = ctxVars[i];
88
+ const nextVar = ctxVars[i + 1];
89
+ yield* generateVar(code, templateRefNames, curVar, nextVar);
90
+ if (nextVar.isShorthand) {
91
+ yield [code.substring(curVar.offset + curVar.text.length, nextVar.offset + nextVar.text.length), curVar.offset + curVar.text.length];
93
92
  yield [': ', undefined];
94
93
  }
95
94
  else {
96
- yield [code.substring(ctxVars[i].offset, ctxVars[i + 1].offset), ctxVars[i].offset];
95
+ yield [code.substring(curVar.offset + curVar.text.length, nextVar.offset), curVar.offset + curVar.text.length];
97
96
  }
98
97
  }
99
- yield ['', ctxVars[ctxVars.length - 1].offset, true];
100
- yield ['__VLS_ctx.', undefined];
101
- yield [code.substring(ctxVars[ctxVars.length - 1].offset), ctxVars[ctxVars.length - 1].offset];
98
+ const lastVar = ctxVars.at(-1);
99
+ yield* generateVar(code, templateRefNames, lastVar);
100
+ yield [code.substring(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length];
102
101
  }
103
102
  else {
104
103
  yield [code, 0];
105
104
  }
106
105
  }
106
+ function* generateVar(code, templateRefNames, curVar, nextVar = curVar) {
107
+ // fix https://github.com/vuejs/language-tools/issues/1205
108
+ // fix https://github.com/vuejs/language-tools/issues/1264
109
+ yield ['', nextVar.offset, true];
110
+ const isTemplateRef = templateRefNames?.has(curVar.text) ?? false;
111
+ if (isTemplateRef) {
112
+ yield [`__VLS_unref(`, undefined];
113
+ yield [code.substring(curVar.offset, curVar.offset + curVar.text.length), curVar.offset];
114
+ yield [`)`, undefined];
115
+ }
116
+ else {
117
+ yield [`__VLS_ctx.`, undefined];
118
+ yield [code.substring(curVar.offset, curVar.offset + curVar.text.length), curVar.offset];
119
+ }
120
+ }
107
121
  function walkIdentifiers(ts, node, ast, cb, ctx, blockVars = [], isRoot = true) {
108
122
  if (ts.isIdentifier(node)) {
109
123
  cb(node, false);
@@ -9,6 +9,7 @@ export declare function parseScriptRanges(ts: typeof import('typescript'), ast:
9
9
  argsNode: ts.ObjectLiteralExpression | undefined;
10
10
  componentsOption: TextRange | undefined;
11
11
  componentsOptionNode: ts.ObjectLiteralExpression | undefined;
12
+ directivesOption: TextRange | undefined;
12
13
  nameOption: TextRange | undefined;
13
14
  inheritAttrsOption: string | undefined;
14
15
  }) | undefined;
@@ -24,6 +24,7 @@ function parseScriptRanges(ts, ast, hasScriptSetup, withNode) {
24
24
  }
25
25
  if (obj) {
26
26
  let componentsOptionNode;
27
+ let directivesOptionNode;
27
28
  let nameOptionNode;
28
29
  let inheritAttrsOption;
29
30
  ts.forEachChild(obj, node => {
@@ -32,10 +33,13 @@ function parseScriptRanges(ts, ast, hasScriptSetup, withNode) {
32
33
  if (name === 'components' && ts.isObjectLiteralExpression(node.initializer)) {
33
34
  componentsOptionNode = node.initializer;
34
35
  }
35
- if (name === 'name') {
36
+ else if (name === 'directives' && ts.isObjectLiteralExpression(node.initializer)) {
37
+ directivesOptionNode = node.initializer;
38
+ }
39
+ else if (name === 'name') {
36
40
  nameOptionNode = node.initializer;
37
41
  }
38
- if (name === 'inheritAttrs') {
42
+ else if (name === 'inheritAttrs') {
39
43
  inheritAttrsOption = (0, scriptSetupRanges_1.getNodeText)(ts, node.initializer, ast);
40
44
  }
41
45
  }
@@ -47,6 +51,7 @@ function parseScriptRanges(ts, ast, hasScriptSetup, withNode) {
47
51
  argsNode: withNode ? obj : undefined,
48
52
  componentsOption: componentsOptionNode ? _getStartEnd(componentsOptionNode) : undefined,
49
53
  componentsOptionNode: withNode ? componentsOptionNode : undefined,
54
+ directivesOption: directivesOptionNode ? _getStartEnd(directivesOptionNode) : undefined,
50
55
  nameOption: nameOptionNode ? _getStartEnd(nameOptionNode) : undefined,
51
56
  inheritAttrsOption,
52
57
  };
@@ -296,7 +296,7 @@ function parseScriptSetupRanges(ts, ast, vueCompilerOptions) {
296
296
  }
297
297
  }
298
298
  }
299
- else if (vueCompilerOptions.macros.templateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) {
299
+ else if (vueCompilerOptions.composibles.useTemplateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) {
300
300
  const define = parseDefineFunction(node);
301
301
  define.arg = _getStartEnd(node.arguments[0]);
302
302
  let name;
@@ -5,7 +5,7 @@ const plugin = () => {
5
5
  return {
6
6
  version: 2.1,
7
7
  getEmbeddedCodes(_fileName, sfc) {
8
- if (sfc.template) {
8
+ if (sfc.template?.lang === 'html') {
9
9
  return [{
10
10
  id: 'template',
11
11
  lang: sfc.template.lang,
@@ -14,7 +14,7 @@ const plugin = () => {
14
14
  return [];
15
15
  },
16
16
  resolveEmbeddedCode(_fileName, sfc, embeddedFile) {
17
- if (embeddedFile.id === 'template' && sfc.template) {
17
+ if (embeddedFile.id === 'template' && sfc.template?.lang === 'html') {
18
18
  embeddedFile.content.push([
19
19
  sfc.template.content,
20
20
  sfc.template.name,
@@ -8,6 +8,7 @@ export declare const tsCodegen: WeakMap<Sfc, {
8
8
  argsNode: import("typescript").ObjectLiteralExpression | undefined;
9
9
  componentsOption: import("../types").TextRange | undefined;
10
10
  componentsOptionNode: import("typescript").ObjectLiteralExpression | undefined;
11
+ directivesOption: import("../types").TextRange | undefined;
11
12
  nameOption: import("../types").TextRange | undefined;
12
13
  inheritAttrsOption: string | undefined;
13
14
  }) | undefined;
@@ -143,6 +144,7 @@ export declare const tsCodegen: WeakMap<Sfc, {
143
144
  inlayHints: import("../codegen/types").InlayHintInfo[];
144
145
  hasSlot: boolean;
145
146
  inheritedAttrVars: Set<unknown>;
147
+ templateRefs: Map<string, [string, number]>;
146
148
  singleRootNode: import("@vue/compiler-dom").ElementNode | undefined;
147
149
  accessExternalVariable(name: string, offset?: number): void;
148
150
  hasLocalVariable: (name: string) => boolean;