json-as 0.9.14 → 0.9.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/CHANGELOG CHANGED
@@ -22,6 +22,7 @@ v0.9.11 - Remove MpZ--implement custom serializers and deserializers in the work
22
22
  v0.9.12 - Add compat with aspect
23
23
  v0.9.13 - Fix empty strings not indexing correctly
24
24
  v0.9.14 - Ignore properties of type Function
25
+ v0.9.15 - Support JSON.Raw blocks
25
26
 
26
27
  [UNRELEASED] v1.0.0
27
28
  - Allow nullable primitives
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  __| || __|| || | | ___ | _ || __|
4
4
  | | ||__ || | || | | ||___|| ||__ |
5
5
  |_____||_____||_____||_|___| |__|__||_____|
6
- v0.9.14
6
+ v0.9.15
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -3,17 +3,13 @@
3
3
  "./assembly/__tests__/*.spec.ts"
4
4
  ],
5
5
  "outDir": "./build",
6
- "config": "./asconfig.json",
7
- "suites": [],
8
- "coverage": {
9
- "enabled": true,
10
- "show": false
6
+ "config": "none",
7
+ "plugins": {
8
+ "coverage": false
11
9
  },
12
10
  "buildOptions": {
13
11
  "args": [],
14
- "wasi": true,
15
- "parallel": true,
16
- "verbose": true
12
+ "target": "wasi"
17
13
  },
18
14
  "runOptions": {
19
15
  "runtime": {
@@ -1,4 +1,4 @@
1
- import { JSON } from "..";
1
+ import { JSON } from "json-as";
2
2
  import {
3
3
  describe,
4
4
  expect,
@@ -33,4 +33,4 @@ declare function omitnull(): Function;
33
33
  * Property decorator that allows a field to be flattened.
34
34
  * @param fieldName - Points to the field to flatten. Can use dot-notation here like @omit("foo.identifier.text")
35
35
  */
36
- declare function flatten(fieldName: string = "value"): Function;
36
+ declare function flatten(fieldName: string = "value"): Function;
package/assembly/index.ts CHANGED
@@ -13,21 +13,151 @@ import { deserializeFloat } from "./deserialize/float";
13
13
  import { deserializeObject } from "./deserialize/object";
14
14
  import { deserializeMap } from "./deserialize/map";
15
15
  import { deserializeDate } from "./deserialize/date";
16
- import { NULL_WORD } from "./custom/chars";
16
+ import { BRACKET_LEFT, NULL_WORD } from "./custom/chars";
17
17
  import { deserializeInteger } from "./deserialize/integer";
18
18
  import { deserializeString } from "./deserialize/string";
19
+ import { Sink } from "./custom/sink";
20
+ import { bs } from "./custom/bs";
21
+
22
+ /**
23
+ * Offset of the 'storage' property in the JSON.Value class.
24
+ */
25
+ // @ts-ignore: Decorator valid here
26
+ @inline const STORAGE = offsetof<JSON.Value>("storage");
19
27
 
20
28
  /**
21
29
  * JSON Encoder/Decoder for AssemblyScript
22
30
  */
23
31
  export namespace JSON {
32
+ /**
33
+ * Enum representing the different types supported by JSON.
34
+ */
35
+ export enum Types {
36
+ Raw = 0,
37
+ U8 = 1,
38
+ U16 = 2,
39
+ U32 = 3,
40
+ U64 = 4,
41
+ F32 = 5,
42
+ F64 = 6,
43
+ Bool = 7,
44
+ String = 8,
45
+ Obj = 8,
46
+ Array = 9
47
+ }
48
+ export type Raw = string;
49
+ export class Value {
50
+ public type: i32;
51
+
52
+ // @ts-ignore
53
+ private storage: u64;
54
+
55
+ private constructor() { unreachable(); }
56
+
57
+ /**
58
+ * Creates an JSON.Value instance from a given value.
59
+ * @param value - The value to be encapsulated.
60
+ * @returns An instance of JSON.Value.
61
+ */
62
+ @inline static from<T>(value: T): JSON.Value {
63
+ if (value instanceof JSON.Value) {
64
+ return value;
65
+ }
66
+ const out = changetype<JSON.Value>(__new(offsetof<JSON.Value>(), idof<JSON.Value>()));
67
+ out.set<T>(value);
68
+ return out;
69
+ }
70
+
71
+ /**
72
+ * Sets the value of the JSON.Value instance.
73
+ * @param value - The value to be set.
74
+ */
75
+ @inline set<T>(value: T): void {
76
+ if (isBoolean<T>()) {
77
+ this.type = JSON.Types.Bool;
78
+ store<T>(changetype<usize>(this), value, STORAGE);
79
+ } else if (value instanceof u8 || value instanceof i8) {
80
+ this.type = JSON.Types.U8;
81
+ store<T>(changetype<usize>(this), value, STORAGE);
82
+ } else if (value instanceof u16 || value instanceof i16) {
83
+ this.type = JSON.Types.U16;
84
+ store<T>(changetype<usize>(this), value, STORAGE);
85
+ } else if (value instanceof u32 || value instanceof i32) {
86
+ this.type = JSON.Types.U32;
87
+ store<T>(changetype<usize>(this), value, STORAGE);
88
+ } else if (value instanceof u64 || value instanceof i64) {
89
+ this.type = JSON.Types.U64;
90
+ store<T>(changetype<usize>(this), value, STORAGE);
91
+ } else if (value instanceof f32) {
92
+ this.type = JSON.Types.F64;
93
+ store<T>(changetype<usize>(this), value, STORAGE);
94
+ } else if (value instanceof f64) {
95
+ this.type = JSON.Types.F64;
96
+ store<T>(changetype<usize>(this), value, STORAGE);
97
+ } else if (isString<T>()) {
98
+ this.type = JSON.Types.String;
99
+ store<T>(changetype<usize>(this), value, STORAGE);
100
+ } else if (value instanceof Map) {
101
+ if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
102
+ abort("Maps must be of type Map<string, JSON.Value>!");
103
+ }
104
+ this.type = JSON.Types.Obj;
105
+ store<T>(changetype<usize>(this), value, STORAGE);
106
+ } else if (isArray<T>()) {
107
+ // @ts-ignore: T satisfies constraints of any[]
108
+ this.type = JSON.Types.Array + getArrayDepth<T>(0);
109
+ store<T>(changetype<usize>(this), value, STORAGE);
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Gets the value of the JSON.Value instance.
115
+ * @returns The encapsulated value.
116
+ */
117
+ @inline get<T>(): T {
118
+ return load<T>(changetype<usize>(this), STORAGE);
119
+ }
120
+
121
+ /**
122
+ * Converts the JSON.Value to a string representation.
123
+ * @param useString - If true, treats Buffer as a string.
124
+ * @returns The string representation of the JSON.Value.
125
+ */
126
+ toString(): string {
127
+ switch (this.type) {
128
+ case JSON.Types.U8: return this.get<u8>().toString();
129
+ case JSON.Types.U16: return this.get<u16>().toString();
130
+ case JSON.Types.U32: return this.get<u32>().toString();
131
+ case JSON.Types.U64: return this.get<u64>().toString();
132
+ case JSON.Types.String: return "\"" + this.get<string>() + "\"";
133
+ case JSON.Types.Bool: return this.get<boolean>() ? "true" : "false";
134
+ default: {
135
+ const arr = this.get<JSON.Value[]>();
136
+ if (!arr.length) return "[]";
137
+ const out = Sink.fromStringLiteral("[");
138
+ const end = arr.length - 1;
139
+ for (let i = 0; i < end; i++) {
140
+ const element = unchecked(arr[i]);
141
+ out.write(element.toString());
142
+ out.write(",");
143
+ }
144
+
145
+ const element = unchecked(arr[end]);
146
+ out.write(element.toString());
147
+
148
+ out.write("]");
149
+ return out.toString();
150
+ }
151
+ }
152
+ }
153
+ }
24
154
  export class Box<T> {
25
- constructor(public value: T) {}
155
+ constructor(public value: T) { }
26
156
  @inline static from<T>(value: T): Box<T> {
27
157
  return new Box(value);
28
158
  }
29
159
  }
30
-
160
+
31
161
  /**
32
162
  * Stringifies valid JSON data.
33
163
  * ```js
@@ -52,7 +182,7 @@ export namespace JSON {
52
182
  // @ts-ignore
53
183
  } else if (isString<nonnull<T>>()) {
54
184
  return serializeString(changetype<string>(data));
55
- // @ts-ignore: Supplied by trasnform
185
+ // @ts-ignore: Supplied by transform
56
186
  } else if (isDefined(data.__SERIALIZE)) {
57
187
  // @ts-ignore
58
188
  return serializeObject(changetype<nonnull<T>>(data));
package/assembly/test.ts CHANGED
@@ -1,42 +1,33 @@
1
1
  // import { JSON } from ".";
2
- import { bs } from "./custom/bs";
3
- // @json
4
- // class Vec3 {
5
- // x: f32 = 0.0;
6
- // y: f32 = 0.0;
7
- // z: f32 = 0.0;
8
- // }
2
+ import { JSON } from ".";
3
+ @json
4
+ class Vec3 {
5
+ x: f32 = 0.0;
6
+ y: f32 = 0.0;
7
+ z: f32 = 0.0;
8
+ }
9
9
 
10
- // @json
11
- // class Player {
12
- // @alias("first name")
13
- // firstName!: string;
14
- // lastName!: string;
15
- // lastActive!: i32[];
16
- // // Drop in a code block, function, or expression that evaluates to a boolean
17
- // @omitif("this.age < 18")
18
- // age!: i32;
19
- // @omitnull()
20
- // pos!: Vec3 | null;
21
- // isVerified!: boolean;
22
- // }
10
+ @json
11
+ class Player {
12
+ firstName!: string;
13
+ lastName!: string;
14
+ lastActive!: i32[];
15
+ age!: i32;
16
+ pos!: JSON.Raw;
17
+ isVerified!: boolean;
18
+ }
23
19
 
24
- // const player: Player = {
25
- // firstName: "Emmet",
26
- // lastName: "West",
27
- // lastActive: [8, 27, 2022],
28
- // age: 23,
29
- // pos: {
30
- // x: 3.4,
31
- // y: 1.2,
32
- // z: 8.3
33
- // },
34
- // isVerified: true
35
- // };
20
+ const player: Player = {
21
+ firstName: "Emmet",
22
+ lastName: "West",
23
+ lastActive: [8, 27, 2022],
24
+ age: 23,
25
+ pos: "{\"x\":3.4,\"y\":1.2,\"z\":8.3}",
26
+ isVerified: true
27
+ };
36
28
 
37
- // const stringified = JSON.stringify<Player>(player);
38
-
39
- // const parsed = JSON.parse<Player>(stringified);
40
-
41
- bs.write_32(6422620);
42
- console.log(bs.out<string>())
29
+ const stringified = JSON.stringify<Player>(player);
30
+ console.log(stringified);
31
+ console.log(idof<JSON.Raw>().toString());
32
+ console.log(idof<string>().toString())
33
+ // const parsed = JSON.parse<Player>(stringified);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.9.14",
3
+ "version": "0.9.15",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -32,7 +32,7 @@
32
32
  "@assemblyscript/wasi-shim": "^0.1.0",
33
33
  "as-bench": "^0.0.0-alpha",
34
34
  "as-console": "^7.0.0",
35
- "as-test": "0.1.9",
35
+ "as-test": "0.3.1",
36
36
  "assemblyscript": "^0.27.29",
37
37
  "assemblyscript-prettier": "^3.0.1",
38
38
  "benchmark": "^2.1.4",
@@ -117,8 +117,14 @@ class JSONTransform extends BaseVisitor {
117
117
  }
118
118
  if (!mem.flags.length) {
119
119
  mem.flags = [PropertyFlags.None];
120
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
121
- mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
120
+ if (type == "JSON.Raw") {
121
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<string>(this." + name.text + ")}";
122
+ mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<string>(data.substring(value_start, value_end));";
123
+ }
124
+ else {
125
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
126
+ mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));";
127
+ }
122
128
  }
123
129
  if (mem.flags.includes(PropertyFlags.OmitNull)) {
124
130
  mem.serialize = "${changetype<usize>(this." + mem.name + ") == <usize>0" + " ? \"\" : '" + escapeString(JSON.stringify(mem.alias || mem.name)) + ":' + __SERIALIZE<" + type + ">(this." + name.text + ") + \",\"}";
@@ -136,11 +142,11 @@ class JSONTransform extends BaseVisitor {
136
142
  else if (mem.flags.includes(PropertyFlags.Flatten)) {
137
143
  const nullable = mem.node.type.isNullable;
138
144
  if (nullable) {
139
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
145
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
140
146
  mem.deserialize = "if (value_end - value_start == 4 && load<u64>(changetype<usize>(data) + <usize>(value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");\n }";
141
147
  }
142
148
  else {
143
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0] : '') + ") : \"null\"}";
149
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
144
150
  mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args[0] + "\":' + data.substring(value_start, value_end) + \"}\");";
145
151
  }
146
152
  mem.name = name.text;
@@ -164,6 +170,9 @@ class JSONTransform extends BaseVisitor {
164
170
  else if (t === "bool" || t === "boolean") {
165
171
  mem.initialize = "this." + name.text + " = false";
166
172
  }
173
+ else if (t === "JSON.Raw") {
174
+ mem.initialize = "this." + name.text + " = \"\"";
175
+ }
167
176
  else if (t === "u8" ||
168
177
  t === "u16" ||
169
178
  t === "u32" ||
@@ -284,6 +293,8 @@ class JSONTransform extends BaseVisitor {
284
293
  let f = true;
285
294
  for (let i = 0; i < memberSet.length; i++) {
286
295
  const member = memberSet[i];
296
+ if (!member.deserialize)
297
+ continue;
287
298
  const name = encodeKey(member.alias || member.name);
288
299
  if (name.length === 1) {
289
300
  DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.9.14",
3
+ "version": "0.9.15",
4
4
  "description": "The only JSON library you'll need for AssemblyScript. SIMD enabled",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",
@@ -135,8 +135,13 @@ class JSONTransform extends BaseVisitor {
135
135
 
136
136
  if (!mem.flags.length) {
137
137
  mem.flags = [PropertyFlags.None];
138
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
139
- mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
138
+ if (type == "JSON.Raw") {
139
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<string>(this." + name.text + ")}";
140
+ mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<string>(data.substring(value_start, value_end));"
141
+ } else {
142
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${__SERIALIZE<" + type + ">(this." + name.text + ")}";
143
+ mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">(data.substring(value_start, value_end));"
144
+ }
140
145
  }
141
146
 
142
147
  if (mem.flags.includes(PropertyFlags.OmitNull)) {
@@ -152,10 +157,10 @@ class JSONTransform extends BaseVisitor {
152
157
  } else if (mem.flags.includes(PropertyFlags.Flatten)) {
153
158
  const nullable = (mem.node.type as NamedTypeNode).isNullable;
154
159
  if (nullable) {
155
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
160
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(changetype<nonnull<" + type + ">>(this." + name.text + ")" + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
156
161
  mem.deserialize = "if (value_end - value_start == 4 && load<u64>(changetype<usize>(data) + <usize>(value_start << 1)) == " + charCodeAt64("null", 0) + ") {\n this." + name.text + " = null;\n } else {\n this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");\n }";
157
162
  } else {
158
- mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args[0]! : '') + ") : \"null\"}";
163
+ mem.serialize = escapeString(JSON.stringify(mem.alias || mem.name)) + ":${this." + name.text + " ? __SERIALIZE(this." + name.text + (mem.args?.length ? '.' + mem.args.join(".") : '') + ") : \"null\"}";
159
164
  mem.deserialize = "this." + name.text + " = " + "__DESERIALIZE<" + type + ">('{\"" + mem.args![0]! + "\":' + data.substring(value_start, value_end) + \"}\");";
160
165
  }
161
166
  mem.name = name.text;
@@ -174,6 +179,8 @@ class JSONTransform extends BaseVisitor {
174
179
  mem.initialize = "this." + name.text + " = instantiate<" + mem.type + ">()";
175
180
  } else if (t === "bool" || t === "boolean") {
176
181
  mem.initialize = "this." + name.text + " = false";
182
+ } else if (t === "JSON.Raw") {
183
+ mem.initialize = "this." + name.text + " = \"\"";
177
184
  } else if (
178
185
  t === "u8" ||
179
186
  t === "u16" ||
@@ -300,6 +307,7 @@ class JSONTransform extends BaseVisitor {
300
307
  let f = true;
301
308
  for (let i = 0; i < memberSet.length; i++) {
302
309
  const member = memberSet[i]!;
310
+ if (!member.deserialize) continue;
303
311
  const name = encodeKey(member.alias || member.name);
304
312
  if (name.length === 1) {
305
313
  DESERIALIZE += ` case ${name.charCodeAt(0)}: {\n ${member.deserialize}\n return true;\n }\n`;
@@ -1,543 +0,0 @@
1
- import {
2
- ArrayLiteralExpression,
3
- AssertionExpression,
4
- BinaryExpression,
5
- CallExpression,
6
- ElementAccessExpression,
7
- FloatLiteralExpression,
8
- FunctionTypeNode,
9
- IdentifierExpression,
10
- NamedTypeNode,
11
- Node,
12
- ObjectLiteralExpression,
13
- Source,
14
- TypeNode,
15
- TypeParameterNode,
16
- BlockStatement,
17
- BreakStatement,
18
- ClassDeclaration,
19
- ClassExpression,
20
- CommaExpression,
21
- ConstructorExpression,
22
- ContinueStatement,
23
- DecoratorNode,
24
- DoStatement,
25
- EmptyStatement,
26
- EnumDeclaration,
27
- EnumValueDeclaration,
28
- ExportDefaultStatement,
29
- ExportImportStatement,
30
- ExportMember,
31
- ExportStatement,
32
- Expression,
33
- ExpressionStatement,
34
- FalseExpression,
35
- FieldDeclaration,
36
- ForStatement,
37
- FunctionDeclaration,
38
- FunctionExpression,
39
- IfStatement,
40
- ImportDeclaration,
41
- ImportStatement,
42
- IndexSignatureNode,
43
- InstanceOfExpression,
44
- IntegerLiteralExpression,
45
- InterfaceDeclaration,
46
- LiteralExpression,
47
- MethodDeclaration,
48
- NamespaceDeclaration,
49
- NewExpression,
50
- NullExpression,
51
- ParameterNode,
52
- ParenthesizedExpression,
53
- PropertyAccessExpression,
54
- RegexpLiteralExpression,
55
- ReturnStatement,
56
- Statement,
57
- StringLiteralExpression,
58
- SuperExpression,
59
- SwitchCase,
60
- SwitchStatement,
61
- TemplateLiteralExpression,
62
- TernaryExpression,
63
- ThisExpression,
64
- ThrowStatement,
65
- TrueExpression,
66
- TryStatement,
67
- TypeDeclaration,
68
- TypeName,
69
- UnaryExpression,
70
- UnaryPostfixExpression,
71
- UnaryPrefixExpression,
72
- VariableDeclaration,
73
- VariableStatement,
74
- VoidStatement,
75
- WhileStatement,
76
- } from "assemblyscript/dist/assemblyscript.js";
77
-
78
- export declare type Collection<T extends unknown> =
79
- | Node
80
- | T[]
81
- | Map<string, T | T[] | Iterable<T>>
82
- | Iterable<T>;
83
-
84
- export class Visitor {
85
- public currentSource: Source | null = null;
86
- public depth = 0;
87
- visit<T extends unknown>(node: T | T[] | Map<string, T | T[] | Iterable<T>> | Iterable<T>): void {
88
- if (node == null) {
89
- return;
90
- } else if (node instanceof Array) {
91
- for (const element of node) {
92
- this.visit(element);
93
- }
94
- } else if (node instanceof Map) {
95
- for (const element of node.values()) {
96
- this.visit(element);
97
- }
98
- // @ts-ignore
99
- } else if (typeof node[Symbol.iterator] === "function") {
100
- // @ts-ignore
101
- for (const element of node) {
102
- this.visit(element);
103
- }
104
- } else if (node instanceof Node) {
105
- this._visit(node);
106
- } else {
107
- throw new Error("Could not visit invalid type!");
108
- }
109
- }
110
- private _visit(node: Node): void {
111
- if (node instanceof Source) {
112
- this.visitSource(node);
113
- } else if (node instanceof NamedTypeNode) {
114
- this.visitNamedTypeNode(node);
115
- } else if (node instanceof FunctionTypeNode) {
116
- this.visitFunctionTypeNode(node);
117
- } else if (node instanceof TypeName) {
118
- this.visitTypeName(node);
119
- } else if (node instanceof TypeParameterNode) {
120
- this.visitTypeParameter(node);
121
- } else if (node instanceof IdentifierExpression) {
122
- this.visitIdentifierExpression(node);
123
- } else if (node instanceof AssertionExpression) {
124
- this.visitAssertionExpression(node);
125
- } else if (node instanceof BinaryExpression) {
126
- this.visitBinaryExpression(node);
127
- } else if (node instanceof CallExpression) {
128
- this.visitCallExpression(node);
129
- } else if (node instanceof ClassExpression) {
130
- this.visitClassExpression(node);
131
- } else if (node instanceof CommaExpression) {
132
- this.visitCommaExpression(node);
133
- } else if (node instanceof ElementAccessExpression) {
134
- this.visitElementAccessExpression(node);
135
- } else if (node instanceof FunctionExpression) {
136
- this.visitFunctionExpression(node);
137
- } else if (node instanceof InstanceOfExpression) {
138
- this.visitInstanceOfExpression(node);
139
- } else if (node instanceof LiteralExpression) {
140
- this.visitLiteralExpression(node);
141
- } else if (node instanceof NewExpression) {
142
- this.visitNewExpression(node);
143
- } else if (node instanceof ParenthesizedExpression) {
144
- this.visitParenthesizedExpression(node);
145
- } else if (node instanceof PropertyAccessExpression) {
146
- this.visitPropertyAccessExpression(node);
147
- } else if (node instanceof TernaryExpression) {
148
- this.visitTernaryExpression(node);
149
- } else if (node instanceof UnaryPostfixExpression) {
150
- this.visitUnaryPostfixExpression(node);
151
- } else if (node instanceof UnaryPrefixExpression) {
152
- this.visitUnaryPrefixExpression(node);
153
- } else if (node instanceof BlockStatement) {
154
- this.visitBlockStatement(node);
155
- } else if (node instanceof BreakStatement) {
156
- this.visitBreakStatement(node);
157
- } else if (node instanceof ContinueStatement) {
158
- this.visitContinueStatement(node);
159
- } else if (node instanceof DoStatement) {
160
- this.visitDoStatement(node);
161
- } else if (node instanceof EmptyStatement) {
162
- this.visitEmptyStatement(node);
163
- } else if (node instanceof ExportStatement) {
164
- this.visitExportStatement(node);
165
- } else if (node instanceof ExportDefaultStatement) {
166
- this.visitExportDefaultStatement(node);
167
- } else if (node instanceof ExportImportStatement) {
168
- this.visitExportImportStatement(node);
169
- } else if (node instanceof ExpressionStatement) {
170
- this.visitExpressionStatement(node);
171
- } else if (node instanceof ForStatement) {
172
- this.visitForStatement(node);
173
- } else if (node instanceof IfStatement) {
174
- this.visitIfStatement(node);
175
- } else if (node instanceof ImportStatement) {
176
- this.visitImportStatement(node);
177
- } else if (node instanceof ReturnStatement) {
178
- this.visitReturnStatement(node);
179
- } else if (node instanceof SwitchStatement) {
180
- this.visitSwitchStatement(node);
181
- } else if (node instanceof ThrowStatement) {
182
- this.visitThrowStatement(node);
183
- } else if (node instanceof TryStatement) {
184
- this.visitTryStatement(node);
185
- } else if (node instanceof VariableStatement) {
186
- this.visitVariableStatement(node);
187
- } else if (node instanceof WhileStatement) {
188
- this.visitWhileStatement(node);
189
- } else if (node instanceof ClassDeclaration) {
190
- this.visitClassDeclaration(node);
191
- } else if (node instanceof EnumDeclaration) {
192
- this.visitEnumDeclaration(node);
193
- } else if (node instanceof EnumValueDeclaration) {
194
- this.visitEnumValueDeclaration(node);
195
- } else if (node instanceof FieldDeclaration) {
196
- this.visitFieldDeclaration(node);
197
- } else if (node instanceof FunctionDeclaration) {
198
- this.visitFunctionDeclaration(node);
199
- } else if (node instanceof ImportDeclaration) {
200
- this.visitImportDeclaration(node);
201
- } else if (node instanceof InterfaceDeclaration) {
202
- this.visitInterfaceDeclaration(node);
203
- } else if (node instanceof MethodDeclaration) {
204
- this.visitMethodDeclaration(node);
205
- } else if (node instanceof NamespaceDeclaration) {
206
- this.visitNamespaceDeclaration(node);
207
- } else if (node instanceof TypeDeclaration) {
208
- this.visitTypeDeclaration(node);
209
- } else if (node instanceof VariableDeclaration) {
210
- this.visitVariableDeclaration(node);
211
- } else if (node instanceof DecoratorNode) {
212
- this.visitDecoratorNode(node);
213
- } else if (node instanceof ExportMember) {
214
- this.visitExportMember(node);
215
- } else if (node instanceof ParameterNode) {
216
- this.visitParameter(node);
217
- } else if (node instanceof SwitchCase) {
218
- this.visitSwitchCase(node);
219
- } else if (node instanceof IndexSignatureNode) {
220
- this.visitIndexSignature(node);
221
- } else {
222
- throw new Error("Could not visit invalid type!");
223
- }
224
- }
225
- visitSource(node: Source): void {
226
- this.currentSource = node;
227
- for (const stmt of node.statements) {
228
- this.depth++;
229
- this._visit(stmt);
230
- this.depth--;
231
- }
232
- this.currentSource = null;
233
- }
234
- visitTypeNode(node: TypeNode): void { }
235
- visitTypeName(node: TypeName): void {
236
- this.visit(node.identifier);
237
- this.visit(node.next);
238
- }
239
- visitNamedTypeNode(node: NamedTypeNode): void {
240
- this.visit(node.name);
241
- this.visit(node.typeArguments);
242
- }
243
- visitFunctionTypeNode(node: FunctionTypeNode): void {
244
- this.visit(node.parameters);
245
- this.visit(node.returnType);
246
- this.visit(node.explicitThisType);
247
- }
248
- visitTypeParameter(node: TypeParameterNode): void {
249
- this.visit(node.name);
250
- this.visit(node.extendsType);
251
- this.visit(node.defaultType);
252
- }
253
- visitIdentifierExpression(node: IdentifierExpression): void { }
254
- visitArrayLiteralExpression(node: ArrayLiteralExpression) {
255
- this.visit(node.elementExpressions);
256
- }
257
- visitObjectLiteralExpression(node: ObjectLiteralExpression) {
258
- this.visit(node.names);
259
- this.visit(node.values);
260
- }
261
- visitAssertionExpression(node: AssertionExpression) {
262
- this.visit(node.toType);
263
- this.visit(node.expression);
264
- }
265
- visitBinaryExpression(node: BinaryExpression) {
266
- this.visit(node.left);
267
- this.visit(node.right);
268
- }
269
- visitCallExpression(node: CallExpression) {
270
- this.visit(node.expression);
271
- this.visitArguments(node.typeArguments, node.args);
272
- }
273
- visitArguments(typeArguments: TypeNode[] | null, args: Expression[]) {
274
- this.visit(typeArguments);
275
- this.visit(args);
276
- }
277
- visitClassExpression(node: ClassExpression) {
278
- this.visit(node.declaration);
279
- }
280
- visitCommaExpression(node: CommaExpression) {
281
- this.visit(node.expressions);
282
- }
283
- visitElementAccessExpression(node: ElementAccessExpression) {
284
- this.visit(node.elementExpression);
285
- this.visit(node.expression);
286
- }
287
- visitFunctionExpression(node: FunctionExpression) {
288
- this.visit(node.declaration);
289
- }
290
- visitLiteralExpression(node: LiteralExpression) {
291
- if (node instanceof FloatLiteralExpression) {
292
- this.visitFloatLiteralExpression(node);
293
- } else if (node instanceof IntegerLiteralExpression) {
294
- this.visitIntegerLiteralExpression(node);
295
- } else if (node instanceof StringLiteralExpression) {
296
- this.visitStringLiteralExpression(node);
297
- } else if (node instanceof TemplateLiteralExpression) {
298
- this.visitTemplateLiteralExpression(node);
299
- } else if (node instanceof RegexpLiteralExpression) {
300
- this.visitRegexpLiteralExpression(node);
301
- } else if (node instanceof ArrayLiteralExpression) {
302
- this.visitArrayLiteralExpression(node);
303
- } else if (node instanceof ObjectLiteralExpression) {
304
- this.visitObjectLiteralExpression(node);
305
- } else {
306
- throw new Error(
307
- "Invalid LiteralKind at visitLiteralExpression(): " + node.literalKind,
308
- );
309
- }
310
- }
311
- visitFloatLiteralExpression(node: FloatLiteralExpression) { }
312
- visitInstanceOfExpression(node: InstanceOfExpression) {
313
- this.visit(node.expression);
314
- this.visit(node.isType);
315
- }
316
- visitIntegerLiteralExpression(node: IntegerLiteralExpression) { }
317
- visitStringLiteral(str: string, singleQuoted: boolean = false) { }
318
- visitStringLiteralExpression(node: StringLiteralExpression) {
319
- this.visitStringLiteral(node.value);
320
- }
321
- visitTemplateLiteralExpression(node: TemplateLiteralExpression) { }
322
- visitRegexpLiteralExpression(node: RegexpLiteralExpression) { }
323
- visitNewExpression(node: NewExpression) {
324
- this.visit(node.typeArguments);
325
- this.visitArguments(node.typeArguments, node.args);
326
- this.visit(node.args);
327
- }
328
- visitParenthesizedExpression(node: ParenthesizedExpression) {
329
- this.visit(node.expression);
330
- }
331
- visitPropertyAccessExpression(node: PropertyAccessExpression) {
332
- this.visit(node.property);
333
- this.visit(node.expression);
334
- }
335
- visitTernaryExpression(node: TernaryExpression) {
336
- this.visit(node.condition);
337
- this.visit(node.ifThen);
338
- this.visit(node.ifElse);
339
- }
340
- visitUnaryExpression(node: UnaryExpression) {
341
- this.visit(node.operand);
342
- }
343
- visitUnaryPostfixExpression(node: UnaryPostfixExpression) {
344
- this.visit(node.operand);
345
- }
346
- visitUnaryPrefixExpression(node: UnaryPrefixExpression) {
347
- this.visit(node.operand);
348
- }
349
- visitSuperExpression(node: SuperExpression) { }
350
- visitFalseExpression(node: FalseExpression) { }
351
- visitTrueExpression(node: TrueExpression) { }
352
- visitThisExpression(node: ThisExpression) { }
353
- visitNullExperssion(node: NullExpression) { }
354
- visitConstructorExpression(node: ConstructorExpression) { }
355
- visitNodeAndTerminate(statement: Statement) { }
356
- visitBlockStatement(node: BlockStatement) {
357
- this.depth++;
358
- this.visit(node.statements);
359
- this.depth--;
360
- }
361
- visitBreakStatement(node: BreakStatement) {
362
- this.visit(node.label);
363
- }
364
- visitContinueStatement(node: ContinueStatement) {
365
- this.visit(node.label);
366
- }
367
- visitClassDeclaration(node: ClassDeclaration, isDefault: boolean = false) {
368
- this.visit(node.name);
369
- this.depth++;
370
- this.visit(node.decorators);
371
- if (
372
- node.isGeneric ? node.typeParameters != null : node.typeParameters == null
373
- ) {
374
- this.visit(node.typeParameters);
375
- this.visit(node.extendsType);
376
- this.visit(node.implementsTypes);
377
- this.visit(node.members);
378
- this.depth--;
379
- } else {
380
- throw new Error(
381
- "Expected to type parameters to match class declaration, but found type mismatch instead!",
382
- );
383
- }
384
- }
385
- visitDoStatement(node: DoStatement) {
386
- this.visit(node.condition);
387
- this.visit(node.body);
388
- }
389
- visitEmptyStatement(node: EmptyStatement) { }
390
- visitEnumDeclaration(node: EnumDeclaration, isDefault: boolean = false) {
391
- this.visit(node.name);
392
- this.visit(node.decorators);
393
- this.visit(node.values);
394
- }
395
- visitEnumValueDeclaration(node: EnumValueDeclaration) {
396
- this.visit(node.name);
397
- this.visit(node.initializer);
398
- }
399
- visitExportImportStatement(node: ExportImportStatement) {
400
- this.visit(node.name);
401
- this.visit(node.externalName);
402
- }
403
- visitExportMember(node: ExportMember) {
404
- this.visit(node.localName);
405
- this.visit(node.exportedName);
406
- }
407
- visitExportStatement(node: ExportStatement) {
408
- this.visit(node.path);
409
- this.visit(node.members);
410
- }
411
- visitExportDefaultStatement(node: ExportDefaultStatement) {
412
- this.visit(node.declaration);
413
- }
414
- visitExpressionStatement(node: ExpressionStatement) {
415
- this.visit(node.expression);
416
- }
417
- visitFieldDeclaration(node: FieldDeclaration) {
418
- this.visit(node.name);
419
- this.visit(node.type);
420
- this.visit(node.initializer);
421
- this.visit(node.decorators);
422
- }
423
- visitForStatement(node: ForStatement) {
424
- this.visit(node.initializer);
425
- this.visit(node.condition);
426
- this.visit(node.incrementor);
427
- this.visit(node.body);
428
- }
429
- visitFunctionDeclaration(
430
- node: FunctionDeclaration,
431
- isDefault: boolean = false,
432
- ) {
433
- this.visit(node.name);
434
- this.visit(node.decorators);
435
- this.visit(node.typeParameters);
436
- this.visit(node.signature);
437
- this.depth++;
438
- this.visit(node.body);
439
- this.depth--;
440
- }
441
- visitIfStatement(node: IfStatement) {
442
- this.visit(node.condition);
443
- this.visit(node.ifTrue);
444
- this.visit(node.ifFalse);
445
- }
446
- visitImportDeclaration(node: ImportDeclaration) {
447
- this.visit(node.foreignName);
448
- this.visit(node.name);
449
- this.visit(node.decorators);
450
- }
451
- visitImportStatement(node: ImportStatement) {
452
- this.visit(node.namespaceName);
453
- this.visit(node.declarations);
454
- }
455
- visitIndexSignature(node: IndexSignatureNode) {
456
- this.visit(node.keyType);
457
- this.visit(node.valueType);
458
- }
459
- visitInterfaceDeclaration(
460
- node: InterfaceDeclaration,
461
- isDefault: boolean = false,
462
- ) {
463
- this.visit(node.name);
464
- this.visit(node.typeParameters);
465
- this.visit(node.implementsTypes);
466
- this.visit(node.extendsType);
467
- this.depth++;
468
- this.visit(node.members);
469
- this.depth--;
470
- }
471
- visitMethodDeclaration(node: MethodDeclaration) {
472
- this.visit(node.name);
473
- this.visit(node.typeParameters);
474
- this.visit(node.signature);
475
- this.visit(node.decorators);
476
- this.depth++;
477
- this.visit(node.body);
478
- this.depth--;
479
- }
480
- visitNamespaceDeclaration(
481
- node: NamespaceDeclaration,
482
- isDefault: boolean = false,
483
- ) {
484
- this.visit(node.name);
485
- this.visit(node.decorators);
486
- this.visit(node.members);
487
- }
488
- visitReturnStatement(node: ReturnStatement) {
489
- this.visit(node.value);
490
- }
491
- visitSwitchCase(node: SwitchCase) {
492
- this.visit(node.label);
493
- this.visit(node.statements);
494
- }
495
- visitSwitchStatement(node: SwitchStatement) {
496
- this.visit(node.condition);
497
- this.depth++;
498
- this.visit(node.cases);
499
- this.depth--;
500
- }
501
- visitThrowStatement(node: ThrowStatement) {
502
- this.visit(node.value);
503
- }
504
- visitTryStatement(node: TryStatement) {
505
- this.visit(node.bodyStatements);
506
- this.visit(node.catchVariable);
507
- this.visit(node.catchStatements);
508
- this.visit(node.finallyStatements);
509
- }
510
- visitTypeDeclaration(node: TypeDeclaration) {
511
- this.visit(node.name);
512
- this.visit(node.decorators);
513
- this.visit(node.type);
514
- this.visit(node.typeParameters);
515
- }
516
- visitVariableDeclaration(node: VariableDeclaration) {
517
- this.visit(node.name);
518
- this.visit(node.type);
519
- this.visit(node.initializer);
520
- }
521
- visitVariableStatement(node: VariableStatement) {
522
- this.visit(node.decorators);
523
- this.visit(node.declarations);
524
- }
525
- visitWhileStatement(node: WhileStatement) {
526
- this.visit(node.condition);
527
- this.depth++;
528
- this.visit(node.body);
529
- this.depth--;
530
- }
531
- visitVoidStatement(node: VoidStatement) { }
532
- visitComment(node: Comment) { }
533
- visitDecoratorNode(node: DecoratorNode) {
534
- this.visit(node.name);
535
- this.visit(node.args);
536
- }
537
- visitParameter(node: ParameterNode) {
538
- this.visit(node.name);
539
- this.visit(node.implicitFieldDeclaration);
540
- this.visit(node.initializer);
541
- this.visit(node.type);
542
- }
543
- }