@luna-park/plugin 1.2.0 → 1.2.1

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/dist/extractor.js CHANGED
@@ -1,577 +1,512 @@
1
- import M from "node:fs";
2
- import { i as m, o as F, F as d, u as x, a as I, m as U } from "./index-Dv9uuje9.js";
3
- import * as y from "typescript";
4
- import p from "typescript";
5
- import * as j from "path";
6
- import * as J from "fs";
7
- function S(t, e = {}) {
8
- if (typeof t != "object" || t === null)
9
- return JSON.stringify(t);
10
- const o = {};
11
- for (const n of ["default", "description", "name", "target", "promise", "optional", "example", "enum"])
12
- t[n] && (o[n] = t[n]);
13
- if (t.type === m.array)
14
- return b(`LogicType.${t.type}`, [
15
- S(t.items, e),
16
- h(o)
17
- ]);
18
- if (t.type === m.interface)
19
- return b(`LogicType.${t.type}`, [
20
- JSON.stringify(t.className),
21
- h(o)
22
- ]);
23
- if (t.type === m.union)
24
- return b(`LogicType.${t.type}`, [
25
- `[${t.elements.map((n) => S(n, e)).join(", ")}]`,
26
- h(o)
27
- ]);
28
- if (t.type === m.function)
29
- return b(`LogicType.${t.type}`, [
30
- S(t.inputs, e),
31
- S(t.output, e),
32
- h(o)
33
- ]);
34
- if (t.type === m.object)
35
- return b(`LogicType.${t.type}`, [
36
- h(
37
- F(
38
- t.properties,
39
- ({ value: n }) => S(n, e)
40
- ),
41
- !0
42
- ),
43
- h(o)
44
- ]);
45
- if (t.type === m.type) {
46
- const n = t.target.at(-1), i = h(o);
47
- return i !== "{}" ? n : `{ ...${n}, ${i.slice(1, -1)} }`;
48
- }
49
- return b(`LogicType.${t.type}`, [h(o)]);
1
+ import { c as typeToSchema, d as objectMap, i as LogicType, l as cleanObject, r as EVarType, s as schemaLoop } from "./dist-BUZt4gnZ.js";
2
+ import fs from "node:fs";
3
+ import * as ts$1 from "typescript";
4
+ import ts from "typescript";
5
+ import * as path from "path";
6
+ import * as fs$1 from "fs";
7
+ function generateCode(c, k = {}) {
8
+ if (typeof c != "object" || !c) return JSON.stringify(c);
9
+ let A = {};
10
+ for (let O of [
11
+ "default",
12
+ "description",
13
+ "name",
14
+ "target",
15
+ "promise",
16
+ "optional",
17
+ "example",
18
+ "enum"
19
+ ]) c[O] && (A[O] = c[O]);
20
+ if (c.type === EVarType.array) return buildFunctionCall(`LogicType.${c.type}`, [generateCode(c.items, k), getCodeFromObject(A)]);
21
+ if (c.type === EVarType.interface) return buildFunctionCall(`LogicType.${c.type}`, [JSON.stringify(c.className), getCodeFromObject(A)]);
22
+ if (c.type === EVarType.union) return buildFunctionCall(`LogicType.${c.type}`, [`[${c.elements.map((c) => generateCode(c, k)).join(", ")}]`, getCodeFromObject(A)]);
23
+ if (c.type === EVarType.function) return buildFunctionCall(`LogicType.${c.type}`, [
24
+ generateCode(c.inputs, k),
25
+ generateCode(c.output, k),
26
+ getCodeFromObject(A)
27
+ ]);
28
+ if (c.type === EVarType.object) return buildFunctionCall(`LogicType.${c.type}`, [getCodeFromObject(objectMap(c.properties, ({ value: c }) => generateCode(c, k)), !0), getCodeFromObject(A)]);
29
+ if (c.type === EVarType.type) {
30
+ let O = c.target.at(-1), k = getCodeFromObject(A);
31
+ return k === "{}" ? `{ ...${O}, ${k.slice(1, -1)} }` : O;
32
+ }
33
+ return buildFunctionCall(`LogicType.${c.type}`, [getCodeFromObject(A)]);
50
34
  }
51
- function b(t, e) {
52
- const o = e.findLastIndex((n) => n !== "{}");
53
- return `${t}(${e.slice(0, o + 1).join(", ")})`;
35
+ function buildFunctionCall(c, O) {
36
+ let k = O.findLastIndex((c) => c !== "{}");
37
+ return `${c}(${O.slice(0, k + 1).join(", ")})`;
54
38
  }
55
- function h(t, e) {
56
- return `{${Object.entries(t).map(([o, n]) => `${_(o)}: ${e ? n : JSON.stringify(n)}`).join(", ")}}`;
39
+ function getCodeFromObject(c, O) {
40
+ return `{${Object.entries(c).map(([c, k]) => `${getSafeKey(c)}: ${O ? k : JSON.stringify(k)}`).join(", ")}}`;
57
41
  }
58
- function _(t) {
59
- return /^[a-zA-Z]+$/.test(t) ? t : JSON.stringify(t);
42
+ function getSafeKey(c) {
43
+ return /^[a-zA-Z]+$/.test(c) ? c : JSON.stringify(c);
60
44
  }
61
- function V(t, e) {
62
- const o = [], n = t.components.default;
63
- Object.keys(t.types);
64
- for (const r of ["properties", "slots", "models", "emits"])
65
- n[r] && Object.keys(n[r]).length && o.push({
66
- key: r,
67
- value: Object.entries(n[r]).map(([l, s]) => `${_(l)}: ${S(s, {})}`).join(`,
68
- `)
69
- });
70
- const i = Object.entries(t.types).map(([r, l]) => `const ${r} = ${S(l, {})};`).join(`
45
+ function getComponentCode(c, O) {
46
+ let k = [], A = c.components.default, j = Object.keys(c.types);
47
+ for (let c of [
48
+ "properties",
49
+ "slots",
50
+ "models",
51
+ "emits"
52
+ ]) A[c] && Object.keys(A[c]).length && k.push({
53
+ key: c,
54
+ value: Object.entries(A[c]).map(([c, O]) => `${getSafeKey(c)}: ${generateCode(O, { lib: j })}`).join(",\n ")
55
+ });
56
+ let M = Object.entries(c.types).map(([c, O]) => `const ${c} = ${generateCode(O, { lib: j })};`).join("\n\n");
57
+ return `import { LogicType, type TComponent } from "@luna-park/plugin";
58
+ import ${O.name} from "${O.path}";
71
59
 
72
- `);
73
- return `import { LogicType, type TComponent } from "@luna-park/plugin";
74
- import ${e.name} from "${e.path}";
60
+ ${M}
75
61
 
76
- ${i}
77
-
78
- export const ${e.name.toLocaleLowerCase()}: TComponent = {
79
- name: "${e.name}",
80
- component: ${e.name},
81
- ${o.map(({ key: r, value: l }) => `${r}: {
82
- ${l}
83
- }`).join(`,
84
- `)}
62
+ export const ${O.name.toLocaleLowerCase()}: TComponent = {
63
+ name: "${O.name}",
64
+ component: ${O.name},
65
+ ${k.map(({ key: c, value: O }) => `${c}: {\n ${O}\n }`).join(",\n ")}
85
66
  }`;
86
67
  }
87
- function T(t, e) {
88
- const o = {
89
- description: C(t.getDocumentationComment(e)) || void 0
90
- };
91
- for (const n of t.getJsDocTags()) {
92
- if (n.name === "param") {
93
- o.params || (o.params = {});
94
- let i = C(n.text?.slice(1)).trim();
95
- i.startsWith("- ") && (i = i.slice(2));
96
- const r = n.text[0]?.text;
97
- o.params[r] = i;
98
- continue;
99
- }
100
- o[n.name] = C(n.text);
101
- }
102
- return d(o);
103
- }
104
- function C(t = []) {
105
- return t.map((e) => e.text).join("");
68
+ function getDocumentationFromSymbol(c, O) {
69
+ let k = { description: mergeSymbolDisplayParts(c.getDocumentationComment(O)) || void 0 };
70
+ for (let O of c.getJsDocTags()) {
71
+ if (O.name === "param") {
72
+ k.params ||= {};
73
+ let c = mergeSymbolDisplayParts(O.text?.slice(1)).trim();
74
+ c.startsWith("- ") && (c = c.slice(2));
75
+ let A = O.text[0]?.text;
76
+ k.params[A] = c;
77
+ continue;
78
+ }
79
+ k[O.name] = mergeSymbolDisplayParts(O.text);
80
+ }
81
+ return cleanObject(k);
106
82
  }
107
- function v(t, e) {
108
- const o = e.checker.getPropertiesOfType(t), n = {};
109
- return o.forEach((i) => {
110
- const r = z(e.checker.getTypeOfSymbol(i)).type, s = e.checker.getSignaturesOfType(r, y.SignatureKind.Call)[0]?.parameters?.[0];
111
- if (s) {
112
- const c = e.checker.getTypeOfSymbol(s);
113
- n[i.name] = {
114
- type: e.typeToString(c)
115
- }, e.extractType(c);
116
- } else
117
- n[i.name] = {
118
- type: "{}"
119
- };
120
- }), n;
83
+ function mergeSymbolDisplayParts(c = []) {
84
+ return c.map((c) => c.text).join("");
121
85
  }
122
- function L(t, e) {
123
- const o = e.checker.getPropertiesOfType(t), n = {}, i = {};
124
- return o.forEach((r) => {
125
- if (["onVnode", "ref_"].some((s) => r.name.startsWith(s)) || ["ref", "class", "style", "key"].includes(r.name))
126
- return;
127
- const l = e.checker.getTypeOfSymbol(r);
128
- if (e.extractType(l), W(r.name)) {
129
- i[H(r.name)] = A(l, r, e);
130
- return;
131
- }
132
- n[r.name] = A(l, r, e);
133
- }), { emits: i, props: n };
86
+ function getSlots(c, O) {
87
+ let k = O.checker.getPropertiesOfType(c), A = {};
88
+ return k.forEach((c) => {
89
+ let k = getDefinedType(O.checker.getTypeOfSymbol(c)).type, j = O.checker.getSignaturesOfType(k, ts$1.SignatureKind.Call)[0]?.parameters?.[0];
90
+ if (j) {
91
+ let k = O.checker.getTypeOfSymbol(j);
92
+ A[c.name] = { type: O.typeToString(k) }, O.extractType(k);
93
+ } else A[c.name] = { type: "{}" };
94
+ }), A;
134
95
  }
135
- function E(t) {
136
- return t.models = Object.keys(t.properties ?? {}).reduce((e, o) => (t.emits?.[`update:${o}`] && (e[o] = t.properties[o], delete t.properties[o], delete t.emits[`update:${o}`]), e), {}), t;
96
+ function getProps(c, O) {
97
+ let k = O.checker.getPropertiesOfType(c), A = {}, j = {};
98
+ return k.forEach((c) => {
99
+ if (["onVnode", "ref_"].some((O) => c.name.startsWith(O)) || [
100
+ "ref",
101
+ "class",
102
+ "style",
103
+ "key"
104
+ ].includes(c.name)) return;
105
+ let k = O.checker.getTypeOfSymbol(c);
106
+ if (O.extractType(k), isEmits(c.name)) {
107
+ j[emitName(c.name)] = getExportType(k, c, O);
108
+ return;
109
+ }
110
+ A[c.name] = getExportType(k, c, O);
111
+ }), {
112
+ emits: j,
113
+ props: A
114
+ };
137
115
  }
138
- function z(t) {
139
- if (t.isUnion()) {
140
- const e = t.types.filter((o) => o.flags !== y.TypeFlags.Undefined);
141
- if (e.length === 1)
142
- return { optional: !0, type: e[0] };
143
- }
144
- return { type: t };
116
+ function computeModels(c) {
117
+ return c.models = Object.keys(c.properties ?? {}).reduce((O, k) => (c.emits?.[`update:${k}`] && (O[k] = c.properties[k], delete c.properties[k], delete c.emits[`update:${k}`]), O), {}), c;
145
118
  }
146
- function A(t, e, o) {
147
- const n = z(t);
148
- return d({
149
- description: T(e, o.checker).description,
150
- optional: n.optional,
151
- type: o.typeToString(n.type)
152
- });
119
+ function getDefinedType(c) {
120
+ if (c.isUnion()) {
121
+ let O = c.types.filter((c) => c.flags !== ts$1.TypeFlags.Undefined);
122
+ if (O.length === 1) return {
123
+ optional: !0,
124
+ type: O[0]
125
+ };
126
+ }
127
+ return { type: c };
153
128
  }
154
- function W(t) {
155
- return /^on[A-Z]/.test(t);
129
+ function getExportType(c, O, k) {
130
+ let j = getDefinedType(c);
131
+ return cleanObject({
132
+ description: getDocumentationFromSymbol(O, k.checker).description,
133
+ optional: j.optional,
134
+ type: k.typeToString(j.type)
135
+ });
156
136
  }
157
- function H(t) {
158
- return t = t.slice(2), t.charAt(0).toLowerCase() + t.slice(1);
137
+ function isEmits(c) {
138
+ return /^on[A-Z]/.test(c);
159
139
  }
160
- function K(t, e) {
161
- return Object.fromEntries((t.getTypeParameters() ?? []).map((o) => {
162
- let n = "unknown";
163
- const i = o.getDefault();
164
- i && (n = e.typeToString(i));
165
- const r = o.getConstraint();
166
- return r && (n = e.typeToString(r)), [o.symbol.name, n];
167
- }));
140
+ function emitName(c) {
141
+ return c = c.slice(2), c.charAt(0).toLowerCase() + c.slice(1);
168
142
  }
169
- function Q(t, e) {
170
- const o = /* @__PURE__ */ new Map(), n = t.getExportsOfModule(e);
171
- for (const i of n) {
172
- let r = i;
173
- for (; r.flags & y.SymbolFlags.Alias; )
174
- r = t.getAliasedSymbol(r);
175
- o.set(i.name, r);
176
- }
177
- return o;
143
+ function getTemplateTypeParameters(c, O) {
144
+ return Object.fromEntries((c.getTypeParameters() ?? []).map((c) => {
145
+ let k = "unknown", A = c.getDefault();
146
+ A && (k = O.typeToString(A));
147
+ let j = c.getConstraint();
148
+ return j && (k = O.typeToString(j)), [c.symbol.name, k];
149
+ }));
178
150
  }
179
- function $(t, e) {
180
- let o = t.getFullyQualifiedName(e);
181
- return o.includes('".') && (o = o.split('".').pop()), o.includes(").") && (o = o.split(").").pop()), o;
151
+ function getResolvedExports(c, O) {
152
+ let k = /* @__PURE__ */ new Map(), A = c.getExportsOfModule(O);
153
+ for (let O of A) {
154
+ let A = O;
155
+ for (; A.flags & ts$1.SymbolFlags.Alias;) A = c.getAliasedSymbol(A);
156
+ k.set(O.name, A);
157
+ }
158
+ return k;
182
159
  }
183
- function O(t, e) {
184
- for (const [o, n] of Object.entries(e))
185
- t = t.replace(new RegExp(`\\b${o}\\b`, "g"), n);
186
- return t;
160
+ function getFullName(c, O) {
161
+ let k = c.getFullyQualifiedName(O);
162
+ return k.includes("\".") && (k = k.split("\".").pop()), k.includes(").") && (k = k.split(").").pop()), k;
187
163
  }
188
- function R(t) {
189
- const e = [];
190
- return t.members.forEach((n) => {
191
- const i = n.name.getText();
192
- if (n.initializer) {
193
- let r;
194
- y.isStringLiteral(n.initializer) ? r = `"${n.initializer.text}"` : y.isNumericLiteral(n.initializer) ? r = n.initializer.text : r = n.initializer.getText(), e.push(`${i} = ${r}`);
195
- } else
196
- e.push(i);
197
- }), `{ ${e.join(", ")} }`;
164
+ function swapType(c, O) {
165
+ for (let [k, A] of Object.entries(O)) c = c.replace(RegExp(`\\b${k}\\b`, "g"), A);
166
+ return c;
198
167
  }
199
- function w(t) {
200
- if (t.getFlags() & (p.TypeFlags.StringLike | p.TypeFlags.NumberLike | p.TypeFlags.BooleanLike | p.TypeFlags.ESSymbolLike | p.TypeFlags.Void | p.TypeFlags.Undefined | p.TypeFlags.Null | p.TypeFlags.Any | p.TypeFlags.Unknown | p.TypeFlags.Never))
201
- return !1;
202
- const o = t.getSymbol();
203
- if (!o)
204
- return !0;
205
- const n = o.declarations?.[0];
206
- if (!n)
207
- return !0;
208
- const i = n.getSourceFile();
209
- return !(/lib\..*\.d\.ts$/.test(i.fileName) || i.fileName.startsWith("/@vue/"));
168
+ function extractEnumDefinition(c) {
169
+ let O = [];
170
+ return c.members.forEach((c) => {
171
+ let k = c.name.getText();
172
+ if (c.initializer) {
173
+ let A;
174
+ A = ts$1.isStringLiteral(c.initializer) ? `"${c.initializer.text}"` : ts$1.isNumericLiteral(c.initializer) ? c.initializer.text : c.initializer.getText(), O.push(`${k} = ${A}`);
175
+ } else O.push(k);
176
+ }), `{ ${O.join(", ")} }`;
210
177
  }
211
- const D = p.TypeFormatFlags.WriteArrowStyleSignature | p.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope | p.TypeFormatFlags.NoTruncation | p.TypeFormatFlags.UseFullyQualifiedType;
212
- class q {
213
- constructor(e) {
214
- this.program = e, this.checker = e.getTypeChecker();
215
- }
216
- checker;
217
- index;
218
- cache = /* @__PURE__ */ new Set();
219
- result;
220
- get(e) {
221
- if (this.index = this.program.getSourceFile(e), !this.index)
222
- throw new Error(`Cannot find file ${e}`);
223
- const o = this.checker.getSymbolAtLocation(this.index);
224
- if (!o)
225
- throw new Error(`Cannot find module ${e}`);
226
- this.result = {
227
- components: {},
228
- functions: {},
229
- types: {}
230
- };
231
- const n = this.index.statements.find((i) => p.isExportAssignment(i));
232
- if (n) {
233
- const r = this.checker.getTypeAtLocation(n.expression).getCallSignatures()[0];
234
- r && this.exportFunction("default", r);
235
- }
236
- return this.exportModule(o), this.result;
237
- }
238
- exportModule(e) {
239
- const o = Q(this.checker, e);
240
- for (const [n, i] of o.entries()) {
241
- let r = this.checker.getTypeOfSymbol(i);
242
- if (r.symbol && r.symbol.flags & p.SymbolFlags.Module) {
243
- const s = r.symbol.exports?.get("default");
244
- s && (r = this.checker.getTypeOfSymbol(s));
245
- }
246
- const l = r.symbol || r.aliasSymbol;
247
- if (l?.flags & p.SymbolFlags.Function) {
248
- const s = this.checker.getTypeOfSymbol(l), c = this.checker.getSignaturesOfType(s, p.SignatureKind.Call)[0];
249
- if (c) {
250
- const a = $(this.checker, l);
251
- this.exportFunction(a, c);
252
- }
253
- } else if (this.typeToString(r).includes("DefineComponent<"))
254
- try {
255
- this.exportComponent(n, r);
256
- } catch (c) {
257
- console.error(`Fail to export component ${n}`), console.error(c);
258
- }
259
- }
260
- }
261
- exportComponent(e, o) {
262
- const n = o.getConstructSignatures()[0];
263
- if (!n)
264
- return;
265
- const i = this.checker.getReturnTypeOfSignature(n), r = {}, l = this.checker.getPropertyOfType(i, "$props");
266
- if (l) {
267
- const c = this.checker.getTypeOfSymbol(l), a = L(c, this);
268
- r.properties = a.props, r.emits = a.emits;
269
- }
270
- const s = this.checker.getPropertyOfType(i, "$slots");
271
- if (s) {
272
- const c = this.checker.getTypeOfSymbol(s);
273
- r.slots = v(c, this);
274
- }
275
- this.result.components[e] = E(r);
276
- }
277
- exportFunction(e, o) {
278
- const n = T(o, this.checker), i = K(o, this), r = o.getParameters().map((s) => {
279
- const c = s.valueDeclaration ?? this.index, a = this.checker.getTypeOfSymbol(s);
280
- this.extractType(a, c);
281
- const f = s.valueDeclaration?.initializer?.getText();
282
- return d({
283
- default: f,
284
- description: n.params?.[s.name],
285
- name: s.name,
286
- optional: !!c.questionToken || f !== void 0 || void 0,
287
- type: O(this.typeToString(a), i)
288
- });
289
- });
290
- if (r.some((s) => s.name?.startsWith("__VLS_"))) {
291
- const s = {}, c = r.findIndex((u) => u.name === "__VLS_props"), a = o.getParameters()[c];
292
- if (a) {
293
- const u = this.checker.getTypeOfSymbol(a), k = L(u, this);
294
- s.properties = k.props, s.emits = k.emits, Object.values(s.properties).forEach((N) => {
295
- N.type = O(N.type, i);
296
- });
297
- }
298
- const f = r.findIndex((u) => u.name === "__VLS_slots"), g = o.getParameters()[f];
299
- if (g) {
300
- const u = this.checker.getTypeOfSymbol(g);
301
- s.slots = v(u, this), Object.values(s.slots).forEach((k) => {
302
- k.type = O(k.type, i);
303
- });
304
- }
305
- this.result.components[e] = E(s);
306
- return;
307
- }
308
- const l = this.checker.getReturnTypeOfSignature(o);
309
- this.extractType(l), this.result.functions[e] = d({
310
- description: n.description,
311
- parameters: r,
312
- return: {
313
- description: n.returns,
314
- type: O(this.typeToString(l), i)
315
- }
316
- });
317
- }
318
- extractType(e, o = this.index) {
319
- if (this.cache.has(e))
320
- return;
321
- this.cache.add(e);
322
- const n = e.symbol || e.aliasSymbol;
323
- if ((e.isUnion() || e.isIntersection()) && !(n?.flags & p.SymbolFlags.Enum) && e.types.forEach((i) => {
324
- this.extractType(i, o);
325
- }), n) {
326
- if (this.checker.getFullyQualifiedName(n).includes("/@vue/"))
327
- return;
328
- const i = $(this.checker, n);
329
- if (n.flags & p.SymbolFlags.Enum) {
330
- const r = n.valueDeclaration || n.declarations?.[0];
331
- if (r && p.isEnumDeclaration(r)) {
332
- const l = $(this.checker, n);
333
- this.result.types[l] = d({
334
- ...T(n, this.checker),
335
- type: R(r)
336
- });
337
- }
338
- return;
339
- }
340
- if (n.flags & p.SymbolFlags.Namespace) {
341
- const r = n;
342
- this.exportModule(r);
343
- }
344
- if (n.flags & p.SymbolFlags.TypeAlias) {
345
- const r = n.valueDeclaration || n.declarations?.[0];
346
- if (!r)
347
- return;
348
- const l = Object.fromEntries((r?.typeParameters ?? []).map((s) => {
349
- let c = "unknown";
350
- const a = s.default;
351
- if (a) {
352
- const g = this.checker.getTypeFromTypeNode(a);
353
- c = this.typeToString(g);
354
- }
355
- const f = s.constraint;
356
- if (f) {
357
- const g = this.checker.getTypeFromTypeNode(f);
358
- c = this.typeToString(g);
359
- }
360
- return [s.symbol.name, c];
361
- }));
362
- this.result.types[i] = {
363
- ...T(n, this.checker),
364
- type: O(this.typeToString(e, D | p.TypeFormatFlags.InTypeAlias), l)
365
- };
366
- }
367
- if (n.flags & p.SymbolFlags.Interface && w(e)) {
368
- const r = n.getDeclarations()[0];
369
- if (r) {
370
- const l = this.checker.getPropertiesOfType(e), s = {};
371
- l.forEach((c) => {
372
- const a = this.checker.getTypeOfSymbol(c);
373
- this.extractType(a, r), s[c.name] = {
374
- ...T(c, this.checker),
375
- optional: !!(c.flags & p.SymbolFlags.Optional) || void 0,
376
- type: this.typeToString(a)
377
- };
378
- }), this.result.types[i] = {
379
- ...T(n, this.checker),
380
- type: s
381
- };
382
- }
383
- }
384
- if (n.flags & p.SymbolFlags.TypeLiteral) {
385
- const r = $(this.checker, e.aliasSymbol || e.symbol), l = this.checker.getPropertiesOfType(e), s = {};
386
- if (["Promise", "Partial", "Pick", "Omit", "Required"].includes(r)) {
387
- this.cache.delete(e), this.extractType(e.aliasTypeArguments[0]);
388
- return;
389
- }
390
- if (e.aliasTypeArguments?.some((c) => !w(c)))
391
- return;
392
- l.forEach((c) => {
393
- const a = this.checker.getTypeOfSymbol(c);
394
- s[c.name] = {
395
- ...T(c, this.checker),
396
- optional: !!(c.flags & p.SymbolFlags.Optional),
397
- type: this.typeToString(a)
398
- };
399
- }), r !== "__type" && (this.result.types[r] = {
400
- ...T(n, this.checker),
401
- type: s
402
- });
403
- }
404
- }
405
- if (e.flags & p.TypeFlags.Object) {
406
- const i = e;
407
- if (w(e) && this.checker.getPropertiesOfType(e).forEach((l) => {
408
- const s = this.checker.getTypeOfSymbol(l);
409
- this.extractType(s, o);
410
- }), i.objectFlags & p.ObjectFlags.Reference) {
411
- const r = i;
412
- this.checker.getTypeArguments(r).forEach((s) => {
413
- this.extractType(s, o);
414
- });
415
- }
416
- }
417
- }
418
- typeToString(e, o = D) {
419
- return this.checker.typeToString(e, void 0, o);
420
- }
178
+ function isUserType(c) {
179
+ if (c.getFlags() & (ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike | ts.TypeFlags.BooleanLike | ts.TypeFlags.ESSymbolLike | ts.TypeFlags.Void | ts.TypeFlags.Undefined | ts.TypeFlags.Null | ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never)) return !1;
180
+ let O = c.getSymbol();
181
+ if (!O) return !0;
182
+ let k = O.declarations?.[0];
183
+ if (!k) return !0;
184
+ let A = k.getSourceFile();
185
+ return !(/lib\..*\.d\.ts$/.test(A.fileName) || A.fileName.startsWith("/@vue/"));
421
186
  }
422
- function Z(t, e = "unknown") {
423
- const o = {
424
- components: {},
425
- functions: {},
426
- types: {}
427
- };
428
- for (const [s, c] of Object.entries(t.types))
429
- o.types[s] = n(c, s);
430
- for (const [s, c] of Object.entries(t.functions))
431
- o.functions[s] = i(c, s);
432
- for (const [s, c] of Object.entries(t.components))
433
- o.components[s] = r(c, s);
434
- return o;
435
- function n(s, c = "") {
436
- if (typeof s.type == "string") {
437
- if (s.type === "{}")
438
- return x.void();
439
- const a = {
440
- ...l(I(s.type)),
441
- ...d({
442
- description: s.description,
443
- name: c?.split(".").pop() || void 0,
444
- optional: s.optional
445
- })
446
- };
447
- if (s.default)
448
- switch (a.type) {
449
- case m.boolean:
450
- a.default = s.default === "true";
451
- break;
452
- case m.number:
453
- a.default = Number(s.default);
454
- break;
455
- case m.string:
456
- a.default = s.default;
457
- break;
458
- }
459
- return a.type === m.object && !Object.keys(a.properties).length && (a.customizable = !0), a;
460
- } else {
461
- const a = x.object(
462
- F(
463
- s.type,
464
- ({ key: f, value: g }) => n(g, f)
465
- ),
466
- d({
467
- description: s.description,
468
- name: c?.split(".").pop(),
469
- optional: s.optional
470
- })
471
- );
472
- return Object.keys(a.properties).length || (a.customizable = !0), a;
473
- }
474
- }
475
- function i(s, c = "") {
476
- return {
477
- ...d({
478
- description: s.description,
479
- name: c?.split(".").pop()
480
- }),
481
- parameters: (s.parameters ?? []).map((a) => n(a, a.name)),
482
- return: n(s.return, "result")
483
- };
484
- }
485
- function r(s, c = "") {
486
- return {
487
- ...d({
488
- name: c?.split(".").pop()
489
- }),
490
- emits: F(s.emits, ({ value: a }) => n(a)),
491
- models: F(s.models, ({ value: a }) => n(a)),
492
- properties: F(s.properties, ({ key: a, value: f }) => n(f, a)),
493
- slots: F(s.slots, ({ value: a }) => n(a))
494
- };
495
- }
496
- function l(s, c = "") {
497
- return U(s, (f) => {
498
- if (f.type === m.interface)
499
- return {
500
- ...a(f.className, c),
501
- description: f.description,
502
- name: f.name,
503
- optional: f.optional
504
- };
505
- });
506
- function a(f, g = "") {
507
- let u = `${g}${f}`;
508
- for (; !t.types[u] && u.indexOf(".") !== -1; )
509
- u = u.substring(u.indexOf(".") + 1);
510
- return t.types[u] ? x.type([
511
- "packages",
512
- e,
513
- ...u.split(".")
514
- ]) : x.interface(f);
515
- }
516
- }
187
+ var FLAGS = ts.TypeFormatFlags.WriteArrowStyleSignature | ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope | ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.UseFullyQualifiedType, CTypeExport = class {
188
+ checker;
189
+ index;
190
+ cache = /* @__PURE__ */ new Set();
191
+ result;
192
+ constructor(c) {
193
+ this.program = c, this.checker = c.getTypeChecker();
194
+ }
195
+ get(c) {
196
+ if (this.index = this.program.getSourceFile(c), !this.index) throw Error(`Cannot find file ${c}`);
197
+ let O = this.checker.getSymbolAtLocation(this.index);
198
+ if (!O) throw Error(`Cannot find module ${c}`);
199
+ this.result = {
200
+ components: {},
201
+ functions: {},
202
+ types: {}
203
+ };
204
+ let k = this.index.statements.find((c) => ts.isExportAssignment(c));
205
+ if (k) {
206
+ let c = this.checker.getTypeAtLocation(k.expression).getCallSignatures()[0];
207
+ c && this.exportFunction("default", c);
208
+ }
209
+ return this.exportModule(O), this.result;
210
+ }
211
+ exportModule(c) {
212
+ let O = getResolvedExports(this.checker, c);
213
+ for (let [c, k] of O.entries()) {
214
+ let O = this.checker.getTypeOfSymbol(k);
215
+ if (O.symbol && O.symbol.flags & ts.SymbolFlags.Module) {
216
+ let c = O.symbol.exports?.get("default");
217
+ c && (O = this.checker.getTypeOfSymbol(c));
218
+ }
219
+ let A = O.symbol || O.aliasSymbol;
220
+ if (A?.flags & ts.SymbolFlags.Function) {
221
+ let c = this.checker.getTypeOfSymbol(A), O = this.checker.getSignaturesOfType(c, ts.SignatureKind.Call)[0];
222
+ if (O) {
223
+ let c = getFullName(this.checker, A);
224
+ this.exportFunction(c, O);
225
+ }
226
+ } else if (this.typeToString(O).includes("DefineComponent<")) try {
227
+ this.exportComponent(c, O);
228
+ } catch (O) {
229
+ console.error(`Fail to export component ${c}`), console.error(O);
230
+ }
231
+ }
232
+ }
233
+ exportComponent(c, O) {
234
+ let k = O.getConstructSignatures()[0];
235
+ if (!k) return;
236
+ let A = this.checker.getReturnTypeOfSignature(k), j = {}, M = this.checker.getPropertyOfType(A, "$props");
237
+ if (M) {
238
+ let c = getProps(this.checker.getTypeOfSymbol(M), this);
239
+ j.properties = c.props, j.emits = c.emits;
240
+ }
241
+ let N = this.checker.getPropertyOfType(A, "$slots");
242
+ N && (j.slots = getSlots(this.checker.getTypeOfSymbol(N), this)), this.result.components[c] = computeModels(j);
243
+ }
244
+ exportFunction(c, O) {
245
+ let k = getDocumentationFromSymbol(O, this.checker), j = getTemplateTypeParameters(O, this), M = O.getParameters().map((c) => {
246
+ let O = c.valueDeclaration ?? this.index, M = this.checker.getTypeOfSymbol(c);
247
+ this.extractType(M, O);
248
+ let N = c.valueDeclaration?.initializer?.getText();
249
+ return cleanObject({
250
+ default: N,
251
+ description: k.params?.[c.name],
252
+ name: c.name,
253
+ optional: !!O.questionToken || N !== void 0 || void 0,
254
+ type: swapType(this.typeToString(M), j)
255
+ });
256
+ });
257
+ if (M.some((c) => c.name?.startsWith("__VLS_"))) {
258
+ let k = {}, A = M.findIndex((c) => c.name === "__VLS_props"), N = O.getParameters()[A];
259
+ if (N) {
260
+ let c = getProps(this.checker.getTypeOfSymbol(N), this);
261
+ k.properties = c.props, k.emits = c.emits, Object.values(k.properties).forEach((c) => {
262
+ c.type = swapType(c.type, j);
263
+ });
264
+ }
265
+ let P = M.findIndex((c) => c.name === "__VLS_slots"), F = O.getParameters()[P];
266
+ F && (k.slots = getSlots(this.checker.getTypeOfSymbol(F), this), Object.values(k.slots).forEach((c) => {
267
+ c.type = swapType(c.type, j);
268
+ })), this.result.components[c] = computeModels(k);
269
+ return;
270
+ }
271
+ let N = this.checker.getReturnTypeOfSignature(O);
272
+ this.extractType(N), this.result.functions[c] = cleanObject({
273
+ description: k.description,
274
+ parameters: M,
275
+ return: {
276
+ description: k.returns,
277
+ type: swapType(this.typeToString(N), j)
278
+ }
279
+ });
280
+ }
281
+ extractType(c, O = this.index) {
282
+ if (this.cache.has(c)) return;
283
+ this.cache.add(c);
284
+ let k = c.symbol || c.aliasSymbol;
285
+ if ((c.isUnion() || c.isIntersection()) && !(k?.flags & ts.SymbolFlags.Enum) && c.types.forEach((c) => {
286
+ this.extractType(c, O);
287
+ }), k) {
288
+ if (this.checker.getFullyQualifiedName(k).includes("/@vue/")) return;
289
+ let O = getFullName(this.checker, k);
290
+ if (k.flags & ts.SymbolFlags.Enum) {
291
+ let c = k.valueDeclaration || k.declarations?.[0];
292
+ if (c && ts.isEnumDeclaration(c)) {
293
+ let O = getFullName(this.checker, k);
294
+ this.result.types[O] = cleanObject({
295
+ ...getDocumentationFromSymbol(k, this.checker),
296
+ type: extractEnumDefinition(c)
297
+ });
298
+ }
299
+ return;
300
+ }
301
+ if (k.flags & ts.SymbolFlags.Namespace) {
302
+ let c = k;
303
+ this.exportModule(c);
304
+ }
305
+ if (k.flags & ts.SymbolFlags.TypeAlias) {
306
+ let A = k.valueDeclaration || k.declarations?.[0];
307
+ if (!A) return;
308
+ let j = Object.fromEntries((A?.typeParameters ?? []).map((c) => {
309
+ let O = "unknown", k = c.default;
310
+ if (k) {
311
+ let c = this.checker.getTypeFromTypeNode(k);
312
+ O = this.typeToString(c);
313
+ }
314
+ let A = c.constraint;
315
+ if (A) {
316
+ let c = this.checker.getTypeFromTypeNode(A);
317
+ O = this.typeToString(c);
318
+ }
319
+ return [c.symbol.name, O];
320
+ }));
321
+ this.result.types[O] = {
322
+ ...getDocumentationFromSymbol(k, this.checker),
323
+ type: swapType(this.typeToString(c, FLAGS | ts.TypeFormatFlags.InTypeAlias), j)
324
+ };
325
+ }
326
+ if (k.flags & ts.SymbolFlags.Interface && isUserType(c)) {
327
+ let A = k.getDeclarations()[0];
328
+ if (A) {
329
+ let j = this.checker.getPropertiesOfType(c), M = {};
330
+ j.forEach((c) => {
331
+ let O = this.checker.getTypeOfSymbol(c);
332
+ this.extractType(O, A), M[c.name] = {
333
+ ...getDocumentationFromSymbol(c, this.checker),
334
+ optional: !!(c.flags & ts.SymbolFlags.Optional) || void 0,
335
+ type: this.typeToString(O)
336
+ };
337
+ }), this.result.types[O] = {
338
+ ...getDocumentationFromSymbol(k, this.checker),
339
+ type: M
340
+ };
341
+ }
342
+ }
343
+ if (k.flags & ts.SymbolFlags.TypeLiteral) {
344
+ let O = getFullName(this.checker, c.aliasSymbol || c.symbol), A = this.checker.getPropertiesOfType(c), j = {};
345
+ if ([
346
+ "Promise",
347
+ "Partial",
348
+ "Pick",
349
+ "Omit",
350
+ "Required"
351
+ ].includes(O)) {
352
+ this.cache.delete(c), this.extractType(c.aliasTypeArguments[0]);
353
+ return;
354
+ }
355
+ if (c.aliasTypeArguments?.some((c) => !isUserType(c))) return;
356
+ A.forEach((c) => {
357
+ let O = this.checker.getTypeOfSymbol(c);
358
+ j[c.name] = {
359
+ ...getDocumentationFromSymbol(c, this.checker),
360
+ optional: !!(c.flags & ts.SymbolFlags.Optional),
361
+ type: this.typeToString(O)
362
+ };
363
+ }), O !== "__type" && (this.result.types[O] = {
364
+ ...getDocumentationFromSymbol(k, this.checker),
365
+ type: j
366
+ });
367
+ }
368
+ }
369
+ if (c.flags & ts.TypeFlags.Object) {
370
+ let k = c;
371
+ if (isUserType(c) && this.checker.getPropertiesOfType(c).forEach((c) => {
372
+ let k = this.checker.getTypeOfSymbol(c);
373
+ this.extractType(k, O);
374
+ }), k.objectFlags & ts.ObjectFlags.Reference) {
375
+ let c = k;
376
+ this.checker.getTypeArguments(c).forEach((c) => {
377
+ this.extractType(c, O);
378
+ });
379
+ }
380
+ }
381
+ }
382
+ typeToString(c, O = FLAGS) {
383
+ return this.checker.typeToString(c, void 0, O);
384
+ }
385
+ };
386
+ function generateNodes(N, P = "unknown") {
387
+ let F = {
388
+ components: {},
389
+ functions: {},
390
+ types: {}
391
+ };
392
+ for (let [c, O] of Object.entries(N.types)) F.types[c] = I(O, c);
393
+ for (let [c, O] of Object.entries(N.functions)) F.functions[c] = L(O, c);
394
+ for (let [c, O] of Object.entries(N.components)) F.components[c] = R(O, c);
395
+ return F;
396
+ function I(M, N = "") {
397
+ if (typeof M.type == "string") {
398
+ if (M.type === "{}") return LogicType.void();
399
+ let O = {
400
+ ...z(typeToSchema(M.type)),
401
+ ...cleanObject({
402
+ description: M.description,
403
+ name: N?.split(".").pop() || void 0,
404
+ optional: M.optional
405
+ })
406
+ };
407
+ if (M.default) switch (O.type) {
408
+ case EVarType.boolean:
409
+ O.default = M.default === "true";
410
+ break;
411
+ case EVarType.number:
412
+ O.default = Number(M.default);
413
+ break;
414
+ case EVarType.string:
415
+ O.default = M.default;
416
+ break;
417
+ }
418
+ return O.type === EVarType.object && !Object.keys(O.properties).length && (O.customizable = !0), O;
419
+ } else {
420
+ let c = LogicType.object(objectMap(M.type, ({ key: c, value: O }) => I(O, c)), cleanObject({
421
+ description: M.description,
422
+ name: N?.split(".").pop(),
423
+ optional: M.optional
424
+ }));
425
+ return Object.keys(c.properties).length || (c.customizable = !0), c;
426
+ }
427
+ }
428
+ function L(c, O = "") {
429
+ return {
430
+ ...cleanObject({
431
+ description: c.description,
432
+ name: O?.split(".").pop()
433
+ }),
434
+ parameters: (c.parameters ?? []).map((c) => I(c, c.name)),
435
+ return: I(c.return, "result")
436
+ };
437
+ }
438
+ function R(c, k = "") {
439
+ return {
440
+ ...cleanObject({ name: k?.split(".").pop() }),
441
+ emits: objectMap(c.emits, ({ value: c }) => I(c)),
442
+ models: objectMap(c.models, ({ value: c }) => I(c)),
443
+ properties: objectMap(c.properties, ({ key: c, value: O }) => I(O, c)),
444
+ slots: objectMap(c.slots, ({ value: c }) => I(c))
445
+ };
446
+ }
447
+ function z(c, O = "") {
448
+ return schemaLoop(c, ({ schema: c }) => {
449
+ if (c.type === EVarType.interface) return {
450
+ ...A(c.className, O),
451
+ description: c.description,
452
+ name: c.name,
453
+ optional: c.optional
454
+ };
455
+ });
456
+ function A(c, O = "") {
457
+ let A = `${O}${c}`;
458
+ for (; !N.types[A] && A.indexOf(".") !== -1;) A = A.substring(A.indexOf(".") + 1);
459
+ return N.types[A] ? LogicType.type([
460
+ "packages",
461
+ P,
462
+ ...A.split(".")
463
+ ]) : LogicType.interface(c);
464
+ }
465
+ }
517
466
  }
518
- function B(t) {
519
- for (; ; ) {
520
- const e = j.join(t, "tsconfig.json");
521
- if (J.existsSync(e))
522
- return e;
523
- const o = j.dirname(t);
524
- if (o === t)
525
- return;
526
- t = o;
527
- }
467
+ function findNearestTsConfig(c) {
468
+ for (;;) {
469
+ let O = path.join(c, "tsconfig.json");
470
+ if (fs$1.existsSync(O)) return O;
471
+ let k = path.dirname(c);
472
+ if (k === c) return;
473
+ c = k;
474
+ }
528
475
  }
529
- function G(t) {
530
- const e = y.readConfigFile(t, y.sys.readFile);
531
- if (e.error)
532
- throw new Error(`Failed to read tsconfig.json: ${e.error.messageText}`);
533
- return y.parseJsonConfigFileContent(e.config, y.sys, j.dirname(t));
476
+ function getTsConfig(c) {
477
+ let O = ts$1.readConfigFile(c, ts$1.sys.readFile);
478
+ if (O.error) throw Error(`Failed to read tsconfig.json: ${O.error.messageText}`);
479
+ return ts$1.parseJsonConfigFileContent(O.config, ts$1.sys, path.dirname(c));
534
480
  }
535
- function X(t, e) {
536
- const o = j.join(process.cwd(), "__temp__.ts"), n = y.createCompilerHost(e), i = y.resolveModuleName(
537
- t,
538
- o,
539
- e,
540
- n
541
- );
542
- if (i.resolvedModule)
543
- return i.resolvedModule.resolvedFileName;
481
+ function resolveModulePath(c, O) {
482
+ let k = path.join(process.cwd(), "__temp__.ts"), A = ts$1.createCompilerHost(O), j = ts$1.resolveModuleName(c, k, O, A);
483
+ if (j.resolvedModule) return j.resolvedModule.resolvedFileName;
544
484
  }
545
- function Y(t) {
546
- const e = B(t);
547
- if (!e)
548
- throw new Error(`Could not find tsconfig.json for: ${t}`);
549
- const n = G(e).options, i = X(t, n);
550
- if (!i)
551
- throw new Error(`Could not resolve module: ${t}`);
552
- const r = y.createCompilerHost(n), l = y.createProgram({
553
- host: r,
554
- options: n,
555
- rootNames: [i]
556
- }), s = l.getSourceFile(i);
557
- if (!s)
558
- throw new Error(`Could not get source file for: ${i}`);
559
- return {
560
- program: l,
561
- sourceFile: s
562
- };
485
+ function getSourceFile(c) {
486
+ let O = findNearestTsConfig(c);
487
+ if (!O) throw Error(`Could not find tsconfig.json for: ${c}`);
488
+ let k = getTsConfig(O).options, A = resolveModulePath(c, k);
489
+ if (!A) throw Error(`Could not resolve module: ${c}`);
490
+ let j = ts$1.createCompilerHost(k), M = ts$1.createProgram({
491
+ host: j,
492
+ options: k,
493
+ rootNames: [A]
494
+ }), N = M.getSourceFile(A);
495
+ if (!N) throw Error(`Could not get source file for: ${A}`);
496
+ return {
497
+ program: M,
498
+ sourceFile: N
499
+ };
563
500
  }
564
- const P = M.promises;
565
- async function oe(t, e) {
566
- const o = t.split("/").pop().split(".")[0], { program: n, sourceFile: i } = Y(t), l = new q(n).get(i.fileName), s = Z(l, o);
567
- if (s.components.default) {
568
- const c = V(s, { name: o, path: t });
569
- await P.mkdir(e.split("/").slice(0, -1).join("/"), { recursive: !0 }), await P.writeFile(e, c);
570
- }
501
+ var fsp = fs.promises;
502
+ async function extractFile(c, O) {
503
+ let k = c.split("/").pop().split(".")[0], { program: A, sourceFile: j } = getSourceFile(c), M = generateNodes(new CTypeExport(A).get(j.fileName), k);
504
+ if (M.components.default) {
505
+ let A = getComponentCode(M, {
506
+ name: k,
507
+ path: c
508
+ });
509
+ await fsp.mkdir(O.split("/").slice(0, -1).join("/"), { recursive: !0 }), await fsp.writeFile(O, A);
510
+ }
571
511
  }
572
- export {
573
- q as CTypeExport,
574
- oe as extractFile,
575
- Z as generateNodes,
576
- Y as getSourceFile
577
- };
512
+ export { CTypeExport, extractFile, generateNodes, getSourceFile };