@vue/language-core 3.0.2 → 3.0.3

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.
@@ -68,14 +68,6 @@ function generateGlobalTypes({ lib, target, checkUnknownProps, checkUnknownEvent
68
68
  expose?: (exposed: T) => void,
69
69
  }
70
70
  };
71
- type __VLS_NormalizeSlotReturns<S, R = NonNullable<S> extends (...args: any) => infer K ? K : any> = R extends any[] ? {
72
- [K in keyof R]: R[K] extends infer V
73
- ? V extends Element ? V
74
- : V extends new (...args: any) => infer R ? ReturnType<__VLS_FunctionalComponent<R>>
75
- : V extends (...args: any) => infer R ? R
76
- : any
77
- : never
78
- } : R;
79
71
  type __VLS_IsFunction<T, K> = K extends keyof T
80
72
  ? __VLS_IsAny<T[K]> extends false
81
73
  ? unknown extends T[K]
@@ -6,6 +6,7 @@ exports.generateEmitsOption = generateEmitsOption;
6
6
  exports.generatePropsOption = generatePropsOption;
7
7
  const codeFeatures_1 = require("../codeFeatures");
8
8
  const utils_1 = require("../utils");
9
+ const merge_1 = require("../utils/merge");
9
10
  function* generateComponent(options, ctx, scriptSetup, scriptSetupRanges) {
10
11
  if (options.sfc.script && options.scriptRanges?.exportDefault
11
12
  && options.scriptRanges.exportDefault.expression.start !== options.scriptRanges.exportDefault.args.start) {
@@ -17,14 +18,21 @@ function* generateComponent(options, ctx, scriptSetup, scriptSetupRanges) {
17
18
  yield `(await import('${options.vueCompilerOptions.lib}')).defineComponent({${utils_1.newLine}`;
18
19
  }
19
20
  yield `setup() {${utils_1.newLine}`;
20
- yield `return {${utils_1.newLine}`;
21
+ const returns = [];
21
22
  if (ctx.bypassDefineComponent) {
23
+ yield* `const __VLS_returns = {${utils_1.newLine}`;
22
24
  yield* generateComponentSetupReturns(scriptSetupRanges);
25
+ yield `}${utils_1.endOfLine}`;
26
+ returns.push(`typeof __VLS_returns`);
23
27
  }
24
28
  if (scriptSetupRanges.defineExpose) {
25
- yield `...__VLS_exposed,${utils_1.newLine}`;
29
+ returns.push(`typeof __VLS_exposed`);
30
+ }
31
+ if (returns.length) {
32
+ yield `return {} as `;
33
+ yield* (0, merge_1.generateIntersectMerge)(returns);
34
+ yield utils_1.endOfLine;
26
35
  }
27
- yield `}${utils_1.endOfLine}`;
28
36
  yield `},${utils_1.newLine}`;
29
37
  if (!ctx.bypassDefineComponent) {
30
38
  const emitOptionCodes = [...generateEmitsOption(options, scriptSetupRanges)];
@@ -77,10 +85,14 @@ function* generateEmitsOption(options, scriptSetupRanges) {
77
85
  }
78
86
  }
79
87
  if (options.vueCompilerOptions.target >= 3.5 && typeOptionCodes.length) {
80
- yield* generateIntersectMerge('__typeEmits', typeOptionCodes);
88
+ yield `__typeEmits: {} as `;
89
+ yield* (0, merge_1.generateIntersectMerge)(typeOptionCodes);
90
+ yield `,${utils_1.newLine}`;
81
91
  }
82
92
  else if (optionCodes.length) {
83
- yield* generateSpreadMerge('emits', optionCodes);
93
+ yield `emits: `;
94
+ yield* (0, merge_1.generateSpreadMerge)(optionCodes);
95
+ yield `,${utils_1.newLine}`;
84
96
  }
85
97
  }
86
98
  function* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges, hasEmitsOption, inheritAttrs) {
@@ -121,35 +133,14 @@ function* generatePropsOption(options, ctx, scriptSetup, scriptSetupRanges, hasE
121
133
  && scriptSetupRanges.withDefaults?.arg) {
122
134
  yield `__defaults: __VLS_withDefaultsArg,${utils_1.newLine}`;
123
135
  }
124
- yield* generateSpreadMerge('__typeProps', typeOptionCodes);
136
+ yield `__typeProps: `;
137
+ yield* (0, merge_1.generateSpreadMerge)(typeOptionCodes);
138
+ yield `,${utils_1.newLine}`;
125
139
  }
