crankscript 0.10.6 → 0.10.8

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.
Files changed (75) hide show
  1. package/assets/ImportMap/ImportMap.d.ts +25 -0
  2. package/assets/ImportMap/ImportMap.js +83 -0
  3. package/assets/ImportMap/ImportMap.js.map +1 -0
  4. package/assets/ImportMap/index.d.ts +1 -0
  5. package/assets/ImportMap/index.js +8 -0
  6. package/assets/ImportMap/index.js.map +1 -0
  7. package/assets/beforeEmit/getBeforeEmit.d.ts +2 -0
  8. package/assets/beforeEmit/getBeforeEmit.js +22 -0
  9. package/assets/beforeEmit/getBeforeEmit.js.map +1 -0
  10. package/assets/beforeEmit/index.d.ts +1 -0
  11. package/assets/beforeEmit/index.js +8 -0
  12. package/assets/beforeEmit/index.js.map +1 -0
  13. package/assets/beforeEmit/withAutomaticImportWarning.d.ts +1 -0
  14. package/assets/beforeEmit/withAutomaticImportWarning.js +15 -0
  15. package/assets/beforeEmit/withAutomaticImportWarning.js.map +1 -0
  16. package/assets/index.d.ts +14 -0
  17. package/assets/index.js +32 -0
  18. package/assets/index.js.map +1 -0
  19. package/assets/transformCallExpression/getTransformCallExpression.d.ts +4 -0
  20. package/assets/transformCallExpression/getTransformCallExpression.js +29 -0
  21. package/assets/transformCallExpression/getTransformCallExpression.js.map +1 -0
  22. package/assets/transformCallExpression/index.d.ts +1 -0
  23. package/assets/transformCallExpression/index.js +8 -0
  24. package/assets/transformCallExpression/index.js.map +1 -0
  25. package/assets/transformClassDeclaration/createClasCall.d.ts +3 -0
  26. package/assets/transformClassDeclaration/createClasCall.js +32 -0
  27. package/assets/transformClassDeclaration/createClasCall.js.map +1 -0
  28. package/assets/transformClassDeclaration/getExportedClassDeclarationStatements.d.ts +3 -0
  29. package/assets/transformClassDeclaration/getExportedClassDeclarationStatements.js +23 -0
  30. package/assets/transformClassDeclaration/getExportedClassDeclarationStatements.js.map +1 -0
  31. package/assets/transformClassDeclaration/getTransformClassDeclaration.d.ts +5 -0
  32. package/assets/transformClassDeclaration/getTransformClassDeclaration.js +72 -0
  33. package/assets/transformClassDeclaration/getTransformClassDeclaration.js.map +1 -0
  34. package/assets/transformClassDeclaration/index.d.ts +1 -0
  35. package/assets/transformClassDeclaration/index.js +8 -0
  36. package/assets/transformClassDeclaration/index.js.map +1 -0
  37. package/assets/transformClassDeclaration/transformConstructor.d.ts +4 -0
  38. package/assets/transformClassDeclaration/transformConstructor.js +51 -0
  39. package/assets/transformClassDeclaration/transformConstructor.js.map +1 -0
  40. package/assets/transformClassDeclaration/transformMethodDeclaration.d.ts +4 -0
  41. package/assets/transformClassDeclaration/transformMethodDeclaration.js +20 -0
  42. package/assets/transformClassDeclaration/transformMethodDeclaration.js.map +1 -0
  43. package/assets/transformClassDeclaration/transformPropertyName.d.ts +3 -0
  44. package/assets/transformClassDeclaration/transformPropertyName.js +26 -0
  45. package/assets/transformClassDeclaration/transformPropertyName.js.map +1 -0
  46. package/assets/transformNewExpression/index.d.ts +1 -0
  47. package/assets/transformNewExpression/index.js +8 -0
  48. package/assets/transformNewExpression/index.js.map +1 -0
  49. package/assets/transformNewExpression/transformNewExpression.d.ts +3 -0
  50. package/assets/transformNewExpression/transformNewExpression.js +24 -0
  51. package/assets/transformNewExpression/transformNewExpression.js.map +1 -0
  52. package/assets/transformPropertyAccessExpression/getTransformPropertyAccessExpression.d.ts +3 -0
  53. package/assets/transformPropertyAccessExpression/getTransformPropertyAccessExpression.js +22 -0
  54. package/assets/transformPropertyAccessExpression/getTransformPropertyAccessExpression.js.map +1 -0
  55. package/assets/transformPropertyAccessExpression/index.d.ts +1 -0
  56. package/assets/transformPropertyAccessExpression/index.js +8 -0
  57. package/assets/transformPropertyAccessExpression/index.js.map +1 -0
  58. package/assets/transformSuperExpression/index.d.ts +1 -0
  59. package/assets/transformSuperExpression/index.js +8 -0
  60. package/assets/transformSuperExpression/index.js.map +1 -0
  61. package/assets/transformSuperExpression/transformSuperExpression.d.ts +7 -0
  62. package/assets/transformSuperExpression/transformSuperExpression.js +24 -0
  63. package/assets/transformSuperExpression/transformSuperExpression.js.map +1 -0
  64. package/assets/types.d.ts +6 -0
  65. package/assets/types.js +6 -0
  66. package/assets/types.js.map +1 -0
  67. package/package.json +2 -3
  68. package/src/commands/TranspileCommand/fn/_tests/test-super/src/index.js +23 -0
  69. package/src/commands/TranspileCommand/fn/_tests/test-super/src/index.js.map +1 -0
  70. package/src/commands/TranspileCommand/fn/transpile.d.ts +2 -1
  71. package/src/commands/TranspileCommand/fn/transpile.js +2 -2
  72. package/src/commands/TranspileCommand/fn/transpile.js.map +1 -1
  73. package/assets/plugin.js +0 -260
  74. package/assets/plugin.ts +0 -372
  75. package/assets/tsconfig.json +0 -10
