json-as 0.5.0 → 0.5.2

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,430 +0,0 @@
1
- /*import {
2
- IdentifierExpression,
3
- Parser,
4
- ClassDeclaration,
5
- CommonFlags,
6
- TypeName,
7
- NamedTypeNode,
8
- VariableLikeDeclarationStatement,
9
- DiagnosticCode,
10
- ObjectLiteralExpression,
11
- Expression,
12
- Range,
13
- ParameterKind,
14
- Statement,
15
- } from "assemblyscript";
16
- import { Transform } from "assemblyscript/transform";
17
-
18
- export default class MyTransform extends Transform {
19
- // @ts-ignore
20
- readFile(filename: string, baseDir: string) {
21
- return null;
22
- }
23
- afterParse(p: Parser) {
24
- p.sources.forEach((s) => {
25
- if (s.isLibrary) return;
26
- s.statements.forEach((s) => {
27
- if (s instanceof ClassDeclaration) {
28
- let __DECOR;
29
- if (
30
- __DECOR = s.decorators?.find(
31
- (d) =>
32
- d.name instanceof IdentifierExpression && d.name.text == "json"
33
- )
34
- ) {
35
- const __DEFAULT_RANGE = __DECOR.range;
36
- const members = s.members.filter<VariableLikeDeclarationStatement>(
37
- (e): e is VariableLikeDeclarationStatement =>
38
- e instanceof VariableLikeDeclarationStatement
39
- );
40
- members.forEach((e) => {
41
- if (!e.type) {
42
- p.error(
43
- DiagnosticCode.User_defined_0,
44
- e.range,
45
- "Type information must be specified for JSON serialization.", ""
46
- );
47
- }
48
- });
49
- const SimpleNames = ["u8", "u16", "string", "f32"];
50
- const Param = Expression.createIdentifierExpression(
51
- "param",
52
- __DEFAULT_RANGE
53
- );
54
- const GenericMapType = Expression.createNamedType(
55
- Expression.createSimpleTypeName("string", __DEFAULT_RANGE),
56
- [
57
- Expression.createNamedType(
58
- new TypeName(
59
- Expression.createIdentifierExpression(
60
- "JSON",
61
- __DEFAULT_RANGE
62
- ),
63
- Expression.createSimpleTypeName(
64
- "_Variant",
65
- __DEFAULT_RANGE
66
- ),
67
- __DEFAULT_RANGE
68
- ),
69
- null,
70
- false,
71
- __DEFAULT_RANGE
72
- ),
73
- ],
74
- false,
75
- __DEFAULT_RANGE
76
- );
77
- let kvr: [IdentifierExpression, Expression][] = [];
78
-
79
- members.forEach((m) => {
80
- if (
81
- m.type instanceof NamedTypeNode &&
82
- m.type.name.next == null &&
83
- SimpleNames.includes(m.type.name.identifier.text)
84
- ) {
85
- kvr.push([
86
- Expression.createIdentifierExpression(m.name.text, m.range),
87
- Expression.createCallExpression(
88
- Expression.createElementAccessExpression(
89
- Param,
90
- Expression.createIdentifierExpression(
91
- "get",
92
- __DEFAULT_RANGE
93
- ),
94
- __DEFAULT_RANGE
95
- ),
96
- [m.type!],
97
- [],
98
- __DEFAULT_RANGE
99
- ),
100
- ]);
101
- } else {
102
- kvr.push([
103
- Expression.createIdentifierExpression(m.name.text, m.range),
104
- Expression.createCallExpression(
105
- Expression.createPropertyAccessExpression(
106
- m.type!,
107
- Expression.createIdentifierExpression(
108
- "__Json__Deserialize",
109
- __DEFAULT_RANGE
110
- ),
111
- __DEFAULT_RANGE
112
- ),
113
- [],
114
- [
115
- Expression.createCallExpression(
116
- Expression.createElementAccessExpression(
117
- Param,
118
- Expression.createIdentifierExpression(
119
- "get",
120
- __DEFAULT_RANGE
121
- ),
122
- __DEFAULT_RANGE
123
- ),
124
- [GenericMapType],
125
- [],
126
- __DEFAULT_RANGE
127
- ),
128
- ],
129
- __DEFAULT_RANGE
130
- ),
131
- ]);
132
- }
133
- });
134
- const objectliteral = new ObjectLiteralExpression(
135
- kvr.map((e) => e[0]),
136
- kvr.map((e) => e[1]),
137
- __DEFAULT_RANGE
138
- );
139
- console.log(kvr[0]?.map(e => !!e))
140
- console.log(kvr[1]?.map(e => !!e))
141
- s.members.push(
142
- Expression.createMethodDeclaration(
143
- Expression.createIdentifierExpression(
144
- "__Json__Deserialize",
145
- __DEFAULT_RANGE
146
- ),
147
- null,
148
- CommonFlags.STATIC,
149
- null,
150
- Expression.createFunctionType(
151
- [
152
- Expression.createParameter(
153
- ParameterKind.DEFAULT,
154
- Expression.createIdentifierExpression(
155
- "param",
156
- __DEFAULT_RANGE
157
- ),
158
- GenericMapType,
159
- null,
160
- __DEFAULT_RANGE
161
- ),
162
- ],
163
- Expression.createNamedType(
164
- (console.log(s.name.text), Expression.createSimpleTypeName(s.name.text + '1', __DEFAULT_RANGE)),
165
- null,
166
- false,
167
- __DEFAULT_RANGE
168
- ),
169
- null,
170
- false,
171
- __DEFAULT_RANGE
172
- ),
173
- Statement.createBlockStatement(
174
- [
175
- Statement.createReturnStatement(
176
- objectliteral,
177
- __DEFAULT_RANGE
178
- ),
179
- ],
180
- __DEFAULT_RANGE
181
- ),
182
- __DEFAULT_RANGE
183
- )
184
- );
185
- }
186
- }
187
- });
188
- });
189
- }
190
- }
191
- */
192
- import {
193
- ClassPrototype,
194
- IdentifierExpression,
195
- ImportStatement,
196
- Parser,
197
- Program,
198
- FieldPrototype,
199
- ClassDeclaration,
200
- MethodDeclaration,
201
- CommonFlags,
202
- FunctionTypeNode,
203
- TypeName,
204
- BlockStatement,
205
- FunctionPrototype,
206
- NamedTypeNode,
207
- ReturnStatement,
208
- StringLiteralExpression,
209
- VariableDeclaration,
210
- VariableStatement,
211
- BinaryExpression,
212
- ExpressionStatement,
213
- Token,
214
- PropertyAccessExpression,
215
- CallExpression,
216
- ThisExpression,
217
- } from "assemblyscript/assemblyscript";
218
- import { Transform } from "assemblyscript/transform";
219
- import {
220
- NodeKind,
221
- TypeNode,
222
- TypeParameterNode,
223
- } from "types:assemblyscript/src/ast";
224
-
225
- export default class MyTransform extends Transform {
226
- readFile(filename: string, baseDir: string) {
227
- return null;
228
- }
229
- afterParse(p: Parser) {
230
- p.sources.forEach((s) => {
231
- if (s.isLibrary) return;
232
- s.statements.forEach((s) => {
233
- if (s instanceof ImportStatement) {
234
- // console.log(s.internalPath);
235
- // console.log(s.path.value.charAt(s.path.value.length - 1));
236
- if (s.path.value.charAt(s.path.value.length - 1) == "~") {
237
- s.path.value = s.path.value.slice(0, s.path.value.length - 1);
238
- s.internalPath = s.internalPath.slice(0, s.internalPath.length - 1);
239
- }
240
- }
241
- });
242
- });
243
- }
244
- afterInitialize(program: Program): void {
245
- // initializer(program);
246
- // console.log(program.managedClasses);
247
- //program.filesByName.forEach((e) => {
248
- // console.log(e.name);
249
- //});
250
- program.elementsByName.forEach((c) => {
251
- //program.elementsByName.forEach((e) => {});
252
- if (c instanceof ClassPrototype) {
253
- if (
254
- !!c.decoratorNodes?.find((e) => {
255
- /** detect decorator */
256
- return (
257
- e.name instanceof IdentifierExpression && e.name.text == "json"
258
- );
259
- })
260
- ) {
261
- // console.log({ ...c, program: {}, parent: {} });
262
-
263
- c.members?.forEach((m) => {
264
- //console.log(ElementKind[m.kind]);
265
- if (m instanceof FieldPrototype) {
266
- console.log(m.name);
267
- }
268
- });
269
- let fnBody = new BlockStatement(
270
- [
271
- new VariableStatement(
272
- null,
273
- [
274
- new VariableDeclaration(
275
- new IdentifierExpression(
276
- "_str",
277
- false,
278
- c.declaration.range
279
- ),
280
- null,
281
- CommonFlags.LET,
282
- null,
283
- new StringLiteralExpression("{", c.declaration.range),
284
- c.declaration.range
285
- ),
286
- ],
287
- c.declaration.range
288
- ),
289
- ],
290
- c.declaration.range
291
- );
292
- const escapeChars = (e: string) => {
293
- let outChars = "";
294
- let cchhar = "";
295
- for (let i = 0; i < e.length; i++) {
296
- cchhar = e.charAt(i);
297
-
298
- if (cchhar == '"') {
299
- outChars += `\\"` + cchhar;
300
- } else {
301
- outChars += cchhar;
302
- }
303
- }
304
- return outChars;
305
- };
306
- let i = 0;
307
- const size = c.instanceMembers!.size;
308
- let d;
309
- for (const field of c.instanceMembers!.values()) {
310
- // Leave out trailing commas
311
- if (i < size - 1) {
312
- d = new StringLiteralExpression(",", c.declaration.range);
313
- } else {
314
- d = new StringLiteralExpression("", c.declaration.range);
315
- }
316
- if (field instanceof FieldPrototype) {
317
- fnBody.statements.push(
318
- new ExpressionStatement(
319
- new BinaryExpression(
320
- Token.PLUS_EQUALS,
321
- new IdentifierExpression(
322
- "_str",
323
- false,
324
- c.declaration.range
325
- ),
326
- new BinaryExpression(
327
- Token.PLUS,
328
- new StringLiteralExpression(
329
- '"' + escapeChars(field.name) + '":',
330
- c.declaration.range
331
- ),
332
- new BinaryExpression(
333
- Token.PLUS,
334
- new CallExpression(
335
- new PropertyAccessExpression(
336
- new IdentifierExpression(
337
- "JSON",
338
- false,
339
- c.declaration.range
340
- ),
341
- new IdentifierExpression(
342
- "stringify",
343
- false,
344
- c.declaration.range
345
- ),
346
- c.declaration.range
347
- ),
348
- null,
349
- [
350
- new PropertyAccessExpression(
351
- new ThisExpression(c.declaration.range),
352
- new IdentifierExpression(
353
- field.name,
354
- false,
355
- c.declaration.range
356
- ),
357
- c.declaration.range
358
- ),
359
- ],
360
- c.declaration.range
361
- ),
362
- d,
363
- c.declaration.range
364
- ),
365
- c.declaration.range
366
- ),
367
- c.declaration.range
368
- )
369
- )
370
- );
371
- }
372
- i++;
373
- }
374
- fnBody.statements.push(
375
- new ReturnStatement(
376
- new BinaryExpression(
377
- Token.PLUS,
378
- new IdentifierExpression("_str", false, c.declaration.range),
379
- new StringLiteralExpression("}", c.declaration.range),
380
- c.declaration.range
381
- ),
382
- c.declaration.range
383
- )
384
- );
385
- let __JSON_Serialize = new FunctionPrototype(
386
- "__JSON_Serialize",
387
- c,
388
- new MethodDeclaration(
389
- new IdentifierExpression(
390
- "__JSON_Serialize",
391
- false,
392
- c.declaration.range
393
- ),
394
- null,
395
- CommonFlags.INSTANCE,
396
- null,
397
- new FunctionTypeNode(
398
- [],
399
- new NamedTypeNode(
400
- new TypeName(
401
- new IdentifierExpression(
402
- "string",
403
- false,
404
- c.declaration.range
405
- ),
406
- null,
407
- c.declaration.range
408
- ),
409
- null,
410
- false,
411
- c.declaration.range
412
- ),
413
- null,
414
- false,
415
- c.declaration.range
416
- ),
417
- fnBody,
418
- c.declaration.range
419
- )
420
- );
421
- c.instanceMembers?.set("__JSON_Serialize", __JSON_Serialize);
422
- (c.declaration as ClassDeclaration).members.push(
423
- __JSON_Serialize.declaration
424
- );
425
- }
426
- }
427
- });
428
- }
429
- }
430
-