vue-component-meta 0.39.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present Johnson Chu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # vue-component-meta
2
+
3
+ ## Usage
4
+
5
+ See https://github.com/johnsoncodehk/volar/blob/master/packages/vue-component-meta/tests/index.spec.ts.
6
+
7
+ ## Sponsors
8
+
9
+ <p align="center">
10
+ <a href="https://cdn.jsdelivr.net/gh/johnsoncodehk/sponsors/company/sponsors.svg">
11
+ <img src="https://cdn.jsdelivr.net/gh/johnsoncodehk/sponsors/company/sponsors.svg"/>
12
+ </a>
13
+ </p>
14
+
15
+ ---
16
+
17
+ <p align="center">
18
+ <a href="https://cdn.jsdelivr.net/gh/johnsoncodehk/sponsors/sponsors.svg">
19
+ <img src="https://cdn.jsdelivr.net/gh/johnsoncodehk/sponsors/sponsors.svg"/>
20
+ </a>
21
+ </p>
package/out/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ export declare function createComponentMetaChecker(tsconfigPath: string): {
2
+ getComponentMeta: (componentPath: string) => {
3
+ props: {
4
+ name: string;
5
+ isOptional: boolean;
6
+ type: string;
7
+ documentationComment: string;
8
+ }[];
9
+ events: {
10
+ name: any;
11
+ parametersType: string;
12
+ parameters: {
13
+ name: string;
14
+ type: string;
15
+ isOptional: string;
16
+ }[];
17
+ documentationComment: string;
18
+ }[];
19
+ slots: {
20
+ name: string;
21
+ propsType: string;
22
+ documentationComment: string;
23
+ }[];
24
+ };
25
+ };
package/out/index.js ADDED
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createComponentMetaChecker = void 0;
4
+ const vue = require("@volar/vue-language-core");
5
+ const ts = require("typescript/lib/tsserverlibrary");
6
+ function createComponentMetaChecker(tsconfigPath) {
7
+ const parsedCommandLine = vue.tsShared.createParsedCommandLine(ts, {
8
+ useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
9
+ readDirectory: (path, extensions, exclude, include, depth) => {
10
+ return ts.sys.readDirectory(path, [...extensions, '.vue'], exclude, include, depth);
11
+ },
12
+ fileExists: ts.sys.fileExists,
13
+ readFile: ts.sys.readFile,
14
+ }, tsconfigPath);
15
+ const scriptSnapshot = {};
16
+ const core = vue.createLanguageContext(Object.assign(Object.assign({}, ts.sys), { getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options), useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, getCompilationSettings: () => parsedCommandLine.options, getScriptFileNames: () => {
17
+ const result = [...parsedCommandLine.fileNames];
18
+ for (const fileName of parsedCommandLine.fileNames) {
19
+ if (fileName.endsWith('.vue')) {
20
+ result.push(fileName + '.meta.ts');
21
+ }
22
+ }
23
+ return result;
24
+ }, getProjectReferences: () => parsedCommandLine.projectReferences, getScriptVersion: (fileName) => '0', getScriptSnapshot: (fileName) => {
25
+ if (!scriptSnapshot[fileName]) {
26
+ const fileText = fileName.endsWith('.meta.ts') ? getMetaScriptContent(fileName) : ts.sys.readFile(fileName);
27
+ if (fileText !== undefined) {
28
+ scriptSnapshot[fileName] = ts.ScriptSnapshot.fromString(fileText);
29
+ }
30
+ }
31
+ return scriptSnapshot[fileName];
32
+ }, getTypeScriptModule: () => ts, getVueCompilationSettings: () => parsedCommandLine.vueOptions }));
33
+ const tsLs = ts.createLanguageService(core.typescriptLanguageServiceHost);
34
+ const program = tsLs.getProgram();
35
+ const typeChecker = program.getTypeChecker();
36
+ return {
37
+ getComponentMeta,
38
+ };
39
+ function getMetaScriptContent(fileName) {
40
+ return `
41
+ import Component from '${fileName.substring(0, fileName.length - '.meta.ts'.length)}';
42
+ export default new Component();
43
+ `;
44
+ }
45
+ function getComponentMeta(componentPath) {
46
+ var _a;
47
+ const sourceFile = program === null || program === void 0 ? void 0 : program.getSourceFile(componentPath + '.meta.ts');
48
+ if (!sourceFile) {
49
+ throw 'Could not find main source file';
50
+ }
51
+ const moduleSymbol = typeChecker.getSymbolAtLocation(sourceFile);
52
+ if (!moduleSymbol) {
53
+ throw 'Could not find module symbol';
54
+ }
55
+ const exportedSymbols = typeChecker.getExportsOfModule(moduleSymbol);
56
+ let symbolNode;
57
+ for (const symbol of exportedSymbols) {
58
+ const [declaration] = (_a = symbol.getDeclarations()) !== null && _a !== void 0 ? _a : [];
59
+ if (ts.isExportAssignment(declaration)) {
60
+ symbolNode = declaration.expression;
61
+ }
62
+ }
63
+ if (!symbolNode) {
64
+ throw 'Could not find symbol node';
65
+ }
66
+ const symbolType = typeChecker.getTypeAtLocation(symbolNode);
67
+ const symbolProperties = symbolType.getProperties();
68
+ return {
69
+ props: getProps(),
70
+ events: getEvents(),
71
+ slots: getSlots(),
72
+ };
73
+ function getProps() {
74
+ const $props = symbolProperties.find(prop => prop.escapedName === '$props');
75
+ if ($props) {
76
+ const type = typeChecker.getTypeOfSymbolAtLocation($props, symbolNode);
77
+ const properties = type.getProperties();
78
+ return properties.map(prop => {
79
+ var _a, _b;
80
+ return ({
81
+ name: prop.escapedName,
82
+ // @ts-ignore
83
+ isOptional: !!((_b = (_a = prop.declarations) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.questionToken),
84
+ type: typeChecker.typeToString(typeChecker.getTypeOfSymbolAtLocation(prop, symbolNode)),
85
+ documentationComment: ts.displayPartsToString(prop.getDocumentationComment(typeChecker)),
86
+ });
87
+ });
88
+ }
89
+ return [];
90
+ }
91
+ function getEvents() {
92
+ const $emit = symbolProperties.find(prop => prop.escapedName === '$emit');
93
+ if ($emit) {
94
+ const type = typeChecker.getTypeOfSymbolAtLocation($emit, symbolNode);
95
+ const calls = type.getCallSignatures();
96
+ return calls.map(call => ({
97
+ // @ts-ignore
98
+ name: typeChecker.getTypeOfSymbolAtLocation(call.parameters[0], symbolNode).value,
99
+ parametersType: typeChecker.typeToString(typeChecker.getTypeOfSymbolAtLocation(call.parameters[1], symbolNode)),
100
+ // @ts-ignore
101
+ parameters: typeChecker.getTypeArguments(typeChecker.getTypeOfSymbolAtLocation(call.parameters[1], symbolNode)).map(arg => ({
102
+ name: 'TODO',
103
+ type: typeChecker.typeToString(arg),
104
+ isOptional: 'TODO',
105
+ })),
106
+ documentationComment: ts.displayPartsToString(call.getDocumentationComment(typeChecker)),
107
+ }));
108
+ }
109
+ return [];
110
+ }
111
+ function getSlots() {
112
+ var _a;
113
+ const propertyName = ((_a = parsedCommandLine.vueOptions.target) !== null && _a !== void 0 ? _a : 3) < 3 ? '$scopedSlots' : '$slots';
114
+ const $slots = symbolProperties.find(prop => prop.escapedName === propertyName);
115
+ if ($slots) {
116
+ const type = typeChecker.getTypeOfSymbolAtLocation($slots, symbolNode);
117
+ const properties = type.getProperties();
118
+ return properties.map(prop => ({
119
+ name: prop.escapedName,
120
+ propsType: typeChecker.typeToString(typeChecker.getTypeOfSymbolAtLocation(typeChecker.getTypeOfSymbolAtLocation(prop, symbolNode).getCallSignatures()[0].parameters[0], symbolNode)),
121
+ // props: {}, // TODO
122
+ documentationComment: ts.displayPartsToString(prop.getDocumentationComment(typeChecker)),
123
+ }));
124
+ }
125
+ return [];
126
+ }
127
+ }
128
+ }
129
+ exports.createComponentMetaChecker = createComponentMetaChecker;
130
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "vue-component-meta",
3
+ "version": "0.39.0",
4
+ "main": "out/index.js",
5
+ "license": "MIT",
6
+ "files": [
7
+ "out/**/*.js",
8
+ "out/**/*.d.ts"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/johnsoncodehk/volar.git",
13
+ "directory": "packages/vue-component-meta"
14
+ },
15
+ "dependencies": {
16
+ "@volar/vue-language-core": "0.39.0"
17
+ },
18
+ "peerDependencies": {
19
+ "typescript": "*"
20
+ }
21
+ }