json-as 1.1.20 → 1.1.22

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,4 +1,4 @@
1
- import { ClassDeclaration, Expression, FieldDeclaration, Source, NodeKind, Node, NamespaceDeclaration, DeclarationStatement, TypeName, Parser, ImportStatement, CommonFlags } from "assemblyscript/dist/assemblyscript.js";
1
+ import { ClassDeclaration, Expression, FieldDeclaration, Source, NodeKind, Node, NamespaceDeclaration, DeclarationStatement, TypeName, Parser, ImportStatement, CommonFlags, EnumDeclaration } from "assemblyscript/dist/assemblyscript.js";
2
2
  import { TypeAlias } from "./linkers/alias.js";
3
3
  import { stripNull } from "./index.js";
4
4
 
@@ -100,9 +100,10 @@ export class Src {
100
100
  public schemas: Schema[];
101
101
  public aliases: TypeAlias[];
102
102
  public exports: Schema[];
103
+ public imports: ImportStatement[] = [];
103
104
  private nodeMap: Map<Node, NamespaceDeclaration[]> = new Map<Node, NamespaceDeclaration[]>();
104
105
  private classes: Record<string, ClassDeclaration> = {};
105
- private imports: ImportStatement[] = [];
106
+ private enums: Record<string, EnumDeclaration> = {};
106
107
 
107
108
  constructor(
108
109
  source: Source,
@@ -130,6 +131,10 @@ export class Src {
130
131
  const classDeclaration = node as ClassDeclaration;
131
132
  this.classes[this.qualifiedName(classDeclaration, path)] = classDeclaration;
132
133
  break;
134
+ case NodeKind.EnumDeclaration:
135
+ const enumDeclaration = node as EnumDeclaration;
136
+ this.enums[this.qualifiedName(enumDeclaration, path)] = enumDeclaration;
137
+ break;
133
138
  case NodeKind.Import:
134
139
  const importStatement = node as ImportStatement;
135
140
  this.imports.push(importStatement);
@@ -157,6 +162,15 @@ export class Src {
157
162
  return this.classes[qualifiedName] || null;
158
163
  }
159
164
 
165
+ /**
166
+ * Get an enum declaration by its qualified name.
167
+ * @param qualifiedName Qualified name (eg. "Namespace.MyEnum")
168
+ * @returns Enum declaration or null if not found.
169
+ */
170
+ getEnum(qualifiedName: string): EnumDeclaration | null {
171
+ return this.enums[qualifiedName] || null;
172
+ }
173
+
160
174
  /**
161
175
  * Get imported class from other sources in the parser.
162
176
  * @param qualifiedName Qualified name of class.
@@ -177,6 +191,26 @@ export class Src {
177
191
  return null;
178
192
  }
179
193
 
194
+ /**
195
+ * Get imported enum from other sources in the parser.
196
+ * @param qualifiedName Qualified name of enum.
197
+ * @param parser AssemblyScript parser.
198
+ * @returns Enum declaration or null if not found.
199
+ */
200
+ getImportedEnum(qualifiedName: string, parser: Parser): EnumDeclaration | null {
201
+ for (const stmt of this.imports) {
202
+ const externalSource = parser.sources.filter((src) => src.internalPath != this.internalPath).find((src) => src.internalPath == stmt.internalPath);
203
+ if (!externalSource) continue;
204
+
205
+ const source = this.sourceSet.get(externalSource);
206
+ const enumDeclaration = source.getEnum(qualifiedName);
207
+ if (enumDeclaration && enumDeclaration.flags & CommonFlags.Export) {
208
+ return enumDeclaration;
209
+ }
210
+ }
211
+ return null;
212
+ }
213
+
180
214
  /**
181
215
  * Gets a unique path string to the node by combining the internalPath with
182
216
  * the qualified name of the node.