@vue/language-core 3.1.4 → 3.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 (101) hide show
  1. package/index.d.ts +2 -1
  2. package/index.js +3 -2
  3. package/lib/codegen/codeFeatures.d.ts +5 -9
  4. package/lib/codegen/codeFeatures.js +5 -5
  5. package/lib/codegen/globalTypes.js +15 -20
  6. package/lib/codegen/localTypes.d.ts +1 -1
  7. package/lib/codegen/localTypes.js +6 -6
  8. package/lib/codegen/names.d.ts +30 -0
  9. package/lib/codegen/names.js +34 -0
  10. package/lib/codegen/script/component.js +45 -54
  11. package/lib/codegen/script/context.d.ts +2 -5
  12. package/lib/codegen/script/context.js +1 -7
  13. package/lib/codegen/script/index.d.ts +10 -12
  14. package/lib/codegen/script/index.js +74 -73
  15. package/lib/codegen/script/scriptSetup.d.ts +3 -2
  16. package/lib/codegen/script/scriptSetup.js +209 -283
  17. package/lib/codegen/script/src.js +9 -3
  18. package/lib/codegen/script/template.js +64 -108
  19. package/lib/codegen/style/common.d.ts +3 -0
  20. package/lib/codegen/style/common.js +43 -0
  21. package/lib/codegen/style/index.d.ts +63 -0
  22. package/lib/codegen/style/index.js +38 -0
  23. package/lib/codegen/style/modules.d.ts +3 -2
  24. package/lib/codegen/style/modules.js +12 -11
  25. package/lib/codegen/style/scopedClasses.d.ts +2 -3
  26. package/lib/codegen/style/scopedClasses.js +23 -21
  27. package/lib/codegen/template/context.d.ts +10 -19
  28. package/lib/codegen/template/context.js +82 -94
  29. package/lib/codegen/template/element.js +174 -65
  30. package/lib/codegen/template/elementDirectives.js +32 -12
  31. package/lib/codegen/template/elementEvents.d.ts +1 -1
  32. package/lib/codegen/template/elementEvents.js +30 -35
  33. package/lib/codegen/template/elementProps.d.ts +3 -3
  34. package/lib/codegen/template/elementProps.js +64 -83
  35. package/lib/codegen/template/index.d.ts +11 -22
  36. package/lib/codegen/template/index.js +85 -80
  37. package/lib/codegen/template/interpolation.d.ts +3 -3
  38. package/lib/codegen/template/interpolation.js +108 -155
  39. package/lib/codegen/template/objectProperty.js +8 -4
  40. package/lib/codegen/template/propertyAccess.d.ts +1 -1
  41. package/lib/codegen/template/propertyAccess.js +5 -7
  42. package/lib/codegen/template/slotOutlet.js +26 -14
  43. package/lib/codegen/template/styleScopedClasses.d.ts +3 -6
  44. package/lib/codegen/template/styleScopedClasses.js +23 -149
  45. package/lib/codegen/template/templateChild.d.ts +0 -1
  46. package/lib/codegen/template/templateChild.js +11 -68
  47. package/lib/codegen/template/vFor.js +10 -13
  48. package/lib/codegen/template/vIf.js +5 -3
  49. package/lib/codegen/template/vSlot.js +20 -16
  50. package/lib/codegen/utils/boundary.d.ts +3 -0
  51. package/lib/codegen/utils/boundary.js +13 -0
  52. package/lib/codegen/utils/camelized.js +3 -3
  53. package/lib/codegen/utils/escaped.js +4 -2
  54. package/lib/codegen/utils/index.d.ts +3 -6
  55. package/lib/codegen/utils/index.js +41 -26
  56. package/lib/codegen/utils/merge.d.ts +2 -2
  57. package/lib/codegen/utils/merge.js +9 -9
  58. package/lib/codegen/utils/stringLiteralKey.js +6 -3
  59. package/lib/codegen/utils/transform.d.ts +8 -0
  60. package/lib/codegen/utils/transform.js +27 -0
  61. package/lib/codegen/utils/unicode.js +4 -2
  62. package/lib/compilerOptions.js +4 -4
  63. package/lib/languagePlugin.d.ts +1 -1
  64. package/lib/languagePlugin.js +18 -25
  65. package/lib/plugins/vue-template-html.js +12 -9
  66. package/lib/plugins/vue-template-inline-css.js +8 -18
  67. package/lib/plugins/vue-template-inline-ts.js +12 -14
  68. package/lib/plugins/vue-tsx.d.ts +14 -23
  69. package/lib/plugins/vue-tsx.js +121 -69
  70. package/lib/plugins.js +1 -1
  71. package/lib/types.d.ts +5 -4
  72. package/lib/utils/parseSfc.js +10 -11
  73. package/lib/utils/shared.d.ts +1 -0
  74. package/lib/utils/shared.js +9 -0
  75. package/lib/utils/signals.d.ts +2 -2
  76. package/lib/utils/signals.js +8 -6
  77. package/lib/virtualCode/embeddedCodes.d.ts +12 -0
  78. package/lib/virtualCode/embeddedCodes.js +249 -0
  79. package/lib/{virtualFile/vueFile.d.ts → virtualCode/index.d.ts} +9 -9
  80. package/lib/virtualCode/index.js +81 -0
  81. package/lib/virtualCode/ir.d.ts +4 -0
  82. package/lib/{virtualFile/computedSfc.js → virtualCode/ir.js} +65 -96
  83. package/lib/virtualCode/normalize.d.ts +2 -0
  84. package/lib/virtualCode/normalize.js +170 -0
  85. package/package.json +4 -4
  86. package/lib/codegen/style/classProperty.d.ts +0 -2
  87. package/lib/codegen/style/classProperty.js +0 -18
  88. package/lib/codegen/style/imports.d.ts +0 -2
  89. package/lib/codegen/style/imports.js +0 -27
  90. package/lib/codegen/template/elementChildren.d.ts +0 -5
  91. package/lib/codegen/template/elementChildren.js +0 -12
  92. package/lib/codegen/utils/wrapWith.d.ts +0 -3
  93. package/lib/codegen/utils/wrapWith.js +0 -24
  94. package/lib/virtualFile/computedEmbeddedCodes.d.ts +0 -4
  95. package/lib/virtualFile/computedEmbeddedCodes.js +0 -262
  96. package/lib/virtualFile/computedSfc.d.ts +0 -6
  97. package/lib/virtualFile/computedVueSfc.d.ts +0 -4
  98. package/lib/virtualFile/computedVueSfc.js +0 -41
  99. package/lib/virtualFile/embeddedFile.d.ts +0 -11
  100. package/lib/virtualFile/embeddedFile.js +0 -14
  101. package/lib/virtualFile/vueFile.js +0 -49
