/
index.ts
462 lines (396 loc) · 13.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import * as vue from '@volar/vue-language-core';
import * as ts from 'typescript/lib/tsserverlibrary';
import type {
MetaCheckerOptions,
ComponentMeta,
EventMeta,
ExposeMeta,
MetaCheckerSchemaOptions,
PropertyMeta,
PropertyMetaSchema,
SlotMeta
} from './types';
export type {
MetaCheckerOptions,
ComponentMeta,
EventMeta,
ExposeMeta,
MetaCheckerSchemaOptions,
PropertyMeta,
PropertyMetaSchema,
SlotMeta
};
export function createComponentMetaChecker(tsconfigPath: string, checkerOptions: MetaCheckerOptions = {}) {
const parsedCommandLine = vue.tsShared.createParsedCommandLine(ts, {
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
readDirectory: (path, extensions, exclude, include, depth) => {
return ts.sys.readDirectory(path, [...extensions, '.vue'], exclude, include, depth);
},
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
}, tsconfigPath);
const scriptSnapshot: Record<string, ts.IScriptSnapshot> = {};
const globalComponentName = tsconfigPath.replace(/\\/g, '/') + '.global.ts';
const host: vue.LanguageServiceHost = {
...ts.sys,
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options), // should use ts.getDefaultLibFilePath not ts.getDefaultLibFileName
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
getCompilationSettings: () => parsedCommandLine.options,
getScriptFileNames: () => {
return [
...parsedCommandLine.fileNames,
...parsedCommandLine.fileNames.map(getMetaFileName),
globalComponentName,
getMetaFileName(globalComponentName),
];
},
getProjectReferences: () => parsedCommandLine.projectReferences,
getScriptVersion: (fileName) => '0',
getScriptSnapshot: (fileName) => {
if (!scriptSnapshot[fileName]) {
let fileText: string | undefined;
if (fileName.endsWith('.meta.ts')) {
fileText = getMetaScriptContent(fileName);
}
else if (fileName === globalComponentName) {
fileText = `
import { defineComponent } from 'vue';
export default defineComponent({});
`;
}
else {
fileText = ts.sys.readFile(fileName);
}
if (fileText !== undefined) {
scriptSnapshot[fileName] = ts.ScriptSnapshot.fromString(fileText);
}
}
return scriptSnapshot[fileName];
},
getTypeScriptModule: () => ts,
getVueCompilationSettings: () => parsedCommandLine.vueOptions,
};
const core = vue.createLanguageContext(host);
const proxyApis: Partial<ts.LanguageServiceHost> = checkerOptions.forceUseTs ? {
getScriptKind: (fileName) => {
if (fileName.endsWith('.vue.js')) {
return ts.ScriptKind.TS;
}
if (fileName.endsWith('.vue.jsx')) {
return ts.ScriptKind.TSX;
}
return core.typescriptLanguageServiceHost.getScriptKind!(fileName);
},
} : {};
const proxyHost = new Proxy(core.typescriptLanguageServiceHost, {
get(target, propKey: keyof ts.LanguageServiceHost) {
if (propKey in proxyApis) {
return proxyApis[propKey];
}
return target[propKey];
}
});
const tsLs = ts.createLanguageService(proxyHost);
const program = tsLs.getProgram()!;
const typeChecker = program.getTypeChecker();
return {
getGlobalPropNames,
getExportNames,
getComponentMeta,
};
/**
* Get helper array to map internal properties added by vue to any components
*
* @example
* ```ts
* import { createComponentMetaChecker } from 'vue-component-meta'
*
* const checker = createComponentMetaChecker('path/to/tsconfig.json')
* const meta = checker.getComponentMeta('path/to/component.vue')
* const globalPropNames = checker.getGlobalPropNames();
* const props = meta.props.filter(prop => !globalPropNames.includes(prop.name))
* ```
*/
function getGlobalPropNames() {
const meta = getComponentMeta(globalComponentName);
return meta.props.map(prop => prop.name);
}
function getMetaFileName(fileName: string) {
return (fileName.endsWith('.vue') ? fileName : fileName.substring(0, fileName.lastIndexOf('.'))) + '.meta.ts';
}
function getMetaScriptContent(fileName: string) {
return `
import * as Components from '${fileName.substring(0, fileName.length - '.meta.ts'.length)}';
export default {} as { [K in keyof typeof Components]: InstanceType<typeof Components[K]>; };;
`;
}
function getExportNames(componentPath: string) {
return _getExports(componentPath).exports.map(e => e.getName());
}
function getComponentMeta(componentPath: string, exportName = 'default'): ComponentMeta {
const { symbolNode, exports } = _getExports(componentPath);
const _export = exports.find((property) => property.getName() === exportName);
if (!_export) {
throw `Could not find export ${exportName}`;
}
const componentType = typeChecker.getTypeOfSymbolAtLocation(_export, symbolNode!);
const symbolProperties = componentType.getProperties() ?? [];
const {
resolveNestedProperties,
resolveEventSignature,
resolveExposedProperties,
resolveSlotProperties,
} = createSchemaResolvers(typeChecker, symbolNode!, checkerOptions.schema);
return {
props: getProps(),
events: getEvents(),
slots: getSlots(),
exposed: getExposed(),
};
function getProps() {
const $props = symbolProperties.find(prop => prop.escapedName === '$props');
const propEventRegex = /^(on[A-Z])/;
let result: PropertyMeta[] = [];
if ($props) {
const type = typeChecker.getTypeOfSymbolAtLocation($props, symbolNode!);
const properties = type.getApparentProperties();
result = properties
.map(resolveNestedProperties)
.filter((prop) => !prop.name.match(propEventRegex));
}
// fill defaults
if (componentPath.endsWith('.vue') && exportName === 'default') {
const snapshot = host.getScriptSnapshot(componentPath)!;
const defaults = readCmponentDefaultProps(snapshot.getText(0, snapshot.getLength()));
for (const propName in defaults) {
const prop = result.find(p => p.name === propName);
if (prop) {
prop.default = defaults[propName];
}
}
}
return result;
}
function getEvents() {
const $emit = symbolProperties.find(prop => prop.escapedName === '$emit');
if ($emit) {
const type = typeChecker.getTypeOfSymbolAtLocation($emit, symbolNode!);
const calls = type.getCallSignatures();
return calls.map(resolveEventSignature).filter(event => event.name);
}
return [];
}
function getSlots() {
const propertyName = (parsedCommandLine.vueOptions.target ?? 3) < 3 ? '$scopedSlots' : '$slots';
const $slots = symbolProperties.find(prop => prop.escapedName === propertyName);
if ($slots) {
const type = typeChecker.getTypeOfSymbolAtLocation($slots, symbolNode!);
const properties = type.getProperties();
return properties.map(resolveSlotProperties);
}
return [];
}
function getExposed() {
const exposed = symbolProperties.filter(prop =>
// only exposed props will have a syntheticOrigin
Boolean((prop as any).syntheticOrigin)
);
if (exposed.length) {
return exposed.map(resolveExposedProperties);
}
return [];
}
}
function _getExports(componentPath: string) {
const sourceFile = program?.getSourceFile(getMetaFileName(componentPath));
if (!sourceFile) {
throw 'Could not find main source file';
}
const moduleSymbol = typeChecker.getSymbolAtLocation(sourceFile);
if (!moduleSymbol) {
throw 'Could not find module symbol';
}
const exportedSymbols = typeChecker.getExportsOfModule(moduleSymbol);
let symbolNode: ts.Expression | undefined;
for (const symbol of exportedSymbols) {
const [declaration] = symbol.getDeclarations() ?? [];
if (ts.isExportAssignment(declaration)) {
symbolNode = declaration.expression;
}
}
if (!symbolNode) {
throw 'Could not find symbol node';
}
const exportDefaultType = typeChecker.getTypeAtLocation(symbolNode);
const exports = exportDefaultType.getProperties();
return {
symbolNode,
exports,
};
}
}
function createSchemaResolvers(typeChecker: ts.TypeChecker, symbolNode: ts.Expression, options: MetaCheckerSchemaOptions = {}) {
const ignore = options.ignore ?? [];
const enabled = options.enabled ?? false;
function shouldIgnore(subtype: ts.Type) {
const type = typeChecker.typeToString(subtype);
if (type === 'any') {
return true;
}
if (ignore.length === 0) {
return false;
}
return ignore.includes(type);
}
function reducer(acc: any, cur: any) {
acc[cur.name] = cur;
return acc;
}
function resolveNestedProperties(prop: ts.Symbol): PropertyMeta {
const subtype = typeChecker.getTypeOfSymbolAtLocation(prop, symbolNode!);
const schema = enabled ? resolveSchema(subtype) : undefined;
return {
name: prop.getEscapedName().toString(),
description: ts.displayPartsToString(prop.getDocumentationComment(typeChecker)),
tags: prop.getJsDocTags(typeChecker).map(tag => ({
name: tag.name,
text: tag.text?.map(part => part.text).join(''),
})),
required: !Boolean((prop.declarations?.[0] as ts.ParameterDeclaration)?.questionToken ?? false),
type: typeChecker.typeToString(subtype),
schema,
};
}
function resolveSlotProperties(prop: ts.Symbol): SlotMeta {
const subtype = typeChecker.getTypeOfSymbolAtLocation(typeChecker.getTypeOfSymbolAtLocation(prop, symbolNode!).getCallSignatures()[0].parameters[0], symbolNode!);
const schema = enabled ? resolveSchema(subtype) : undefined;
return {
name: prop.getName(),
type: typeChecker.typeToString(subtype),
description: ts.displayPartsToString(prop.getDocumentationComment(typeChecker)),
schema,
};
}
function resolveExposedProperties(expose: ts.Symbol): ExposeMeta {
const subtype = typeChecker.getTypeOfSymbolAtLocation(expose, symbolNode!);
const schema = enabled ? resolveSchema(subtype) : undefined;
return {
name: expose.getName(),
type: typeChecker.typeToString(subtype),
description: ts.displayPartsToString(expose.getDocumentationComment(typeChecker)),
schema,
};
}
function resolveEventSignature(call: ts.Signature): EventMeta {
const subtype = typeChecker.getTypeOfSymbolAtLocation(call.parameters[1], symbolNode!);
const schema = enabled
? typeChecker.getTypeArguments(subtype as ts.TypeReference).map(resolveSchema)
: undefined;
return {
name: (typeChecker.getTypeOfSymbolAtLocation(call.parameters[0], symbolNode!) as ts.StringLiteralType).value,
type: typeChecker.typeToString(subtype),
signature: typeChecker.signatureToString(call),
schema,
};
}
function resolveCallbackSchema(signature: ts.Signature): PropertyMetaSchema {
const schema = enabled && signature.parameters.length > 0
? typeChecker
.getTypeArguments(typeChecker.getTypeOfSymbolAtLocation(signature.parameters[0], symbolNode) as ts.TypeReference)
.map(resolveSchema)
: undefined;
return {
kind: 'event',
type: typeChecker.signatureToString(signature),
schema,
};
}
function resolveEventSchema(subtype: ts.Type): PropertyMetaSchema {
return (subtype.getCallSignatures().length === 1)
? resolveCallbackSchema(subtype.getCallSignatures()[0])
: typeChecker.typeToString(subtype);
}
function resolveNestedSchema(subtype: ts.Type): PropertyMetaSchema {
if (
subtype.getCallSignatures().length === 0 &&
(subtype.isClassOrInterface() || subtype.isIntersection() || (subtype as ts.ObjectType).objectFlags & ts.ObjectFlags.Anonymous)
) {
if (shouldIgnore(subtype)) {
return typeChecker.typeToString(subtype);
}
return {
kind: 'object',
type: typeChecker.typeToString(subtype),
schema: subtype.getProperties().map(resolveNestedProperties).reduce(reducer, {})
};
}
return resolveEventSchema(subtype);
}
function resolveArraySchema(subtype: ts.Type): PropertyMetaSchema {
// @ts-ignore - typescript internal, isArrayLikeType exists
if (typeChecker.isArrayLikeType(subtype)) {
if (shouldIgnore(subtype)) {
return typeChecker.typeToString(subtype);
}
return {
kind: 'array',
type: typeChecker.typeToString(subtype),
schema: typeChecker.getTypeArguments(subtype as ts.TypeReference).map(resolveSchema)
};
}
return resolveNestedSchema(subtype);
}
function resolveSchema(subtype: ts.Type): PropertyMetaSchema {
return subtype.isUnion()
? {
kind: 'enum',
type: typeChecker.typeToString(subtype),
schema: subtype.types.map(resolveArraySchema)
}
: resolveArraySchema(subtype);
}
return {
resolveNestedProperties,
resolveSlotProperties,
resolveEventSignature,
resolveExposedProperties,
resolveCallbackSchema,
resolveEventSchema,
resolveNestedSchema,
resolveArraySchema,
resolveSchema,
};
}
function readCmponentDefaultProps(fileText: string) {
const vueSourceFile = vue.createSourceFile('/tmp.vue', fileText, {}, {}, ts);
const descriptor = vueSourceFile.getDescriptor();
const scriptSetupRanges = vueSourceFile.getScriptSetupRanges();
const result: Record<string, string> = {};
if (descriptor.scriptSetup && scriptSetupRanges?.withDefaultsArg) {
const defaultsText = descriptor.scriptSetup.content.substring(scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end);
const ast = ts.createSourceFile('/tmp.' + descriptor.scriptSetup.lang, '(' + defaultsText + ')', ts.ScriptTarget.Latest);
const obj = findObjectLiteralExpression(ast);
if (obj) {
for (const prop of obj.properties) {
if (ts.isPropertyAssignment(prop)) {
const name = prop.name.getText(ast);
const exp = prop.initializer.getText(ast);
result[name] = exp;
}
}
}
function findObjectLiteralExpression(node: ts.Node) {
if (ts.isObjectLiteralExpression(node)) {
return node;
}
let result: ts.ObjectLiteralExpression | undefined;
node.forEachChild(child => {
if (!result) {
result = findObjectLiteralExpression(child);
}
});
return result;
}
}
return result;
}