crankscript 0.9.3 → 0.9.5
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/assets/plugin.ts
ADDED
@@ -0,0 +1,297 @@
|
|
1
|
+
import * as ts from 'typescript';
|
2
|
+
import * as tstl from 'typescript-to-lua';
|
3
|
+
import { FunctionVisitor, TransformationContext } from 'typescript-to-lua';
|
4
|
+
import * as lua from 'typescript-to-lua/dist/LuaAST';
|
5
|
+
import { ScopeType } from 'typescript-to-lua/dist/transformation/utils/scope';
|
6
|
+
import { transformCallAndArguments } from 'typescript-to-lua/dist/transformation/visitors/call';
|
7
|
+
import { transformClassInstanceFields } from 'typescript-to-lua/dist/transformation/visitors/class/members/fields';
|
8
|
+
import {
|
9
|
+
getExtendedNode,
|
10
|
+
isStaticNode,
|
11
|
+
} from 'typescript-to-lua/dist/transformation/visitors/class/utils';
|
12
|
+
import {
|
13
|
+
transformFunctionBodyContent,
|
14
|
+
transformFunctionToExpression,
|
15
|
+
transformParameters,
|
16
|
+
} from 'typescript-to-lua/dist/transformation/visitors/function';
|
17
|
+
|
18
|
+
function createClassCall(
|
19
|
+
context: tstl.TransformationContext,
|
20
|
+
className: tstl.Identifier,
|
21
|
+
extendsNode?: ts.ExpressionWithTypeArguments
|
22
|
+
): tstl.Statement {
|
23
|
+
// class('X')
|
24
|
+
const classCall = tstl.createCallExpression(
|
25
|
+
tstl.createIdentifier('class'),
|
26
|
+
[tstl.createStringLiteral(className.text)]
|
27
|
+
);
|
28
|
+
let classCreationExpression: tstl.Expression;
|
29
|
+
if (extendsNode) {
|
30
|
+
// class('X').extends(Blah)
|
31
|
+
classCreationExpression = tstl.createCallExpression(
|
32
|
+
tstl.createTableIndexExpression(
|
33
|
+
classCall,
|
34
|
+
tstl.createStringLiteral('extends')
|
35
|
+
),
|
36
|
+
[context.transformExpression(extendsNode.expression)]
|
37
|
+
);
|
38
|
+
} else {
|
39
|
+
classCreationExpression = tstl.createCallExpression(
|
40
|
+
tstl.createTableIndexExpression(
|
41
|
+
classCall,
|
42
|
+
tstl.createStringLiteral('extends')
|
43
|
+
),
|
44
|
+
[tstl.createIdentifier('Object')]
|
45
|
+
);
|
46
|
+
}
|
47
|
+
return tstl.createExpressionStatement(classCreationExpression);
|
48
|
+
}
|
49
|
+
|
50
|
+
export function transformPropertyName(
|
51
|
+
context: TransformationContext,
|
52
|
+
node: ts.PropertyName
|
53
|
+
): tstl.Expression {
|
54
|
+
if (ts.isComputedPropertyName(node)) {
|
55
|
+
return context.transformExpression(node.expression);
|
56
|
+
} else if (ts.isIdentifier(node)) {
|
57
|
+
return tstl.createStringLiteral(node.text);
|
58
|
+
} else if (ts.isPrivateIdentifier(node)) {
|
59
|
+
throw new Error('PrivateIdentifier is not supported');
|
60
|
+
} else {
|
61
|
+
return context.transformExpression(node);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
function transformConstructor(
|
66
|
+
context: TransformationContext,
|
67
|
+
className: tstl.Identifier,
|
68
|
+
instanceFields: ts.PropertyDeclaration[],
|
69
|
+
constructor?: ts.ConstructorDeclaration
|
70
|
+
): tstl.Statement | undefined {
|
71
|
+
const methodName = 'init';
|
72
|
+
context.pushScope(ScopeType.Function);
|
73
|
+
const bodyStatements: tstl.Statement[] = [];
|
74
|
+
let params: tstl.Identifier[];
|
75
|
+
if (constructor) {
|
76
|
+
[params] = transformParameters(
|
77
|
+
context,
|
78
|
+
constructor?.parameters,
|
79
|
+
tstl.createIdentifier('self')
|
80
|
+
);
|
81
|
+
} else {
|
82
|
+
params = [tstl.createIdentifier('self')];
|
83
|
+
}
|
84
|
+
bodyStatements.push(
|
85
|
+
tstl.createExpressionStatement(
|
86
|
+
tstl.createCallExpression(
|
87
|
+
tstl.createTableIndexExpression(
|
88
|
+
tstl.createTableIndexExpression(
|
89
|
+
className,
|
90
|
+
tstl.createStringLiteral('super')
|
91
|
+
),
|
92
|
+
tstl.createStringLiteral('init')
|
93
|
+
),
|
94
|
+
params
|
95
|
+
)
|
96
|
+
)
|
97
|
+
);
|
98
|
+
const classInstanceFields = transformClassInstanceFields(
|
99
|
+
context,
|
100
|
+
instanceFields
|
101
|
+
);
|
102
|
+
// initializers have to come before any body of the constructor
|
103
|
+
bodyStatements.push(...classInstanceFields);
|
104
|
+
if (constructor?.body) {
|
105
|
+
const body = transformFunctionBodyContent(context, constructor.body);
|
106
|
+
// if the first expression in the body is a super call, ignore it, because we have
|
107
|
+
// constructed our own super call.
|
108
|
+
// if it's not, make sure to include the entire body.
|
109
|
+
const firstStatement = constructor.body.statements[0];
|
110
|
+
if (
|
111
|
+
firstStatement &&
|
112
|
+
ts.isExpressionStatement(firstStatement) &&
|
113
|
+
ts.isCallExpression(firstStatement.expression) &&
|
114
|
+
firstStatement.expression.expression.kind ===
|
115
|
+
ts.SyntaxKind.SuperKeyword
|
116
|
+
) {
|
117
|
+
bodyStatements.push(...body.slice(1));
|
118
|
+
} else {
|
119
|
+
bodyStatements.push(...body);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
context.popScope();
|
123
|
+
return tstl.createAssignmentStatement(
|
124
|
+
tstl.createTableIndexExpression(
|
125
|
+
className,
|
126
|
+
tstl.createStringLiteral(methodName)
|
127
|
+
),
|
128
|
+
tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params)
|
129
|
+
);
|
130
|
+
}
|
131
|
+
|
132
|
+
function transformMethodDeclaration(
|
133
|
+
context: TransformationContext,
|
134
|
+
node: ts.MethodDeclaration,
|
135
|
+
className: tstl.Identifier
|
136
|
+
): tstl.Statement | undefined {
|
137
|
+
const [functionExpression] = transformFunctionToExpression(context, node);
|
138
|
+
return tstl.createAssignmentStatement(
|
139
|
+
tstl.createTableIndexExpression(
|
140
|
+
className,
|
141
|
+
transformPropertyName(context, node.name)
|
142
|
+
),
|
143
|
+
functionExpression
|
144
|
+
);
|
145
|
+
}
|
146
|
+
interface ClassSuperInfo {
|
147
|
+
className: lua.Identifier;
|
148
|
+
extendedTypeNode?: ts.ExpressionWithTypeArguments;
|
149
|
+
}
|
150
|
+
|
151
|
+
export const transformClassDeclaration: FunctionVisitor<
|
152
|
+
ts.ClassLikeDeclaration
|
153
|
+
> = (
|
154
|
+
declaration,
|
155
|
+
context: TransformationContext & { classSuperInfos?: [ClassSuperInfo] }
|
156
|
+
) => {
|
157
|
+
let className: tstl.Identifier;
|
158
|
+
if (declaration.name) {
|
159
|
+
className = tstl.createIdentifier(declaration.name.text);
|
160
|
+
} else {
|
161
|
+
className = tstl.createIdentifier(
|
162
|
+
context.createTempName('class'),
|
163
|
+
declaration
|
164
|
+
);
|
165
|
+
}
|
166
|
+
|
167
|
+
const extension = getExtendedNode(declaration);
|
168
|
+
if (context.classSuperInfos) {
|
169
|
+
context.classSuperInfos.push({
|
170
|
+
className,
|
171
|
+
extendedTypeNode: extension,
|
172
|
+
});
|
173
|
+
} else {
|
174
|
+
context.classSuperInfos = [{ className, extendedTypeNode: extension }];
|
175
|
+
}
|
176
|
+
|
177
|
+
// Get all properties with value
|
178
|
+
const properties = declaration.members
|
179
|
+
.filter(ts.isPropertyDeclaration)
|
180
|
+
.filter((member) => member.initializer);
|
181
|
+
|
182
|
+
// Divide properties into static and non-static
|
183
|
+
const instanceFields = properties.filter((prop) => !isStaticNode(prop));
|
184
|
+
|
185
|
+
const statements: tstl.Statement[] = [];
|
186
|
+
|
187
|
+
// class('X')
|
188
|
+
statements.push(createClassCall(context, className, extension));
|
189
|
+
|
190
|
+
// function X:init()
|
191
|
+
// X.super.init(self)
|
192
|
+
// end
|
193
|
+
const constructor = declaration.members.find(
|
194
|
+
(n): n is ts.ConstructorDeclaration =>
|
195
|
+
ts.isConstructorDeclaration(n) && n.body !== undefined
|
196
|
+
);
|
197
|
+
const transformedConstructor = transformConstructor(
|
198
|
+
context,
|
199
|
+
className,
|
200
|
+
instanceFields,
|
201
|
+
constructor
|
202
|
+
);
|
203
|
+
if (transformedConstructor) {
|
204
|
+
statements.push(transformedConstructor);
|
205
|
+
}
|
206
|
+
|
207
|
+
const methods = declaration.members
|
208
|
+
.filter(ts.isMethodDeclaration)
|
209
|
+
.map((method) => transformMethodDeclaration(context, method, className))
|
210
|
+
.filter((method): method is tstl.Statement => method !== undefined);
|
211
|
+
statements.push(...methods);
|
212
|
+
|
213
|
+
return statements;
|
214
|
+
};
|
215
|
+
|
216
|
+
const transformNewExpression: FunctionVisitor<ts.NewExpression> = (
|
217
|
+
node,
|
218
|
+
context
|
219
|
+
) => {
|
220
|
+
const signature = context.checker.getResolvedSignature(node);
|
221
|
+
const [name, params] = transformCallAndArguments(
|
222
|
+
context,
|
223
|
+
node.expression,
|
224
|
+
node.arguments ?? [ts.factory.createTrue()],
|
225
|
+
signature
|
226
|
+
);
|
227
|
+
return tstl.createCallExpression(name, params);
|
228
|
+
};
|
229
|
+
|
230
|
+
export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (
|
231
|
+
expression,
|
232
|
+
context: TransformationContext & { classSuperInfos?: ClassSuperInfo[] }
|
233
|
+
) => {
|
234
|
+
const superInfos = context.classSuperInfos;
|
235
|
+
let superInfo: ClassSuperInfo | undefined = undefined;
|
236
|
+
if (superInfos) {
|
237
|
+
superInfo = superInfos[superInfos.length - 1];
|
238
|
+
}
|
239
|
+
if (!superInfo) return lua.createAnonymousIdentifier(expression);
|
240
|
+
const { className } = superInfo;
|
241
|
+
|
242
|
+
// Using `super` without extended type node is a TypeScript error
|
243
|
+
// const extendsExpression = extendedTypeNode?.expression;
|
244
|
+
// let baseClassName: lua.AssignmentLeftHandSideExpression | undefined;
|
245
|
+
|
246
|
+
// if (extendsExpression && ts.isIdentifier(extendsExpression)) {
|
247
|
+
// const symbol = context.checker.getSymbolAtLocation(extendsExpression);
|
248
|
+
// if (symbol && !isSymbolExported(context, symbol)) {
|
249
|
+
// // Use "baseClassName" if base is a simple identifier
|
250
|
+
// baseClassName = transformIdentifier(context, extendsExpression);
|
251
|
+
// }
|
252
|
+
// }
|
253
|
+
|
254
|
+
// if (!baseClassName) {
|
255
|
+
// // Use "className.____super" if the base is not a simple identifier
|
256
|
+
// baseClassName = lua.createTableIndexExpression(
|
257
|
+
// className,
|
258
|
+
// lua.createStringLiteral('____super'),
|
259
|
+
// expression
|
260
|
+
// );
|
261
|
+
// }
|
262
|
+
|
263
|
+
return lua.createTableIndexExpression(
|
264
|
+
className,
|
265
|
+
lua.createStringLiteral('super')
|
266
|
+
);
|
267
|
+
};
|
268
|
+
|
269
|
+
const plugin = {
|
270
|
+
visitors: {
|
271
|
+
[ts.SyntaxKind.ClassDeclaration]: transformClassDeclaration,
|
272
|
+
[ts.SyntaxKind.SuperKeyword]: transformSuperExpression,
|
273
|
+
[ts.SyntaxKind.NewExpression]: transformNewExpression,
|
274
|
+
[ts.SyntaxKind.CallExpression]: (node, context) => {
|
275
|
+
if (
|
276
|
+
ts.isIdentifier(node.expression) &&
|
277
|
+
node.expression.escapedText === 'require'
|
278
|
+
) {
|
279
|
+
console.log(1);
|
280
|
+
const normalNode = context.superTransformExpression(
|
281
|
+
node
|
282
|
+
) as unknown as {
|
283
|
+
expression: { text: string; originalName: string };
|
284
|
+
};
|
285
|
+
|
286
|
+
normalNode.expression.text = 'import';
|
287
|
+
normalNode.expression.originalName = 'import';
|
288
|
+
|
289
|
+
return normalNode as unknown as lua.Expression;
|
290
|
+
} else {
|
291
|
+
return context.superTransformExpression(node);
|
292
|
+
}
|
293
|
+
},
|
294
|
+
},
|
295
|
+
} satisfies tstl.Plugin;
|
296
|
+
|
297
|
+
export default plugin;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "crankscript",
|
3
|
-
"version": "0.9.
|
3
|
+
"version": "0.9.5",
|
4
4
|
"scripts": {
|
5
5
|
"dev": "tsx src/index.ts",
|
6
6
|
"post-build": "tsc-alias --project tsconfig.json"
|
@@ -17,9 +17,10 @@
|
|
17
17
|
"ink": "^5.0.1",
|
18
18
|
"react": "^18.3.1",
|
19
19
|
"ts-morph": "^23.0.0",
|
20
|
+
"ts-node": "^10.9.2",
|
20
21
|
"typanion": "^3.14.0",
|
21
|
-
"typescript
|
22
|
-
"typescript": "
|
22
|
+
"typescript": "~5.6.2",
|
23
|
+
"typescript-to-lua": "^1.27.0"
|
23
24
|
},
|
24
25
|
"type": "module",
|
25
26
|
"main": "./src/index.js",
|
@@ -79,10 +79,6 @@ export const createTypeProvider = (version)=>{
|
|
79
79
|
const getStatements = ()=>{
|
80
80
|
return provider.statements;
|
81
81
|
};
|
82
|
-
const isPropertyStatic = (property)=>{
|
83
|
-
const { isStatic } = getPropertyDetails(property);
|
84
|
-
return isStatic;
|
85
|
-
};
|
86
82
|
const getFunctionReturnType = (func)=>{
|
87
83
|
const { returnType } = getFunctionDetails(func);
|
88
84
|
return returnType;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/cli/src/commands/GenerateTypes/utils/createTypeProvider.ts"],"sourcesContent":["import { readFileSync } from 'fs';\nimport { existsSync, writeFileSync } from 'node:fs';\nimport { join } from 'node:path';\nimport {\n FunctionDeclarationStructure,\n ParameterDeclarationStructure,\n StructureKind,\n} from 'ts-morph';\nimport { DataFolder } from '@/cli/constants.js';\nimport {\n FunctionDescription,\n FunctionDetails,\n ParameterDetails,\n PropertyDescription,\n PropertyDetails,\n TypeProviderData,\n} from '@/cli/types.js';\n\nfunction kebabToCamelCase(str: string): string {\n return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());\n}\n\nexport const createTypeProvider = (version: string) => {\n const path = join(DataFolder, `${version}.json`);\n const fallbackProvider = existsSync(path)\n ? (JSON.parse(readFileSync(path, 'utf-8')) as TypeProviderData)\n : ({\n globalStatements: [],\n statements: [],\n properties: {},\n functions: {},\n } satisfies TypeProviderData);\n const provider = {\n globalStatements: fallbackProvider.globalStatements,\n statements: fallbackProvider.statements,\n properties: {},\n functions: {},\n } as TypeProviderData;\n const visitedProperties = new Map<string, PropertyDetails>();\n const visitedFunctions = new Map<string, FunctionDetails>();\n\n const getPropertyDetails = (property: PropertyDescription) => {\n if (visitedProperties.has(property.signature)) {\n return visitedProperties.get(property.signature) as PropertyDetails;\n }\n\n let result: PropertyDetails;\n let prop = provider.properties[property.signature];\n\n if (!prop) {\n prop = fallbackProvider.properties[property.signature];\n }\n\n if (!prop) {\n const details = {\n signature: property.signature,\n type: 'any',\n } satisfies PropertyDetails;\n\n provider.properties[property.signature] = details;\n\n result = details;\n } else {\n provider.properties[property.signature] = prop;\n\n result = prop;\n }\n\n visitedProperties.set(property.signature, result);\n\n return result;\n };\n\n const getFunctionDetails = (func: FunctionDescription): FunctionDetails => {\n if (visitedFunctions.has(func.signature)) {\n return visitedFunctions.get(func.signature) as FunctionDetails;\n }\n\n let result: FunctionDetails;\n let fn = provider.functions[func.signature];\n\n if (!fn) {\n fn = fallbackProvider.functions[func.signature];\n }\n\n if (!fn) {\n const details = {\n signature: func.signature,\n parameters: func.parameters.map((p) => ({\n name: p.name,\n type: 'any',\n })),\n returnType: 'any',\n } satisfies FunctionDetails;\n\n provider.functions[func.signature] = details;\n\n result = details;\n } else {\n provider.functions[func.signature] = fn;\n\n result = fn;\n }\n\n visitedFunctions.set(func.signature, result);\n\n return result;\n };\n\n const getGlobalStatements = () => {\n return provider.globalStatements;\n };\n\n const getStatements = () => {\n return provider.statements;\n };\n\n const isPropertyStatic = (property: PropertyDescription) => {\n const { isStatic } = getPropertyDetails(property);\n\n return isStatic;\n };\n\n const getFunctionReturnType = (func: FunctionDescription) => {\n const { returnType } = getFunctionDetails(func);\n\n return returnType;\n };\n\n const getParameterDetails = (\n func: FunctionDescription,\n parameter: string\n ) => {\n const { parameters } = getFunctionDetails(func);\n const param = parameters.find((p) => p.name === parameter);\n\n if (!param) {\n return {\n name: parameter,\n type: 'any',\n } satisfies ParameterDetails;\n }\n\n return param;\n };\n\n const getParameters = (\n func: FunctionDescription\n ): FunctionDeclarationStructure['parameters'] => {\n const { overrideParameters = false, parameters } =\n getFunctionDetails(func);\n const getParameterFromDetails = (parameter: ParameterDetails) => {\n return {\n kind: StructureKind.Parameter,\n name: kebabToCamelCase(parameter.name),\n type: parameter.type,\n ...(parameter.overrideOptions ?? {}),\n } satisfies ParameterDeclarationStructure;\n };\n\n if (overrideParameters) {\n return parameters.map((details) => {\n return getParameterFromDetails(details);\n });\n }\n\n return func.parameters.map((parameter) => {\n const details = getParameterDetails(func, parameter.name);\n\n return {\n hasQuestionToken: !parameter.required,\n ...getParameterFromDetails(details),\n } satisfies ParameterDeclarationStructure;\n });\n };\n\n const getFunctionOverrideOptions = (func: FunctionDescription) => {\n return getFunctionDetails(func).overrideOptions ?? {};\n };\n\n const save = () => {\n const contents = JSON.stringify(provider, null, 4) + '\\n';\n\n writeFileSync(path, contents, 'utf-8');\n };\n\n return {\n getGlobalStatements,\n getStatements,\n getPropertyDetails,\n getFunctionReturnType,\n getParameterDetails,\n getParameters,\n getFunctionOverrideOptions,\n save,\n };\n};\n"],"names":["readFileSync","existsSync","writeFileSync","join","StructureKind","DataFolder","kebabToCamelCase","str","replace","_","letter","toUpperCase","createTypeProvider","version","path","fallbackProvider","JSON","parse","globalStatements","statements","properties","functions","provider","visitedProperties","Map","visitedFunctions","getPropertyDetails","property","has","signature","get","result","prop","details","type","set","getFunctionDetails","func","fn","parameters","map","p","name","returnType","getGlobalStatements","getStatements","isPropertyStatic","isStatic","getFunctionReturnType","getParameterDetails","parameter","param","find","getParameters","overrideParameters","getParameterFromDetails","kind","Parameter","overrideOptions","hasQuestionToken","required","getFunctionOverrideOptions","save","contents","stringify"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";AAAA,SAASA,YAAY,QAAQ,KAAK;AAClC,SAASC,UAAU,EAAEC,aAAa,QAAQ,UAAU;AACpD,SAASC,IAAI,QAAQ,YAA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NhB,MAAM;YACV;QACJ;QAEA,OAAOiB;IACX;IAEA,MAAME,gBAAgB,CAClBhB;QAEA,MAAM,EAAEiB,qBAAqB,KAAK,EAAEf,UAAU,EAAE,GAC5CH,mBAAmBC;QACvB,MAAMkB,0BAA0B,CAACL;gBAKrBA;YAJR,OAAO;gBACHM,MAAMpD,cAAcqD,SAAS;gBAC7Bf,MAAMpC,iBAAiB4C,UAAUR,IAAI;gBACrCR,MAAMgB,UAAUhB,IAAI;eAChBgB,CAAAA,6BAAAA,UAAUQ,eAAe,YAAzBR,6BAA6B,CAAC;QAE1C;QAEA,IAAII,oBAAoB;YACpB,OAAOf,WAAWC,GAAG,CAAC,CAACP;gBACnB,OAAOsB,wBAAwBtB;YACnC;QACJ;QAEA,OAAOI,KAAKE,UAAU,CAACC,GAAG,CAAC,CAACU;YACxB,MAAMjB,UAAUgB,oBAAoBZ,MAAMa,UAAUR,IAAI;YAExD,OAAO;gBACHiB,kBAAkB,CAACT,UAAUU,QAAQ;eAClCL,wBAAwBtB;QAEnC;IACJ;IAEA,MAAM4B,6BAA6B,CAACxB;YACzBD;QAAP,OAAOA,CAAAA,sCAAAA,mBAAmBC,MAAMqB,eAAe,YAAxCtB,sCAA4C,CAAC;IACxD;IAEA,MAAM0B,OAAO;QACT,MAAMC,WAAW/C,KAAKgD,SAAS,CAAC1C,UAAU,MAAM,KAAK;QAErDpB,cAAcY,MAAMiD,UAAU;IAClC;IAEA,OAAO;QACHnB;QACAC;QACAnB;QACAsB;QACAC;QACAI;QACAQ;QACAC;IACJ;AACJ,EAAE"}
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/cli/src/commands/GenerateTypes/utils/createTypeProvider.ts"],"sourcesContent":["import { readFileSync } from 'fs';\nimport { existsSync, writeFileSync } from 'node:fs';\nimport { join } from 'node:path';\nimport {\n FunctionDeclarationStructure,\n ParameterDeclarationStructure,\n StructureKind,\n} from 'ts-morph';\nimport { DataFolder } from '@/cli/constants.js';\nimport {\n FunctionDescription,\n FunctionDetails,\n ParameterDetails,\n PropertyDescription,\n PropertyDetails,\n TypeProviderData,\n} from '@/cli/types.js';\n\nfunction kebabToCamelCase(str: string): string {\n return str.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());\n}\n\nexport const createTypeProvider = (version: string) => {\n const path = join(DataFolder, `${version}.json`);\n const fallbackProvider = existsSync(path)\n ? (JSON.parse(readFileSync(path, 'utf-8')) as TypeProviderData)\n : ({\n globalStatements: [],\n statements: [],\n properties: {},\n functions: {},\n } satisfies TypeProviderData);\n const provider = {\n globalStatements: fallbackProvider.globalStatements,\n statements: fallbackProvider.statements,\n properties: {},\n functions: {},\n } as TypeProviderData;\n const visitedProperties = new Map<string, PropertyDetails>();\n const visitedFunctions = new Map<string, FunctionDetails>();\n\n const getPropertyDetails = (property: PropertyDescription) => {\n if (visitedProperties.has(property.signature)) {\n return visitedProperties.get(property.signature) as PropertyDetails;\n }\n\n let result: PropertyDetails;\n let prop = provider.properties[property.signature];\n\n if (!prop) {\n prop = fallbackProvider.properties[property.signature];\n }\n\n if (!prop) {\n const details = {\n signature: property.signature,\n type: 'any',\n } satisfies PropertyDetails;\n\n provider.properties[property.signature] = details;\n\n result = details;\n } else {\n provider.properties[property.signature] = prop;\n\n result = prop;\n }\n\n visitedProperties.set(property.signature, result);\n\n return result;\n };\n\n const getFunctionDetails = (func: FunctionDescription): FunctionDetails => {\n if (visitedFunctions.has(func.signature)) {\n return visitedFunctions.get(func.signature) as FunctionDetails;\n }\n\n let result: FunctionDetails;\n let fn = provider.functions[func.signature];\n\n if (!fn) {\n fn = fallbackProvider.functions[func.signature];\n }\n\n if (!fn) {\n const details = {\n signature: func.signature,\n parameters: func.parameters.map((p) => ({\n name: p.name,\n type: 'any',\n })),\n returnType: 'any',\n } satisfies FunctionDetails;\n\n provider.functions[func.signature] = details;\n\n result = details;\n } else {\n provider.functions[func.signature] = fn;\n\n result = fn;\n }\n\n visitedFunctions.set(func.signature, result);\n\n return result;\n };\n\n const getGlobalStatements = () => {\n return provider.globalStatements;\n };\n\n const getStatements = () => {\n return provider.statements;\n };\n\n const getFunctionReturnType = (func: FunctionDescription) => {\n const { returnType } = getFunctionDetails(func);\n\n return returnType;\n };\n\n const getParameterDetails = (\n func: FunctionDescription,\n parameter: string\n ) => {\n const { parameters } = getFunctionDetails(func);\n const param = parameters.find((p) => p.name === parameter);\n\n if (!param) {\n return {\n name: parameter,\n type: 'any',\n } satisfies ParameterDetails;\n }\n\n return param;\n };\n\n const getParameters = (\n func: FunctionDescription\n ): FunctionDeclarationStructure['parameters'] => {\n const { overrideParameters = false, parameters } =\n getFunctionDetails(func);\n const getParameterFromDetails = (parameter: ParameterDetails) => {\n return {\n kind: StructureKind.Parameter,\n name: kebabToCamelCase(parameter.name),\n type: parameter.type,\n ...(parameter.overrideOptions ?? {}),\n } satisfies ParameterDeclarationStructure;\n };\n\n if (overrideParameters) {\n return parameters.map((details) => {\n return getParameterFromDetails(details);\n });\n }\n\n return func.parameters.map((parameter) => {\n const details = getParameterDetails(func, parameter.name);\n\n return {\n hasQuestionToken: !parameter.required,\n ...getParameterFromDetails(details),\n } satisfies ParameterDeclarationStructure;\n });\n };\n\n const getFunctionOverrideOptions = (func: FunctionDescription) => {\n return getFunctionDetails(func).overrideOptions ?? {};\n };\n\n const save = () => {\n const contents = JSON.stringify(provider, null, 4) + '\\n';\n\n writeFileSync(path, contents, 'utf-8');\n };\n\n return {\n getGlobalStatements,\n getStatements,\n getPropertyDetails,\n getFunctionReturnType,\n getParameterDetails,\n getParameters,\n getFunctionOverrideOptions,\n save,\n };\n};\n"],"names":["readFileSync","existsSync","writeFileSync","join","StructureKind","DataFolder","kebabToCamelCase","str","replace","_","letter","toUpperCase","createTypeProvider","version","path","fallbackProvider","JSON","parse","globalStatements","statements","properties","functions","provider","visitedProperties","Map","visitedFunctions","getPropertyDetails","property","has","signature","get","result","prop","details","type","set","getFunctionDetails","func","fn","parameters","map","p","name","returnType","getGlobalStatements","getStatements","getFunctionReturnType","getParameterDetails","parameter","param","find","getParameters","overrideParameters","getParameterFromDetails","kind","Parameter","overrideOptions","hasQuestionToken","required","getFunctionOverrideOptions","save","contents","stringify"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";AAAA,SAASA,YAAY,QAAQ,KAAK;AAClC,SAASC,UAAU,EAAEC,aAAa,QAAQ,UAAU;AACpD,SAASC,IAAI,QAAQ,YAAY;AACjC,SAGIC,aAAa,QACV,WAAW;AAClB,SAASC,UAAU,QAAQ,qBAAqB;AAUhD,SAASC,iBAAiBC,GAAW;IACjC,OAAOA,IAAIC,OAAO,CAAC,aAAa,CAACC,GAAGC,SAAWA,OAAOC,WAAW;AACrE;AAEA,OAAO,MAAMC,qBAAqB,CAACC;IAC/B,MAAMC,OAAOX,KAAKE,YAAY,CAAC,EAAEQ,QAAQ,KAAK,CAAC;IAC/C,MAAME,mBAAmBd,WAAWa,QAC7BE,KAAKC,KAAK,CAACjB,aAAac,MAAM,YAC9B;QACGI,kBAAkB,EAAE;QACpBC,YAAY,EAAE;QACdC,YAAY,CAAC;QACbC,WAAW,CAAC;IAChB;IACN,MAAMC,WAAW;QACbJ,kBAAkBH,iBAAiBG,gBAAgB;QACnDC,YAAYJ,iBAAiBI,UAAU;QACvCC,YAAY,CAAC;QACbC,WAAW,CAAC;IAChB;IACA,MAAME,oBAAoB,IAAIC;IAC9B,MAAMC,mBAAmB,IAAID;IAE7B,MAAME,qBAAqB,CAACC;QACxB,IAAIJ,kBAAkBK,GAAG,CAACD,SAASE,SAAS,GAAG;YAC3C,OAAON,kBAAkBO,GAAG,CAACH,SAASE,SAAS;QACnD;QAEA,IAAIE;QACJ,IAAIC,OAAOV,SAASF,UAAU,CAACO,SAASE,SAAS,CAAC;QAElD,IAAI,CAACG,MAAM;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