@luna-park/plugin 1.3.1 → 1.4.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.
@@ -1,14 +0,0 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./chunk-rCEYsGS8.cjs`),t=require(`./dist-BkSFkjuo.cjs`);let n=require(`node:fs`);n=e.n(n);let r=require(`typescript`);r=e.n(r);let i=require(`path`);i=e.n(i);let a=require(`fs`);a=e.n(a);function o(e,n={}){if(typeof e!=`object`||!e)return JSON.stringify(e);let r={};for(let t of[`default`,`description`,`name`,`target`,`promise`,`optional`,`example`,`enum`])e[t]&&(r[t]=e[t]);if(e.type===t.r.array)return s(`LogicType.${e.type}`,[o(e.items,n),c(r)]);if(e.type===t.r.interface)return s(`LogicType.${e.type}`,[JSON.stringify(e.className),c(r)]);if(e.type===t.r.union)return s(`LogicType.${e.type}`,[`[${e.elements.map(e=>o(e,n)).join(`, `)}]`,c(r)]);if(e.type===t.r.function)return s(`LogicType.${e.type}`,[o(e.inputs,n),o(e.output,n),c(r)]);if(e.type===t.r.object)return s(`LogicType.${e.type}`,[c(t.d(e.properties,({value:e})=>o(e,n)),!0),c(r)]);if(e.type===t.r.type){let t=e.target.at(-1),n=c(r);return n===`{}`?`{ ...${t}, ${n.slice(1,-1)} }`:t}return s(`LogicType.${e.type}`,[c(r)])}function s(e,t){let n=t.findLastIndex(e=>e!==`{}`);return`${e}(${t.slice(0,n+1).join(`, `)})`}function c(e,t){return`{${Object.entries(e).map(([e,n])=>`${l(e)}: ${t?n:JSON.stringify(n)}`).join(`, `)}}`}function l(e){return/^[a-zA-Z]+$/.test(e)?e:JSON.stringify(e)}function u(e,t){let n=[],r=e.components.default,i=Object.keys(e.types);for(let e of[`properties`,`slots`,`models`,`emits`])r[e]&&Object.keys(r[e]).length&&n.push({key:e,value:Object.entries(r[e]).map(([e,t])=>`${l(e)}: ${o(t,{lib:i})}`).join(`,
2
- `)});let a=Object.entries(e.types).map(([e,t])=>`const ${e} = ${o(t,{lib:i})};`).join(`
3
-
4
- `);return`import { LogicType, type TComponent } from "@luna-park/plugin";
5
- import ${t.name} from "${t.path}";
6
-
7
- ${a}
8
-
9
- export const ${t.name.toLocaleLowerCase()}: TComponent = {
10
- name: "${t.name}",
11
- component: ${t.name},
12
- ${n.map(({key:e,value:t})=>`${e}: {\n ${t}\n }`).join(`,
13
- `)}
14
- }`}function d(e,n){let r={description:f(e.getDocumentationComment(n))||void 0};for(let t of e.getJsDocTags()){if(t.name===`param`){r.params||={};let e=f(t.text?.slice(1)).trim();e.startsWith(`- `)&&(e=e.slice(2));let n=t.text[0]?.text;r.params[n]=e;continue}r[t.name]=f(t.text)}return t.l(r)}function f(e=[]){return e.map(e=>e.text).join(``)}function p(e,t){let n=t.checker.getPropertiesOfType(e),i={};return n.forEach(e=>{let n=g(t.checker.getTypeOfSymbol(e)).type,a=t.checker.getSignaturesOfType(n,r.SignatureKind.Call)[0]?.parameters?.[0];if(a){let n=t.checker.getTypeOfSymbol(a);i[e.name]={type:t.typeToString(n)},t.extractType(n)}else i[e.name]={type:`{}`}}),i}function m(e,t){let n=t.checker.getPropertiesOfType(e),r={},i={};return n.forEach(e=>{if([`onVnode`,`ref_`].some(t=>e.name.startsWith(t))||[`ref`,`class`,`style`,`key`].includes(e.name))return;let n=t.checker.getTypeOfSymbol(e);if(t.extractType(n),v(e.name)){i[y(e.name)]=_(n,e,t);return}r[e.name]=_(n,e,t)}),{emits:i,props:r}}function h(e){return e.models=Object.keys(e.properties??{}).reduce((t,n)=>(e.emits?.[`update:${n}`]&&(t[n]=e.properties[n],delete e.properties[n],delete e.emits[`update:${n}`]),t),{}),e}function g(e){if(e.isUnion()){let t=e.types.filter(e=>e.flags!==r.TypeFlags.Undefined);if(t.length===1)return{optional:!0,type:t[0]}}return{type:e}}function _(e,n,r){let i=g(e);return t.l({description:d(n,r.checker).description,optional:i.optional,type:r.typeToString(i.type)})}function v(e){return/^on[A-Z]/.test(e)}function y(e){return e=e.slice(2),e.charAt(0).toLowerCase()+e.slice(1)}function b(e,t){return Object.fromEntries((e.getTypeParameters()??[]).map(e=>{let n=`unknown`,r=e.getDefault();r&&(n=t.typeToString(r));let i=e.getConstraint();return i&&(n=t.typeToString(i)),[e.symbol.name,n]}))}function x(e,t){let n=new Map,i=e.getExportsOfModule(t);for(let t of i){let i=t;for(;i.flags&r.SymbolFlags.Alias;)i=e.getAliasedSymbol(i);n.set(t.name,i)}return n}function S(e,t){let n=e.getFullyQualifiedName(t);return n.includes(`".`)&&(n=n.split(`".`).pop()),n.includes(`).`)&&(n=n.split(`).`).pop()),n}function C(e,t){for(let[n,r]of Object.entries(t))e=e.replace(RegExp(`\\b${n}\\b`,`g`),r);return e}function w(e){let t=[];return e.members.forEach(e=>{let n=e.name.getText();if(e.initializer){let i;i=r.isStringLiteral(e.initializer)?`"${e.initializer.text}"`:r.isNumericLiteral(e.initializer)?e.initializer.text:e.initializer.getText(),t.push(`${n} = ${i}`)}else t.push(n)}),`{ ${t.join(`, `)} }`}function T(e){if(e.getFlags()&(r.default.TypeFlags.StringLike|r.default.TypeFlags.NumberLike|r.default.TypeFlags.BooleanLike|r.default.TypeFlags.ESSymbolLike|r.default.TypeFlags.Void|r.default.TypeFlags.Undefined|r.default.TypeFlags.Null|r.default.TypeFlags.Any|r.default.TypeFlags.Unknown|r.default.TypeFlags.Never))return!1;let t=e.getSymbol();if(!t)return!0;let n=t.declarations?.[0];if(!n)return!0;let i=n.getSourceFile();return!(/lib\..*\.d\.ts$/.test(i.fileName)||i.fileName.startsWith(`/@vue/`))}var E=r.default.TypeFormatFlags.WriteArrowStyleSignature|r.default.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope|r.default.TypeFormatFlags.NoTruncation|r.default.TypeFormatFlags.UseFullyQualifiedType,D=class{checker;index;cache=new Set;result;constructor(e){this.program=e,this.checker=e.getTypeChecker()}get(e){if(this.index=this.program.getSourceFile(e),!this.index)throw Error(`Cannot find file ${e}`);let t=this.checker.getSymbolAtLocation(this.index);if(!t)throw Error(`Cannot find module ${e}`);this.result={components:{},functions:{},types:{}};let n=this.index.statements.find(e=>r.default.isExportAssignment(e));if(n){let e=this.checker.getTypeAtLocation(n.expression).getCallSignatures()[0];e&&this.exportFunction(`default`,e)}return this.exportModule(t),this.result}exportModule(e){let t=x(this.checker,e);for(let[e,n]of t.entries()){let t=this.checker.getTypeOfSymbol(n);if(t.symbol&&t.symbol.flags&r.default.SymbolFlags.Module){let e=t.symbol.exports?.get(`default`);e&&(t=this.checker.getTypeOfSymbol(e))}let i=t.symbol||t.aliasSymbol;if(i?.flags&r.default.SymbolFlags.Function){let e=this.checker.getTypeOfSymbol(i),t=this.checker.getSignaturesOfType(e,r.default.SignatureKind.Call)[0];if(t){let e=S(this.checker,i);this.exportFunction(e,t)}}else if(this.typeToString(t).includes(`DefineComponent<`))try{this.exportComponent(e,t)}catch(t){console.error(`Fail to export component ${e}`),console.error(t)}}}exportComponent(e,t){let n=t.getConstructSignatures()[0];if(!n)return;let r=this.checker.getReturnTypeOfSignature(n),i={},a=this.checker.getPropertyOfType(r,`$props`);if(a){let e=m(this.checker.getTypeOfSymbol(a),this);i.properties=e.props,i.emits=e.emits}let o=this.checker.getPropertyOfType(r,`$slots`);o&&(i.slots=p(this.checker.getTypeOfSymbol(o),this)),this.result.components[e]=h(i)}exportFunction(e,n){let r=d(n,this.checker),i=b(n,this),a=n.getParameters().map(e=>{let n=e.valueDeclaration??this.index,a=this.checker.getTypeOfSymbol(e);this.extractType(a,n);let o=e.valueDeclaration?.initializer?.getText();return t.l({default:o,description:r.params?.[e.name],name:e.name,optional:!!n.questionToken||o!==void 0||void 0,type:C(this.typeToString(a),i)})});if(a.some(e=>e.name?.startsWith(`__VLS_`))){let t={},r=a.findIndex(e=>e.name===`__VLS_props`),o=n.getParameters()[r];if(o){let e=m(this.checker.getTypeOfSymbol(o),this);t.properties=e.props,t.emits=e.emits,Object.values(t.properties).forEach(e=>{e.type=C(e.type,i)})}let s=a.findIndex(e=>e.name===`__VLS_slots`),c=n.getParameters()[s];c&&(t.slots=p(this.checker.getTypeOfSymbol(c),this),Object.values(t.slots).forEach(e=>{e.type=C(e.type,i)})),this.result.components[e]=h(t);return}let o=this.checker.getReturnTypeOfSignature(n);this.extractType(o),this.result.functions[e]=t.l({description:r.description,parameters:a,return:{description:r.returns,type:C(this.typeToString(o),i)}})}extractType(e,n=this.index){if(this.cache.has(e))return;this.cache.add(e);let i=e.symbol||e.aliasSymbol;if((e.isUnion()||e.isIntersection())&&!(i?.flags&r.default.SymbolFlags.Enum)&&e.types.forEach(e=>{this.extractType(e,n)}),i){if(this.checker.getFullyQualifiedName(i).includes(`/@vue/`))return;let n=S(this.checker,i);if(i.flags&r.default.SymbolFlags.Enum){let e=i.valueDeclaration||i.declarations?.[0];if(e&&r.default.isEnumDeclaration(e)){let n=S(this.checker,i);this.result.types[n]=t.l({...d(i,this.checker),type:w(e)})}return}if(i.flags&r.default.SymbolFlags.Namespace){let e=i;this.exportModule(e)}if(i.flags&r.default.SymbolFlags.TypeAlias){let t=i.valueDeclaration||i.declarations?.[0];if(!t)return;let a=Object.fromEntries((t?.typeParameters??[]).map(e=>{let t=`unknown`,n=e.default;if(n){let e=this.checker.getTypeFromTypeNode(n);t=this.typeToString(e)}let r=e.constraint;if(r){let e=this.checker.getTypeFromTypeNode(r);t=this.typeToString(e)}return[e.symbol.name,t]}));this.result.types[n]={...d(i,this.checker),type:C(this.typeToString(e,E|r.default.TypeFormatFlags.InTypeAlias),a)}}if(i.flags&r.default.SymbolFlags.Interface&&T(e)){let t=i.getDeclarations()[0];if(t){let a=this.checker.getPropertiesOfType(e),o={};a.forEach(e=>{let n=this.checker.getTypeOfSymbol(e);this.extractType(n,t),o[e.name]={...d(e,this.checker),optional:!!(e.flags&r.default.SymbolFlags.Optional)||void 0,type:this.typeToString(n)}}),this.result.types[n]={...d(i,this.checker),type:o}}}if(i.flags&r.default.SymbolFlags.TypeLiteral){let t=S(this.checker,e.aliasSymbol||e.symbol),n=this.checker.getPropertiesOfType(e),a={};if([`Promise`,`Partial`,`Pick`,`Omit`,`Required`].includes(t)){this.cache.delete(e),this.extractType(e.aliasTypeArguments[0]);return}if(e.aliasTypeArguments?.some(e=>!T(e)))return;n.forEach(e=>{let t=this.checker.getTypeOfSymbol(e);a[e.name]={...d(e,this.checker),optional:!!(e.flags&r.default.SymbolFlags.Optional),type:this.typeToString(t)}}),t!==`__type`&&(this.result.types[t]={...d(i,this.checker),type:a})}}if(e.flags&r.default.TypeFlags.Object){let t=e;if(T(e)&&this.checker.getPropertiesOfType(e).forEach(e=>{let t=this.checker.getTypeOfSymbol(e);this.extractType(t,n)}),t.objectFlags&r.default.ObjectFlags.Reference){let e=t;this.checker.getTypeArguments(e).forEach(e=>{this.extractType(e,n)})}}}typeToString(e,t=E){return this.checker.typeToString(e,void 0,t)}};function O(e,n=`unknown`){let r={components:{},functions:{},types:{}};for(let[t,n]of Object.entries(e.types))r.types[t]=i(n,t);for(let[t,n]of Object.entries(e.functions))r.functions[t]=a(n,t);for(let[t,n]of Object.entries(e.components))r.components[t]=o(n,t);return r;function i(e,n=``){if(typeof e.type==`string`){if(e.type===`{}`)return t.i.void();let r={...s(t.c(e.type)),...t.l({description:e.description,name:n?.split(`.`).pop()||void 0,optional:e.optional})};if(e.default)switch(r.type){case t.r.boolean:r.default=e.default===`true`;break;case t.r.number:r.default=Number(e.default);break;case t.r.string:r.default=e.default;break}return r.type===t.r.object&&!Object.keys(r.properties).length&&(r.customizable=!0),r}else{let r=t.i.object(t.d(e.type,({key:e,value:t})=>i(t,e)),t.l({description:e.description,name:n?.split(`.`).pop(),optional:e.optional}));return Object.keys(r.properties).length||(r.customizable=!0),r}}function a(e,n=``){return{...t.l({description:e.description,name:n?.split(`.`).pop()}),parameters:(e.parameters??[]).map(e=>i(e,e.name)),return:i(e.return,`result`)}}function o(e,n=``){return{...t.l({name:n?.split(`.`).pop()}),emits:t.d(e.emits,({value:e})=>i(e)),models:t.d(e.models,({value:e})=>i(e)),properties:t.d(e.properties,({key:e,value:t})=>i(t,e)),slots:t.d(e.slots,({value:e})=>i(e))}}function s(r,i=``){return t.s(r,({schema:e})=>{if(e.type===t.r.interface)return{...a(e.className,i),description:e.description,name:e.name,optional:e.optional}});function a(r,i=``){let a=`${i}${r}`;for(;!e.types[a]&&a.indexOf(`.`)!==-1;)a=a.substring(a.indexOf(`.`)+1);return e.types[a]?t.i.type([`packages`,n,...a.split(`.`)]):t.i.interface(r)}}}function k(e){for(;;){let t=i.join(e,`tsconfig.json`);if(a.existsSync(t))return t;let n=i.dirname(e);if(n===e)return;e=n}}function A(e){let t=r.readConfigFile(e,r.sys.readFile);if(t.error)throw Error(`Failed to read tsconfig.json: ${t.error.messageText}`);return r.parseJsonConfigFileContent(t.config,r.sys,i.dirname(e))}function j(e,t){let n=i.join(process.cwd(),`__temp__.ts`),a=r.createCompilerHost(t),o=r.resolveModuleName(e,n,t,a);if(o.resolvedModule)return o.resolvedModule.resolvedFileName}function M(e){let t=k(e);if(!t)throw Error(`Could not find tsconfig.json for: ${e}`);let n=A(t).options,i=j(e,n);if(!i)throw Error(`Could not resolve module: ${e}`);let a=r.createCompilerHost(n),o=r.createProgram({host:a,options:n,rootNames:[i]}),s=o.getSourceFile(i);if(!s)throw Error(`Could not get source file for: ${i}`);return{program:o,sourceFile:s}}var N=n.default.promises;async function P(e,t){let n=e.split(`/`).pop().split(`.`)[0],{program:r,sourceFile:i}=M(e),a=O(new D(r).get(i.fileName),n);if(a.components.default){let r=u(a,{name:n,path:e});await N.mkdir(t.split(`/`).slice(0,-1).join(`/`),{recursive:!0}),await N.writeFile(t,r)}}exports.CTypeExport=D,exports.extractFile=P,exports.generateNodes=O,exports.getSourceFile=M;
@@ -1 +0,0 @@
1
- export { }
package/dist/extractor.js DELETED
@@ -1,512 +0,0 @@
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)]);
34
- }
35
- function buildFunctionCall(c, O) {
36
- let k = O.findLastIndex((c) => c !== "{}");
37
- return `${c}(${O.slice(0, k + 1).join(", ")})`;
38
- }
39
- function getCodeFromObject(c, O) {
40
- return `{${Object.entries(c).map(([c, k]) => `${getSafeKey(c)}: ${O ? k : JSON.stringify(k)}`).join(", ")}}`;
41
- }
42
- function getSafeKey(c) {
43
- return /^[a-zA-Z]+$/.test(c) ? c : JSON.stringify(c);
44
- }
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}";
59
-
60
- ${M}
61
-
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 ")}
66
- }`;
67
- }
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);
82
- }
83
- function mergeSymbolDisplayParts(c = []) {
84
- return c.map((c) => c.text).join("");
85
- }
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;
95
- }
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
- };
115
- }
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;
118
- }
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 };
128
- }
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
- });
136
- }
137
- function isEmits(c) {
138
- return /^on[A-Z]/.test(c);
139
- }
140
- function emitName(c) {
141
- return c = c.slice(2), c.charAt(0).toLowerCase() + c.slice(1);
142
- }
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
- }));
150
- }
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;
159
- }
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;
163
- }
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;
167
- }
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(", ")} }`;
177
- }
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/"));
186
- }
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
- }
466
- }
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
- }
475
- }
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));
480
- }
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;
484
- }
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
- };
500
- }
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
- }
511
- }
512
- export { CTypeExport, extractFile, generateNodes, getSourceFile };