json-as 1.1.10 → 1.1.12
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.md +18 -1
- package/README.md +1 -1
- package/assembly/__tests__/generics.spec.ts +40 -0
- package/assembly/__tests__/types.spec.ts +26 -0
- package/package.json +2 -3
- package/transform/lib/index.js +132 -106
- package/transform/lib/index.js.map +1 -1
- package/transform/lib/linkers/alias.js +49 -0
- package/transform/lib/linkers/alias.js.map +1 -0
- package/transform/lib/{linker.js → linkers/classes.js} +6 -20
- package/transform/lib/linkers/classes.js.map +1 -0
- package/transform/lib/linkers/custom.js +32 -0
- package/transform/lib/linkers/custom.js.map +1 -0
- package/transform/lib/linkers/imports.js +17 -0
- package/transform/lib/linkers/imports.js.map +1 -0
- package/transform/lib/types.js +13 -0
- package/transform/lib/types.js.map +1 -1
- package/transform/src/index.ts +199 -184
- package/transform/src/linkers/alias.ts +59 -0
- package/transform/src/{linker.ts → linkers/classes.ts} +13 -25
- package/transform/src/linkers/custom.ts +32 -0
- package/transform/src/linkers/imports.ts +22 -0
- package/transform/src/types.ts +15 -1
- package/transform/tsconfig.json +1 -1
- package/transform/lib/linker.js.map +0 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Node, Source, TypeDeclaration, TypeNode } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { Visitor } from "../visitor.js";
|
|
3
|
+
import { toString } from "../util.js";
|
|
4
|
+
|
|
5
|
+
class AliasFinder extends Visitor {
|
|
6
|
+
visitTypeDeclaration(node: TypeDeclaration, ref?: Node | null): void {
|
|
7
|
+
TypeAlias.add(node.name.text, node.type);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class TypeAlias {
|
|
12
|
+
public name: string;
|
|
13
|
+
public type: TypeAlias | string;
|
|
14
|
+
|
|
15
|
+
constructor(name: string, type: TypeAlias | string) {
|
|
16
|
+
this.name = name;
|
|
17
|
+
this.type = type;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getBaseType(type: TypeAlias | string = this.type): string {
|
|
21
|
+
if (typeof type === "string") return type;
|
|
22
|
+
return this.getBaseType(type.type);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static foundAliases: Map<string, string> = new Map<string, string>();
|
|
26
|
+
static aliases: Map<string, TypeAlias> = new Map<string, TypeAlias>();
|
|
27
|
+
|
|
28
|
+
static add(name: string, type: TypeNode): void {
|
|
29
|
+
if (!TypeAlias.foundAliases.has(name)) {
|
|
30
|
+
TypeAlias.foundAliases.set(name, toString(type));
|
|
31
|
+
} else {
|
|
32
|
+
const existingType = TypeAlias.foundAliases.get(name);
|
|
33
|
+
if (existingType !== toString(type)) {
|
|
34
|
+
throw new Error(`Type alias conflict for ${name}: "${existingType}" vs "${toString(type)}"`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static getAliases(source: Source): TypeAlias[] {
|
|
40
|
+
this.foundAliases.clear();
|
|
41
|
+
this.aliases.clear();
|
|
42
|
+
|
|
43
|
+
const finder = new AliasFinder();
|
|
44
|
+
finder.visit(source);
|
|
45
|
+
|
|
46
|
+
for (const [name, typeStr] of this.foundAliases) {
|
|
47
|
+
this.aliases.set(name, new TypeAlias(name, typeStr));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const alias of this.aliases.values()) {
|
|
51
|
+
if (typeof alias.type === "string" && this.aliases.has(alias.type)) {
|
|
52
|
+
alias.type = this.aliases.get(alias.type)!;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return [...this.aliases.values()];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
import { ClassDeclaration, CommonFlags,
|
|
2
|
-
import { Visitor } from "
|
|
3
|
-
import {
|
|
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
|
-
}
|
|
19
|
-
|
|
20
|
-
export function getImports(source: Source): ImportStatement[] {
|
|
21
|
-
return ImportGetter.getImports(source);
|
|
22
|
-
}
|
|
1
|
+
import { ClassDeclaration, CommonFlags, NodeKind, Parser, Source } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { Visitor } from "../visitor.js";
|
|
3
|
+
import { getImports } from "./imports.js";
|
|
23
4
|
|
|
24
5
|
export function getImportedClass(name: string, source: Source, parser: Parser): ClassDeclaration | null {
|
|
25
6
|
for (const stmt of getImports(source)) {
|
|
@@ -27,6 +8,7 @@ export function getImportedClass(name: string, source: Source, parser: Parser):
|
|
|
27
8
|
if (!externalSource) continue;
|
|
28
9
|
|
|
29
10
|
const classDeclaration = ClassGetter.getClass(name, externalSource);
|
|
11
|
+
if (!classDeclaration) continue;
|
|
30
12
|
if (!(classDeclaration.flags & CommonFlags.Export)) continue;
|
|
31
13
|
return classDeclaration;
|
|
32
14
|
}
|
|
@@ -42,14 +24,20 @@ class ClassGetter extends Visitor {
|
|
|
42
24
|
this.classes.push(node);
|
|
43
25
|
}
|
|
44
26
|
|
|
27
|
+
// visitTypeName(node: TypeName, ref?: Node | null): void {}
|
|
28
|
+
// visitParameter(node: ParameterNode, ref?: Node | null): void {}
|
|
29
|
+
// visitFunctionTypeNode(node: FunctionTypeNode, ref?: Node | null): void {}
|
|
30
|
+
// visitNamedTypeNode(node: NamedTypeNode, ref?: Node | null): void {}
|
|
31
|
+
|
|
45
32
|
static getClass(name: string, source: Source): ClassDeclaration | null {
|
|
46
33
|
return ClassGetter.getClasses(source).find((c) => c.name.text == name) || null;
|
|
47
34
|
}
|
|
48
35
|
|
|
49
36
|
static getClasses(source: Source): ClassDeclaration[] {
|
|
50
|
-
ClassGetter.SN.classes = [];
|
|
51
|
-
ClassGetter.SN.visit(source);
|
|
52
|
-
return ClassGetter.SN.classes;
|
|
37
|
+
// ClassGetter.SN.classes = [];
|
|
38
|
+
// ClassGetter.SN.visit(source);
|
|
39
|
+
// return ClassGetter.SN.classes;
|
|
40
|
+
return source.statements.filter((stmt) => stmt.kind == NodeKind.ClassDeclaration) as ClassDeclaration[];
|
|
53
41
|
}
|
|
54
42
|
}
|
|
55
43
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CallExpression, IdentifierExpression, Node, NodeKind, PropertyAccessExpression } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { Visitor } from "../visitor.js";
|
|
3
|
+
|
|
4
|
+
export class CustomTransform extends Visitor {
|
|
5
|
+
static SN: CustomTransform = new CustomTransform();
|
|
6
|
+
private modify: boolean = false;
|
|
7
|
+
visitCallExpression(node: CallExpression) {
|
|
8
|
+
super.visit(node.args, node);
|
|
9
|
+
if (node.expression.kind != NodeKind.PropertyAccess || (node.expression as PropertyAccessExpression).property.text != "stringify") return;
|
|
10
|
+
if ((node.expression as PropertyAccessExpression).expression.kind != NodeKind.Identifier || ((node.expression as PropertyAccessExpression).expression as IdentifierExpression).text != "JSON") return;
|
|
11
|
+
|
|
12
|
+
if (this.modify) {
|
|
13
|
+
(node.expression as PropertyAccessExpression).expression = Node.createPropertyAccessExpression(Node.createIdentifierExpression("JSON", node.expression.range), Node.createIdentifierExpression("internal", node.expression.range), node.expression.range);
|
|
14
|
+
}
|
|
15
|
+
this.modify = true;
|
|
16
|
+
|
|
17
|
+
// console.log(toString(node));
|
|
18
|
+
// console.log(SimpleParser.parseStatement("JSON.internal.stringify").expression.expression)
|
|
19
|
+
}
|
|
20
|
+
static visit(node: Node | Node[], ref: Node | null = null): void {
|
|
21
|
+
if (!node) return;
|
|
22
|
+
CustomTransform.SN.modify = true;
|
|
23
|
+
CustomTransform.SN.visit(node, ref);
|
|
24
|
+
CustomTransform.SN.modify = false;
|
|
25
|
+
}
|
|
26
|
+
static hasCall(node: Node | Node[]): boolean {
|
|
27
|
+
if (!node) return false;
|
|
28
|
+
CustomTransform.SN.modify = false;
|
|
29
|
+
CustomTransform.SN.visit(node);
|
|
30
|
+
return CustomTransform.SN.modify;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ImportStatement, Node, Source } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { Visitor } from "../visitor.js";
|
|
3
|
+
|
|
4
|
+
class ImportGetter extends Visitor {
|
|
5
|
+
static SN: ImportGetter = new ImportGetter();
|
|
6
|
+
|
|
7
|
+
private imports: ImportStatement[] = [];
|
|
8
|
+
|
|
9
|
+
visitImportStatement(node: ImportStatement, ref?: Node | null): void {
|
|
10
|
+
this.imports.push(node);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
static getImports(source: Source): ImportStatement[] {
|
|
14
|
+
ImportGetter.SN.imports = [];
|
|
15
|
+
ImportGetter.SN.visit(source);
|
|
16
|
+
return ImportGetter.SN.imports;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getImports(source: Source): ImportStatement[] {
|
|
21
|
+
return ImportGetter.getImports(source);
|
|
22
|
+
}
|
package/transform/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ClassDeclaration, Expression, FieldDeclaration } from "assemblyscript/dist/assemblyscript.js";
|
|
1
|
+
import { ClassDeclaration, Expression, FieldDeclaration, Source } from "assemblyscript/dist/assemblyscript.js";
|
|
2
|
+
import { TypeAlias } from "./linkers/alias.js";
|
|
2
3
|
|
|
3
4
|
export enum PropertyFlags {
|
|
4
5
|
OmitNull,
|
|
@@ -15,6 +16,7 @@ export class Property {
|
|
|
15
16
|
public flags: Map<PropertyFlags, Expression | null> = new Map<PropertyFlags, Expression | null>();
|
|
16
17
|
public node!: FieldDeclaration;
|
|
17
18
|
public byteSize: number = 0;
|
|
19
|
+
public generic: boolean = false;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
export class Schema {
|
|
@@ -28,3 +30,15 @@ export class Schema {
|
|
|
28
30
|
public deps: Schema[] = [];
|
|
29
31
|
public custom: boolean = false;
|
|
30
32
|
}
|
|
33
|
+
|
|
34
|
+
export class Src {
|
|
35
|
+
public internalPath: string;
|
|
36
|
+
public schemas: Schema[];
|
|
37
|
+
public aliases: TypeAlias[];
|
|
38
|
+
public imports: Schema[];
|
|
39
|
+
public exports: Schema[];
|
|
40
|
+
constructor(source: Source) {
|
|
41
|
+
this.internalPath = source.internalPath;
|
|
42
|
+
this.aliases = TypeAlias.getAliases(source);
|
|
43
|
+
}
|
|
44
|
+
}
|
package/transform/tsconfig.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"linker.js","sourceRoot":"","sources":["../src/linker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAGvC,MAAM,YAAa,SAAQ,OAAO;IAChC,MAAM,CAAC,EAAE,GAAiB,IAAI,YAAY,EAAE,CAAC;IAErC,OAAO,GAAsB,EAAE,CAAC;IAExC,oBAAoB,CAAC,IAAqB,EAAE,GAAiB;QAC3D,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IACD,MAAM,CAAC,UAAU,CAAC,MAAc;QAC9B,YAAY,CAAC,EAAE,CAAC,OAAO,GAAG,EAAE,CAAC;QAC7B,YAAY,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC9B,OAAO,YAAY,CAAC,EAAE,CAAC,OAAO,CAAC;IACjC,CAAC;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