package/assets/plugin.ts DELETED
@@ -1,372 +0,0 @@
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
- const importMap = {
19
- graphics: new Set(['graphics']),
20
- sprites: new Set(['sprite']),
21
- crank: new Set(['getCrankTicks']),
22
- object: new Set(['printTable']),
23
- 'utilities/where': new Set(['where']),
24
- easing: new Set(['easingFunctions']),
25
- nineSlice: new Set(['nineSlice']),
26
- qrcode: new Set(['generateQRCode']),
27
- animation: new Set(['animation']),
28
- animator: new Set(['animator']),
29
- keyboard: new Set(['keyboard']),
30
- math: new Set(['math']),
31
- string: new Set(['string']),
32
- timer: new Set(['timer']),
33
- frameTimer: new Set(['frameTimer']),
34
- ui: new Set(['ui']),
35
- };
36
-
37
- const imports = new Set<string>();
38
-
39
- function createClassCall(
40
- context: tstl.TransformationContext,
41
- className: tstl.Identifier,
42
- extendsNode?: ts.ExpressionWithTypeArguments
43
- ): tstl.Statement {
44
- // class('X')
45
- const classCall = tstl.createCallExpression(
46
- tstl.createIdentifier('class'),
47
- [tstl.createStringLiteral(className.text)]
48
- );
49
- let classCreationExpression: tstl.Expression;
50
- if (extendsNode) {
51
- // class('X').extends(Blah)
52
- classCreationExpression = tstl.createCallExpression(
53
- tstl.createTableIndexExpression(
54
- classCall,
55
- tstl.createStringLiteral('extends')
56
- ),
57
- [context.transformExpression(extendsNode.expression)]
58
- );
59
- } else {
60
- classCreationExpression = tstl.createCallExpression(
61
- tstl.createTableIndexExpression(
62
- classCall,
63
- tstl.createStringLiteral('extends')
64
- ),
65
- [tstl.createIdentifier('Object')]
66
- );
67
- }
68
- return tstl.createExpressionStatement(classCreationExpression);
69
- }
70
-
71
- export function transformPropertyName(
72
- context: TransformationContext,
73
- node: ts.PropertyName
74
- ): tstl.Expression {
75
- if (ts.isComputedPropertyName(node)) {
76
- return context.transformExpression(node.expression);
77
- } else if (ts.isIdentifier(node)) {
78
- return tstl.createStringLiteral(node.text);
79
- } else if (ts.isPrivateIdentifier(node)) {
80
- throw new Error('PrivateIdentifier is not supported');
81
- } else {
82
- return context.transformExpression(node);
83
- }
84
- }
85
-
86
- function transformConstructor(
87
- context: TransformationContext,
88
- className: tstl.Identifier,
89
- instanceFields: ts.PropertyDeclaration[],
90
- constructor?: ts.ConstructorDeclaration
91
- ): tstl.Statement | undefined {
92
- const methodName = 'init';
93
- context.pushScope(ScopeType.Function);
94
- const bodyStatements: tstl.Statement[] = [];
95
- let params: tstl.Identifier[];
96
- if (constructor) {
97
- [params] = transformParameters(
98
- context,
99
- constructor?.parameters,
100
- tstl.createIdentifier('self')
101
- );
102
- } else {
103
- params = [tstl.createIdentifier('self')];
104
- }
105
- bodyStatements.push(
106
- tstl.createExpressionStatement(
107
- tstl.createCallExpression(
108
- tstl.createTableIndexExpression(
109
- tstl.createTableIndexExpression(
110
- className,
111
- tstl.createStringLiteral('super')
112
- ),
113
- tstl.createStringLiteral('init')
114
- ),
115
- params
116
- )
117
- )
118
- );
119
- const classInstanceFields = transformClassInstanceFields(
120
- context,
121
- instanceFields
122
- );
123
- // initializers have to come before any body of the constructor
124
- bodyStatements.push(...classInstanceFields);
125
- if (constructor?.body) {
126
- const body = transformFunctionBodyContent(context, constructor.body);
127
- // if the first expression in the body is a super call, ignore it, because we have
128
- // constructed our own super call.
129
- // if it's not, make sure to include the entire body.
130
- const firstStatement = constructor.body.statements[0];
131
- if (
132
- firstStatement &&
133
- ts.isExpressionStatement(firstStatement) &&
134
- ts.isCallExpression(firstStatement.expression) &&
135
- firstStatement.expression.expression.kind ===
136
- ts.SyntaxKind.SuperKeyword
137
- ) {
138
- bodyStatements.push(...body.slice(1));
139
- } else {
140
- bodyStatements.push(...body);
141
- }
142
- }
143
- context.popScope();
144
- return tstl.createAssignmentStatement(
145
- tstl.createTableIndexExpression(
146
- className,
147
- tstl.createStringLiteral(methodName)
148
- ),
149
- tstl.createFunctionExpression(tstl.createBlock(bodyStatements), params)
150
- );
151
- }
152
-
153
- function transformMethodDeclaration(
154
- context: TransformationContext,
155
- node: ts.MethodDeclaration,
156
- className: tstl.Identifier
157
- ): tstl.Statement | undefined {
158
- const [functionExpression] = transformFunctionToExpression(context, node);
159
- return tstl.createAssignmentStatement(
160
- tstl.createTableIndexExpression(
161
- className,
162
- transformPropertyName(context, node.name)
163
- ),
164
- functionExpression
165
- );
166
- }
167
- interface ClassSuperInfo {
168
- className: lua.Identifier;
169
- extendedTypeNode?: ts.ExpressionWithTypeArguments;
170
- }
171
-
172
- export const transformClassDeclaration: FunctionVisitor<
173
- ts.ClassLikeDeclaration
174
- > = (
175
- declaration,
176
- context: TransformationContext & { classSuperInfos?: [ClassSuperInfo] }
177
- ) => {
178
- imports.add('object');
179
-
180
- let className: tstl.Identifier;
181
- if (declaration.name) {
182
- className = tstl.createIdentifier(declaration.name.text);
183
- } else {
184
- className = tstl.createIdentifier(
185
- context.createTempName('class'),
186
- declaration
187
- );
188
- }
189
-
190
- const extension = getExtendedNode(declaration);
191
- if (context.classSuperInfos) {
192
- context.classSuperInfos.push({
193
- className,
194
- extendedTypeNode: extension,
195
- });
196
- } else {
197
- context.classSuperInfos = [{ className, extendedTypeNode: extension }];
198
- }
199
-
200
- // Get all properties with value
201
- const properties = declaration.members
202
- .filter(ts.isPropertyDeclaration)
203
- .filter((member) => member.initializer);
204
-
205
- // Divide properties into static and non-static
206
- const instanceFields = properties.filter((prop) => !isStaticNode(prop));
207
-
208
- const statements: tstl.Statement[] = [];
209
-
210
- // class('X')
211
- statements.push(createClassCall(context, className, extension));
212
-
213
- // function X:init()
214
- // X.super.init(self)
215
- // end
216
- const constructor = declaration.members.find(
217
- (n): n is ts.ConstructorDeclaration =>
218
- ts.isConstructorDeclaration(n) && n.body !== undefined
219
- );
220
- const transformedConstructor = transformConstructor(
221
- context,
222
- className,
223
- instanceFields,
224
- constructor
225
- );
226
- if (transformedConstructor) {
227
- statements.push(transformedConstructor);
228
- }
229
-
230
- const methods = declaration.members
231
- .filter(ts.isMethodDeclaration)
232
- .map((method) => transformMethodDeclaration(context, method, className))
233
- .filter((method): method is tstl.Statement => method !== undefined);
234
- statements.push(...methods);
235
-
236
- // export the class if needed
237
- // todo: check if there is a cleaner way to do this
238
- if (
239
- 'localSymbol' in declaration &&
240
- typeof declaration.localSymbol === 'object' &&
241
- 'exportSymbol' in declaration.localSymbol &&
242
- typeof declaration.localSymbol.exportSymbol === 'object' &&
243
- 'escapedName' in declaration.localSymbol.exportSymbol &&
244
- typeof declaration.localSymbol.exportSymbol.escapedName === 'string'
245
- ) {
246
- const escapedName = declaration.localSymbol.exportSymbol.escapedName;
247
-
248
- statements.push(
249
- tstl.createAssignmentStatement(
250
- tstl.createTableIndexExpression(
251
- tstl.createIdentifier('____exports'),
252
- tstl.createStringLiteral(escapedName)
253
- ),
254
- className
255
- )
256
- );
257
- }
258
-
259
- return statements;
260
- };
261
-
262
- const transformNewExpression: FunctionVisitor<ts.NewExpression> = (
263
- node,
264
- context
265
- ) => {
266
- const signature = context.checker.getResolvedSignature(node);
267
- const [name, params] = transformCallAndArguments(
268
- context,
269
- node.expression,
270
- node.arguments ?? [ts.factory.createTrue()],
271
- signature
272
- );
273
- return tstl.createCallExpression(name, params);
274
- };
275
-
276
- export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (
277
- expression,
278
- context: TransformationContext & { classSuperInfos?: ClassSuperInfo[] }
279
- ) => {
280
- const superInfos = context.classSuperInfos;
281
- let superInfo: ClassSuperInfo | undefined = undefined;
282
- if (superInfos) {
283
- superInfo = superInfos[superInfos.length - 1];
284
- }
285
- if (!superInfo) return lua.createAnonymousIdentifier(expression);
286
- const { className } = superInfo;
287
-
288
- // Using `super` without extended type node is a TypeScript error
289
- // const extendsExpression = extendedTypeNode?.expression;
290
- // let baseClassName: lua.AssignmentLeftHandSideExpression | undefined;
291
-
292
- // if (extendsExpression && ts.isIdentifier(extendsExpression)) {
293
- // const symbol = context.checker.getSymbolAtLocation(extendsExpression);
294
- // if (symbol && !isSymbolExported(context, symbol)) {
295
- // // Use "baseClassName" if base is a simple identifier
296
- // baseClassName = transformIdentifier(context, extendsExpression);
297
- // }
298
- // }
299
-
300
- // if (!baseClassName) {
301
- // // Use "className.____super" if the base is not a simple identifier
302
- // baseClassName = lua.createTableIndexExpression(
303
- // className,
304
- // lua.createStringLiteral('____super'),
305
- // expression
306
- // );
307
- // }
308
-
309
- return lua.createTableIndexExpression(
310
- className,
311
- lua.createStringLiteral('super')
312
- );
313
- };
314
-
315
- const processName = (name: string) => {
316
- for (const [key, value] of Object.entries(importMap)) {
317
- if (value instanceof Set && value.has(name)) {
318
- imports.add(key);
319
- }
320
- }
321
- };
322
-
323
- const plugin = {
324
- beforeEmit: (_, __, ___, result) => {
325
- const importsString = Array.from(imports)
326
- .map((importString) => `import "CoreLibs/${importString}"`)
327
- .join('\n');
328
-
329
- if (importsString.trim() === '') {
330
- return;
331
- }
332
-
333
- result[0].code = `-- These imports were added automatically\n\n${importsString}\n\n-- End of automatic imports\n\n${result[0].code}`;
334
- },
335
- visitors: {
336
- [ts.SyntaxKind.ClassDeclaration]: transformClassDeclaration,
337
- [ts.SyntaxKind.SuperKeyword]: transformSuperExpression,
338
- [ts.SyntaxKind.NewExpression]: transformNewExpression,
339
- [ts.SyntaxKind.PropertyAccessExpression]: (node, context) => {
340
- if (ts.isIdentifier(node.expression)) {
341
- processName(node.name.text);
342
- }
343
-
344
- return context.superTransformExpression(node);
345
- },
346
- [ts.SyntaxKind.CallExpression]: (node, context) => {
347
- if (ts.isIdentifier(node.expression)) {
348
- processName(node.expression.escapedText.toString());
349
- }
350
-
351
- if (
352
- ts.isIdentifier(node.expression) &&
353
- node.expression.escapedText === 'require'
354
- ) {
355
- const normalNode = context.superTransformExpression(
356
- node
357
- ) as unknown as {
358
- expression: { text: string; originalName: string };
359
- };
360
-
361
- normalNode.expression.text = 'import';
362
- normalNode.expression.originalName = 'import';
363
-
364
- return normalNode as unknown as lua.Expression;
365
- } else {
366
- return context.superTransformExpression(node);
367
- }
368
- },
369
- },
370
- } satisfies tstl.Plugin;
371
-
372
- export default plugin;
@@ -1,10 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES5",
4
- "module": "Node16",
5
- "moduleResolution": "Node16"
6
- },
7
- "files": [
8
- "plugin.ts"
9
- ]
10
- }