json-as 1.1.10 → 1.1.11

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.
@@ -72,6 +72,16 @@ class JSONTransform extends Visitor {
72
72
 
73
73
  private visitedClasses: Set<string> = new Set<string>();
74
74
 
75
+ visitClassDeclarationRef(node: ClassDeclaration): void {
76
+ if (
77
+ !node.decorators?.length ||
78
+ !node.decorators.some((decorator) => {
79
+ const name = (<IdentifierExpression>decorator.name).text;
80
+ return name === "json" || name === "serializable";
81
+ })
82
+ ) throw new Error("Class " + node.name.text + " is missing an @json or @serializable decorator in " + node.range.source.internalPath);
83
+ this.visitClassDeclaration(node);
84
+ }
75
85
  visitClassDeclaration(node: ClassDeclaration): void {
76
86
  if (!node.decorators?.length) return;
77
87
 
@@ -100,26 +110,36 @@ class JSONTransform extends Visitor {
100
110
  const depSearch = schema.deps.find((v) => v.name == extendsName);
101
111
  if (depSearch) {
102
112
  if (DEBUG > 0) console.log("Found " + extendsName + " in dependencies of " + node.range.source.internalPath);
103
- schema.deps.push(depSearch);
104
- schema.parent = depSearch
113
+ if (!schema.deps.some(v => v.name == depSearch.name)) schema.deps.push(depSearch);
114
+ schema.parent = depSearch;
105
115
  } else {
106
116
  const internalSearch = getClass(extendsName, node.range.source);
107
117
  if (internalSearch) {
108
118
  if (DEBUG > 0) console.log("Found " + extendsName + " internally from " + node.range.source.internalPath);
109
- this.visitClassDeclaration(internalSearch);
110
- schema.deps.push(this.schema);
111
- this.schemas.get(node.range.source.internalPath).push(this.schema);
112
- schema.parent = this.schema;
113
- this.schema = schema;
119
+ if (!this.visitedClasses.has(internalSearch.range.source.internalPath + internalSearch.name.text)) {
120
+ this.visitClassDeclarationRef(internalSearch);
121
+ this.schemas.get(internalSearch.range.source.internalPath).push(this.schema);
122
+ this.visitClassDeclaration(node);
123
+ return;
124
+ }
125
+ const schem = this.schemas.get(internalSearch.range.source.internalPath)?.find(s => s.name == internalSearch.name.text);
126
+ if (!schem) throw new Error("Could not find schema for " + internalSearch.name.text + " in " + internalSearch.range.source.internalPath);
127
+ schema.deps.push(schem);
128
+ schema.parent = schem;
114
129
  } else {
115
130
  const externalSearch = getImportedClass(extendsName, node.range.source, this.parser);
116
131
  if (externalSearch) {
117
132
  if (DEBUG > 0) console.log("Found " + externalSearch.name.text + " externally from " + node.range.source.internalPath);
118
- this.visitClassDeclaration(externalSearch);
119
- schema.deps.push(this.schema);
120
- this.schemas.get(node.range.source.internalPath).push(this.schema);
121
- schema.parent = this.schema;
122
- this.schema = schema;
133
+ if (!this.visitedClasses.has(externalSearch.range.source.internalPath + externalSearch.name.text)) {
134
+ this.visitClassDeclarationRef(externalSearch);
135
+ this.schemas.get(externalSearch.range.source.internalPath).push(this.schema);
136
+ this.visitClassDeclaration(node);
137
+ return;
138
+ }
139
+ const schem = this.schemas.get(externalSearch.range.source.internalPath)?.find(s => s.name == externalSearch.name.text);
140
+ if (!schem) throw new Error("Could not find schema for " + externalSearch.name.text + " in " + externalSearch.range.source.internalPath);
141
+ schema.deps.push(schem);
142
+ schema.parent = schem;
123
143
  }
124
144
  }
125
145
  }
@@ -162,24 +182,34 @@ class JSONTransform extends Visitor {
162
182
  const depSearch = schema.deps.find((v) => v.name == unknownType);
163
183
  if (depSearch) {
164
184
  if (DEBUG > 0) console.log("Found " + unknownType + " in dependencies of " + node.range.source.internalPath);
165
- schema.deps.push(depSearch);
166
- continue;
167
- }
168
- const internalSearch = getClass(unknownType, node.range.source);
169
- if (internalSearch) {
170
- if (DEBUG > 0) console.log("Found " + unknownType + " internally from " + node.range.source.internalPath);
171
- this.visitClassDeclaration(internalSearch);
172
- this.schemas.get(node.range.source.internalPath).push(this.schema);
173
- schema.deps.push(this.schema);
174
- this.schema = schema;
185
+ if (!schema.deps.some(v => v.name == depSearch.name)) schema.deps.push(depSearch);
175
186
  } else {
176
- const externalSearch = getImportedClass(unknownType, node.range.source, this.parser);
177
- if (externalSearch) {
178
- if (DEBUG > 0) console.log("Found " + externalSearch.name.text + " externally from " + node.range.source.internalPath);
179
- this.visitClassDeclaration(externalSearch);
180
- this.schemas.get(node.range.source.internalPath).push(this.schema);
181
- schema.deps.push(this.schema);
182
- this.schema = schema;
187
+ const internalSearch = getClass(unknownType, node.range.source);
188
+ if (internalSearch) {
189
+ if (DEBUG > 0) console.log("Found " + unknownType + " internally from " + node.range.source.internalPath);
190
+ if (!this.visitedClasses.has(internalSearch.range.source.internalPath + internalSearch.name.text)) {
191
+ this.visitClassDeclarationRef(internalSearch);
192
+ this.schemas.get(internalSearch.range.source.internalPath).push(this.schema);
193
+ this.visitClassDeclaration(node);
194
+ return;
195
+ }
196
+ const schem = this.schemas.get(internalSearch.range.source.internalPath)?.find(s => s.name == internalSearch.name.text);
197
+ if (!schem) throw new Error("Could not find schema for " + internalSearch.name.text + " in " + internalSearch.range.source.internalPath);
198
+ schema.deps.push(schem);
199
+ } else {
200
+ const externalSearch = getImportedClass(unknownType, node.range.source, this.parser);
201
+ if (externalSearch) {
202
+ if (DEBUG > 0) console.log("Found " + externalSearch.name.text + " externally from " + node.range.source.internalPath);
203
+ if (!this.visitedClasses.has(externalSearch.range.source.internalPath + externalSearch.name.text)) {
204
+ this.visitClassDeclarationRef(externalSearch);
205
+ this.schemas.get(externalSearch.range.source.internalPath).push(this.schema);
206
+ this.visitClassDeclaration(node);
207
+ return;
208
+ }
209
+ const schem = this.schemas.get(externalSearch.range.source.internalPath)?.find(s => s.name == externalSearch.name.text);
210
+ if (!schem) throw new Error("Could not find schema for " + externalSearch.name.text + " in " + externalSearch.range.source.internalPath);
211
+ schema.deps.push(schem);
212
+ }
183
213
  }
184
214
  }
185
215
  }
@@ -599,7 +629,7 @@ class JSONTransform extends Visitor {
599
629
  DESERIALIZE += " while (srcStart < srcEnd) {\n";
600
630
  DESERIALIZE += " const code = load<u16>(srcStart);\n";
601
631
  DESERIALIZE += " if (code == 34 && load<u16>(srcStart - 2) !== 92) {\n";
602
- if (DEBUG > 1)DESERIALIZE += " console.log(\"Value (string, " + (++id) + "): \" + JSON.Util.ptrToStr(lastIndex, srcStart + 2));";
632
+ if (DEBUG > 1) DESERIALIZE += " console.log(\"Value (string, " + (++id) + "): \" + JSON.Util.ptrToStr(lastIndex, srcStart + 2));";
603
633
  generateGroups(sortedMembers.string, (group) => {
604
634
  generateConsts(group);
605
635
  const first = group[0];
@@ -611,7 +641,7 @@ class JSONTransform extends Visitor {
611
641
  DESERIALIZE += indent + " break;\n";
612
642
  DESERIALIZE += indent + " }";
613
643
 
614
- for (let i = 1; i <group.length; i++) {
644
+ for (let i = 1; i < group.length; i++) {
615
645
  const mem = group[i];
616
646
  const memName = mem.alias || mem.name;
617
647
  DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
@@ -813,39 +843,39 @@ class JSONTransform extends Visitor {
813
843
  DESERIALIZE += " srcStart += 8;\n";
814
844
  if (DEBUG > 1) DESERIALIZE += " console.log(\"Value (bool, " + (++id) + "): \" + JSON.Util.ptrToStr(lastIndex, srcStart - 8));";
815
845
  generateGroups(sortedMembers.boolean, (group) => {
816
- generateConsts(group);
846
+ generateConsts(group);
817
847
  const first = group[0];
818
- const fName =first.alias || first.name;
819
- DESERIALIZE += indent + " if (" + getComparision(fName) + ") { // " + fName + "\n";
820
- DESERIALIZE += indent + " store<" + first.type + ">(changetype<usize>(out), true, offsetof<this>(" + JSON.stringify(first.name) + "));\n";
848
+ const fName = first.alias || first.name;
849
+ DESERIALIZE += indent + " if (" + getComparision(fName) + ") { // " + fName + "\n";
850
+ DESERIALIZE += indent + " store<" + first.type + ">(changetype<usize>(out), true, offsetof<this>(" + JSON.stringify(first.name) + "));\n";
851
+ DESERIALIZE += indent + " srcStart += 2;\n";
852
+ DESERIALIZE += indent + " keyStart = 0;\n";
853
+ DESERIALIZE += indent + " break;\n";
854
+ DESERIALIZE += indent + " }";
855
+
856
+ for (let i = 1; i < group.length; i++) {
857
+ const mem = group[i];
858
+ const memName = mem.alias || mem.name;
859
+ DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
860
+ DESERIALIZE += indent + " store<" + mem.type + ">(changetype<usize>(out), true, offsetof<this>(" + JSON.stringify(mem.name) + "));\n";
821
861
  DESERIALIZE += indent + " srcStart += 2;\n";
822
862
  DESERIALIZE += indent + " keyStart = 0;\n";
823
863
  DESERIALIZE += indent + " break;\n";
824
864
  DESERIALIZE += indent + " }";
865
+ }
825
866
 
826
- for (let i = 1; i < group.length; i++) {
827
- const mem = group[i];
828
- const memName = mem.alias || mem.name;
829
- DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
830
- DESERIALIZE += indent + " store<" + mem.type + ">(changetype<usize>(out), true, offsetof<this>(" + JSON.stringify(mem.name) + "));\n";
831
- DESERIALIZE += indent + " srcStart += 2;\n";
832
- DESERIALIZE += indent + " keyStart = 0;\n";
833
- DESERIALIZE += indent + " break;\n";
834
- DESERIALIZE += indent + " }";
835
- }
836
-
837
- if (STRICT) {
838
- DESERIALIZE += " else {\n";
839
- DESERIALIZE += indent + ' throw new Error("Unexpected key value pair in JSON object \'" + JSON.Util.ptrToStr(keyStart, keyEnd) + ":" + JSON.Util.ptrToStr(lastIndex, srcStart) + "\' at position " + (srcEnd - srcStart).toString());\n';
840
- DESERIALIZE += indent + " }\n";
841
- } else {
842
- DESERIALIZE += " else { \n";
843
- DESERIALIZE += indent + " srcStart += 2;\n";
844
- DESERIALIZE += indent + " keyStart = 0;\n";
845
- DESERIALIZE += indent + " break;\n";
846
- DESERIALIZE += indent + " }\n";
847
- }
848
- },
867
+ if (STRICT) {
868
+ DESERIALIZE += " else {\n";
869
+ DESERIALIZE += indent + ' throw new Error("Unexpected key value pair in JSON object \'" + JSON.Util.ptrToStr(keyStart, keyEnd) + ":" + JSON.Util.ptrToStr(lastIndex, srcStart) + "\' at position " + (srcEnd - srcStart).toString());\n';
870
+ DESERIALIZE += indent + " }\n";
871
+ } else {
872
+ DESERIALIZE += " else { \n";
873
+ DESERIALIZE += indent + " srcStart += 2;\n";
874
+ DESERIALIZE += indent + " keyStart = 0;\n";
875
+ DESERIALIZE += indent + " break;\n";
876
+ DESERIALIZE += indent + " }\n";
877
+ }
878
+ },
849
879
  "boolean",
850
880
  );
851
881
 
@@ -863,41 +893,41 @@ class JSONTransform extends Visitor {
863
893
  DESERIALIZE += " if (load<u64>(srcStart, 2) == 28429466576093281) {\n";
864
894
  DESERIALIZE += " srcStart += 10;\n";
865
895
  if (DEBUG > 1) DESERIALIZE += " console.log(\"Value (bool, " + (++id) + "): \" + JSON.Util.ptrToStr(lastIndex, srcStart - 10));";
866
- generateGroups( sortedMembers.boolean, (group) => {
867
- generateConsts(group);
896
+ generateGroups(sortedMembers.boolean, (group) => {
897
+ generateConsts(group);
868
898
 
869
- const first = group[0];
870
- const fName = first.alias || first.name;
871
- DESERIALIZE += indent + " if (" + getComparision(fName) + ") { // " + fName + "\n";
872
- DESERIALIZE += indent + " store<" + first.type + ">(changetype<usize>(out), false, offsetof<this>(" + JSON.stringify(first.name) + "));\n";
899
+ const first = group[0];
900
+ const fName = first.alias || first.name;
901
+ DESERIALIZE += indent + " if (" + getComparision(fName) + ") { // " + fName + "\n";
902
+ DESERIALIZE += indent + " store<" + first.type + ">(changetype<usize>(out), false, offsetof<this>(" + JSON.stringify(first.name) + "));\n";
903
+ DESERIALIZE += indent + " srcStart += 2;\n";
904
+ DESERIALIZE += indent + " keyStart = 0;\n";
905
+ DESERIALIZE += indent + " break;\n";
906
+ DESERIALIZE += indent + " }";
907
+
908
+ for (let i = 1; i < group.length; i++) {
909
+ const mem = group[i];
910
+ const memName = mem.alias || mem.name;
911
+ DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
912
+ DESERIALIZE += indent + " store<" + mem.type + ">(changetype<usize>(out), false, offsetof<this>(" + JSON.stringify(mem.name) + "));\n";
873
913
  DESERIALIZE += indent + " srcStart += 2;\n";
874
914
  DESERIALIZE += indent + " keyStart = 0;\n";
875
915
  DESERIALIZE += indent + " break;\n";
876
916
  DESERIALIZE += indent + " }";
917
+ }
877
918
 
878
- for (let i = 1; i < group.length; i++) {
879
- const mem = group[i];
880
- const memName = mem.alias || mem.name;
881
- DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
882
- DESERIALIZE += indent + " store<" + mem.type + ">(changetype<usize>(out), false, offsetof<this>(" + JSON.stringify(mem.name) + "));\n";
883
- DESERIALIZE += indent + " srcStart += 2;\n";
884
- DESERIALIZE += indent + " keyStart = 0;\n";
885
- DESERIALIZE += indent + " break;\n";
886
- DESERIALIZE += indent + " }";
887
- }
888
-
889
- if (STRICT) {
890
- DESERIALIZE += " else {\n";
891
- DESERIALIZE += indent + ' throw new Error("Unexpected key value pair in JSON object \'" + JSON.Util.ptrToStr(keyStart, keyEnd) + ":" + JSON.Util.ptrToStr(lastIndex, srcStart) + "\' at position " + (srcEnd - srcStart).toString());\n';
892
- DESERIALIZE += indent + " }\n";
893
- } else {
894
- DESERIALIZE += " else { \n";
895
- DESERIALIZE += indent + " srcStart += 2;\n";
896
- DESERIALIZE += indent + " keyStart = 0;\n";
897
- DESERIALIZE += indent + " break;\n";
898
- DESERIALIZE += indent + " }\n";
899
- }
900
- },
919
+ if (STRICT) {
920
+ DESERIALIZE += " else {\n";
921
+ DESERIALIZE += indent + ' throw new Error("Unexpected key value pair in JSON object \'" + JSON.Util.ptrToStr(keyStart, keyEnd) + ":" + JSON.Util.ptrToStr(lastIndex, srcStart) + "\' at position " + (srcEnd - srcStart).toString());\n';
922
+ DESERIALIZE += indent + " }\n";
923
+ } else {
924
+ DESERIALIZE += " else { \n";
925
+ DESERIALIZE += indent + " srcStart += 2;\n";
926
+ DESERIALIZE += indent + " keyStart = 0;\n";
927
+ DESERIALIZE += indent + " break;\n";
928
+ DESERIALIZE += indent + " }\n";
929
+ }
930
+ },
901
931
  "boolean",
902
932
  );
903
933
 
@@ -920,37 +950,37 @@ class JSONTransform extends Visitor {
920
950
  generateConsts(group);
921
951
 
922
952
  const first = group[0];
923
- const fName = first.alias || first.name;
924
- DESERIALIZE += indent + " if (" + getComparision(fName) + ") { // " + fName + "\n";
925
- DESERIALIZE += indent + " store<" + first.type + ">(changetype<usize>(out), null, offsetof<this>(" + JSON.stringify(first.name) + "));\n";
953
+ const fName = first.alias || first.name;
954
+ DESERIALIZE += indent + " if (" + getComparision(fName) + ") { // " + fName + "\n";
955
+ DESERIALIZE += indent + " store<" + first.type + ">(changetype<usize>(out), null, offsetof<this>(" + JSON.stringify(first.name) + "));\n";
956
+ DESERIALIZE += indent + " srcStart += 2;\n";
957
+ DESERIALIZE += indent + " keyStart = 0;\n";
958
+ DESERIALIZE += indent + " break;\n";
959
+ DESERIALIZE += indent + " }";
960
+
961
+ for (let i = 1; i < group.length; i++) {
962
+ const mem = group[i];
963
+ const memName = mem.alias || mem.name;
964
+ DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
965
+ DESERIALIZE += indent + " store<" + mem.type + ">(changetype<usize>(out), null, offsetof<this>(" + JSON.stringify(mem.name) + "));\n";
926
966
  DESERIALIZE += indent + " srcStart += 2;\n";
927
967
  DESERIALIZE += indent + " keyStart = 0;\n";
928
968
  DESERIALIZE += indent + " break;\n";
929
969
  DESERIALIZE += indent + " }";
970
+ }
930
971
 
931
- for (let i = 1; i < group.length; i++) {
932
- const mem = group[i];
933
- const memName = mem.alias || mem.name;
934
- DESERIALIZE += indent + " else if (" + getComparision(memName) + ") { // " + memName + "\n";
935
- DESERIALIZE += indent + " store<" + mem.type + ">(changetype<usize>(out), null, offsetof<this>(" + JSON.stringify(mem.name) + "));\n";
936
- DESERIALIZE += indent + " srcStart += 2;\n";
937
- DESERIALIZE += indent + " keyStart = 0;\n";
938
- DESERIALIZE += indent + " break;\n";
939
- DESERIALIZE += indent + " }";
940
- }
941
-
942
- if (STRICT) {
943
- DESERIALIZE += " else {\n";
944
- DESERIALIZE += indent + ' throw new Error("Unexpected key value pair in JSON object \'" + JSON.Util.ptrToStr(keyStart, keyEnd) + ":" + JSON.Util.ptrToStr(lastIndex, srcStart) + "\' at position " + (srcEnd - srcStart).toString());\n';
945
- DESERIALIZE += indent + " }\n";
946
- } else {
947
- DESERIALIZE += " else { \n";
948
- DESERIALIZE += indent + " srcStart += 2;\n";
949
- DESERIALIZE += indent + " keyStart = 0;\n";
950
- DESERIALIZE += indent + " break;\n";
951
- DESERIALIZE += indent + " }\n";
952
- }
953
- },
972
+ if (STRICT) {
973
+ DESERIALIZE += " else {\n";
974
+ DESERIALIZE += indent + ' throw new Error("Unexpected key value pair in JSON object \'" + JSON.Util.ptrToStr(keyStart, keyEnd) + ":" + JSON.Util.ptrToStr(lastIndex, srcStart) + "\' at position " + (srcEnd - srcStart).toString());\n';
975
+ DESERIALIZE += indent + " }\n";
976
+ } else {
977
+ DESERIALIZE += " else { \n";
978
+ DESERIALIZE += indent + " srcStart += 2;\n";
979
+ DESERIALIZE += indent + " keyStart = 0;\n";
980
+ DESERIALIZE += indent + " break;\n";
981
+ DESERIALIZE += indent + " }\n";
982
+ }
983
+ },
954
984
  "null",
955
985
  );
956
986
 
@@ -1074,7 +1104,7 @@ class JSONTransform extends Visitor {
1074
1104
  )
1075
1105
  ? path.join(pkgPath, fromPath.slice(5))
1076
1106
  : fromPath
1077
- : path.join(baseDir, fromPath);
1107
+ : path.join(this.baseCWD, fromPath);
1078
1108
 
1079
1109
  const bsImport = this.imports.find((i) => i.declarations?.find((d) => d.foreignName.text == "bs" || d.name.text == "bs"));
1080
1110
  const jsonImport = this.imports.find((i) => i.declarations?.find((d) => d.foreignName.text == "JSON" || d.name.text == "JSON"));
@@ -10,6 +10,7 @@ class ImportGetter extends Visitor {
10
10
  visitImportStatement(node: ImportStatement, ref?: Node | null): void {
11
11
  this.imports.push(node);
12
12
  }
13
+
13
14
  static getImports(source: Source): ImportStatement[] {
14
15
  ImportGetter.SN.imports = [];
15
16
  ImportGetter.SN.visit(source);
@@ -27,6 +28,7 @@ export function getImportedClass(name: string, source: Source, parser: Parser):
27
28
  if (!externalSource) continue;
28
29
 
29
30
  const classDeclaration = ClassGetter.getClass(name, externalSource);
31
+ if (!classDeclaration) continue;
30
32
  if (!(classDeclaration.flags & CommonFlags.Export)) continue;
31
33
  return classDeclaration;
32
34
  }
@@ -42,14 +44,20 @@ class ClassGetter extends Visitor {
42
44
  this.classes.push(node);
43
45
  }
44
46
 
47
+ // visitTypeName(node: TypeName, ref?: Node | null): void {}
48
+ // visitParameter(node: ParameterNode, ref?: Node | null): void {}
49
+ // visitFunctionTypeNode(node: FunctionTypeNode, ref?: Node | null): void {}
50
+ // visitNamedTypeNode(node: NamedTypeNode, ref?: Node | null): void {}
51
+
45
52
  static getClass(name: string, source: Source): ClassDeclaration | null {
46
53
  return ClassGetter.getClasses(source).find((c) => c.name.text == name) || null;
47
54
  }
48
55
 
49
56
  static getClasses(source: Source): ClassDeclaration[] {
50
- ClassGetter.SN.classes = [];
51
- ClassGetter.SN.visit(source);
52
- return ClassGetter.SN.classes;
57
+ // ClassGetter.SN.classes = [];
58
+ // ClassGetter.SN.visit(source);
59
+ // return ClassGetter.SN.classes;
60
+ return source.statements.filter((stmt) => stmt.kind == NodeKind.ClassDeclaration) as ClassDeclaration[];
53
61
  }
54
62
  }
55
63