json-as 0.5.13 → 0.5.15
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/README.md +3 -4
- package/assembly/test.ts +2 -2
- package/package.json +1 -1
- package/transform/lib/index.js +34 -10
- package/transform/package.json +1 -1
- package/transform/src/index.ts +27 -6
package/README.md
CHANGED
|
@@ -37,16 +37,15 @@ Or, add it to `asconfig.json`
|
|
|
37
37
|
```js
|
|
38
38
|
import { JSON } from "json-as/assembly";
|
|
39
39
|
|
|
40
|
-
// @
|
|
41
|
-
@
|
|
40
|
+
// @json or @serializable work here
|
|
41
|
+
@JSON
|
|
42
42
|
class Vec3 {
|
|
43
43
|
x!: f32;
|
|
44
44
|
y!: f32;
|
|
45
45
|
z!: f32;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
@json
|
|
48
|
+
@JSON
|
|
50
49
|
class Player {
|
|
51
50
|
firstName!: string;
|
|
52
51
|
lastName!: string;
|
package/assembly/test.ts
CHANGED
package/package.json
CHANGED
package/transform/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ClassDecorator, registerDecorator, } from "visitor-as/dist/decorator.js";
|
|
2
1
|
import { getName, toString } from "visitor-as/dist/utils.js";
|
|
3
|
-
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
2
|
+
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
3
|
+
import { Transform } from "assemblyscript/dist/transform.js";
|
|
4
4
|
class SchemaData {
|
|
5
5
|
constructor() {
|
|
6
6
|
this.keys = [];
|
|
@@ -12,7 +12,7 @@ class SchemaData {
|
|
|
12
12
|
this.setDataStmts = [];
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
class AsJSONTransform extends
|
|
15
|
+
class AsJSONTransform extends BaseVisitor {
|
|
16
16
|
constructor() {
|
|
17
17
|
super(...arguments);
|
|
18
18
|
this.schemasList = [];
|
|
@@ -48,7 +48,17 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
48
48
|
//);
|
|
49
49
|
}
|
|
50
50
|
visitClassDeclaration(node) {
|
|
51
|
-
var _a, _b, _c, _d;
|
|
51
|
+
var _a, _b, _c, _d, _e;
|
|
52
|
+
if (!((_a = node.decorators) === null || _a === void 0 ? void 0 : _a.length))
|
|
53
|
+
return;
|
|
54
|
+
let foundDecorator = false;
|
|
55
|
+
for (const decorator of node.decorators) {
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
if (decorator.name.text.toLowerCase() == "json" || decorator.name.text.toLowerCase() == "serializable")
|
|
58
|
+
foundDecorator = true;
|
|
59
|
+
}
|
|
60
|
+
if (!foundDecorator)
|
|
61
|
+
return;
|
|
52
62
|
if (!node.members) {
|
|
53
63
|
return;
|
|
54
64
|
}
|
|
@@ -73,9 +83,9 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
73
83
|
return v;
|
|
74
84
|
}
|
|
75
85
|
});
|
|
76
|
-
if (parentSchema.length > 0 && ((
|
|
77
|
-
(
|
|
78
|
-
this.currentClass.encodeStmts.push(...(
|
|
86
|
+
if (parentSchema.length > 0 && ((_b = parentSchema[0]) === null || _b === void 0 ? void 0 : _b.encodeStmts)) {
|
|
87
|
+
(_c = parentSchema[0]) === null || _c === void 0 ? void 0 : _c.encodeStmts.push(((_d = parentSchema[0]) === null || _d === void 0 ? void 0 : _d.encodeStmts.pop()) + ",");
|
|
88
|
+
this.currentClass.encodeStmts.push(...(_e = parentSchema[0]) === null || _e === void 0 ? void 0 : _e.encodeStmts);
|
|
79
89
|
}
|
|
80
90
|
else {
|
|
81
91
|
//console.log("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?")
|
|
@@ -114,8 +124,22 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
114
124
|
node.members.push(setDataMethod);
|
|
115
125
|
this.schemasList.push(this.currentClass);
|
|
116
126
|
}
|
|
117
|
-
|
|
118
|
-
|
|
127
|
+
visitSource(node) {
|
|
128
|
+
super.visitSource(node);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export default class Transformer extends Transform {
|
|
132
|
+
// Trigger the transform after parse.
|
|
133
|
+
afterParse(parser) {
|
|
134
|
+
// Create new transform
|
|
135
|
+
const transformer = new AsJSONTransform();
|
|
136
|
+
// Loop over every source
|
|
137
|
+
for (const source of parser.sources) {
|
|
138
|
+
// Ignore all lib (std lib). Visit everything else.
|
|
139
|
+
if (!source.isLibrary && !source.internalPath.startsWith(`~lib/`)) {
|
|
140
|
+
transformer.visit(source);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
119
143
|
}
|
|
120
144
|
}
|
|
121
|
-
|
|
145
|
+
;
|
package/transform/package.json
CHANGED
package/transform/src/index.ts
CHANGED
|
@@ -2,14 +2,15 @@ import {
|
|
|
2
2
|
ClassDeclaration,
|
|
3
3
|
FieldDeclaration,
|
|
4
4
|
Source,
|
|
5
|
+
Parser
|
|
5
6
|
} from "assemblyscript/dist/assemblyscript";
|
|
6
7
|
import {
|
|
7
8
|
ClassDecorator,
|
|
8
9
|
registerDecorator,
|
|
9
10
|
} from "visitor-as/dist/decorator.js";
|
|
10
11
|
import { getName, toString } from "visitor-as/dist/utils.js";
|
|
11
|
-
import { SimpleParser } from "visitor-as/dist/index.js";
|
|
12
|
-
import {
|
|
12
|
+
import { BaseVisitor, SimpleParser } from "visitor-as/dist/index.js";
|
|
13
|
+
import { Transform } from "assemblyscript/dist/transform.js";
|
|
13
14
|
|
|
14
15
|
class SchemaData {
|
|
15
16
|
public keys: string[] = [];
|
|
@@ -21,7 +22,7 @@ class SchemaData {
|
|
|
21
22
|
public encodeStmts: string[] = [];
|
|
22
23
|
public setDataStmts: string[] = [];
|
|
23
24
|
}
|
|
24
|
-
class AsJSONTransform extends
|
|
25
|
+
class AsJSONTransform extends BaseVisitor {
|
|
25
26
|
public schemasList: SchemaData[] = [];
|
|
26
27
|
public currentClass!: SchemaData;
|
|
27
28
|
public sources: Source[] = [];
|
|
@@ -62,6 +63,13 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
62
63
|
//);
|
|
63
64
|
}
|
|
64
65
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
66
|
+
if (!node.decorators?.length) return;
|
|
67
|
+
let foundDecorator = false;
|
|
68
|
+
for (const decorator of node.decorators!) {
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
if (decorator.name.text.toLowerCase() == "json" || decorator.name.text.toLowerCase() == "serializable") foundDecorator = true;
|
|
71
|
+
}
|
|
72
|
+
if (!foundDecorator) return;
|
|
65
73
|
if (!node.members) {
|
|
66
74
|
return;
|
|
67
75
|
}
|
|
@@ -143,9 +151,22 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
143
151
|
|
|
144
152
|
this.schemasList.push(this.currentClass);
|
|
145
153
|
}
|
|
146
|
-
|
|
147
|
-
|
|
154
|
+
visitSource(node: Source): void {
|
|
155
|
+
super.visitSource(node);
|
|
148
156
|
}
|
|
149
157
|
}
|
|
150
158
|
|
|
151
|
-
export default
|
|
159
|
+
export default class Transformer extends Transform {
|
|
160
|
+
// Trigger the transform after parse.
|
|
161
|
+
afterParse(parser: Parser): void {
|
|
162
|
+
// Create new transform
|
|
163
|
+
const transformer = new AsJSONTransform();
|
|
164
|
+
// Loop over every source
|
|
165
|
+
for (const source of parser.sources) {
|
|
166
|
+
// Ignore all lib (std lib). Visit everything else.
|
|
167
|
+
if (!source.isLibrary && !source.internalPath.startsWith(`~lib/`)) {
|
|
168
|
+
transformer.visit(source);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|