@vue/language-core 2.2.0 → 2.2.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 (84) hide show
  1. package/index.d.ts +0 -1
  2. package/index.js +1 -2
  3. package/lib/codeFeatures.d.ts +1 -0
  4. package/lib/codeFeatures.js +3 -0
  5. package/lib/codegen/codeFeatures.d.ts +83 -0
  6. package/lib/codegen/codeFeatures.js +71 -0
  7. package/lib/codegen/common.d.ts +12 -0
  8. package/lib/codegen/common.js +79 -0
  9. package/lib/codegen/globalTypes.d.ts +3 -1
  10. package/lib/codegen/globalTypes.js +30 -14
  11. package/lib/codegen/localTypes.d.ts +1 -1
  12. package/lib/codegen/localTypes.js +4 -4
  13. package/lib/codegen/script/binding.d.ts +4 -0
  14. package/lib/codegen/script/binding.js +41 -0
  15. package/lib/codegen/script/component.d.ts +1 -1
  16. package/lib/codegen/script/component.js +7 -7
  17. package/lib/codegen/script/componentSelf.d.ts +1 -1
  18. package/lib/codegen/script/componentSelf.js +2 -2
  19. package/lib/codegen/script/context.d.ts +1 -1
  20. package/lib/codegen/script/index.d.ts +1 -8
  21. package/lib/codegen/script/index.js +15 -39
  22. package/lib/codegen/script/scriptSetup.d.ts +1 -1
  23. package/lib/codegen/script/scriptSetup.js +46 -58
  24. package/lib/codegen/script/src.js +2 -2
  25. package/lib/codegen/script/styleModulesType.d.ts +1 -1
  26. package/lib/codegen/script/styleModulesType.js +2 -2
  27. package/lib/codegen/script/template.d.ts +1 -1
  28. package/lib/codegen/script/template.js +11 -32
  29. package/lib/codegen/template/camelized.d.ts +2 -0
  30. package/lib/codegen/template/camelized.js +31 -0
  31. package/lib/codegen/template/context.d.ts +17 -13
  32. package/lib/codegen/template/context.js +35 -89
  33. package/lib/codegen/template/element.js +32 -252
  34. package/lib/codegen/template/elementChildren.d.ts +1 -1
  35. package/lib/codegen/template/elementChildren.js +3 -4
  36. package/lib/codegen/template/elementDirectives.js +18 -11
  37. package/lib/codegen/template/elementProps.d.ts +2 -1
  38. package/lib/codegen/template/elementProps.js +22 -26
  39. package/lib/codegen/template/index.d.ts +1 -0
  40. package/lib/codegen/template/index.js +39 -33
  41. package/lib/codegen/template/interpolation.js +13 -9
  42. package/lib/codegen/template/slotOutlet.js +39 -17
  43. package/lib/codegen/template/styleScopedClasses.d.ts +3 -0
  44. package/lib/codegen/template/styleScopedClasses.js +141 -0
  45. package/lib/codegen/template/templateChild.js +9 -2
  46. package/lib/codegen/template/vSlot.d.ts +5 -0
  47. package/lib/codegen/template/vSlot.js +80 -0
  48. package/lib/codegen/utils/index.d.ts +11 -1
  49. package/lib/codegen/utils/index.js +15 -4
  50. package/lib/codegen/utils/src.d.ts +2 -0
  51. package/lib/codegen/utils/src.js +19 -0
  52. package/lib/parsers/scriptSetupRanges.d.ts +5 -3
  53. package/lib/parsers/scriptSetupRanges.js +8 -11
  54. package/lib/parsers/vueCompilerOptions.d.ts +2 -2
  55. package/lib/plugins/vue-style-class-names.d.ts +5 -0
  56. package/lib/plugins/vue-style-class-names.js +32 -0
  57. package/lib/plugins/vue-style-reference-link.d.ts +1 -0
  58. package/lib/plugins/vue-style-reference-link.js +3 -0
  59. package/lib/plugins/vue-style-reference-links.d.ts +3 -0
  60. package/lib/plugins/vue-style-reference-links.js +26 -0
  61. package/lib/plugins/vue-template-inline-ts.js +53 -12
  62. package/lib/plugins/vue-tsx.d.ts +35 -28
  63. package/lib/plugins/vue-tsx.js +75 -55
  64. package/lib/plugins/vue-vine.d.ts +3 -0
  65. package/lib/plugins/vue-vine.js +35 -0
  66. package/lib/types.d.ts +6 -2
  67. package/lib/utils/findDestructuredProps.d.ts +1 -0
  68. package/lib/utils/findDestructuredProps.js +3 -0
  69. package/lib/utils/parseCssImports.d.ts +4 -0
  70. package/lib/utils/parseCssImports.js +19 -0
  71. package/lib/utils/parseSfc.js +4 -3
  72. package/lib/utils/signals.d.ts +2 -0
  73. package/lib/utils/signals.js +54 -0
  74. package/lib/utils/ts.d.ts +14 -2
  75. package/lib/utils/ts.js +117 -88
  76. package/lib/virtualFile/computedEmbeddedCodes.d.ts +1 -1
  77. package/lib/virtualFile/computedEmbeddedCodes.js +11 -11
  78. package/lib/virtualFile/computedSfc.d.ts +1 -2
  79. package/lib/virtualFile/computedSfc.js +79 -82
  80. package/lib/virtualFile/computedVueSfc.d.ts +1 -2
  81. package/lib/virtualFile/computedVueSfc.js +7 -7
  82. package/lib/virtualFile/vueFile.d.ts +5 -5
  83. package/lib/virtualFile/vueFile.js +6 -6
  84. package/package.json +4 -4