126
140
  if (useOption) {
127
- yield* generateSpreadMerge('props', getOptionCodes.map(fn => fn()));
128
- }
129
- }
130
- function* generateIntersectMerge(key, codes) {
131
- yield `${key}: {} as `;
132
- yield codes[0];
133
- for (let i = 1; i < codes.length; i++) {
134
- yield ` & `;
135
- yield codes[i];
136
- }
137
- yield `,${utils_1.newLine}`;
138
- }
139
- function* generateSpreadMerge(key, codes) {
140
- yield `${key}: `;
141
- if (codes.length === 1) {
142
- yield codes[0];
143
- }
144
- else {
145
- yield `{${utils_1.newLine}`;
146
- for (const code of codes) {
147
- yield `...`;
148
- yield code;
149
- yield `,${utils_1.newLine}`;
150
- }
151
- yield `}`;
141
+ yield `props: `;
142
+ yield* (0, merge_1.generateSpreadMerge)(getOptionCodes.map(fn => fn()));
143
+ yield `,${utils_1.newLine}`;
152
144
  }
153
- yield `,${utils_1.newLine}`;
154
145
  }
155
146
  //# sourceMappingURL=component.js.map
@@ -12,24 +12,7 @@ const src_1 = require("./src");
12
12
  const template_1 = require("./template");
13
13
  function* generateScript(options) {
14
14
  const ctx = (0, context_1.createScriptCodegenContext)(options);
15
- if (options.vueCompilerOptions.globalTypesPath) {
16
- const globalTypesPath = options.vueCompilerOptions.globalTypesPath;
17
- if (path.isAbsolute(globalTypesPath)) {
18
- let relativePath = path.relative(path.dirname(options.fileName), globalTypesPath);
19
- if (relativePath !== globalTypesPath
20
- && !relativePath.startsWith('./')
21
- && !relativePath.startsWith('../')) {
22
- relativePath = './' + relativePath;
23
- }
24
- yield `/// <reference types="${relativePath}" />${utils_1.newLine}`;
25
- }
26
- else {
27
- yield `/// <reference types="${globalTypesPath}" />${utils_1.newLine}`;
28
- }
29
- }
30
- else {
31
- yield `/* placeholder */${utils_1.newLine}`;
32
- }
15
+ yield* generateGlobalTypesPath(options);
33
16
  if (options.sfc.script?.src) {
34
17
  yield* (0, src_1.generateSrc)(options.sfc.script.src);
35
18
  }
@@ -112,6 +95,24 @@ function* generateScript(options) {
112
95
  }
113
96
  return ctx;
114
97
  }
