json-as 1.1.8 → 1.1.10

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,62 @@
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
+ static getImports(source: Source): ImportStatement[] {
14
+ ImportGetter.SN.imports = [];
15
+ ImportGetter.SN.visit(source);
16
+ return ImportGetter.SN.imports;
17
+ }
18
+ }
2
19
 
3
20
  export function getImports(source: Source): ImportStatement[] {
4
- return source.statements.filter((v) => v.kind === NodeKind.Import) as ImportStatement[];
21
+ return ImportGetter.getImports(source);
5
22
  }
6
23
 
7
24
  export function getImportedClass(name: string, source: Source, parser: Parser): ClassDeclaration | null {
8
25
  for (const stmt of getImports(source)) {
9
- const externalSource = parser.sources.find((src) => src.internalPath === stmt.internalPath);
26
+ const externalSource = parser.sources.filter((src) => src.internalPath != source.internalPath).find((src) => src.internalPath == stmt.internalPath);
10
27
  if (!externalSource) continue;
11
28
 
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;
29
+ const classDeclaration = ClassGetter.getClass(name, externalSource);
30
+ if (!(classDeclaration.flags & CommonFlags.Export)) continue;
31
+ return classDeclaration;
15
32
  }
16
33
  return null;
17
34
  }
18
35
 
36
+ class ClassGetter extends Visitor {
37
+ static SN: ClassGetter = new ClassGetter();
38
+
39
+ private classes: ClassDeclaration[] = [];
40
+
41
+ visitClassDeclaration(node: ClassDeclaration): void {
42
+ this.classes.push(node);
43
+ }
44
+
45
+ static getClass(name: string, source: Source): ClassDeclaration | null {
46
+ return ClassGetter.getClasses(source).find((c) => c.name.text == name) || null;
47
+ }
48
+
49
+ static getClasses(source: Source): ClassDeclaration[] {
50
+ ClassGetter.SN.classes = [];
51
+ ClassGetter.SN.visit(source);
52
+ return ClassGetter.SN.classes;
53
+ }
54
+ }
55
+
19
56
  export function getClasses(source: Source): ClassDeclaration[] {
20
- return source.statements.filter((v) => v.kind === NodeKind.ClassDeclaration) as ClassDeclaration[];
57
+ return ClassGetter.getClasses(source);
21
58
  }
59
+
60
+ export function getClass(name: string, source: Source): ClassDeclaration | null {
61
+ return ClassGetter.getClass(name, source);
62
+ }
@@ -4,7 +4,7 @@ export enum PropertyFlags {
4
4
  OmitNull,
5
5
  OmitIf,
6
6
  Raw,
7
- Custom
7
+ Custom,
8
8
  }
9
9
 
10
10
  export class Property {
@@ -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 {
@@ -55,11 +56,7 @@ export function toString(node: Node): string {
55
56
  return ASTBuilder.build(node);
56
57
  }
57
58
 
58
- export function replaceRef(
59
- node: Node,
60
- replacement: Node | Node[],
61
- ref: Node | Node[] | null,
62
- ): void {
59
+ export function replaceRef(node: Node, replacement: Node | Node[], ref: Node | Node[] | null): void {
63
60
  if (!node || !ref) return;
64
61
  const nodeExpr = stripExpr(node);
65
62
 
@@ -77,8 +74,7 @@ export function replaceRef(
77
74
  if (Array.isArray(current)) {
78
75
  for (let i = 0; i < current.length; i++) {
79
76
  if (stripExpr(current[i]) === nodeExpr) {
80
- if (Array.isArray(replacement))
81
- current.splice(i, 1, ...replacement);
77
+ if (Array.isArray(replacement)) current.splice(i, 1, ...replacement);
82
78
  else current.splice(i, 1, replacement);
83
79
  return;
84
80
  }
@@ -91,17 +87,11 @@ export function replaceRef(
91
87
  }
92
88
  }
93
89
 
94
- export function cloneNode(
95
- input: Node | Node[] | null,
96
- seen = new WeakMap(),
97
- path = "",
98
- ): Node | Node[] | null {
90
+ export function cloneNode(input: Node | Node[] | null, seen = new WeakMap(), path = ""): Node | Node[] | null {
99
91
  if (input === null || typeof input !== "object") return input;
100
92
 
101
93
  if (Array.isArray(input)) {
102
- return input.map((item, index) =>
103
- cloneNode(item, seen, `${path}[${index}]`),
104
- ) as Node | Node[] | null;
94
+ return input.map((item, index) => cloneNode(item, seen, `${path}[${index}]`)) as Node | Node[] | null;
105
95
  }
106
96
 
107
97
  if (seen.has(input)) return seen.get(input);
@@ -130,4 +120,9 @@ export function stripExpr(node: Node): Node {
130
120
  if (!node) return node;
131
121
  if (node.kind == NodeKind.Expression) return node["expression"];
132
122
  return node;
133
- }
123
+ }
124
+
125
+ export function removeExtension(filePath: string): string {
126
+ const parsed = path.parse(filePath);
127
+ return path.join(parsed.dir, parsed.name);
128
+ }