@@ -2,108 +2,116 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateTemplate = generate;
4
4
  const codeFeatures_1 = require("../codeFeatures");
5
+ const names = require("../names");
5
6
  const utils_1 = require("../utils");
6
- const wrapWith_1 = require("../utils/wrapWith");
7
+ const boundary_1 = require("../utils/boundary");
7
8
  const context_1 = require("./context");
8
9
  const objectProperty_1 = require("./objectProperty");
9
- const styleScopedClasses_1 = require("./styleScopedClasses");
10
10
  const templateChild_1 = require("./templateChild");
11
11
  function generate(options) {
12
- const context = (0, context_1.createTemplateCodegenContext)(options, options.template.ast);
13
- const codegen = generateTemplate(options, context);
12
+ const ctx = (0, context_1.createTemplateCodegenContext)();
13
+ const codeGenerator = generateWorker(options, ctx);
14
14
  const codes = [];
15
- for (const code of codegen) {
15
+ for (const code of codeGenerator) {
16
16
  if (typeof code === 'object') {
17
- code[3] = context.resolveCodeFeatures(code[3]);
17
+ code[3] = ctx.resolveCodeFeatures(code[3]);
18
18
  }
19
19
  codes.push(code);
20
20
  }
21
- return {
22
- ...context,
23
- codes,
24
- };
21
+ return { ...ctx, codes };
25
22
  }
26
- function* generateTemplate(options, ctx) {
27
- if (options.slotsAssignName) {
28
- ctx.addLocalVariable(options.slotsAssignName);
23
+ function* generateWorker(options, ctx) {
24
+ const endScope = ctx.startScope();
25
+ ctx.declare(...options.setupConsts);
26
+ const { slotsAssignName, propsAssignName, vueCompilerOptions, template, } = options;
27
+ if (slotsAssignName) {
28
+ ctx.declare(slotsAssignName);
29
29
  }
30
- if (options.propsAssignName) {
31
- ctx.addLocalVariable(options.propsAssignName);
30
+ if (propsAssignName) {
31
+ ctx.declare(propsAssignName);
32
32
  }
33
- if (options.vueCompilerOptions.inferTemplateDollarSlots) {
33
+ if (vueCompilerOptions.inferTemplateDollarSlots) {
34
34
  ctx.dollarVars.add('$slots');
35
35
  }
36
- if (options.vueCompilerOptions.inferTemplateDollarAttrs) {
36
+ if (vueCompilerOptions.inferTemplateDollarAttrs) {
37
37
  ctx.dollarVars.add('$attrs');
38
38
  }
39
- if (options.vueCompilerOptions.inferTemplateDollarRefs) {
39
+ if (vueCompilerOptions.inferTemplateDollarRefs) {
40
40
  ctx.dollarVars.add('$refs');
41
41
  }
42
- if (options.vueCompilerOptions.inferTemplateDollarEl) {
42
+ if (vueCompilerOptions.inferTemplateDollarEl) {
43
43
  ctx.dollarVars.add('$el');
44
44
  }
45
- if (options.template.ast) {
46
- yield* (0, templateChild_1.generateTemplateChild)(options, ctx, options.template.ast);
45
+ if (template.ast) {
46
+ yield* (0, templateChild_1.generateTemplateChild)(options, ctx, template.ast);
47
47
  }
48
- yield* (0, styleScopedClasses_1.generateStyleScopedClassReferences)(ctx);
49
48
  yield* ctx.generateHoistVariables();
50
- const dollarTypes = [
51
- ['$slots', yield* generateSlots(options, ctx)],
52
- ['$attrs', yield* generateInheritedAttrs(options, ctx)],
53
- ['$refs', yield* generateTemplateRefs(options, ctx)],
54
- ['$el', yield* generateRootEl(ctx)],
55
- ].filter(([name]) => ctx.dollarVars.has(name));
56
- if (dollarTypes.length) {
57
- yield `var __VLS_dollars!: {${utils_1.newLine}`;
58
- for (const [name, type] of dollarTypes) {
59
- yield `${name}: ${type}${utils_1.endOfLine}`;
49
+ yield* generateSlotsType(options, ctx);
50
+ yield* generateInheritedAttrsType(ctx);
51
+ yield* generateTemplateRefsType(options, ctx);
52
+ yield* generateRootElType(ctx);
53
+ if (ctx.dollarVars.size) {
54
+ yield `var ${names.dollars}!: {${utils_1.newLine}`;
55
+ if (ctx.dollarVars.has('$slots')) {
56
+ const type = ctx.generatedTypes.has(names.Slots) ? names.Slots : `{}`;
57
+ yield `$slots: ${type}${utils_1.endOfLine}`;
60
58
  }
61
- yield `} & { [K in keyof import('${options.vueCompilerOptions.lib}').ComponentPublicInstance]: unknown }${utils_1.endOfLine}`;
59
+ if (ctx.dollarVars.has('$attrs')) {
60
+ yield `$attrs: import('${vueCompilerOptions.lib}').ComponentPublicInstance['$attrs']`;
61
+ if (ctx.generatedTypes.has(names.InheritedAttrs)) {
62
+ yield ` & ${names.InheritedAttrs}`;
63
+ }
64
+ yield utils_1.endOfLine;
65
+ }
66
+ if (ctx.dollarVars.has('$refs')) {
67
+ const type = ctx.generatedTypes.has(names.TemplateRefs) ? names.TemplateRefs : `{}`;
68
+ yield `$refs: ${type}${utils_1.endOfLine}`;
69
+ }
70
+ if (ctx.dollarVars.has('$el')) {
71
+ const type = ctx.generatedTypes.has(names.RootEl) ? names.RootEl : `any`;
72
+ yield `$el: ${type}${utils_1.endOfLine}`;
73
+ }
74
+ yield `} & { [K in keyof import('${vueCompilerOptions.lib}').ComponentPublicInstance]: unknown }${utils_1.endOfLine}`;
62
75
  }
76
+ yield* endScope();
63
77
  }
64
- function* generateSlots(options, ctx) {
65
- if (!options.hasDefineSlots) {
66
- yield `type __VLS_Slots = {}`;
67
- for (const { expVar, propsVar } of ctx.dynamicSlots) {
68
- yield `${utils_1.newLine}& { [K in NonNullable<typeof ${expVar}>]?: (props: typeof ${propsVar}) => any }`;
78
+ function* generateSlotsType(options, ctx) {
79
+ if (options.hasDefineSlots || (!ctx.slots.length && !ctx.dynamicSlots.length)) {
80
+ return;
81
+ }
82
+ ctx.generatedTypes.add(names.Slots);
83
+ yield `type ${names.Slots} = {}`;
84
+ for (const { expVar, propsVar } of ctx.dynamicSlots) {
85
+ yield `${utils_1.newLine}& { [K in NonNullable<typeof ${expVar}>]?: (props: typeof ${propsVar}) => any }`;
86
+ }
87
+ for (const slot of ctx.slots) {
88
+ yield `${utils_1.newLine}& { `;
89
+ if (slot.name && slot.offset !== undefined) {
90
+ yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slot.name, slot.offset, codeFeatures_1.codeFeatures.navigation);
69
91
  }
70
- for (const slot of ctx.slots) {
71
- yield `${utils_1.newLine}& { `;
72
- if (slot.name && slot.offset !== undefined) {
73
- yield* (0, objectProperty_1.generateObjectProperty)(options, ctx, slot.name, slot.offset, codeFeatures_1.codeFeatures.navigation);
74
- }
75
- else {
76
- yield* (0, wrapWith_1.wrapWith)(slot.tagRange[0], slot.tagRange[1], codeFeatures_1.codeFeatures.navigation, `default`);
77
- }
78
- yield `?: (props: typeof ${slot.propsVar}) => any }`;
92
+ else {
93
+ const token = yield* (0, boundary_1.startBoundary)('template', slot.tagRange[0], codeFeatures_1.codeFeatures.navigation);
94
+ yield `default`;
95
+ yield (0, boundary_1.endBoundary)(token, slot.tagRange[1]);
79
96
  }
80
- yield utils_1.endOfLine;
97
+ yield `?: (props: typeof ${slot.propsVar}) => any }`;
81
98
  }
82
- return `__VLS_Slots`;
83
- }
84
- function* generateInheritedAttrs(options, ctx) {
85
- yield `type __VLS_InheritedAttrs = ${ctx.inheritedAttrVars.size
86
- ? `Partial<${[...ctx.inheritedAttrVars].map(name => `typeof ${name}`).join(` & `)}>`
87
- : `{}`}`;
88
99
  yield utils_1.endOfLine;
89
- if (ctx.bindingAttrLocs.length) {
90
- yield `[`;
91
- for (const loc of ctx.bindingAttrLocs) {
92
- yield `__VLS_dollars.`;
93
- yield [
94
- loc.source,
95
- 'template',
96
- loc.start.offset,
97
- codeFeatures_1.codeFeatures.all,
98
- ];
99
- yield `,`;
100
- }
101
- yield `]${utils_1.endOfLine}`;
100
+ }
101
+ function* generateInheritedAttrsType(ctx) {
102
+ if (!ctx.inheritedAttrVars.size) {
103
+ return;
102
104
  }
103
- return `import('${options.vueCompilerOptions.lib}').ComponentPublicInstance['$attrs'] & __VLS_InheritedAttrs`;
105
+ ctx.generatedTypes.add(names.InheritedAttrs);
106
+ yield `type ${names.InheritedAttrs} = Partial<${[...ctx.inheritedAttrVars].map(name => `typeof ${name}`).join(` & `)}>`;
107
+ yield utils_1.endOfLine;
104
108
  }
105
- function* generateTemplateRefs(options, ctx) {
106
- yield `type __VLS_TemplateRefs = {}`;
109
+ function* generateTemplateRefsType(options, ctx) {
110
+ if (!ctx.templateRefs.size) {
111
+ return;
112
+ }
113
+ ctx.generatedTypes.add(names.TemplateRefs);
114
+ yield `type ${names.TemplateRefs} = {}`;
107
115
  for (const [name, refs] of ctx.templateRefs) {
108
116
  yield `${utils_1.newLine}& `;
109
117
  if (refs.length >= 2) {
@@ -123,19 +131,16 @@ function* generateTemplateRefs(options, ctx) {
123
131
  }
124
132
  }
125
133
  yield utils_1.endOfLine;
126
- return `__VLS_TemplateRefs`;
127
134
  }
128
- function* generateRootEl(ctx) {
129
- yield `type __VLS_RootEl = `;
130
- if (ctx.singleRootElTypes.length && !ctx.singleRootNodes.has(null)) {
131
- for (const type of ctx.singleRootElTypes) {
132
- yield `${utils_1.newLine}| ${type}`;
133
- }
135
+ function* generateRootElType(ctx) {
136
+ if (!ctx.singleRootElTypes.size || ctx.singleRootNodes.has(null)) {
137
+ return;
134
138
  }
135
- else {
136
- yield `any`;
139
+ ctx.generatedTypes.add(names.RootEl);
140
+ yield `type ${names.RootEl} = `;
141
+ for (const type of ctx.singleRootElTypes) {
142
+ yield `${utils_1.newLine}| ${type}`;
137
143
  }
138
144
  yield utils_1.endOfLine;
139
- return `__VLS_RootEl`;
140
145
  }
141
146
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,5 @@
1
- import type { Code, VueCodeInformation } from '../../types';
2
- import type { ScriptCodegenOptions } from '../script';
1
+ import type { Code, SfcBlock, VueCodeInformation } from '../../types';
2
+ import type { StyleCodegenOptions } from '../style';
3
3
  import type { TemplateCodegenContext } from './context';
4
4
  import type { TemplateCodegenOptions } from './index';
5
- export declare function generateInterpolation(options: TemplateCodegenOptions | ScriptCodegenOptions, ctx: TemplateCodegenContext, source: string, data: VueCodeInformation | ((offset: number) => VueCodeInformation) | undefined, code: string, start: number | undefined, prefix?: string, suffix?: string): Generator<Code>;
5
+ export declare function generateInterpolation(options: Pick<TemplateCodegenOptions | StyleCodegenOptions, 'ts' | 'setupRefs'>, ctx: TemplateCodegenContext, block: SfcBlock, data: VueCodeInformation, code: string, start: number, prefix?: string, suffix?: string): Generator<Code>;
@@ -5,246 +5,199 @@ const shared_1 = require("@vue/shared");
5
5
  const collectBindings_1 = require("../../utils/collectBindings");
6
6
  const shared_2 = require("../../utils/shared");
7
7
  const codeFeatures_1 = require("../codeFeatures");
8
+ const names = require("../names");
8
9
  const utils_1 = require("../utils");
9
10
  // https://github.com/vuejs/core/blob/fb0c3ca519f1fccf52049cd6b8db3a67a669afe9/packages/compiler-core/src/transforms/transformExpression.ts#L47
10
11
  const isLiteralWhitelisted = /*@__PURE__*/ (0, shared_1.makeMap)('true,false,null,this');
11
- function* generateInterpolation(options, ctx, source, data, code, start, prefix = '', suffix = '') {
12
- const { ts, destructuredPropNames, templateRefNames, } = options;
13
- for (let [section, offset, type] of forEachInterpolationSegment(ts, ctx.inlineTsAsts, destructuredPropNames, templateRefNames, ctx, code, start, prefix, suffix)) {
14
- if (offset === undefined) {
15
- yield section;
12
+ function* generateInterpolation(options, ctx, block, data, code, start, prefix = '', suffix = '') {
13
+ for (const segment of forEachInterpolationSegment(options, ctx, block, code, start, prefix, suffix)) {
14
+ if (typeof segment === 'string') {
15
+ yield segment;
16
+ continue;
16
17
  }
17
- else {
18
- offset -= prefix.length;
19
- let addSuffix = '';
20
- const overLength = offset + section.length - code.length;
21
- if (overLength > 0) {
22
- addSuffix = section.slice(section.length - overLength);
23
- section = section.slice(0, -overLength);
24
- }
25
- if (offset < 0) {
26
- yield section.slice(0, -offset);
27
- section = section.slice(-offset);
28
- offset = 0;
29
- }
30
- const shouldSkip = section.length === 0 && (type === 'startText' || type === 'endText');
31
- if (!shouldSkip) {
32
- if (start !== undefined
33
- && data) {
34
- yield [
35
- section,
36
- source,
37
- start + offset,
38
- type === 'errorMappingOnly'
39
- ? codeFeatures_1.codeFeatures.verification
40
- : typeof data === 'function'
41
- ? data(start + offset)
42
- : data,
43
- ];
44
- }
45
- else {
46
- yield section;
47
- }
48
- }
49
- yield addSuffix;
18
+ let [section, offset, type] = segment;
19
+ offset -= prefix.length;
20
+ let addSuffix = '';
21
+ const overLength = offset + section.length - code.length;
22
+ if (overLength > 0) {
23
+ addSuffix = section.slice(section.length - overLength);
24
+ section = section.slice(0, -overLength);
25
+ }
26
+ if (offset < 0) {
27
+ yield section.slice(0, -offset);
28
+ section = section.slice(-offset);
29
+ offset = 0;
50
30
  }
31
+ const shouldSkip = section.length === 0 && type === 'startEnd';
32
+ if (!shouldSkip) {
33
+ yield [
34
+ section,
35
+ block.name,
36
+ start + offset,
37
+ type === 'errorMappingOnly'
38
+ ? codeFeatures_1.codeFeatures.verification
39
+ : data,
40
+ ];
41
+ }
42
+ yield addSuffix;
51
43
  }
52
44
  }
53
- function* forEachInterpolationSegment(ts, inlineTsAsts, destructuredPropNames, templateRefNames, ctx, originalCode, start, prefix, suffix) {
45
+ function* forEachInterpolationSegment({ ts, setupRefs }, ctx, block, originalCode, start, prefix, suffix) {
54
46
  const code = prefix + originalCode + suffix;
55
- const offset = start !== undefined ? start - prefix.length : undefined;
56
- let ctxVars = [];
57
- if (utils_1.identifierRegex.test(originalCode) && !shouldIdentifierSkipped(ctx, originalCode, destructuredPropNames)) {
58
- ctxVars.push({
59
- text: originalCode,
60
- offset: prefix.length,
61
- });
62
- }
63
- else {
64
- const ast = (0, utils_1.createTsAst)(ts, inlineTsAsts, code);
65
- const varCb = (id, isShorthand) => {
66
- const text = (0, shared_2.getNodeText)(ts, id, ast);
67
- if (!shouldIdentifierSkipped(ctx, text, destructuredPropNames)) {
68
- ctxVars.push({
69
- text,
70
- offset: (0, shared_2.getStartEnd)(ts, id, ast).start,
71
- isShorthand,
72
- });
73
- }
74
- };
75
- ts.forEachChild(ast, node => walkIdentifiers(ts, node, ast, varCb, ctx, [], true));
76
- }
77
- ctxVars = ctxVars.sort((a, b) => a.offset - b.offset);
78
- if (ctxVars.length) {
79
- for (let i = 0; i < ctxVars.length; i++) {
80
- const lastVar = ctxVars[i - 1];
81
- const curVar = ctxVars[i];
82
- const lastVarEnd = lastVar ? lastVar.offset + lastVar.text.length : 0;
83
- if (curVar.isShorthand) {
84
- yield [code.slice(lastVarEnd, curVar.offset + curVar.text.length), lastVarEnd];
85
- yield [': ', undefined];
47
+ let prevEnd = 0;
48
+ for (const [name, offset, isShorthand] of forEachIdentifiers(ts, ctx, block, originalCode, code, prefix)) {
49
+ if (isShorthand) {
50
+ yield [code.slice(prevEnd, offset + name.length), prevEnd];
51
+ yield `: `;
52
+ }
53
+ else {
54
+ yield [code.slice(prevEnd, offset), prevEnd, prevEnd > 0 ? undefined : 'startEnd'];
55
+ }
56
+ if (setupRefs.has(name)) {
57
+ yield [name, offset];
58
+ yield `.value`;
59
+ }
60
+ else {
61
+ yield ['', offset, 'errorMappingOnly']; // #1205, #1264
62
+ if (ctx.dollarVars.has(name)) {
63
+ yield names.dollars;
86
64
  }
87
65
  else {
88
- yield [code.slice(lastVarEnd, curVar.offset), lastVarEnd, i ? undefined : 'startText'];
66
+ ctx.recordComponentAccess(block.name, name, start - prefix.length + offset);
67
+ yield names.ctx;
89
68
  }
90
- yield* generateVar(templateRefNames, ctx, code, offset, curVar);
91
- }
92
- const lastVar = ctxVars.at(-1);
93
- if (lastVar.offset + lastVar.text.length < code.length) {
94
- yield [code.slice(lastVar.offset + lastVar.text.length), lastVar.offset + lastVar.text.length, 'endText'];
69
+ yield `.`;
70
+ yield [name, offset];
95
71
  }
72
+ prevEnd = offset + name.length;
96
73
  }
97
- else {
98
- yield [code, 0];
74
+ if (prevEnd < code.length) {
75
+ yield [code.slice(prevEnd), prevEnd, 'startEnd'];
99
76
  }
100
77
  }
101
- function* generateVar(templateRefNames, ctx, code, offset, curVar) {
102
- // fix https://github.com/vuejs/language-tools/issues/1205
103
- // fix https://github.com/vuejs/language-tools/issues/1264
104
- yield ['', curVar.offset, 'errorMappingOnly'];
105
- const isTemplateRef = templateRefNames?.has(curVar.text) ?? false;
106
- if (isTemplateRef) {
107
- yield [`__VLS_unref(`, undefined];
108
- yield [code.slice(curVar.offset, curVar.offset + curVar.text.length), curVar.offset];
109
- yield [`)`, undefined];
110
- }
111
- else {
112
- if (offset !== undefined) {
113
- ctx.accessExternalVariable(curVar.text, offset + curVar.offset);
114
- }
115
- else {
116
- ctx.accessExternalVariable(curVar.text);
117
- }
118
- if (ctx.dollarVars.has(curVar.text)) {
119
- yield [`__VLS_dollars.`, undefined];
120
- }
121
- else {
122
- yield [`__VLS_ctx.`, undefined];
78
+ function* forEachIdentifiers(ts, ctx, block, originalCode, code, prefix) {
79
+ if (utils_1.identifierRegex.test(originalCode) && !shouldIdentifierSkipped(ctx, originalCode)) {
80
+ yield [originalCode, prefix.length, false];
81
+ return;
82
+ }
83
+ const endScope = ctx.startScope();
84
+ const ast = (0, utils_1.getTypeScriptAST)(ts, block, code);
85
+ for (const [id, isShorthand] of forEachDeclarations(ts, ast, ast, ctx)) {
86
+ const text = (0, shared_2.getNodeText)(ts, id, ast);
87
+ if (shouldIdentifierSkipped(ctx, text)) {
88
+ continue;
123
89
  }
124
- yield [code.slice(curVar.offset, curVar.offset + curVar.text.length), curVar.offset];
90
+ yield [text, (0, shared_2.getStartEnd)(ts, id, ast).start, isShorthand];
125
91
  }
92
+ endScope();
126
93
  }
127
- function walkIdentifiers(ts, node, ast, cb, ctx, blockVars, isRoot = false) {
94
+ function* forEachDeclarations(ts, node, ast, ctx) {
128
95
  if (ts.isIdentifier(node)) {
129
- cb(node, false);
96
+ yield [node, false];
130
97
  }
131
98
  else if (ts.isShorthandPropertyAssignment(node)) {
132
- cb(node.name, true);
99
+ yield [node.name, true];
133
100
  }
134
101
  else if (ts.isPropertyAccessExpression(node)) {
135
- walkIdentifiers(ts, node.expression, ast, cb, ctx, blockVars);
102
+ yield* forEachDeclarations(ts, node.expression, ast, ctx);
136
103
  }
137
104
  else if (ts.isVariableDeclaration(node)) {
138
- const bindingNames = (0, collectBindings_1.collectBindingNames)(ts, node.name, ast);
139
- for (const name of bindingNames) {
140
- ctx.addLocalVariable(name);
141
- blockVars.push(name);
142
- }
143
- walkIdentifiersInBinding(ts, node, ast, cb, ctx, blockVars);
105
+ ctx.declare(...(0, collectBindings_1.collectBindingNames)(ts, node.name, ast));
106
+ yield* forEachDeclarationsInBinding(ts, node, ast, ctx);
144
107
  }
145
108
  else if (ts.isArrayBindingPattern(node) || ts.isObjectBindingPattern(node)) {
146
109
  for (const element of node.elements) {
147
110
  if (ts.isBindingElement(element)) {
148
- walkIdentifiersInBinding(ts, element, ast, cb, ctx, blockVars);
111
+ yield* forEachDeclarationsInBinding(ts, element, ast, ctx);
149
112
  }
150
113
  }
151
114
  }
152
115
  else if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
153
- walkIdentifiersInFunction(ts, node, ast, cb, ctx);
116
+ yield* forEachDeclarationsInFunction(ts, node, ast, ctx);
154
117
  }
155
118
  else if (ts.isObjectLiteralExpression(node)) {
156
119
  for (const prop of node.properties) {
157
120
  if (ts.isPropertyAssignment(prop)) {
158
121
  // fix https://github.com/vuejs/language-tools/issues/1176
159
122
  if (ts.isComputedPropertyName(prop.name)) {
160
- walkIdentifiers(ts, prop.name.expression, ast, cb, ctx, blockVars);
123
+ yield* forEachDeclarations(ts, prop.name.expression, ast, ctx);
161
124
  }
162
- walkIdentifiers(ts, prop.initializer, ast, cb, ctx, blockVars);
125
+ yield* forEachDeclarations(ts, prop.initializer, ast, ctx);
163
126
  }
164
127
  // fix https://github.com/vuejs/language-tools/issues/1156
165
128
  else if (ts.isShorthandPropertyAssignment(prop)) {
166
- walkIdentifiers(ts, prop, ast, cb, ctx, blockVars);
129
+ yield* forEachDeclarations(ts, prop, ast, ctx);
167
130
  }
168
131
  // fix https://github.com/vuejs/language-tools/issues/1148#issuecomment-1094378126
169
132
  else if (ts.isSpreadAssignment(prop)) {
170
133
  // TODO: cannot report "Spread types may only be created from object types.ts(2698)"
171
- walkIdentifiers(ts, prop.expression, ast, cb, ctx, blockVars);
134
+ yield* forEachDeclarations(ts, prop.expression, ast, ctx);
172
135
  }
173
136
  // fix https://github.com/vuejs/language-tools/issues/4604
174
137
  else if (ts.isFunctionLike(prop) && prop.body) {
175
- walkIdentifiersInFunction(ts, prop, ast, cb, ctx);
138
+ yield* forEachDeclarationsInFunction(ts, prop, ast, ctx);
176
139
  }
177
140
  }
178
141
  }
179
142
  // fix https://github.com/vuejs/language-tools/issues/1422
180
143
  else if (ts.isTypeNode(node)) {
181
- walkIdentifiersInTypeNode(ts, node, cb);
144
+ yield* forEachDeclarationsInTypeNode(ts, node);
182
145
  }
183
- else {
184
- const _blockVars = blockVars;
185
- if (ts.isBlock(node)) {
186
- blockVars = [];
187
- }
188
- ts.forEachChild(node, node => walkIdentifiers(ts, node, ast, cb, ctx, blockVars));
189
- if (ts.isBlock(node)) {
190
- for (const varName of blockVars) {
191
- ctx.removeLocalVariable(varName);
192
- }
146
+ else if (ts.isBlock(node)) {
147
+ const endScope = ctx.startScope();
148
+ for (const child of (0, utils_1.forEachNode)(ts, node)) {
149
+ yield* forEachDeclarations(ts, child, ast, ctx);
193
150
  }
194
- blockVars = _blockVars;
151
+ endScope();
195
152
  }
196
- if (isRoot) {
197
- for (const varName of blockVars) {
198
- ctx.removeLocalVariable(varName);
153
+ else {
154
+ for (const child of (0, utils_1.forEachNode)(ts, node)) {
155
+ yield* forEachDeclarations(ts, child, ast, ctx);
199
156
  }
200
157
  }
201
158
  }
202
- function walkIdentifiersInBinding(ts, node, ast, cb, ctx, blockVars) {
159
+ function* forEachDeclarationsInBinding(ts, node, ast, ctx) {
203
160
  if ('type' in node && node.type) {
204
- walkIdentifiersInTypeNode(ts, node.type, cb);
161
+ yield* forEachDeclarationsInTypeNode(ts, node.type);
205
162
  }
206
163
  if (!ts.isIdentifier(node.name)) {
207
- walkIdentifiers(ts, node.name, ast, cb, ctx, blockVars);
164
+ yield* forEachDeclarations(ts, node.name, ast, ctx);
208
165
  }
209
166
  if (node.initializer) {
210
- walkIdentifiers(ts, node.initializer, ast, cb, ctx, blockVars);
167
+ yield* forEachDeclarations(ts, node.initializer, ast, ctx);
211
168
  }
212
169
  }
213
- function walkIdentifiersInFunction(ts, node, ast, cb, ctx) {
214
- const functionArgs = [];
170
+ function* forEachDeclarationsInFunction(ts, node, ast, ctx) {
171
+ const endScope = ctx.startScope();
215
172
  for (const param of node.parameters) {
216
- functionArgs.push(...(0, collectBindings_1.collectBindingNames)(ts, param.name, ast));
217
- walkIdentifiersInBinding(ts, param, ast, cb, ctx, functionArgs);
218
- }
219
- for (const varName of functionArgs) {
220
- ctx.addLocalVariable(varName);
173
+ ctx.declare(...(0, collectBindings_1.collectBindingNames)(ts, param.name, ast));
174
+ yield* forEachDeclarationsInBinding(ts, param, ast, ctx);
221
175
  }
222
176
  if (node.body) {
223
- walkIdentifiers(ts, node.body, ast, cb, ctx, [], true);
224
- }
225
- for (const varName of functionArgs) {
226
- ctx.removeLocalVariable(varName);
177
+ yield* forEachDeclarations(ts, node.body, ast, ctx);
227
178
  }
179
+ endScope();
228
180
  }
229
- function walkIdentifiersInTypeNode(ts, node, cb) {
181
+ function* forEachDeclarationsInTypeNode(ts, node) {
230
182
  if (ts.isTypeQueryNode(node)) {
231
183
  let id = node.exprName;
232
184
  while (!ts.isIdentifier(id)) {
233
185
  id = id.left;
234
186
  }
235
- cb(id, false);
187
+ yield [id, false];
236
188
  }
237
189
  else {
238
- ts.forEachChild(node, node => walkIdentifiersInTypeNode(ts, node, cb));
190
+ for (const child of (0, utils_1.forEachNode)(ts, node)) {
191
+ yield* forEachDeclarationsInTypeNode(ts, child);
192
+ }
239
193
  }
240
194
  }
241
- function shouldIdentifierSkipped(ctx, text, destructuredPropNames) {
242
- return ctx.hasLocalVariable(text)
195
+ function shouldIdentifierSkipped(ctx, text) {
196
+ return ctx.scopes.some(scope => scope.has(text))
243
197
  // https://github.com/vuejs/core/blob/245230e135152900189f13a4281302de45fdcfaa/packages/compiler-core/src/transforms/transformExpression.ts#L342-L352
244
198
  || (0, shared_1.isGloballyAllowed)(text)
245
199
  || isLiteralWhitelisted(text)
246
200
  || text === 'require'
247
- || text.startsWith('__VLS_')
248
- || destructuredPropNames?.has(text);
201
+ || text.startsWith('__VLS_');
249
202
  }
250
203
  //# sourceMappingURL=interpolation.js.map
@@ -3,17 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateObjectProperty = generateObjectProperty;
4
4
  const shared_1 = require("@vue/shared");
5
5
  const utils_1 = require("../utils");
6
+ const boundary_1 = require("../utils/boundary");
6
7
  const camelized_1 = require("../utils/camelized");
7
8
  const stringLiteralKey_1 = require("../utils/stringLiteralKey");
8
- const wrapWith_1 = require("../utils/wrapWith");
9
9
  const interpolation_1 = require("./interpolation");
10
10
  function* generateObjectProperty(options, ctx, code, offset, features, shouldCamelize = false, shouldBeConstant = false) {
11
11
  if (code.startsWith('[') && code.endsWith(']')) {
12
12
  if (shouldBeConstant) {
13
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', features, code.slice(1, -1), offset + 1, `[__VLS_tryAsConstant(`, `)]`);
13
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, options.template, features, code.slice(1, -1), offset + 1, `[__VLS_tryAsConstant(`, `)]`);
14
14
  }
15
15
  else {
16
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', features, code, offset);
16
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, options.template, features, code, offset);
17
17
  }
18
18
  }
19
19
  else if (shouldCamelize) {
@@ -21,7 +21,11 @@ function* generateObjectProperty(options, ctx, code, offset, features, shouldCam
21
21
  yield* (0, camelized_1.generateCamelized)(code, 'template', offset, features);
22
22
  }
23
23
  else {
24
- yield* (0, wrapWith_1.wrapWith)(offset, offset + code.length, features, `'`, ...(0, camelized_1.generateCamelized)(code, 'template', offset, utils_1.combineLastMapping), `'`);
24
+ const token = yield* (0, boundary_1.startBoundary)('template', offset, features);
25
+ yield `'`;
26
+ yield* (0, camelized_1.generateCamelized)(code, 'template', offset, { __combineToken: token });
27
+ yield `'`;
28
+ yield (0, boundary_1.endBoundary)(token, offset + code.length);
25
29
  }
26
30
  }
27
31
  else {
@@ -1,4 +1,4 @@
1
1
  import type { Code, VueCodeInformation } from '../../types';
2
2
  import type { TemplateCodegenContext } from './context';
3
3
  import type { TemplateCodegenOptions } from './index';
4
- export declare function generatePropertyAccess(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, code: string, offset?: number, features?: VueCodeInformation): Generator<Code>;
4
+ export declare function generatePropertyAccess(options: TemplateCodegenOptions, ctx: TemplateCodegenContext, code: string, offset: number, features: VueCodeInformation): Generator<Code>;
@@ -5,14 +5,12 @@ const utils_1 = require("../utils");
5
5
  const stringLiteralKey_1 = require("../utils/stringLiteralKey");
6
6
  const interpolation_1 = require("./interpolation");
7
7
  function* generatePropertyAccess(options, ctx, code, offset, features) {
8
- if (!options.compilerOptions.noPropertyAccessFromIndexSignature && utils_1.identifierRegex.test(code)) {
9
- yield `.`;
10
- yield offset !== undefined && features
11
- ? [code, 'template', offset, features]
12
- : code;
8
+ if (code.startsWith('[') && code.endsWith(']')) {
9
+ yield* (0, interpolation_1.generateInterpolation)(options, ctx, options.template, features, code, offset);
13
10
  }
14
- else if (code.startsWith('[') && code.endsWith(']')) {
15
- yield* (0, interpolation_1.generateInterpolation)(options, ctx, 'template', features, code, offset);
11
+ else if (utils_1.identifierRegex.test(code)) {
12
+ yield `.`;
13
+ yield [code, 'template', offset, features];
16
14
  }
17
15
  else {
18
16
  yield `[`;