json-as 0.5.12 → 0.5.14
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 +39 -10
- package/transform/package.json +1 -1
- package/transform/src/index.ts +26 -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 = [];
|
|
@@ -20,6 +20,8 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
20
20
|
}
|
|
21
21
|
visitMethodDeclaration() { }
|
|
22
22
|
visitFieldDeclaration(node) {
|
|
23
|
+
if (toString(node).startsWith("static"))
|
|
24
|
+
return;
|
|
23
25
|
const lineText = toString(node);
|
|
24
26
|
if (lineText.startsWith("private"))
|
|
25
27
|
return;
|
|
@@ -46,10 +48,23 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
46
48
|
//);
|
|
47
49
|
}
|
|
48
50
|
visitClassDeclaration(node) {
|
|
49
|
-
var _a, _b, _c;
|
|
51
|
+
var _a, _b, _c, _d;
|
|
52
|
+
let foundDecorator = false;
|
|
53
|
+
for (const decorator of node.decorators) {
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
if (decorator.name.text.toLowerCase() == "json" || decorator.name.text.toLowerCase() == "serializable")
|
|
56
|
+
foundDecorator = true;
|
|
57
|
+
}
|
|
58
|
+
if (!foundDecorator)
|
|
59
|
+
return;
|
|
50
60
|
if (!node.members) {
|
|
51
61
|
return;
|
|
52
62
|
}
|
|
63
|
+
// Prevent from being triggered twice
|
|
64
|
+
for (const member of node.members) {
|
|
65
|
+
if (member.name.text == "__JSON_Serialize")
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
53
68
|
this.currentClass = {
|
|
54
69
|
name: toString(node.name),
|
|
55
70
|
keys: [],
|
|
@@ -66,9 +81,9 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
66
81
|
return v;
|
|
67
82
|
}
|
|
68
83
|
});
|
|
69
|
-
if (parentSchema.length > 0) {
|
|
70
|
-
(
|
|
71
|
-
this.currentClass.encodeStmts.push(...(
|
|
84
|
+
if (parentSchema.length > 0 && ((_a = parentSchema[0]) === null || _a === void 0 ? void 0 : _a.encodeStmts)) {
|
|
85
|
+
(_b = parentSchema[0]) === null || _b === void 0 ? void 0 : _b.encodeStmts.push(((_c = parentSchema[0]) === null || _c === void 0 ? void 0 : _c.encodeStmts.pop()) + ",");
|
|
86
|
+
this.currentClass.encodeStmts.push(...(_d = parentSchema[0]) === null || _d === void 0 ? void 0 : _d.encodeStmts);
|
|
72
87
|
}
|
|
73
88
|
else {
|
|
74
89
|
//console.log("Class extends " + this.currentClass.parent + ", but parent class not found. Maybe add the @json decorator over parent class?")
|
|
@@ -107,8 +122,22 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
107
122
|
node.members.push(setDataMethod);
|
|
108
123
|
this.schemasList.push(this.currentClass);
|
|
109
124
|
}
|
|
110
|
-
|
|
111
|
-
|
|
125
|
+
visitSource(node) {
|
|
126
|
+
super.visitSource(node);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export default class Transformer extends Transform {
|
|
130
|
+
// Trigger the transform after parse.
|
|
131
|
+
afterParse(parser) {
|
|
132
|
+
// Create new transform
|
|
133
|
+
const transformer = new AsJSONTransform();
|
|
134
|
+
// Loop over every source
|
|
135
|
+
for (const source of parser.sources) {
|
|
136
|
+
// Ignore all lib (std lib). Visit everything else.
|
|
137
|
+
if (!source.isLibrary && !source.internalPath.startsWith(`~lib/`)) {
|
|
138
|
+
transformer.visit(source);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
112
141
|
}
|
|
113
142
|
}
|
|
114
|
-
|
|
143
|
+
;
|
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,12 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
62
63
|
//);
|
|
63
64
|
}
|
|
64
65
|
visitClassDeclaration(node: ClassDeclaration): void {
|
|
66
|
+
let foundDecorator = false;
|
|
67
|
+
for (const decorator of node.decorators!) {
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
if (decorator.name.text.toLowerCase() == "json" || decorator.name.text.toLowerCase() == "serializable") foundDecorator = true;
|
|
70
|
+
}
|
|
71
|
+
if (!foundDecorator) return;
|
|
65
72
|
if (!node.members) {
|
|
66
73
|
return;
|
|
67
74
|
}
|
|
@@ -143,9 +150,22 @@ class AsJSONTransform extends ClassDecorator {
|
|
|
143
150
|
|
|
144
151
|
this.schemasList.push(this.currentClass);
|
|
145
152
|
}
|
|
146
|
-
|
|
147
|
-
|
|
153
|
+
visitSource(node: Source): void {
|
|
154
|
+
super.visitSource(node);
|
|
148
155
|
}
|
|
149
156
|
}
|
|
150
157
|
|
|
151
|
-
export default
|
|
158
|
+
export default class Transformer extends Transform {
|
|
159
|
+
// Trigger the transform after parse.
|
|
160
|
+
afterParse(parser: Parser): void {
|
|
161
|
+
// Create new transform
|
|
162
|
+
const transformer = new AsJSONTransform();
|
|
163
|
+
// Loop over every source
|
|
164
|
+
for (const source of parser.sources) {
|
|
165
|
+
// Ignore all lib (std lib). Visit everything else.
|
|
166
|
+
if (!source.isLibrary && !source.internalPath.startsWith(`~lib/`)) {
|
|
167
|
+
transformer.visit(source);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|