98
+ function* generateGlobalTypesPath(options) {
99
+ const globalTypesPath = options.vueCompilerOptions.globalTypesPath(options.fileName);
100
+ if (!globalTypesPath) {
101
+ yield `/* placeholder */${utils_1.newLine}`;
102
+ }
103
+ else if (path.isAbsolute(globalTypesPath)) {
104
+ let relativePath = path.relative(path.dirname(options.fileName), globalTypesPath);
105
+ if (relativePath !== globalTypesPath
106
+ && !relativePath.startsWith('./')
107
+ && !relativePath.startsWith('../')) {
108
+ relativePath = './' + relativePath;
109
+ }
110
+ yield `/// <reference types="${relativePath}" />${utils_1.newLine}`;
111
+ }
112
+ else {
113
+ yield `/// <reference types="${globalTypesPath}" />${utils_1.newLine}`;
114
+ }
115
+ }
115
116
  function* generateScriptSectionPartiallyEnding(source, end, mark, delimiter = 'debugger') {
116
117
  yield delimiter;
117
118
  yield ['', source, end, codeFeatures_1.codeFeatures.verification];
@@ -11,6 +11,7 @@ const context_1 = require("../template/context");
11
11
  const interpolation_1 = require("../template/interpolation");
12
12
  const styleScopedClasses_1 = require("../template/styleScopedClasses");
13
13
  const utils_1 = require("../utils");
14
+ const merge_1 = require("../utils/merge");
14
15
  function* generateTemplate(options, ctx) {
15
16
  ctx.generatedTemplate = true;
16
17
  const templateCodegenCtx = (0, context_1.createTemplateCodegenContext)({
@@ -33,19 +34,8 @@ function* generateTemplateCtx(options) {
33
34
  exps.push(`{} as __VLS_StyleModules`);
34
35
  }
35
36
  yield `const __VLS_ctx = `;
36
- if (exps.length === 1) {
37
- yield exps[0];
38
- yield `${utils_1.endOfLine}`;
39
- }
40
- else {
41
- yield `{${utils_1.newLine}`;
42
- for (const exp of exps) {
43
- yield `...`;
44
- yield exp;
45
- yield `,${utils_1.newLine}`;
46
- }
47
- yield `}${utils_1.endOfLine}`;
48
- }
37
+ yield* (0, merge_1.generateSpreadMerge)(exps);
38
+ yield utils_1.endOfLine;
49
39
  }
50
40
  function* generateTemplateElements() {
51
41
  yield `let __VLS_elements!: __VLS_IntrinsicElements${utils_1.endOfLine}`;
@@ -65,10 +55,7 @@ function* generateTemplateComponents(options) {
65
55
  types.push(`typeof __VLS_componentsOption`);
66
56
  }
67
57
  yield `type __VLS_LocalComponents =`;
68
- for (const type of types) {
69
- yield ` & `;
70
- yield type;
71
- }
58
+ yield* (0, merge_1.generateIntersectMerge)(types);
72
59
  yield utils_1.endOfLine;
73
60
  yield `let __VLS_components!: __VLS_LocalComponents & __VLS_GlobalComponents${utils_1.endOfLine}`;
74
61
  }
@@ -87,10 +74,7 @@ function* generateTemplateDirectives(options) {
87
74
  types.push(`__VLS_ResolveDirectives<typeof __VLS_directivesOption>`);
88
75
  }
89
76
  yield `type __VLS_LocalDirectives =`;
90
- for (const type of types) {
91
- yield ` & `;
92
- yield type;
93
- }
77
+ yield* (0, merge_1.generateIntersectMerge)(types);
94
78
  yield utils_1.endOfLine;
95
79
  yield `let __VLS_directives!: __VLS_LocalDirectives & __VLS_GlobalDirectives${utils_1.endOfLine}`;
96
80
  }
@@ -163,7 +163,6 @@ export declare function createTemplateCodegenContext(options: Pick<TemplateCodeg
163
163
  }[]>;
164
164
  currentComponent: {
165
165
  ctxVar: string;
166
- childTypes: string[];
167
166
  used: boolean;
168
167
  } | undefined;
169
168
  singleRootElTypes: string[];
@@ -35,10 +35,8 @@ function* generateComponent(options, ctx, node) {
35
35
  const componentVNodeVar = ctx.getInternalVariable();
36
36
  const componentCtxVar = ctx.getInternalVariable();
37
37
  const isComponentTag = node.tag.toLowerCase() === 'component';
38
- ctx.currentComponent?.childTypes.push(`typeof ${componentVNodeVar}`);
39
38
  ctx.currentComponent = {
40
39
  ctxVar: componentCtxVar,
41
- childTypes: [],
42
40
  used: false,
43
41
  };
44
42
  let props = node.props;
@@ -196,7 +194,6 @@ function* generateElement(options, ctx, node) {
196
194
  ? node.loc.start.offset + node.loc.source.lastIndexOf(node.tag)
197
195
  : undefined;
198
196
  const failedPropExps = [];
199
- ctx.currentComponent?.childTypes.push(`__VLS_NativeElements['${node.tag}']`);
200
197
  yield `__VLS_asFunctionalElement(__VLS_elements`;
201
198
  yield* (0, propertyAccess_1.generatePropertyAccess)(options, ctx, node.tag, startTagOffset, ctx.codeFeatures.withoutHighlightAndCompletion);
202
199
  if (endTagOffset !== undefined) {
@@ -45,11 +45,6 @@ function* generateVSlot(options, ctx, node, slotDir) {
45
45
  for (const varName of slotBlockVars) {
46
46
  ctx.removeLocalVariable(varName);
47
47
  }
48
- if (options.vueCompilerOptions.strictSlotChildren && node.children.length) {
49
- yield `(): __VLS_NormalizeSlotReturns<typeof ${slotVar}> => (`;
50
- yield* (0, wrapWith_1.wrapWith)(node.loc.start.offset, node.loc.end.offset, ctx.codeFeatures.verification, `{} as [`, ...ctx.currentComponent.childTypes.map(name => `${name}, `), `]`);
51
- yield `)${utils_1.endOfLine}`;
52
- }
53
48
  if (slotDir) {
54
49
  let isStatic = true;
55
50
  if (slotDir.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
@@ -0,0 +1,3 @@
1
+ import type { Code } from '../../types';
2
+ export declare function generateIntersectMerge(codes: Code[]): Generator<Code>;
3
+ export declare function generateSpreadMerge(codes: Code[]): Generator<Code>;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateIntersectMerge = generateIntersectMerge;
4
+ exports.generateSpreadMerge = generateSpreadMerge;
5
+ const index_1 = require("./index");
6
+ function* generateIntersectMerge(codes) {
7
+ yield codes[0];
8
+ for (let i = 1; i < codes.length; i++) {
9
+ yield ` & `;
10
+ yield codes[i];
11
+ }
12
+ }
13
+ function* generateSpreadMerge(codes) {
14
+ if (codes.length === 1) {
15
+ yield codes[0];
16
+ }
17
+ else {
18
+ yield `{${index_1.newLine}`;
19
+ for (const code of codes) {
20
+ yield `...`;
21
+ yield code;
22
+ yield `,${index_1.newLine}`;
23
+ }
24
+ yield `}`;
25
+ }
26
+ }
27
+ //# sourceMappingURL=merge.js.map
@@ -199,7 +199,6 @@ export declare const tsCodegen: WeakMap<Sfc, {
199
199
  }[]>;
200
200
  currentComponent: {
201
201
  ctxVar: string;
202
- childTypes: string[];
203
202
  used: boolean;
204
203
  } | undefined;
205
204
  singleRootElTypes: string[];
package/lib/types.d.ts CHANGED
@@ -6,9 +6,10 @@ import type * as ts from 'typescript';
6
6
  import type { VueEmbeddedCode } from './virtualFile/embeddedFile';
7
7
  export type { SFCParseResult } from '@vue/compiler-sfc';
8
8
  export { VueEmbeddedCode };
9
- export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' | 'plugins'>> & {
9
+ export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' | 'globalTypesPath' | 'plugins'>> & {
10
10
  strictTemplates?: boolean;
11
11
  target?: 'auto' | 2 | 2.7 | 3 | 3.3 | 3.5 | 3.6 | 99 | number;
12
+ globalTypesPath?: string;
12
13
  plugins?: string[];
13
14
  };
14
15
  export interface VueCodeInformation extends CodeInformation {
@@ -19,12 +20,11 @@ export type Code = Segment<VueCodeInformation>;
19
20
  export interface VueCompilerOptions {
20
21
  target: number;
21
22
  lib: string;
22
- globalTypesPath?: string;
23
+ globalTypesPath: (fileName: string) => string | undefined;
23
24
  extensions: string[];
24
25
  vitePressExtensions: string[];
25
26
  petiteVueExtensions: string[];
26
27
  jsxSlots: boolean;
27
- strictSlotChildren: boolean;
28
28
  strictVModel: boolean;
29
29
  strictCssModules: boolean;
30
30
  checkUnknownProps: boolean;
package/lib/utils/ts.d.ts CHANGED
@@ -10,12 +10,14 @@ export declare function createParsedCommandLine(ts: typeof import('typescript'),
10
10
  export declare class CompilerOptionsResolver {
11
11
  fileExists?: ((path: string) => boolean) | undefined;
12
12
  configRoots: Set<string>;
13
- options: Omit<RawVueCompilerOptions, 'target' | 'plugin'>;
13
+ options: Omit<RawVueCompilerOptions, 'target' | 'globalTypesPath' | 'plugins'>;
14
14
  target: number | undefined;
15
15
  globalTypesPath: string | undefined;
16
16
  plugins: VueLanguagePlugin[];
17
17
  constructor(fileExists?: ((path: string) => boolean) | undefined);
18
18
  addConfig(options: RawVueCompilerOptions, rootDir: string): void;
19
19
  build(defaults?: VueCompilerOptions): VueCompilerOptions;
20
+ private findNodeModulesRoot;
20
21
  }
21
22
  export declare function getDefaultCompilerOptions(target?: number, lib?: string, strictTemplates?: boolean): VueCompilerOptions;
23
+ export declare function writeGlobalTypes(vueOptions: VueCompilerOptions, writeFile: (fileName: string, data: string) => void): void;
package/lib/utils/ts.js CHANGED
@@ -4,6 +4,7 @@ exports.CompilerOptionsResolver = void 0;
4
4
  exports.createParsedCommandLineByJson = createParsedCommandLineByJson;
5
5
  exports.createParsedCommandLine = createParsedCommandLine;
6
6
  exports.getDefaultCompilerOptions = getDefaultCompilerOptions;
7
+ exports.writeGlobalTypes = writeGlobalTypes;
7
8
  const shared_1 = require("@vue/shared");
8
9
  const path_browserify_1 = require("path-browserify");
9
10
  const globalTypes_1 = require("../codegen/globalTypes");
@@ -179,24 +180,35 @@ class CompilerOptionsResolver {
179
180
  experimentalModelPropName: Object.fromEntries(Object.entries(this.options.experimentalModelPropName ?? defaults.experimentalModelPropName).map(([k, v]) => [(0, shared_1.camelize)(k), v])),
180
181
  };
181
182
  if (this.fileExists && this.globalTypesPath === undefined) {
182
- root: for (const rootDir of [...this.configRoots].reverse()) {
183
- let dir = rootDir;
184
- while (!this.fileExists(path_browserify_1.posix.join(dir, 'node_modules', resolvedOptions.lib, 'package.json'))) {
185
- const parentDir = path_browserify_1.posix.dirname(dir);
186
- if (dir === parentDir) {
187
- continue root;
188
- }
189
- dir = parentDir;
183
+ const fileDirToGlobalTypesPath = new Map();
184
+ resolvedOptions.globalTypesPath = fileName => {
185
+ const fileDir = path_browserify_1.posix.dirname(fileName);
186
+ if (fileDirToGlobalTypesPath.has(fileDir)) {
187
+ return fileDirToGlobalTypesPath.get(fileDir);
190
188
  }
191
- resolvedOptions.globalTypesPath = path_browserify_1.posix.join(dir, 'node_modules', '.vue-global-types', (0, globalTypes_1.getGlobalTypesFileName)(resolvedOptions));
192
- break;
193
- }
189
+ const root = this.findNodeModulesRoot(fileDir, resolvedOptions.lib);
190
+ const result = root
191
+ ? path_browserify_1.posix.join(root, 'node_modules', '.vue-global-types', (0, globalTypes_1.getGlobalTypesFileName)(resolvedOptions))
192
+ : undefined;
193
+ fileDirToGlobalTypesPath.set(fileDir, result);
194
+ return result;
195
+ };
194
196
  }
195
197
  else {
196
- resolvedOptions.globalTypesPath = this.globalTypesPath;
198
+ resolvedOptions.globalTypesPath = () => this.globalTypesPath;
197
199
  }
198
200
  return resolvedOptions;
199
201
  }
202
+ findNodeModulesRoot(dir, lib) {
203
+ while (!this.fileExists(path_browserify_1.posix.join(dir, 'node_modules', lib, 'package.json'))) {
204
+ const parentDir = path_browserify_1.posix.dirname(dir);
205
+ if (dir === parentDir) {
206
+ return;
207
+ }
208
+ dir = parentDir;
209
+ }
210
+ return dir;
211
+ }
200
212
  }
201
213
  exports.CompilerOptionsResolver = CompilerOptionsResolver;
202
214
  function findVueVersion(rootDir) {
@@ -227,11 +239,11 @@ function getDefaultCompilerOptions(target = 99, lib = 'vue', strictTemplates = f
227
239
  return {
228
240
  target,
229
241
  lib,
242
+ globalTypesPath: () => undefined,
230
243
  extensions: ['.vue'],
231
244
  vitePressExtensions: [],
232
245
  petiteVueExtensions: [],
233
246
  jsxSlots: false,
234
- strictSlotChildren: strictTemplates,
235
247
  strictVModel: strictTemplates,
236
248
  strictCssModules: false,
237
249
  checkUnknownProps: strictTemplates,
@@ -287,4 +299,19 @@ function getDefaultCompilerOptions(target = 99, lib = 'vue', strictTemplates = f
287
299
  },
288
300
  };
289
301
  }
302
+ function writeGlobalTypes(vueOptions, writeFile) {
303
+ const originalFn = vueOptions.globalTypesPath;
304
+ if (!originalFn) {
305
+ return;
306
+ }
307
+ const writed = new Set();
308
+ vueOptions.globalTypesPath = (fileName) => {
309
+ const result = originalFn(fileName);
310
+ if (result && !writed.has(result)) {
311
+ writed.add(result);
312
+ writeFile(result, (0, globalTypes_1.generateGlobalTypes)(vueOptions));
313
+ }
314
+ return result;
315
+ };
316
+ }
290
317
  //# sourceMappingURL=ts.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vue/language-core",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -13,7 +13,7 @@
13
13
  "directory": "packages/language-core"
14
14
  },
15
15
  "dependencies": {
16
- "@volar/language-core": "2.4.19",
16
+ "@volar/language-core": "2.4.20",
17
17
  "@vue/compiler-dom": "^3.5.0",
18
18
  "@vue/compiler-vue2": "^2.7.16",
19
19
  "@vue/shared": "^3.5.0",
@@ -26,7 +26,7 @@
26
26
  "@types/node": "^22.10.4",
27
27
  "@types/path-browserify": "^1.0.1",
28
28
  "@types/picomatch": "^4.0.0",
29
- "@volar/typescript": "2.4.19",
29
+ "@volar/typescript": "2.4.20",
30
30
  "@vue/compiler-sfc": "^3.5.0"
31
31
  },
32
32
  "peerDependencies": {
@@ -37,5 +37,5 @@
37
37
  "optional": true
38
38
  }
39
39
  },
40
- "gitHead": "7343119e9ca4f6a54f63b57f448f15df4b592a9a"
40
+ "gitHead": "129f30ff8d8d976abf0431063be5c6c4cf88f0fd"
41
41
  }