json-as 1.1.9 → 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.
@@ -1,21 +1,70 @@
1
- import { ClassDeclaration, ImportStatement, NodeKind, Parser, Source } from "assemblyscript/dist/assemblyscript.js";
1
+ import { ClassDeclaration, CommonFlags, ImportStatement, NodeKind, Parser, Source } from "assemblyscript/dist/assemblyscript.js";
2
+ import { Visitor } from "./visitor.js";
3
+ import { Node } from "types:assemblyscript/src/ast";
4
+
5
+ class ImportGetter extends Visitor {
6
+ static SN: ImportGetter = new ImportGetter();
7
+
8
+ private imports: ImportStatement[] = [];
9
+
10
+ visitImportStatement(node: ImportStatement, ref?: Node | null): void {
11
+ this.imports.push(node);
12
+ }
13
+
14
+ static getImports(source: Source): ImportStatement[] {
15
+ ImportGetter.SN.imports = [];
16
+ ImportGetter.SN.visit(source);
17
+ return ImportGetter.SN.imports;
18
+ }
19
+ }
2
20
 
3
21
  export function getImports(source: Source): ImportStatement[] {
4
- return source.statements.filter((v) => v.kind === NodeKind.Import) as ImportStatement[];
22
+ return ImportGetter.getImports(source);
5
23
  }
6
24
 
7
25
  export function getImportedClass(name: string, source: Source, parser: Parser): ClassDeclaration | null {
8
26
  for (const stmt of getImports(source)) {
9
- const externalSource = parser.sources.find((src) => src.internalPath === stmt.internalPath);
27
+ const externalSource = parser.sources.filter((src) => src.internalPath != source.internalPath).find((src) => src.internalPath == stmt.internalPath);
10
28
  if (!externalSource) continue;
11
29
 
12
- const classDeclaration = externalSource.statements.find((s) => s.kind === NodeKind.ClassDeclaration && (<ClassDeclaration>s).name.text === name) as ClassDeclaration | null;
13
-
14
- if (classDeclaration) return classDeclaration;
30
+ const classDeclaration = ClassGetter.getClass(name, externalSource);
31
+ if (!classDeclaration) continue;
32
+ if (!(classDeclaration.flags & CommonFlags.Export)) continue;
33
+ return classDeclaration;
15
34
  }
16
35
  return null;
17
36
  }
18
37
 
38
+ class ClassGetter extends Visitor {
39
+ static SN: ClassGetter = new ClassGetter();
40
+
41
+ private classes: ClassDeclaration[] = [];
42
+
43
+ visitClassDeclaration(node: ClassDeclaration): void {
44
+ this.classes.push(node);
45
+ }
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
+
52
+ static getClass(name: string, source: Source): ClassDeclaration | null {
53
+ return ClassGetter.getClasses(source).find((c) => c.name.text == name) || null;
54
+ }
55
+
56
+ static getClasses(source: Source): ClassDeclaration[] {
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[];
61
+ }
62
+ }
63
+
19
64
  export function getClasses(source: Source): ClassDeclaration[] {
20
- return source.statements.filter((v) => v.kind === NodeKind.ClassDeclaration) as ClassDeclaration[];
65
+ return ClassGetter.getClasses(source);
21
66
  }
67
+
68
+ export function getClass(name: string, source: Source): ClassDeclaration | null {
69
+ return ClassGetter.getClass(name, source);
70
+ }
@@ -1,6 +1,7 @@
1
1
  // Taken from https://github.com/as-pect/visitor-as/blob/master/src/simpleParser.ts
2
2
  import { Parser, Tokenizer, Source, SourceKind, Expression, Statement, NamespaceDeclaration, ClassDeclaration, DeclarationStatement, Range, Node, NodeKind } from "assemblyscript/dist/assemblyscript.js";
3
3
  import { ASTBuilder } from "./builder.js";
4
+ import * as path from "path";
4
5
 
5
6
  export class SimpleParser {
6
7
  private static get parser(): Parser {
@@ -120,3 +121,8 @@ export function stripExpr(node: Node): Node {
120
121
  if (node.kind == NodeKind.Expression) return node["expression"];
121
122
  return node;
122
123
  }
124
+
125
+ export function removeExtension(filePath: string): string {
126
+ const parsed = path.parse(filePath);
127
+ return path.join(parsed.dir, parsed.name);
128
+ }