@@ -5,11 +5,20 @@ exports.generateModifiers = generateModifiers;
5
5
  const CompilerDOM = require("@vue/compiler-dom");
6
6
  const shared_1 = require("@vue/shared");
7
7
  const shared_2 = require("../../utils/shared");
8
+ const codeFeatures_1 = require("../codeFeatures");
8
9
  const utils_1 = require("../utils");
9
10
  const camelized_1 = require("../utils/camelized");
10
11
  const stringLiteralKey_1 = require("../utils/stringLiteralKey");
11
12
  const interpolation_1 = require("./interpolation");
12
13
  const objectProperty_1 = require("./objectProperty");
14
+ const builtInDirectives = new Set([
15
+ 'cloak',
16
+ 'html',
17
+ 'memo',
18
+ 'once',
19
+ 'show',
20
+ 'text',
21
+ ]);
13
22
  function* generateElementDirectives(options, ctx, node) {
14
23
  for (const prop of node.props) {
15
24
  if (prop.type !== CompilerDOM.NodeTypes.DIRECTIVE
@@ -22,24 +31,22 @@ function* generateElementDirectives(options, ctx, node) {
22
31
  continue;
23
32
  }
24
33
  ctx.accessExternalVariable((0, shared_1.camelize)('v-' + prop.name), prop.loc.start.offset);
25
- yield* (0, utils_1.wrapWith)(prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, `__VLS_asFunctionalDirective(`, ...generateIdentifier(ctx, prop), `)(null!, { ...__VLS_directiveBindingRestFields, `, ...generateArg(options, ctx, prop), ...generateModifiers(options, ctx, prop), ...generateValue(options, ctx, prop), ` }, null!, null!)`);
34
+ yield* (0, utils_1.wrapWith)(prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, `__VLS_asFunctionalDirective(`, ...generateIdentifier(options, ctx, prop), `)(null!, { ...__VLS_directiveBindingRestFields, `, ...generateArg(options, ctx, prop), ...generateModifiers(options, ctx, prop), ...generateValue(options, ctx, prop), ` }, null!, null!)`);
26
35
  yield utils_1.endOfLine;
27
36
  }
28
37
  }
29
- function* generateIdentifier(ctx, prop) {
38
+ function* generateIdentifier(options, ctx, prop) {
30
39
  const rawName = 'v-' + prop.name;
31
- yield* (0, utils_1.wrapWith)(prop.loc.start.offset, prop.loc.start.offset + rawName.length, ctx.codeFeatures.verification, `__VLS_directives.`, ...(0, camelized_1.generateCamelized)(rawName, prop.loc.start.offset, {
32
- ...ctx.codeFeatures.all,
33
- verification: false,
34
- completion: {
35
- // fix https://github.com/vuejs/language-tools/issues/1905
36
- isAdditional: true,
37
- },
40
+ yield* (0, utils_1.wrapWith)(prop.loc.start.offset, prop.loc.start.offset + rawName.length, ctx.codeFeatures.verification, `__VLS_directives.`, ...(0, camelized_1.generateCamelized)(rawName, prop.loc.start.offset, ctx.resolveCodeFeatures({
41
+ ...codeFeatures_1.codeFeatures.withoutHighlight,
42
+ // fix https://github.com/vuejs/language-tools/issues/1905
43
+ ...codeFeatures_1.codeFeatures.additionalCompletion,
44
+ verification: options.vueCompilerOptions.checkUnknownDirectives && !builtInDirectives.has(prop.name),
38
45
  navigation: {
39
46
  resolveRenameNewName: shared_1.camelize,
40
47
  resolveRenameEditText: getPropRenameApply(prop.name),
41
48
  },
42
- }));
49
+ })));
43
50
  }
44
51
  function* generateArg(options, ctx, prop) {
45
52
  const { arg } = prop;
@@ -67,7 +74,7 @@ function* generateModifiers(options, ctx, prop, propertyName = 'modifiers') {
67
74
  yield* (0, utils_1.wrapWith)(startOffset, endOffset, ctx.codeFeatures.verification, propertyName);
68
75
  yield `: { `;
69
76
  for (const mod of modifiers) {
70
- yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, mod.content, mod.loc.start.offset, ctx.codeFeatures.withoutNavigation);
77
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, mod.content, mod.loc.start.offset, ctx.codeFeatures.withoutHighlightAndNavigation);
71
78
  yield `: true, `;
72
79
  }
73
80
  yield `}, `;
@@ -1,5 +1,5 @@
1
1
  import * as CompilerDOM from '@vue/compiler-dom';
2
- import type { Code } from '../../types';
2
+ import type { Code, VueCodeInformation } from '../../types';
3
3
  import type { TemplateCodegenContext } from './context';
4
4
  import type { TemplateCodegenOptions } from './index';
5
5
  export interface FailedPropExpression {
@@ -8,3 +8,4 @@ export interface FailedPropExpression {
8
8
  suffix: string;
9
9
  }
10
10
  export declare function generateElementProps(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode, props: CompilerDOM.ElementNode['props'], strictPropsCheck: boolean, enableCodeFeatures: boolean, failedPropExps?: FailedPropExpression[]): Generator<Code>;
11
+ export declare function generatePropExp(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, prop: CompilerDOM.DirectiveNode, exp: CompilerDOM.SimpleExpressionNode | undefined, features: VueCodeInformation, enableCodeFeatures: boolean): Generator<Code>;
@@ -1,11 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateElementProps = generateElementProps;
4
+ exports.generatePropExp = generatePropExp;
4
5
  const CompilerDOM = require("@vue/compiler-dom");
5
6
  const shared_1 = require("@vue/shared");
6
7
  const minimatch_1 = require("minimatch");
7
8
  const muggle_string_1 = require("muggle-string");
8
9
  const shared_2 = require("../../utils/shared");
10
+ const codeFeatures_1 = require("../codeFeatures");
9
11
  const inlayHints_1 = require("../inlayHints");
10
12
  const utils_1 = require("../utils");
11
13
  const camelized_1 = require("../utils/camelized");
@@ -80,7 +82,7 @@ function* generateElementProps(options, ctx, node, props, strictPropsCheck, enab
80
82
  }
81
83
  const codes = (0, utils_1.wrapWith)(prop.loc.start.offset, prop.loc.end.offset, ctx.codeFeatures.verification, ...(prop.arg
82
84
  ? (0, objectProperty_1.generateObjectProperty)(options, ctx, propName, prop.arg.loc.start.offset, codeInfo, prop.loc.name_2 ??= {}, shouldCamelize)
83
- : (0, utils_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), `)`);
85
+ : (0, utils_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, enableCodeFeatures), `)`);
84
86
  if (enableCodeFeatures) {
85
87
  yield* codes;
86
88
  }
@@ -91,7 +93,7 @@ function* generateElementProps(options, ctx, node, props, strictPropsCheck, enab
91
93
  yield ` }`;
92
94
  }
93
95
  yield `,${utils_1.newLine}`;
94
- if (prop.name === 'model' && prop.modifiers.length) {
96
+ if (isComponent && prop.name === 'model' && prop.modifiers.length) {
95
97
  const propertyName = prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
96
98
  ? !prop.arg.isStatic
97
99
  ? `[__VLS_tryAsConstant(\`$\{${prop.arg.content}\}Modifiers\`)]`
@@ -145,7 +147,7 @@ function* generateElementProps(options, ctx, node, props, strictPropsCheck, enab
145
147
  }
146
148
  }
147
149
  else {
148
- const codes = (0, utils_1.wrapWith)(prop.exp.loc.start.offset, prop.exp.loc.end.offset, ctx.codeFeatures.verification, `...`, ...generatePropExp(options, ctx, prop, prop.exp, ctx.codeFeatures.all, false, enableCodeFeatures));
150
+ const codes = (0, utils_1.wrapWith)(prop.exp.loc.start.offset, prop.exp.loc.end.offset, ctx.codeFeatures.verification, `...`, ...generatePropExp(options, ctx, prop, prop.exp, ctx.codeFeatures.all, enableCodeFeatures));
149
151
  if (enableCodeFeatures) {
150
152
  yield* codes;
151
153
  }
@@ -157,7 +159,8 @@ function* generateElementProps(options, ctx, node, props, strictPropsCheck, enab
157
159
  }
158
160
  }
159
161
  }
160
- function* generatePropExp(options, ctx, prop, exp, features, isShorthand, enableCodeFeatures) {
162
+ function* generatePropExp(options, ctx, prop, exp, features, enableCodeFeatures) {
163
+ const isShorthand = prop.arg?.loc.start.offset === prop.exp?.loc.start.offset;
161
164
  if (isShorthand && features.completion) {
162
165
  features = {
163
166
  ...features,
@@ -220,28 +223,21 @@ function getShouldCamelize(options, prop, propName) {
220
223
  && !options.vueCompilerOptions.htmlAttributes.some(pattern => (0, minimatch_1.minimatch)(propName, pattern));
221
224
  }
222
225
  function getPropsCodeInfo(ctx, strictPropsCheck, shouldCamelize) {
223
- const codeInfo = ctx.codeFeatures.withoutHighlightAndCompletion;
224
- return {
225
- ...codeInfo,
226
- navigation: codeInfo.navigation
227
- ? {
228
- resolveRenameNewName: shared_1.camelize,
229
- resolveRenameEditText: shouldCamelize ? shared_2.hyphenateAttr : undefined,
230
- }
231
- : false,
232
- verification: strictPropsCheck
233
- ? codeInfo.verification
234
- : {
235
- shouldReport(_source, code) {
236
- if (String(code) === '2353' || String(code) === '2561') {
237
- return false;
238
- }
239
- return typeof codeInfo.verification === 'object'
240
- ? codeInfo.verification.shouldReport?.(_source, code) ?? true
241
- : true;
242
- },
243
- }
244
- };
226
+ return ctx.resolveCodeFeatures({
227
+ ...codeFeatures_1.codeFeatures.withoutHighlightAndCompletion,
228
+ navigation: {
229
+ resolveRenameNewName: shared_1.camelize,
230
+ resolveRenameEditText: shouldCamelize ? shared_2.hyphenateAttr : undefined,
231
+ },
232
+ verification: strictPropsCheck || {
233
+ shouldReport(_source, code) {
234
+ if (String(code) === '2353' || String(code) === '2561') {
235
+ return false;
236
+ }
237
+ return true;
238
+ },
239
+ },
240
+ });
245
241
  }
246
242
  function getModelPropName(node, vueCompilerOptions) {
247
243
  for (const modelName in vueCompilerOptions.experimentalModelPropName) {
@@ -16,6 +16,7 @@ export interface TemplateCodegenOptions {
16
16
  slotsAssignName?: string;
17
17
  propsAssignName?: string;
18
18
  inheritAttrs: boolean;
19
+ selfComponentName?: string;
19
20
  }
20
21
  export declare function generateTemplate(options: TemplateCodegenOptions): Generator<Code, TemplateCodegenContext>;
21
22
  export declare function forEachElementNode(node: CompilerDOM.RootNode | CompilerDOM.TemplateChildNode): Generator<CompilerDOM.ElementNode>;
@@ -5,7 +5,6 @@ exports.forEachElementNode = forEachElementNode;
5
5
  const CompilerDOM = require("@vue/compiler-dom");
6
6
  const shared_1 = require("../../utils/shared");
7
7
  const utils_1 = require("../utils");
8
- const stringLiteralKey_1 = require("../utils/stringLiteralKey");
9
8
  const context_1 = require("./context");
10
9
  const objectProperty_1 = require("./objectProperty");
11
10
  const styleScopedClasses_1 = require("./styleScopedClasses");
@@ -18,54 +17,62 @@ function* generateTemplate(options) {
18
17
  if (options.propsAssignName) {
19
18
  ctx.addLocalVariable(options.propsAssignName);
20
19
  }
21
- ctx.addLocalVariable((0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target));
22
- ctx.addLocalVariable('$attrs');
23
- ctx.addLocalVariable('$refs');
24
- ctx.addLocalVariable('$el');
20
+ const slotsPropertyName = (0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target);
21
+ ctx.specialVars.add(slotsPropertyName);
22
+ ctx.specialVars.add('$attrs');
23
+ ctx.specialVars.add('$refs');
24
+ ctx.specialVars.add('$el');
25
25
  if (options.template.ast) {
26
26
  yield* (0, templateChild_1.generateTemplateChild)(options, ctx, options.template.ast, undefined);
27
27
  }
28
28
  yield* (0, styleScopedClasses_1.generateStyleScopedClassReferences)(ctx);
29
- yield* generateSlots(options, ctx);
30
- yield* generateInheritedAttrs(ctx);
31
- yield* generateRefs(ctx);
32
- yield* generateRootEl(ctx);
29
+ const speicalTypes = [
30
+ [slotsPropertyName, yield* generateSlots(options, ctx)],
31
+ ['$attrs', yield* generateInheritedAttrs(options, ctx)],
32
+ ['$refs', yield* generateTemplateRefs(options, ctx)],
33
+ ['$el', yield* generateRootEl(ctx)]
34
+ ];
35
+ yield `var __VLS_special!: {${utils_1.newLine}`;
36
+ for (const [name, type] of speicalTypes) {
37
+ yield `${name}: ${type}${utils_1.endOfLine}`;
38
+ }
39
+ yield `} & { [K in keyof typeof __VLS_ctx]: unknown }${utils_1.endOfLine}`;
33
40
  yield* ctx.generateAutoImportCompletion();
34
41
  return ctx;
35
42
  }
36
43
  function* generateSlots(options, ctx) {
37
44
  if (!options.hasDefineSlots) {
38
- yield `var __VLS_slots!: `;
39
- for (const { expVar, varName } of ctx.dynamicSlots) {
45
+ const name = (0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target);
46
+ yield `type __VLS_Slots = __VLS_PrettifyGlobal<__VLS_OmitStringIndex<typeof __VLS_ctx.${name}>`;
47
+ for (const { expVar, propsVar } of ctx.dynamicSlots) {
40
48
  ctx.hasSlot = true;
41
- yield `Partial<Record<NonNullable<typeof ${expVar}>, (_: typeof ${varName}) => any>> &${utils_1.newLine}`;
49
+ yield `${utils_1.newLine}& { [K in NonNullable<typeof ${expVar}>]?: (props: typeof ${propsVar}) => any }`;
42
50
  }
43
- yield `{${utils_1.newLine}`;
44
51
  for (const slot of ctx.slots) {
52
+ yield `${utils_1.newLine}& { `;
45
53
  ctx.hasSlot = true;
46
- if (slot.name && slot.loc !== undefined) {
47
- yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slot.name, slot.loc, ctx.codeFeatures.withoutHighlightAndCompletion, slot.nodeLoc);
54
+ if (slot.name && slot.offset !== undefined) {
55
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slot.name, slot.offset, ctx.codeFeatures.withoutHighlightAndCompletion, slot.nodeLoc);
48
56
  }
49
57
  else {
50
58
  yield* (0, utils_1.wrapWith)(slot.tagRange[0], slot.tagRange[1], ctx.codeFeatures.withoutHighlightAndCompletion, `default`);
51
59
  }
52
- yield `?(_: typeof ${slot.varName}): any,${utils_1.newLine}`;
60
+ yield `?: (props: typeof ${slot.propsVar}) => any }`;
53
61
  }
54
- yield `}${utils_1.endOfLine}`;
62
+ yield `>${utils_1.endOfLine}`;
55
63
  }
56
- const name = (0, shared_1.getSlotsPropertyName)(options.vueCompilerOptions.target);
57
- yield `var ${name}!: typeof ${options.slotsAssignName ?? '__VLS_slots'}${utils_1.endOfLine}`;
64
+ return `__VLS_Slots`;
58
65
  }
59
- function* generateInheritedAttrs(ctx) {
60
- yield 'let __VLS_inheritedAttrs!: {}';
66
+ function* generateInheritedAttrs(options, ctx) {
67
+ yield `type __VLS_InheritedAttrs = {}`;
61
68
  for (const varName of ctx.inheritedAttrVars) {
62
69
  yield ` & typeof ${varName}`;
63
70
  }
64
71
  yield utils_1.endOfLine;
65
- yield `var $attrs!: Partial<typeof __VLS_inheritedAttrs> & Record<string, unknown>${utils_1.endOfLine}`;
66
72
  if (ctx.bindingAttrLocs.length) {
67
73
  yield `[`;
68
74
  for (const loc of ctx.bindingAttrLocs) {
75
+ yield `__VLS_special.`;
69
76
  yield [
70
77
  loc.source,
71
78
  'template',
@@ -76,23 +83,22 @@ function* generateInheritedAttrs(ctx) {
76
83
  }
77
84
  yield `]${utils_1.endOfLine}`;
78
85
  }
86
+ return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & Partial<__VLS_InheritedAttrs>`;
79
87
  }
80
- function* generateRefs(ctx) {
81
- yield `const __VLS_refs = {${utils_1.newLine}`;
88
+ function* generateTemplateRefs(options, ctx) {
89
+ yield `type __VLS_TemplateRefs = {${utils_1.newLine}`;
82
90
  for (const [name, [varName, offset]] of ctx.templateRefs) {
83
- yield* (0, stringLiteralKey_1.generateStringLiteralKey)(name, offset, ctx.codeFeatures.navigationAndCompletion);
84
- yield `: ${varName},${utils_1.newLine}`;
91
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, name, offset, ctx.codeFeatures.navigationAndCompletion);
92
+ yield `: typeof ${varName},${utils_1.newLine}`;
85
93
  }
86
94
  yield `}${utils_1.endOfLine}`;
87
- yield `var $refs!: typeof __VLS_refs${utils_1.endOfLine}`;
95
+ return `__VLS_TemplateRefs`;
88
96
  }
89
97
  function* generateRootEl(ctx) {
90
- if (ctx.singleRootElType) {
91
- yield `var $el!: ${ctx.singleRootElType}${utils_1.endOfLine}`;
92
- }
93
- else {
94
- yield `var $el!: any${utils_1.endOfLine}`;
95
- }
98
+ yield `type __VLS_RootEl = `;
99
+ yield ctx.singleRootElType ?? `any`;
100
+ yield utils_1.endOfLine;
101
+ return `__VLS_RootEl`;
96
102
  }
97
103
  function* forEachElementNode(node) {
98
104
  if (node.type === CompilerDOM.NodeTypes.ROOT) {
@@ -45,15 +45,16 @@ function* generateInterpolation(options, ctx, source, data, _code, start, astHol
45
45
  }
46
46
  }
47
47
  }
48
+ ;
48
49
  function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefNames, ctx, code, offset, ast) {
49
50
  let ctxVars = [];
50
51
  const varCb = (id, isShorthand) => {
51
52
  const text = (0, scriptSetupRanges_1.getNodeText)(ts, id, ast);
52
- if (ctx.hasLocalVariable(text) ||
53
+ if (ctx.hasLocalVariable(text)
53
54
  // https://github.com/vuejs/core/blob/245230e135152900189f13a4281302de45fdcfaa/packages/compiler-core/src/transforms/transformExpression.ts#L342-L352
54
- (0, shared_1.isGloballyAllowed)(text) ||
55
- text === 'require' ||
56
- text.startsWith('__VLS_')) {
55
+ || (0, shared_1.isGloballyAllowed)(text)
56
+ || text === 'require'
57
+ || text.startsWith('__VLS_')) {
57
58
  // localVarOffsets.push(localVar.getStart(ast));
58
59
  }
59
60
  else {
@@ -86,7 +87,7 @@ function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefName
86
87
  for (let i = 0; i < ctxVars.length - 1; i++) {
87
88
  const curVar = ctxVars[i];
88
89
  const nextVar = ctxVars[i + 1];
89
- yield* generateVar(code, destructuredPropNames, templateRefNames, curVar, nextVar);
90
+ yield* generateVar(code, ctx.specialVars, destructuredPropNames, templateRefNames, curVar);
90
91
  if (nextVar.isShorthand) {
91
92
  yield [code.slice(curVar.offset + curVar.text.length, nextVar.offset + nextVar.text.length), curVar.offset + curVar.text.length];
92
93
  yield [': ', undefined];
@@ -96,7 +97,7 @@ function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefName
96
97
  }
97
98
  }
98
99
  const lastVar = ctxVars.at(-1);
99
- yield* generateVar(code, destructuredPropNames, templateRefNames, lastVar);
100
+ yield* generateVar(code, ctx.specialVars, destructuredPropNames, templateRefNames, lastVar);
100
101
  if (lastVar.offset + lastVar.text.length < code.length) {
101
102
  yield [code.slice(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length, 'endText'];
102
103
  }
@@ -105,10 +106,10 @@ function* forEachInterpolationSegment(ts, destructuredPropNames, templateRefName
105
106
  yield [code, 0];
106
107
  }
107
108
  }
108
- function* generateVar(code, destructuredPropNames, templateRefNames, curVar, nextVar = curVar) {
109
+ function* generateVar(code, specialVars, destructuredPropNames, templateRefNames, curVar) {
109
110
  // fix https://github.com/vuejs/language-tools/issues/1205
110
111
  // fix https://github.com/vuejs/language-tools/issues/1264
111
- yield ['', nextVar.offset, 'errorMappingOnly'];
112
+ yield ['', curVar.offset, 'errorMappingOnly'];
112
113
  const isDestructuredProp = destructuredPropNames?.has(curVar.text) ?? false;
113
114
  const isTemplateRef = templateRefNames?.has(curVar.text) ?? false;
114
115
  if (isTemplateRef) {
@@ -117,7 +118,10 @@ function* generateVar(code, destructuredPropNames, templateRefNames, curVar, nex
117
118
  yield [`)`, undefined];
118
119
  }
119
120
  else {
120
- if (!isDestructuredProp) {
121
+ if (specialVars.has(curVar.text)) {
122
+ yield [`__VLS_special.`, undefined];
123
+ }
124
+ else if (!isDestructuredProp) {
121
125
  yield [`__VLS_ctx.`, undefined];
122
126
  }
123
127
  yield [code.slice(curVar.offset, curVar.offset + curVar.text.length), curVar.offset];
@@ -7,9 +7,10 @@ const utils_1 = require("../utils");
7
7
  const elementChildren_1 = require("./elementChildren");
8
8
  const elementProps_1 = require("./elementProps");
9
9
  const interpolation_1 = require("./interpolation");
10
+ const propertyAccess_1 = require("./propertyAccess");
10
11
  function* generateSlotOutlet(options, ctx, node) {
11
12
  const startTagOffset = node.loc.start.offset + options.template.content.slice(node.loc.start.offset).indexOf(node.tag);
12
- const varSlot = ctx.getInternalVariable();
13
+ const propsVar = ctx.getInternalVariable();
13
14
  const nameProp = node.props.find(prop => {
14
15
  if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {
15
16
  return prop.name === 'name';
@@ -22,27 +23,48 @@ function* generateSlotOutlet(options, ctx, node) {
22
23
  });
23
24
  if (options.hasDefineSlots) {
24
25
  yield `__VLS_normalizeSlot(`;
25
- yield* (0, utils_1.wrapWith)(node.loc.start.offset, node.loc.end.offset, ctx.codeFeatures.verification, `${options.slotsAssignName ?? '__VLS_slots'}[`, ...(0, utils_1.wrapWith)(node.loc.start.offset, node.loc.end.offset, ctx.codeFeatures.verification, nameProp?.type === CompilerDOM.NodeTypes.ATTRIBUTE && nameProp.value
26
- ? `'${nameProp.value.content}'`
27
- : nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
28
- ? nameProp.exp.content
29
- : `('default' as const)`), `]`);
26
+ if (nameProp) {
27
+ let codes;
28
+ if (nameProp.type === CompilerDOM.NodeTypes.ATTRIBUTE && nameProp.value) {
29
+ let { source, start: { offset } } = nameProp.value.loc;
30
+ if (source.startsWith('"') || source.startsWith("'")) {
31
+ source = source.slice(1, -1);
32
+ offset++;
33
+ }
34
+ codes = (0, propertyAccess_1.generatePropertyAccess)(options, ctx, source, offset, ctx.codeFeatures.navigationAndVerification);
35
+ }
36
+ else if (nameProp.type === CompilerDOM.NodeTypes.DIRECTIVE
37
+ && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
38
+ codes = [
39
+ `[`,
40
+ ...(0, elementProps_1.generatePropExp)(options, ctx, nameProp, nameProp.exp, ctx.codeFeatures.all, true),
41
+ `]`
42
+ ];
43
+ }
44
+ else {
45
+ codes = [`['default']`];
46
+ }
47
+ yield* (0, utils_1.wrapWith)(nameProp.loc.start.offset, nameProp.loc.end.offset, ctx.codeFeatures.verification, `${options.slotsAssignName ?? '__VLS_slots'}`, ...codes);
48
+ }
49
+ else {
50
+ yield* (0, utils_1.wrapWith)(node.loc.start.offset, node.loc.end.offset, ctx.codeFeatures.verification, `${options.slotsAssignName ?? '__VLS_slots'}['default']`);
51
+ }
30
52
  yield `)?.(`;
31
53
  yield* (0, utils_1.wrapWith)(startTagOffset, startTagOffset + node.tag.length, ctx.codeFeatures.verification, `{${utils_1.newLine}`, ...(0, elementProps_1.generateElementProps)(options, ctx, node, node.props.filter(prop => prop !== nameProp), true, true), `}`);
32
54
  yield `)${utils_1.endOfLine}`;
33
55
  }
34
56
  else {
35
- yield `var ${varSlot} = {${utils_1.newLine}`;
36
- yield* (0, elementProps_1.generateElementProps)(options, ctx, node, node.props.filter(prop => prop !== nameProp), options.vueCompilerOptions.strictTemplates, true);
57
+ yield `var ${propsVar} = {${utils_1.newLine}`;
58
+ yield* (0, elementProps_1.generateElementProps)(options, ctx, node, node.props.filter(prop => prop !== nameProp), options.vueCompilerOptions.checkUnknownProps, true);
37
59
  yield `}${utils_1.endOfLine}`;
38
60
  if (nameProp?.type === CompilerDOM.NodeTypes.ATTRIBUTE
39
61
  && nameProp.value) {
40
62
  ctx.slots.push({
41
63
  name: nameProp.value.content,
42
- loc: nameProp.loc.start.offset + nameProp.loc.source.indexOf(nameProp.value.content, nameProp.name.length),
64
+ offset: nameProp.loc.start.offset + nameProp.loc.source.indexOf(nameProp.value.content, nameProp.name.length),
43
65
  tagRange: [startTagOffset, startTagOffset + node.tag.length],
44
- varName: varSlot,
45
66
  nodeLoc: node.loc,
67
+ propsVar,
46
68
  });
47
69
  }
48
70
  else if (nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE
@@ -51,21 +73,21 @@ function* generateSlotOutlet(options, ctx, node) {
51
73
  if (isShortHand) {
52
74
  ctx.inlayHints.push((0, inlayHints_1.createVBindShorthandInlayHintInfo)(nameProp.exp.loc, 'name'));
53
75
  }
54
- const slotExpVar = ctx.getInternalVariable();
55
- yield `var ${slotExpVar} = `;
56
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, nameProp.exp.content, nameProp.exp.loc.start.offset, nameProp.exp, '(', ')');
57
- yield ` as const${utils_1.endOfLine}`;
76
+ const expVar = ctx.getInternalVariable();
77
+ yield `var ${expVar} = __VLS_tryAsConstant(`;
78
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', ctx.codeFeatures.all, nameProp.exp.content, nameProp.exp.loc.start.offset, nameProp.exp);
79
+ yield `)${utils_1.endOfLine}`;
58
80
  ctx.dynamicSlots.push({
59
- expVar: slotExpVar,
60
- varName: varSlot,
81
+ expVar,
82
+ propsVar,
61
83
  });
62
84
  }
63
85
  else {
64
86
  ctx.slots.push({
65
87
  name: 'default',
66
88
  tagRange: [startTagOffset, startTagOffset + node.tag.length],
67
- varName: varSlot,
68
89
  nodeLoc: node.loc,
90
+ propsVar,
69
91
  });
70
92
  }
71
93
  }
@@ -1,3 +1,6 @@
1
+ import * as CompilerDOM from '@vue/compiler-dom';
1
2
  import type { Code } from '../../types';
2
3
  import type { TemplateCodegenContext } from './context';
4
+ import type { TemplateCodegenOptions } from './index';
3
5
  export declare function generateStyleScopedClassReferences(ctx: TemplateCodegenContext, withDot?: boolean): Generator<Code>;
6
+ export declare function collectStyleScopedClassReferences(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, node: CompilerDOM.ElementNode): void;
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateStyleScopedClassReferences = generateStyleScopedClassReferences;
4
+ exports.collectStyleScopedClassReferences = collectStyleScopedClassReferences;
5
+ const CompilerDOM = require("@vue/compiler-dom");
6
+ const scriptSetupRanges_1 = require("../../parsers/scriptSetupRanges");
4
7
  const utils_1 = require("../utils");
5
8
  function* generateStyleScopedClassReferences(ctx, withDot = false) {
6
9
  if (!ctx.emptyClassOffsets.length && !ctx.scopedClasses.length) {
@@ -72,4 +75,142 @@ function* generateStyleScopedClassReferences(ctx, withDot = false) {
72
75
  }
73
76
  }
74
77
  }
78
+ function collectStyleScopedClassReferences(options, ctx, node) {
79
+ for (const prop of node.props) {
80
+ if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
81
+ && prop.name === 'class'
82
+ && prop.value) {
83
+ if (options.template.lang === 'pug') {
84
+ const getClassOffset = Reflect.get(prop.value.loc.start, 'getClassOffset');
85
+ const content = prop.value.loc.source.slice(1, -1);
86
+ let startOffset = 1;
87
+ for (const className of content.split(' ')) {
88
+ if (className) {
89
+ ctx.scopedClasses.push({
90
+ source: 'template',
91
+ className,
92
+ offset: getClassOffset(startOffset),
93
+ });
94
+ }
95
+ startOffset += className.length + 1;
96
+ }
97
+ }
98
+ else {
99
+ let isWrapped = false;
100
+ const [content, startOffset] = (0, utils_1.normalizeAttributeValue)(prop.value);
101
+ if (content) {
102
+ const classes = collectClasses(content, startOffset + (isWrapped ? 1 : 0));
103
+ ctx.scopedClasses.push(...classes);
104
+ }
105
+ else {
106
+ ctx.emptyClassOffsets.push(startOffset);
107
+ }
108
+ }
109
+ }
110
+ else if (prop.type === CompilerDOM.NodeTypes.DIRECTIVE
111
+ && prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
112
+ && prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
113
+ && prop.arg.content === 'class') {
114
+ const content = '`${' + prop.exp.content + '}`';
115
+ const startOffset = prop.exp.loc.start.offset - 3;
116
+ const { ts } = options;
117
+ const ast = ts.createSourceFile('', content, 99);
118
+ const literals = [];
119
+ ts.forEachChild(ast, node => {
120
+ if (!ts.isExpressionStatement(node) ||
121
+ !isTemplateExpression(node.expression)) {
122
+ return;
123
+ }
124
+ const expression = node.expression.templateSpans[0].expression;
125
+ if (ts.isStringLiteralLike(expression)) {
126
+ literals.push(expression);
127
+ }
128
+ if (ts.isArrayLiteralExpression(expression)) {
129
+ walkArrayLiteral(expression);
130
+ }
131
+ if (ts.isObjectLiteralExpression(expression)) {
132
+ walkObjectLiteral(expression);
133
+ }
134
+ });
135
+ for (const literal of literals) {
136
+ if (literal.text) {
137
+ const classes = collectClasses(literal.text, literal.end - literal.text.length - 1 + startOffset);
138
+ ctx.scopedClasses.push(...classes);
139
+ }
140
+ else {
141
+ ctx.emptyClassOffsets.push(literal.end - 1 + startOffset);
142
+ }
143
+ }
144
+ function walkArrayLiteral(node) {
145
+ const { elements } = node;
146
+ for (const element of elements) {
147
+ if (ts.isStringLiteralLike(element)) {
148
+ literals.push(element);
149
+ }
150
+ else if (ts.isObjectLiteralExpression(element)) {
151
+ walkObjectLiteral(element);
152
+ }
153
+ }
154
+ }
155
+ function walkObjectLiteral(node) {
156
+ const { properties } = node;
157
+ for (const property of properties) {
158
+ if (ts.isPropertyAssignment(property)) {
159
+ const { name } = property;
160
+ if (ts.isIdentifier(name)) {
161
+ walkIdentifier(name);
162
+ }
163
+ else if (ts.isStringLiteral(name)) {
164
+ literals.push(name);
165
+ }
166
+ else if (ts.isComputedPropertyName(name)) {
167
+ const { expression } = name;
168
+ if (ts.isStringLiteralLike(expression)) {
169
+ literals.push(expression);
170
+ }
171
+ }
172
+ }
173
+ else if (ts.isShorthandPropertyAssignment(property)) {
174
+ walkIdentifier(property.name);
175
+ }
176
+ }
177
+ }
178
+ function walkIdentifier(node) {
179
+ const text = (0, scriptSetupRanges_1.getNodeText)(ts, node, ast);
180
+ ctx.scopedClasses.push({
181
+ source: 'template',
182
+ className: text,
183
+ offset: node.end - text.length + startOffset
184
+ });
185
+ }
186
+ }
187
+ }
188
+ }
189
+ function collectClasses(content, startOffset = 0) {
190
+ const classes = [];
191
+ let currentClassName = '';
192
+ let offset = 0;
193
+ for (const char of (content + ' ')) {
194
+ if (char.trim() === '') {
195
+ if (currentClassName !== '') {
196
+ classes.push({
197
+ source: 'template',
198
+ className: currentClassName,
199
+ offset: offset + startOffset
200
+ });
201
+ offset += currentClassName.length;
202
+ currentClassName = '';
203
+ }
204
+ offset += char.length;
205
+ }
206
+ else {
207
+ currentClassName += char;
208
+ }
209
+ }
210
+ return classes;
211
+ }
212
+ // isTemplateExpression is missing in tsc
213
+ function isTemplateExpression(node) {
214
+ return node.kind === 228;
215
+ }
75
216
  //# sourceMappingURL=styleScopedClasses.js.map